コード例 #1
0
ファイル: serializers.py プロジェクト: the-deep/server
class SourceDataSerializer(RemoveNullFieldsMixin,
                           serializers.ModelSerializer):
    existing = serializers.SerializerMethodField()
    key = serializers.CharField(source='id')
    emm_entities = serializers.SerializerMethodField()
    emm_triggers = serializers.SerializerMethodField()

    # M2M authors field
    authors = serializers.SerializerMethodField()
    authors_detail = serializers.SerializerMethodField()

    # TODO: Remove (Legacy)
    author_detail = SimpleOrganizationSerializer(source='author', read_only=True)

    source_detail = SimpleOrganizationSerializer(source='source', read_only=True)
    published_on = serializers.DateField(read_only=True)

    class Meta:
        model = Lead
        fields = (
            'key', 'title', 'source', 'source_type', 'url',
            'published_on', 'existing',
            'emm_entities', 'emm_triggers', 'source_detail',
            'author_detail', 'authors', 'authors_detail',
            'source_raw', 'author_raw',
        )

    def get_authors(self, lead):
        if hasattr(lead, '_authors'):
            return [author.pk for author in lead._authors]
        return []

    def get_authors_detail(self, lead):
        if hasattr(lead, '_authors'):
            return SimpleOrganizationSerializer(lead._authors, many=True).data
        return []

    def get_emm_entities(self, lead):
        if hasattr(lead, '_emm_entities'):
            return SourceEMMEntitiesSerializer(lead._emm_entities, many=True).data
        return []

    def get_emm_triggers(self, lead):
        if hasattr(lead, '_emm_triggers'):
            return SourceEMMTriggerSerializer(lead._emm_triggers, many=True).data
        return []

    def get_existing(self, lead):
        if not self.context.get('request'):
            return False

        return check_if_url_exists(lead.url,
                                   self.context['request'].user,
                                   self.context.get('project'))
コード例 #2
0
ファイル: serializers.py プロジェクト: the-deep/server
class EntryLeadSerializer(RemoveNullFieldsMixin, serializers.ModelSerializer):
    attachment = FileSerializer(read_only=True)
    tabular_book = serializers.SerializerMethodField()

    assignee_details = SimpleUserSerializer(source='get_assignee',
                                            read_only=True)
    authors_detail = SimpleOrganizationSerializer(source='authors',
                                                  many=True,
                                                  read_only=True)
    source_detail = SimpleOrganizationSerializer(source='source',
                                                 read_only=True)
    confidentiality_display = serializers.CharField(
        source='get_confidentiality_display', read_only=True)
    created_by_details = SimpleUserSerializer(source='get_created_by',
                                              read_only=True)
    page_count = serializers.IntegerField(
        source='leadpreview.page_count',
        read_only=True,
    )

    class Meta:
        model = Lead
        fields = (
            'id',
            'title',
            'created_at',
            'url',
            'attachment',
            'tabular_book',
            'client_id',
            'assignee',
            'assignee_details',
            'published_on',
            'authors_detail',
            'source_detail',
            'confidentiality_display',
            'created_by_details',
            'page_count',
            'confidentiality',
        )

    def get_tabular_book(self, obj):
        file = obj.attachment
        if file and hasattr(file, 'book'):
            return file.book.id
        return None
コード例 #3
0
class LeadOptionsSerializer(serializers.Serializer):
    projects = SimpleProjectSerializer(many=True)
    confidentiality = KeyValueSerializer(many=True)
    status = KeyValueSerializer(many=True)
    priority = NumericKeyValueSerializer(many=True)
    lead_groups = SimpleLeadGroupSerializer(many=True)
    members = SimpleUserSerializer(many=True)
    organizations = SimpleOrganizationSerializer(many=True)
    emm_entities = EmmEntitySerializer(many=True)
    emm_keywords = EmmTagSerializer(many=True)
    emm_risk_factors = EmmTagSerializer(many=True)
    has_emm_leads = serializers.BooleanField()
    organization_types = KeyValueSerializer(many=True)
コード例 #4
0
ファイル: serializers.py プロジェクト: the-deep/server
class AnalysisFrameworkSerializer(RemoveNullFieldsMixin, DynamicFieldsMixin,
                                  UserResourceSerializer):
    """
    Analysis Framework Model Serializer
    """
    widgets = SimpleWidgetSerializer(source='widget_set',
                                     many=True,
                                     required=False)
    filters = SimpleFilterSerializer(source='get_active_filters',
                                     many=True,
                                     read_only=True)
    exportables = SimpleExportableSerializer(source='exportable_set',
                                             many=True,
                                             read_only=True)
    questions = FrameworkQuestionSerializer(source='frameworkquestion_set',
                                            many=True,
                                            required=False,
                                            read_only=True)
    entries_count = serializers.IntegerField(
        source='get_entries_count',
        read_only=True,
    )

    is_admin = serializers.SerializerMethodField()
    users_with_add_permission = serializers.SerializerMethodField()
    visible_projects = serializers.SerializerMethodField()
    all_projects_count = serializers.IntegerField(source='project_set.count',
                                                  read_only=True)

    project = serializers.IntegerField(
        write_only=True,
        required=False,
    )

    role = serializers.SerializerMethodField()
    organization_details = SimpleOrganizationSerializer(source='organization',
                                                        read_only=True)

    class Meta:
        model = AnalysisFramework
        fields = ('__all__')

    def get_visible_projects(self, obj):
        from project.serializers import SimpleProjectSerializer
        user = None
        if 'request' in self.context:
            user = self.context['request'].user
        projects = obj.project_set.exclude(
            Q(is_private=True) & ~Q(members=user))
        return SimpleProjectSerializer(projects,
                                       context=self.context,
                                       many=True,
                                       read_only=True).data

    def get_users_with_add_permission(self, obj):
        """
        AF members with access to add other users to AF
        """
        return SimpleUserSerializer(
            User.objects.filter(
                id__in=obj.analysisframeworkmembership_set.filter(
                    role__can_add_user=True).values('member'), ).all(),
            context=self.context,
            many=True,
        ).data

    def get_role(self, obj):
        user = self.context['request'].user
        membership = AnalysisFrameworkMembership.objects.filter(
            framework=obj, member=user).first()

        role = None
        if not membership and not obj.is_private:
            role = obj.get_or_create_default_role()
        elif membership:
            role = membership.role
        else:
            return {}

        return AnalysisFrameworkRoleSerializer(role, context=self.context).data

    def validate_project(self, project):
        try:
            project = Project.objects.get(id=project)
        except Project.DoesNotExist:
            raise serializers.ValidationError(
                'Project matching query does not exist')

        if not project.can_modify(self.context['request'].user):
            raise serializers.ValidationError('Invalid project')
        return project.id

    def create(self, validated_data):
        project = validated_data.pop('project', None)
        private = validated_data.get('is_private', False)

        # Check if user has access to private project feature
        user = self.context['request'].user
        private_access = user.profile.get_accessible_features().filter(
            key=Feature.FeatureKey.PRIVATE_PROJECT).exists()

        if private and not private_access:
            raise exceptions.PermissionDenied({
                "message":
                "You don't have permission to create private framework"
            })

        af = super().create(validated_data)

        if project:
            project = Project.objects.get(id=project)
            project.analysis_framework = af
            project.modified_by = user
            project.save()

        owner_role = af.get_or_create_owner_role()
        af.add_member(self.context['request'].user, owner_role)
        return af

    def update(self, instance, validated_data):
        if 'is_private' not in validated_data:
            return super().update(instance, validated_data)

        if instance.is_private != validated_data['is_private']:
            raise exceptions.PermissionDenied({
                "message":
                "You don't have permission to change framework's privacy"
            })
        return super().update(instance, validated_data)

    def get_is_admin(self, analysis_framework):
        return analysis_framework.can_modify(self.context['request'].user)
コード例 #5
0
ファイル: serializers.py プロジェクト: the-deep/server
 def get_authors_detail(self, lead):
     if hasattr(lead, '_authors'):
         return SimpleOrganizationSerializer(lead._authors, many=True).data
     return []
コード例 #6
0
class LeadSerializer(
    RemoveNullFieldsMixin, DynamicFieldsMixin, WriteOnlyOnCreateSerializerMixin, UserResourceSerializer,
):
    """
    Lead Model Serializer
    """
    # annotated in lead.get_for
    entries_count = serializers.IntegerField(read_only=True)
    controlled_entries_count = serializers.IntegerField(read_only=True)
    filtered_entries_count = serializers.IntegerField(read_only=True)
    controlled_filtered_entries_count = serializers.IntegerField(read_only=True)

    assessment_id = serializers.IntegerField(read_only=True)

    priority_display = serializers.CharField(source='get_priority_display', read_only=True)

    attachment = SimpleFileSerializer(required=False)
    thumbnail = URLCachedFileField(
        source='leadpreview.thumbnail',
        read_only=True,
    )
    thumbnail_height = serializers.IntegerField(
        source='leadpreview.thumbnail_height',
        read_only=True,
    )
    thumbnail_width = serializers.IntegerField(
        source='leadpreview.thumbnail_width',
        read_only=True,
    )
    word_count = serializers.IntegerField(
        source='leadpreview.word_count',
        read_only=True,
    )
    page_count = serializers.IntegerField(
        source='leadpreview.page_count',
        read_only=True,
    )
    classified_doc_id = serializers.IntegerField(
        source='leadpreview.classified_doc_id',
        read_only=True,
    )

    # TODO: Remove (Legacy)
    author_detail = SimpleOrganizationSerializer(source='author', read_only=True)

    authors_detail = SimpleOrganizationSerializer(source='authors', many=True, read_only=True)
    source_detail = SimpleOrganizationSerializer(source='source', read_only=True)

    assignee_details = SimpleUserSerializer(
        source='get_assignee',
        # many=True,
        read_only=True,
    )
    assignee = SingleValueThayMayBeListField(
        source='get_assignee.id',
        required=False,
    )
    tabular_book = serializers.SerializerMethodField()
    emm_triggers = LeadEMMTriggerSerializer(many=True, required=False)
    emm_entities = EMMEntitySerializer(many=True, required=False)
    # extra fields added from entryleadserializer
    confidentiality_display = serializers.CharField(source='get_confidentiality_display', read_only=True)

    class Meta:
        model = Lead
        fields = ('__all__')
        # Legacy Fields
        read_only_fields = ('author_raw', 'source_raw')
        write_only_on_create_fields = ['emm_triggers', 'emm_entities']

    def get_tabular_book(self, obj):
        file = obj.attachment
        if file and hasattr(file, 'book'):
            return file.book.id
        return None

    @staticmethod
    def add_update__validate(data, instance, attachment=None):
        project = data.get('project', instance and instance.project)
        source_type = data.get('source_type', instance and instance.source_type)
        text = data.get('text', instance and instance.text)
        url = data.get('url', instance and instance.url)

        return raise_or_return_existing_lead(
            project,
            instance,
            source_type,
            url,
            text,
            attachment,
        )

    def validate_is_assessment_lead(self, value):
        # Allow setting True
        # For False make sure there are no assessment attached.
        if value is False and hasattr(self.instance, 'assessment'):
            raise serializers.ValidationError('Lead already has an assessment.')
        return value

    def validate(self, data):
        attachment_id = self.get_initial().get('attachment', {}).get('id')
        LeadSerializer.add_update__validate(
            data, self.instance,
            File.objects.filter(pk=attachment_id).first()
        )
        return data

    # TODO: Probably also validate assignee to valid list of users

    def create(self, validated_data):
        assignee_field = validated_data.pop('get_assignee', None)
        assignee_id = assignee_field and assignee_field.get('id', None)
        assignee = assignee_id and get_object_or_404(User, id=assignee_id)

        emm_triggers = validated_data.pop('emm_triggers', [])
        emm_entities_names = [
            entity['name']
            for entity in validated_data.pop('emm_entities', [])
            if isinstance(entity, dict) and 'name' in entity
        ]

        lead = super().create(validated_data)

        for entity in EMMEntity.objects.filter(name__in=emm_entities_names):
            lead.emm_entities.add(entity)
        lead.save()

        with transaction.atomic():
            for trigger in emm_triggers:
                LeadEMMTrigger.objects.create(**trigger, lead=lead)

        if assignee:
            lead.assignee.add(assignee)
        return lead

    def update(self, instance, validated_data):
        assignee_field = validated_data.pop('get_assignee', None)
        assignee_id = assignee_field and assignee_field.get('id', None)
        assignee = assignee_id and get_object_or_404(User, id=assignee_id)

        # We do not update triggers and entities
        validated_data.pop('emm_entities', None)
        validated_data.pop('emm_triggers', None)

        lead = super().update(instance, validated_data)

        if assignee_field:
            lead.assignee.clear()
            if assignee:
                lead.assignee.add(assignee)
        return lead
コード例 #7
0
 def get_organization(self, title, search):
     org = search.get(title)
     if org:
         return SimpleOrganizationSerializer(org).data