Created
April 24, 2017 18:37
-
-
Save bengolder/bd6ac1eaf41d8a4f9fcc4f3495eb71cd to your computer and use it in GitHub Desktop.
Anonymized serializers for SNs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from intake import models | |
import Levenshtein | |
from rest_framework import serializers | |
from .fields import FormattedLocalDateField | |
from intake.services import status_notifications as StatusNotificationsService | |
class StatusNotificationAnalysisSerializer(serializers.ModelSerializer): | |
contact_info = serializers.SerializerMethodField() | |
message_change = serializers.SerializerMethodField() | |
was_edited = serializers.SerializerMethodField() | |
class Meta: | |
model = models.StatusNotification | |
fields = [ | |
'contact_info', | |
'message_change', | |
'was_edited' | |
] | |
def get_contact_info(self, notification): | |
return list(notification.contact_info.keys()) | |
def get_message_change(self, notification): | |
author_profile = notification.status_update.author.profile | |
intro_text = StatusNotificationsService.get_notification_intro( | |
author_profile) + '\n\n' | |
return 1.0 - Levenshtein.ratio( | |
*[message.replace(intro_text, '') | |
for message in ( | |
notification.base_message, notification.sent_message)]) | |
def get_was_edited(self, notification): | |
return notification.base_message != notification.sent_message | |
class StatusUpdateAnalysisSerializer(serializers.ModelSerializer): | |
created = FormattedLocalDateField(format="%Y-%m-%d") | |
notification = StatusNotificationAnalysisSerializer() | |
status_type = serializers.SlugRelatedField( | |
read_only=True, slug_field='display_name') | |
next_steps = serializers.SlugRelatedField( | |
read_only=True, slug_field='display_name', many=True) | |
organization_name = serializers.SerializerMethodField() | |
additional_information_length = serializers.SerializerMethodField() | |
additional_information_in_sent_message = \ | |
serializers.SerializerMethodField() | |
other_next_step_length = serializers.SerializerMethodField() | |
other_next_step_in_sent_message = serializers.SerializerMethodField() | |
class Meta: | |
model = models.StatusUpdate | |
fields = [ | |
'id', | |
'created', | |
'notification', | |
'status_type', | |
'next_steps', | |
'organization_name', | |
'additional_information_length', | |
'additional_information_in_sent_message', | |
'other_next_step_length', | |
'other_next_step_in_sent_message' | |
] | |
def get_organization_name(self, instance): | |
return instance.author.profile.organization.name | |
def get_additional_information_length(self, instance): | |
return len(instance.additional_information) | |
def get_additional_information_in_sent_message(self, instance): | |
notification = getattr(instance, 'notification', None) | |
if notification and instance.additional_information: | |
return (instance.additional_information in | |
instance.notification.sent_message) | |
def get_other_next_step_length(self, instance): | |
return len(instance.other_next_step) | |
def get_other_next_step_in_sent_message(self, instance): | |
notification = getattr(instance, 'notification', None) | |
if notification and instance.other_next_step: | |
return (instance.other_next_step in | |
instance.notification.sent_message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment