Example #1
0
class CategoryListingSerializer(serializers.HyperlinkedModelSerializer):
    agency = agency_serializers.CreateAgencySerializer(required=False)
    categories = ListingCategorySerializer(many=True, required=False)
    owners = CreateListingProfileSerializer(required=False, many=True)

    class Meta:
        model = models.Listing
        fields = ('unique_name', 'title', 'id', 'agency', 'categories',
                  'owners')

        extra_kwargs = {
            'unique_name': {
                'validators': []
            },
            'title': {
                'validators': []
            },
            'agency': {
                'validators': []
            },
            'owners': {
                'validators': []
            }
        }

    def validate(self, data):
        print('Inside of validate')
        print(data)
        return data

    def create(self, validated_data):
        # print('Inside of create')
        # Listing
        return validated_data
Example #2
0
class StorefrontListingSerializer(serializers.HyperlinkedModelSerializer):
    agency = agency_serializers.CreateAgencySerializer(required=False)
    large_banner_icon = image_serializers.ImageSerializer(required=False, allow_null=True)
    banner_icon = image_serializers.ImageSerializer(required=False, allow_null=True)
    owners = listing_serializers.CreateListingProfileSerializer(required=False, allow_null=True, many=True)
    listing_type = listing_serializers.ListingTypeSerializer(required=False, allow_null=True)

    class Meta:
        model = models.Listing
        fields = ('id',
                  'title',
                  'agency',
                  'avg_rate',
                  'total_votes',
                  'total_reviews',
                  'feedback_score',
                  'is_private',
                  'is_bookmarked',
                  'feedback',
                  'description_short',
                  'security_marking',
                  'usage_requirements',
                  'system_requirements',
                  'launch_url',
                  'large_banner_icon',
                  'banner_icon',
                  'owners',
                  'unique_name',
                  'is_enabled',
                  'listing_type',
                  'edited_date',
                  'approved_date')

    def _is_bookmarked(self, request_user, request_listing):
        # TODO: put in listing model_access.py call from there > creative name for method
        bookmarks = models.ApplicationLibraryEntry.objects.filter(listing=request_listing, owner=request_user)
        return len(bookmarks) >= 1

    def _feedback(self, request_user, request_listing):
        # TODO: put in listing model_access.py call from there > creative name for method
        recommendation = models.RecommendationFeedback.objects.for_user(request_user).filter(target_profile=request_user, target_listing=request_listing).first()

        if recommendation is None:
            return 0

        return recommendation.feedback

    def to_representation(self, data):
        ret = super(StorefrontListingSerializer, self).to_representation(data)
        request_user = generic_model_access.get_profile(self.context['request'].user)  # TODO: Get the profile from view request instead of getting it from db again

        ret['feedback'] = self._feedback(request_user, data)
        ret['is_bookmarked'] = self._is_bookmarked(request_user, data)
        return ret
Example #3
0
class ShortListingSerializer(serializers.HyperlinkedModelSerializer):
    agency = agency_serializers.CreateAgencySerializer(required=False)

    class Meta:
        model = models.Listing
        fields = ('unique_name', 'title', 'id', 'agency', 'small_icon', 'is_deleted')
Example #4
0
class ListingSerializer(serializers.ModelSerializer):
    is_bookmarked = serializers.ReadOnlyField()
    feedback = serializers.ReadOnlyField()
    screenshots = ScreenshotSerializer(many=True, required=False)
    doc_urls = DocUrlSerializer(many=True, required=False)
    owners = CreateListingProfileSerializer(required=False, many=True)
    categories = category_serializers.ListingCategorySerializer(many=True, required=False)
    tags = TagSerializer(many=True, required=False)
    custom_fields = CustomFieldValueSerializer(many=True, required=False)
    contacts = contact_type_serializers.ContactSerializer(many=True, required=False)
    intents = intent_serializers.IntentSerializer(many=True, required=False)
    small_icon = image_serializers.ImageSerializer(required=False, allow_null=True)
    large_icon = image_serializers.ImageSerializer(required=False, allow_null=True)
    banner_icon = image_serializers.ImageSerializer(required=False, allow_null=True)
    large_banner_icon = image_serializers.ImageSerializer(required=False, allow_null=True)
    agency = agency_serializers.CreateAgencySerializer(required=False)
    last_activity = ListingActivitySerializer(required=False, read_only=True)
    current_rejection = RejectionListingActivitySerializer(required=False, read_only=True)
    listing_type = ListingTypeSerializer(required=False, allow_null=True)
    cert_issues = CertIssuesField(required=False)

    class Meta:
        model = models.Listing
        depth = 2
        fields = '__all__'

    @staticmethod
    def setup_eager_loading(queryset):
        # prefetch_related many-to-many relationships
        queryset = queryset.prefetch_related('agency__icon')
        queryset = queryset.prefetch_related('screenshots')
        queryset = queryset.prefetch_related('screenshots__small_image')
        queryset = queryset.prefetch_related('screenshots__large_image')
        queryset = queryset.prefetch_related('doc_urls')
        queryset = queryset.prefetch_related('owners')
        queryset = queryset.prefetch_related('owners__user')
        queryset = queryset.prefetch_related('owners__organizations')
        queryset = queryset.prefetch_related('owners__stewarded_organizations')
        queryset = queryset.prefetch_related('categories')
        queryset = queryset.prefetch_related('tags')
        queryset = queryset.prefetch_related('contacts')
        queryset = queryset.prefetch_related('contacts__contact_type')
        queryset = queryset.prefetch_related('listing_type')
        queryset = queryset.prefetch_related('last_activity')
        queryset = queryset.prefetch_related('last_activity__change_details')
        queryset = queryset.prefetch_related('last_activity__author')
        queryset = queryset.prefetch_related('last_activity__author__organizations')
        queryset = queryset.prefetch_related('last_activity__author__stewarded_organizations')
        queryset = queryset.prefetch_related('last_activity__listing')
        queryset = queryset.prefetch_related('last_activity__listing__contacts')
        queryset = queryset.prefetch_related('last_activity__listing__owners')
        queryset = queryset.prefetch_related('last_activity__listing__owners__user')
        queryset = queryset.prefetch_related('last_activity__listing__categories')
        queryset = queryset.prefetch_related('last_activity__listing__tags')
        queryset = queryset.prefetch_related('last_activity__listing__intents')
        queryset = queryset.prefetch_related('current_rejection')
        queryset = queryset.prefetch_related('intents')
        queryset = queryset.prefetch_related('intents__icon')
        return queryset

    def _is_bookmarked(self, request_user, request_listing):
        # TODO: put in listing model_access.py call from there > creative name for method
        bookmarks = models.ApplicationLibraryEntry.objects.filter(listing=request_listing, owner=request_user)
        return len(bookmarks) >= 1

    def _feedback(self, request_user, request_listing):
        # TODO: put in listing model_access.py call from there > creative name for method
        recommendation = models.RecommendationFeedback.objects.for_user(request_user).filter(target_profile=request_user, target_listing=request_listing).first()

        if recommendation is None:
            return 0

        return recommendation.feedback

    def to_representation(self, data):
        ret = super(ListingSerializer, self).to_representation(data)

        anonymize_identifiable_data = system_anonymize_identifiable_data(self.context['request'].user.username)
        request_user = generic_model_access.get_profile(self.context['request'].user)  # TODO: Get the profile from view request instead of getting it from db again

        if anonymize_identifiable_data:
            ret['contacts'] = []
        check_failed = []
        # owners
        if 'owners' in ret and not anonymize_identifiable_data:
            for owner in ret['owners']:
                user_dict = owner.get('user')
                user_username = None if user_dict is None else user_dict.get('username')

                # if not user_username:
                # raise serializers.ValidationError('Owner field requires correct format')

                owner_profile = generic_model_access.get_profile(user_username)
                # if not owner_profile:
                #    raise serializers.ValidationError('Owner Profile not found')

                # Don't allow user to select a security marking that is above
                # their own access level\
                try:
                    if system_has_access_control(owner_profile.user.username, ret.get('security_marking')) is False:
                        check_failed.append(owner_profile.user.username)
                        # raise serializers.ValidationError(owner_profile.user.username + 'User certificate is invalid')
                except Exception:
                    check_failed.append(owner_profile.user.username)

        ret['cert_issues'] = check_failed
        ret['feedback'] = self._feedback(request_user, data)
        ret['is_bookmarked'] = self._is_bookmarked(request_user, data)
        return ret

    def validate(self, data):
        return validate_listing(self, data)

    def create(self, validated_data):
        return create_listing(self, validated_data)

    def update(self, instance, validated_data):
        return update_listing(self, instance, validated_data)