Exemplo n.º 1
0
    def validate(self, validated_data):
        """ Responsible of cleaning and validating user input data """
        initial_data = self.initial_data
        username = self.context['request'].user.username

        if 'message' not in validated_data and self.context['request'].method == 'POST':
            raise serializers.ValidationError('Messsage field is required for POST Request')

        if 'listing' in initial_data and 'agency' in initial_data:
            raise serializers.ValidationError('Notification can only be listing or agency')

        # TODO: Figure how to get listing data using validated data
        listing = initial_data.get('listing')
        if listing and listing.get('id'):
            try:
                validated_data['listing'] = listing_model_access.get_listing_by_id(
                    username, initial_data['listing']['id'], True)
            except ObjectDoesNotExist:
                raise serializers.ValidationError('Valid Listing ID is required, Could not find listing')
        else:
            validated_data['listing'] = None

        agency = initial_data.get('agency')
        if agency and agency.get('id'):
            try:
                validated_data['agency'] = agency_model_access.get_agency_by_id(
                    initial_data['agency']['id'], True)
            except ObjectDoesNotExist:
                raise serializers.ValidationError('Valid agency ID is required, Could not find listing')
        else:
            validated_data['agency'] = None

        return validated_data
Exemplo n.º 2
0
 def test_get_non_existent_agency_by_id(self):
     agency = model_access.get_agency_by_id(0)
     self.assertIsNone(agency)
Exemplo n.º 3
0
 def test__get_agency_by_id__not_found_raises_error(self):
     with self.assertRaises(Agency.DoesNotExist):
         model_access.get_agency_by_id(0, True)
Exemplo n.º 4
0
    def test__get_agency_by_id__not_found(self):
        result = model_access.get_agency_by_id(0)

        self.assertIsNone(result)
Exemplo n.º 5
0
    def validate(self, validated_data):
        """ Responsible of cleaning and validating user input data """
        validated_data['error'] = None
        initial_data = self.initial_data
        username = self.context['request'].user.username

        # Check for notification types
        key_type_list = []

        if 'listing' in initial_data:
            key_type_list.append('listing')

        if 'agency' in initial_data:
            key_type_list.append('agency')

        if 'peer' in initial_data:
            key_type_list.append('peer')

        if len(key_type_list) >= 2:
            raise serializers.ValidationError('Notifications can only be one type. Input: {0}'.format(key_type_list))

        if 'message' not in validated_data and self.context['request'].method == 'POST':
            raise serializers.ValidationError('Messsage field is required for POST Request')

        # TODO: Figure how to get listing data using validated data
        listing = initial_data.get('listing')
        if listing:
            if listing.get('id'):
                try:
                    validated_data['listing'] = listing_model_access.get_listing_by_id(
                        username, initial_data['listing']['id'], True)
                except ObjectDoesNotExist:
                    raise serializers.ValidationError('Could not find listing')
            else:
                raise serializers.ValidationError('Valid Listing ID is required')
        else:
            validated_data['listing'] = None

        # Agency Validation
        agency = initial_data.get('agency')
        if agency:
            if agency.get('id'):
                try:
                    validated_data['agency'] = agency_model_access.get_agency_by_id(
                        initial_data['agency']['id'], True)
                except ObjectDoesNotExist:
                    raise serializers.ValidationError('Could not find agency')
            else:
                raise serializers.ValidationError('Valid Agency ID is required')
        else:
            validated_data['agency'] = None

        # Peer Validation
        peer = initial_data.get('peer')
        if peer:
            temp_peer = {}

            if peer.get('user'):
                temp_peer['user'] = peer.get('user')

            if peer.get('folder_name'):
                temp_peer['folder_name'] = peer.get('folder_name')

            target_username = temp_peer.get('user', {}).get('username')

            if not target_username:
                raise serializers.ValidationError('Valid Username is Required')

            target_username_profile = generic_model_access.get_profile(target_username)

            if not target_username_profile:
                raise serializers.ValidationError('Valid User is Required')

            # Folder Validation - Optional Field
            temp_folder_name = temp_peer.get('folder_name')
            if temp_folder_name:
                library_query = library_model_access.get_self_application_library(username, folder_name=temp_folder_name)
                temp_peer['_bookmark_listing_ids'] = [library_query_entry.listing.id for library_query_entry in library_query]

                # temp_peer['_user_folders'] = library_serializers.UserLibrarySerializer(library_query,
                #      many=True, context={'request': self.context['request']}).data

                if len(temp_peer['_bookmark_listing_ids']) == 0:
                    raise serializers.ValidationError('No entries in target folder')

            validated_data['peer'] = temp_peer
        else:
            validated_data['peer'] = None

        return validated_data
Exemplo n.º 6
0
    def validate(self, validated_data):
        """ Responsible of cleaning and validating user input data """
        validated_data['error'] = None
        initial_data = self.initial_data
        username = self.context['request'].user.username

        # Check for notification types
        key_type_list = []

        if 'listing' in initial_data:
            key_type_list.append('listing')

        if 'agency' in initial_data:
            key_type_list.append('agency')

        if 'peer' in initial_data:
            key_type_list.append('peer')

        if len(key_type_list) >= 2:
            raise serializers.ValidationError(
                'Notifications can only be one type. Input: {0}'.format(
                    key_type_list))

        if 'message' not in validated_data and self.context[
                'request'].method == 'POST':
            raise serializers.ValidationError(
                'Messsage field is required for POST Request')

        # TODO: Figure how to get listing data using validated data
        listing = initial_data.get('listing')
        if listing:
            if listing.get('id'):
                try:
                    validated_data[
                        'listing'] = listing_model_access.get_listing_by_id(
                            username, initial_data['listing']['id'], True)
                except ObjectDoesNotExist:
                    raise serializers.ValidationError('Could not find listing')
            else:
                raise serializers.ValidationError(
                    'Valid Listing ID is required')
        else:
            validated_data['listing'] = None

        # Agency Validation
        agency = initial_data.get('agency')
        if agency:
            if agency.get('id'):
                try:
                    validated_data[
                        'agency'] = agency_model_access.get_agency_by_id(
                            initial_data['agency']['id'], True)
                except ObjectDoesNotExist:
                    raise serializers.ValidationError('Could not find agency')
            else:
                raise serializers.ValidationError(
                    'Valid Agency ID is required')
        else:
            validated_data['agency'] = None

        # Peer Validation
        peer = initial_data.get('peer')
        if peer:
            temp_peer = {}

            if peer.get('user'):
                temp_peer['user'] = peer.get('user')

            if peer.get('folder_name'):
                temp_peer['folder_name'] = peer.get('folder_name')

            target_username = temp_peer.get('user', {}).get('username')

            if not target_username:
                raise serializers.ValidationError('Valid Username is Required')

            target_username_profile = generic_model_access.get_profile(
                target_username)

            if not target_username_profile:
                raise serializers.ValidationError('Valid User is Required')

            validated_data['entity_target'] = target_username_profile
            validated_data['entity_id'] = target_username_profile.pk
            # Folder Validation - Optional Field
            temp_folder_name = temp_peer.get('folder_name')
            if temp_folder_name:
                library_query = library_model_access.get_self_application_library(
                    username, folder_name=temp_folder_name)
                temp_peer['_bookmark_listing_ids'] = [
                    library_query_entry.listing.id
                    for library_query_entry in library_query
                ]

                # temp_peer['_user_folders'] = library_serializers.UserLibrarySerializer(library_query,
                #      many=True, context={'request': self.context['request']}).data

                if len(temp_peer['_bookmark_listing_ids']) == 0:
                    raise serializers.ValidationError(
                        'No entries in target folder')

            validated_data['peer'] = temp_peer
        else:
            validated_data['peer'] = None

        return validated_data