def get_connection(self, obj): current_user_profile_id = self.context['user_id'] initiator_user_id = obj.initiator.id if current_user_profile_id == initiator_user_id: return ProfilePreviewSerializer(obj.recipient).data else: return ProfilePreviewSerializer(obj.initiator).data
class ForumThreadOverviewSerializer(serializers.ModelSerializer): author = ProfilePreviewSerializer(read_only=True) last_post_user = ProfilePreviewSerializer(read_only=True) class Meta: model = ForumThread fields = ['author', 'started_timestamp', 'subject', 'number_of_responses', 'last_post_user', 'last_post_datetime', 'id', 'forum']
class EventSerializer(CountryFieldMixin, GeoFeatureModelSerializer): admin = ProfilePreviewSerializer(read_only=True) cohosts = ProfilePreviewSerializer(read_only=True, many=True) class Meta: model = Event geo_field = "geom" fields = ['datetime_start', 'datetime_end', 'title', 'description', 'category', 'geom', 'address', 'country', 'admin', 'cohosts', 'participants', 'invited', 'public', 'online', 'online_address', 'id']
class ForumSerializer(serializers.ModelSerializer): participant_organizations = OrganizationGroupSerializer(read_only=True, many=True) participant_generic_groups = GenericGroupSerializer(read_only=True, many=True) participant_users_profiles = ProfilePreviewSerializer(read_only=True, many=True) administrators = ProfilePreviewSerializer(read_only=True, many=True) moderators = ProfilePreviewSerializer(read_only=True, many=True) global_level = serializers.CharField(read_only=True) name = serializers.CharField(read_only=True) class Meta: model = Forum fields = ['name', 'participant_organizations', 'participant_generic_groups', 'participant_users_profiles', 'id', 'forum_type', 'administrators', 'moderators', 'global_level']
class FileReadOnlySerializer(serializers.ModelSerializer): owner = ProfilePreviewSerializer(read_only=True) last_edited_by = ProfilePreviewSerializer(read_only=True) users_read = ProfilePreviewSerializer(many=True, read_only=True) users_write = ProfilePreviewSerializer(many=True, read_only=True) orgs_read = OrganizationGroupSerializer(many=True, read_only=True) orgs_write = OrganizationGroupSerializer(many=True, read_only=True) parent_folder = serializers.PrimaryKeyRelatedField(read_only=True) file = serializers.CharField(read_only=True) id = serializers.IntegerField(read_only=True) class Meta: model = SharedFile fields = ('id', 'owner', 'last_edited_by', 'users_read', 'users_write', 'orgs_read', 'orgs_write', 'time_created', 'time_last_edited', 'parent_folder', 'file')
class MessagingMessageSerializer(serializers.ModelSerializer): sender = ProfilePreviewSerializer(read_only=True) def get_sender(self, obj): if not obj.sender.visible: return { -1: { "name": "anonymous", "org_name": None, "thumbnail": '' } } else: if obj.sender.type == UserType.VOL.value or obj.sender.type == UserType.ANO.value: return { obj.sender.id: { "name": obj.sender.user.username, "org_name": None, "thumbnail": obj.sender.thumbnail.url } } else: return { obj.sender.id: { "name": obj.sender.user.username, "org_name": obj.sender.primary_org.display_name, "thumbnail": obj.sender.thumbnail.url } } class Meta: model = MessagingMessage fields = '__all__'
class ForumPostSerializer(serializers.ModelSerializer): sent_timestamp = serializers.DateTimeField(read_only=True) author = ProfilePreviewSerializer(read_only=True) class Meta: model = ForumThreadPost fields = ['sent_timestamp', 'author', 'content', 'thread', 'id']
class ReceivedConnectionRequestSerializer(serializers.ModelSerializer): connection = ProfilePreviewSerializer(read_only=True, source='initiator') note = serializers.CharField(read_only=True) class Meta: model = ConnectionRequest fields = ['connection', 'note', 'id']
class SentConnectionRequestSerializer(serializers.ModelSerializer): connection = ProfilePreviewSerializer(read_only=True, source='recipient') note = serializers.CharField(read_only=True) class Meta: model = ConnectionRequest fields = ['connection', 'note', 'id']
class EventThreadSerializer(serializers.ModelSerializer): author = ProfilePreviewSerializer(read_only=True) messages = EventMessageSerializer(read_only=True, many=True) id = serializers.IntegerField(read_only=True) class Meta: model = EventThread fields = ['author', 'started_timestamp', 'last_post_datetime', 'id', 'messages']
class OnlineReportCommentThreadSerializer(serializers.ModelSerializer): author = ProfilePreviewSerializer(read_only=True) comments = OnlineReportCommentSerializer(read_only=True, many=True) id = serializers.IntegerField(read_only=True) class Meta: model = OnlineReportCommentThread fields = [ 'author', 'started_timestamp', 'last_post_datetime', 'id', 'comments', 'report' ]
class FileUpdateSerializer(serializers.ModelSerializer): owner = ProfilePreviewSerializer(read_only=True) last_edited_by = ProfilePreviewSerializer(read_only=True) users_read = serializers.PrimaryKeyRelatedField( many=True, queryset=UserProfile.objects.all(), required=False) users_write = serializers.PrimaryKeyRelatedField( many=True, queryset=UserProfile.objects.all(), required=False) orgs_read = serializers.PrimaryKeyRelatedField( many=True, queryset=OrganizationGroup.objects.all(), required=False) orgs_write = serializers.PrimaryKeyRelatedField( many=True, queryset=OrganizationGroup.objects.all(), required=False) parent_folder = serializers.PrimaryKeyRelatedField( read_only=True) # set this through URL so users cannot rewrite this file = serializers.CharField(required=False) id = serializers.IntegerField(read_only=True) class Meta: model = SharedFile fields = ('id', 'owner', 'last_edited_by', 'users_read', 'users_write', 'orgs_read', 'orgs_write', 'time_created', 'time_last_edited', 'parent_folder', 'file')
class EventMessageSerializer(serializers.ModelSerializer): author = ProfilePreviewSerializer(read_only=True) thanked = serializers.SerializerMethodField() id = serializers.IntegerField(read_only=True) thanks = serializers.IntegerField(read_only=True) def get_thanked(self, obj): current_user_profile = get_current_user().profile return current_user_profile in obj.thanked_by.all() class Meta: model = EventThreadMessage fields = ["content", "author", "sent_timestamp", "thread", "id", "thanks", "thanked"]
class IssueCommentSerializer(serializers.ModelSerializer): author = ProfilePreviewSerializer(read_only=True) issue = serializers.PrimaryKeyRelatedField(queryset=Issue.objects.all()) id = serializers.IntegerField(read_only=True) thanks = serializers.IntegerField(read_only=True) thanked = serializers.SerializerMethodField() def get_thanked(self, obj): current_user_profile = get_current_user().profile return current_user_profile in obj.thanked_by.all() class Meta: model = IssueComment fields = ('id', 'issue', 'sent_timestamp', 'author', 'content', 'thanks', 'thanked')
class IssueSerializer(serializers.ModelSerializer): author = ProfilePreviewSerializer(read_only=True) issue_status = serializers.CharField(read_only=True) closure_reason = serializers.CharField(read_only=True) id = serializers.IntegerField(read_only=True) thanks = serializers.IntegerField(read_only=True) thanked = serializers.SerializerMethodField() def get_thanked(self, obj): current_user_profile = get_current_user().profile return current_user_profile in obj.thanked_by.all() class Meta: model = Issue fields = ('id', 'issue_type', 'title', 'description', 'author', 'issue_status', 'closure_reason', 'thanks', 'thanked')
class ReportCommentSerializer(serializers.ModelSerializer): id = serializers.IntegerField(read_only=True) thanks = serializers.IntegerField(read_only=True) thanked = serializers.SerializerMethodField() author = ProfilePreviewSerializer(read_only=True) def get_thanked(self, obj): current_user_profile = get_current_user().profile return current_user_profile in obj.thanked_by.all() class Meta: model = ReportComment fields = [ 'id', 'author', 'content', 'thanks', 'thanked', 'sent_timestamp', 'thread' ]
class MessagingThreadSerializer(serializers.ModelSerializer): recipient_users = ProfilePreviewSerializer( many=True ) # serializers.SerializerMethodField('get_all_participants_as_arr', read_only=True) recipient_groups = serializers.SerializerMethodField( 'get_recipient_group_names', read_only=True) related_object = serializers.SerializerMethodField( 'get_related_object_details', read_only=True) initiator = ProfilePreviewSerializer( ) # serializers.SerializerMethodField('get_initiator_name', read_only=True) def get_initiator_name(self, obj): if not obj.initiator.visible: return { -1: { "name": "anonymous", "org_name": None, "thumbnail": '' } } else: if obj.initiator.type != 'VOL': return { obj.initiator.id: { "name": obj.initiator.user.username, "org_name": obj.initiator.primary_org.display_name, "thumbnail": obj.initiator.thumbnail.url } } else: return { obj.initiator.id: { "name": obj.initiator.user.username, "org_name": None, "thumbnail": obj.initiator.thumbnail.url } } def get_all_participants_as_arr(self, obj): result = [] for recipient in obj.recipient_users.all(): if recipient.visible: if recipient.type != 'VOL': result.append({ recipient.id: { "name": recipient.user.username, "org_name": recipient.primary_org.display_name, "thumbnail": recipient.thumbnail.url } }) else: result.append({ recipient.id: { "name": recipient.user.username, "org_name": None, "thumbnail": recipient.thumbnail.url } }) else: result.append({ -1: { "name": "anonymous", "org_name": None, "thumbnail": '' } }) if obj.initiator.visible: if obj.initiator.type != 'VOL': result.append({ obj.initiator.id: { "name": obj.initiator.user.username, "org_name": obj.initiator.primary_org.display_name, "thumbnail": obj.initiator.thumbnail.url } }) else: result.append({ obj.initiator.id: { "name": obj.initiator.user.username, "org_name": None, "thumbnail": obj.initiator.thumbnail.url } }) else: result.append( {-1: { "name": "anonymous", "org_name": None, "thumbnail": '' }}) return result def get_recipient_group_names(self, obj): result = [] for group in obj.recipient_groups.all(): result.append({group.id: group.display_name}) return result def get_related_object_details(self, obj): result = None if obj.content_type and obj.object_id: if obj.content_type.id == ContentType.objects.get_for_model( Report).id: result = 'Re: <a href="{}">Report {}</a>'.format( settings.BASE_URL + reverse('report-detail', kwargs={'pk': obj.object_id}), obj.object_id) if obj.content_type.id == ContentType.objects.get_for_model( OnlineReport).id: result = 'Re: <a href="{}">Online report {}</a>'.format( settings.BASE_URL + reverse('onlinereport-detail', kwargs={'pk': obj.object_id}), obj.object_id) return result class Meta: model = MessagingThread fields = '__all__'