class PhoneSerializer(ModelSerializer): id = IntegerField(read_only=False) class Meta: model = Phone fields = '__all__'
class SaveDocumentIntegerAnswerSerializer(SaveAnswerSerializer): value = IntegerField() class Meta(SaveAnswerSerializer.Meta): pass
class UserSerializer(ModelSerializer): public_email = EmailField(required=False) national_code = CharField(required=False, max_length=20, allow_blank=True) profile_media = IntegerField(required=False, allow_null=True) birth_date = CharField(required=False, max_length=10, allow_blank=True) fax = CharField(required=False, allow_blank=True) telegram_account = CharField(required=False, max_length=256, allow_blank=True) description = CharField(required=False, allow_blank=True) web_site = ListField(child=URLField(required=False), required=False) phone = ListField(child=CharField(max_length=23, required=False), required=False) mobile = ListField(child=CharField(max_length=23, required=False), required=False) password = CharField(max_length=255) auth_mobile = CharField(required=False, validators=[RegexValidator('^[0][9][0-9]{9,9}$')]) class Meta: model = User fields = [ 'id', 'username', 'password', 'first_name', 'last_name', 'email', 'password', 'date_joined', 'web_site', 'public_email', 'national_code', 'profile_media', 'birth_date', 'fax', 'telegram_account', 'description', 'phone', 'mobile', 'auth_mobile' ] def create(self, validated_data): validated_data['username'] = validated_data['username'].lower() validated_data['username'] = validated_data['username'].replace( '.', '') user_validated_data = self.get_user_validated_args(**validated_data) user = User.objects.create(**user_validated_data) user.set_password(validated_data['password']) user.save() profile_validated_data = self.get_profile_validated_data( **validated_data) profile = Profile.objects.get(profile_user=user) # set validated data to profile object for key in profile_validated_data: setattr(profile, key, validated_data.get(key)) profile.save() user_strength = StrengthStates.objects.get(strength_user=user) # check for first & last name strength rate if validated_data.get('first_name') is not None and validated_data.get( 'last_name') is not None: profile.profile_strength += 5 profile.save() user_strength.first_last_name_obtained = True # check for profile media strength rate if 'profile_media' in profile_validated_data: profile.profile_strength += 10 profile.save() user_strength.profile_media_obtained = True user_strength.registration_obtained = True user_strength.save() # add user to default exchange add_user_to_default_exchange(user) return user def update(self, instance, validated_data): user = User.objects.get(pk=instance.id) profile = Profile.objects.get(profile_user=user) user_validated_data = self.get_user_validated_args(**validated_data) try: user_strength = StrengthStates.objects.get(strength_user=user) except StrengthStates.DoesNotExist: user_strength = StrengthStates.objects.create(strength_user=user) # check for first name strength rate if 'first_name' in user_validated_data and user_validated_data[ 'first_name'] != '': user.first_name = user_validated_data['first_name'] if (user.first_name is None or user.first_name == '') and (user.last_name is not None or user.last_name != ''): profile.profile_strength += 5 user_strength.first_last_name_obtained = True # check for last name strength rate if 'last_name' in user_validated_data and user_validated_data[ 'last_name'] != '': user.last_name = user_validated_data['last_name'] if (user.last_name is None or user.last_name == '') and (user.first_name is not None or user.first_name != ''): profile.profile_strength += 5 user_strength.first_last_name_obtained = True # set validated data to user object for key in user_validated_data: if key != 'first_name' and key != 'last_name': setattr(user, key, user_validated_data.get(key)) if 'password' in validated_data: user.set_password(validated_data['password']) user.save() profile_validated_data = self.get_profile_validated_data( **validated_data) # check for profile media strength rate if 'profile_media' in profile_validated_data and profile_validated_data['profile_media'] != '' and \ profile_validated_data['profile_media'] is not None: profile.profile_media = profile_validated_data['profile_media'] if profile.profile_media == '' or profile.profile_media is None: profile.profile_strength += 10 user_strength.profile_media_obtained = True # set validated data to profile object for key in profile_validated_data: if key != 'profile_media': setattr(profile, key, validated_data.get(key)) profile.save() user_strength.save() return user @staticmethod def validate_first_name(value): if len(value) > 20: error = { 'message': "maximum length for first name is 20 character" } raise ValidationError(error) return value @staticmethod def validate_email(value): if value is None: error = {'message': "email field is required"} raise ValidationError(error) elif len(value) > 0: user_count = User.objects.filter(email=value).count() if user_count == 0: return value error = {'message': "email already exist"} raise ValidationError(error) error = {'message': "email can not be blank"} raise ValidationError(error) @staticmethod def validate_last_name(value): if len(value) > 20: error = {'message': "maximum length for last name is 20 character"} raise ValidationError(error) return value @staticmethod def validate_username(value): if len(value) < 5: error = {'message': "minimum length for last name is 5 character"} raise ValidationError(error) if len(value) > 32: error = {'message': "minimum length for last name is 5 character"} raise ValidationError(error) return value @staticmethod def validate_password(value): if len(value) < 8: error = {'message': "minimum length for password is 8 character"} raise ValidationError(error) return value def get_user_validated_args(self, **kwargs): user_kwargs = {} if 'username' in kwargs: user_kwargs['username'] = kwargs['username'] if 'first_name' in kwargs: user_kwargs['first_name'] = kwargs['first_name'] if 'last_name' in kwargs: user_kwargs['last_name'] = kwargs['last_name'] if 'email' in kwargs: user_kwargs['email'] = kwargs['email'] return user_kwargs def get_profile_validated_data(self, **kwargs): profile_kwargs = {} if 'public_email' in kwargs: profile_kwargs['public_email'] = kwargs['public_email'] if 'national_code' in kwargs: profile_kwargs['national_code'] = kwargs['national_code'] if 'profile_media' in kwargs: profile_kwargs['profile_media'] = kwargs['profile_media'] if 'birth_date' in kwargs: profile_kwargs['birth_date'] = kwargs['birth_date'] if 'fax' in kwargs: profile_kwargs['fax'] = kwargs['fax'] if 'telegram_account' in kwargs: profile_kwargs['telegram_account'] = kwargs['telegram_account'] if 'description' in kwargs: profile_kwargs['description'] = kwargs['description'] if 'web_site' in kwargs: profile_kwargs['web_site'] = kwargs['web_site'] if 'phone' in kwargs: profile_kwargs['phone'] = kwargs['phone'] if 'mobile' in kwargs: profile_kwargs['mobile'] = kwargs['mobile'] if 'auth_mobile' in kwargs: profile_kwargs['auth_mobile'] = kwargs['auth_mobile'] return profile_kwargs
class UserSerializer(Serializer): id = IntegerField(read_only=True) username = CharField(max_length=150) first_name = CharField(max_length=150) last_name = CharField(max_length=150)
class GitSerializer(Serializer): time = IntegerField(min_value=0, required=True, label='Time', help_text='Point of time to run report.') url = ChoiceField( [], label='Git URL', default='https://git.openstack.org/openstack/openstack-ansible', help_text='Please select a git url.') filters = ListField(child=FilterSerializer(), label='Filters', help_text="Additional filter criteria.", required=False) def __init__(self, *args, **kwargs): """Init the serializer with updated list of git urls.""" self.fields['url'].choices = self.git_urls() super(GitSerializer, self).__init__(*args, **kwargs) def git_urls(self): """Build list of selectable git urls. :returns: All git urls :rtype: list """ q = ColumnQuery('GitUrl') q.add_column('GitUrl', 'url', 'url') q.orderby('url', 'ASC', label='GitUrl') urls = [] page = 1 pagesize = 1000 page_rows = q.page(page, pagesize) while (page_rows): for row in page_rows: urls.append(row['url']) page += 1 page_rows = q.page(page, pagesize) return urls def validate(self, data): """Custom validation. Ensure filters are acting upon a filterable model. :param data: Data to validate :type data: dict :returns: Validated data :rtype: dict """ model_set = set(FILTERABLE_MODELS) filter_errors = OrderedDict() for i, f in enumerate(data.get('filters', [])): if f['model'] not in model_set: filter_errors[i] = ('Model {} is not one of [{}].'.format( f['model'], ', '.join(FILTERABLE_MODELS))) if filter_errors: raise ValidationError({'filters': filter_errors}) return data def form_data(self): """Describes web client form associated with this serializer. :returns: List of dict objects :rtype: list """ return [{ 'name': 'time', 'label': self.fields['time'].label, 'help_text': self.fields['time'].help_text, 'required': self.fields['time'].required, 'component': 'Time', 'many': False }, { 'name': 'url', 'label': self.fields['url'].label, 'help_text': self.fields['url'].help_text, 'required': self.fields['url'].required, 'default': self.fields['url'].default, 'component': 'Select', 'choices': self.fields['url'].choices }, { 'name': 'filters', 'label': self.fields['filters'].label, 'help_text': self.fields['filters'].help_text, 'component': 'Filter', 'many': True, 'models': FILTERABLE_MODELS }]
class StudentPageVisitsHistogramSerializer(Serializer): lms_user_id = IntegerField() num_visits = IntegerField()
class PkAndTitleSerializer(Serializer): pk = IntegerField() s = CharField()
class BotContextsSerializer(Serializer): name = CharField(required=False, allow_blank=True, max_length=100) parameters = BotParametersSerializer() lifespan = IntegerField(required=False)
class ContextOutSerializer(Serializer): name = CharField(required=False, max_length=100) lifespan = IntegerField() parameters = DictField( child=CharField(), )
class Task1Serializer(TaskSerializerBase): integral = CharField() tolerance = IntegerField()
class ProgramAdminSerializer(ModelSerializer): id = IntegerField(allow_null=True, required=False) name = CharField(required=True, max_length=255) funding_status = CharField(required=True) gaitid = CharField(required=False, allow_blank=True, allow_null=True) description = CharField(allow_null=True, allow_blank=True) sector = NestedSectorSerializer(required=True, many=True) country = NestedCountrySerializer(required=True, many=True) auto_number_indicators = BooleanField(required=False) _using_results_framework = IntegerField(required=False, allow_null=True) def validate_country(self, values): if not values: raise ValidationError("This field may not be blank.") return values class Meta: model = Program fields = ( 'id', 'name', 'funding_status', 'gaitid', 'description', 'sector', 'country', 'auto_number_indicators', '_using_results_framework' ) def to_representation(self, program, with_aggregates=True): ret = super(ProgramAdminSerializer, self).to_representation(program) if not with_aggregates: return ret # Some n+1 queries here. If this is slow, Fix in queryset either either with rawsql or remodel. program_users = ( TolaUser.objects.filter(programs__id=program.id).select_related('organization') | TolaUser.objects.filter(countries__program=program.id).select_related('organization') ).distinct() organizations = set([tu.organization_id for tu in program_users if tu.organization_id]) organization_count = len(organizations) ret['program_users'] = len(program_users) ret['organizations'] = organization_count ret['onlyOrganizationId'] = organizations.pop() if organization_count == 1 else None if ret['_using_results_framework'] == Program.RF_ALWAYS: ret.pop('_using_results_framework') return ret @transaction.atomic def create(self, validated_data): if '_using_results_framework' in validated_data and \ validated_data['_using_results_framework'] is None: validated_data.pop('_using_results_framework') country = validated_data.pop('country') sector = validated_data.pop('sector') if not validated_data['gaitid']: validated_data.pop('gaitid') program = super(ProgramAdminSerializer, self).create(validated_data) program.country.add(*country) program.sector.add(*sector) program.save() ProgramAdminAuditLog.created( program=program, created_by=self.context.get('request').user.tola_user, entry=program.admin_logged_fields, ) return program @transaction.atomic def update(self, instance, validated_data): previous_state = instance.admin_logged_fields if '_using_results_framework' in validated_data and validated_data['_using_results_framework'] is None: validated_data['_using_results_framework'] = instance._using_results_framework # default for any unmigrated program is "auto" - so if someone sets their program to "not grouping" - reset it # to default ("auto") if '_using_results_framework' in validated_data and validated_data['_using_results_framework'] == instance.NOT_MIGRATED: validated_data['auto_number_indicators'] = True original_countries = instance.country.all() incoming_countries = validated_data.pop('country') added_countries = [x for x in incoming_countries if x not in original_countries] removed_countries = [x for x in original_countries if x not in incoming_countries] original_sectors = instance.sector.all() incoming_sectors = validated_data.pop('sector') added_sectors = [x for x in incoming_sectors if x not in original_sectors] removed_sectors = [x for x in original_sectors if x not in incoming_sectors] instance.country.remove(*removed_countries) instance.country.add(*added_countries) instance.sector.remove(*removed_sectors) instance.sector.add(*added_sectors) ProgramAccess.objects.filter(program=instance, country__in=removed_countries).delete() updated_instance = super(ProgramAdminSerializer, self).update(instance, validated_data) ProgramAdminAuditLog.updated( program=instance, changed_by=self.context.get('request').user.tola_user, old=previous_state, new=instance.admin_logged_fields, ) return updated_instance
class LivestockMarketingSerializer(ModelSerializer): id = IntegerField(read_only=False) class Meta: model = LivestockMarketing fields = '__all__'
class ManagementTypeSerializer(ModelSerializer): id = IntegerField(read_only=False) class Meta: model = ManagementType fields = '__all__'
class LandAreaSerializer(ModelSerializer): id = IntegerField(read_only=False) class Meta: model = LandArea fields = '__all__'
class MyLeaderboardRequestSerializer(Serializer): distance = IntegerField(min_value=0)
class GenericSerializer(Serializer): time = IntegerField( min_value=0, required=True, label='Time', help_text='Point of time to run report.' ) model = ChoiceField( _models, label="Model", default='Environment', help_text=( "Please select an end model to pull from. You can create columns " "from any model in the path to this model." ) ) columns = ListField( child=ModelPropertySerializer(), min_length=1, label='Columns', help_text='Please choose each column to be listed in the report.' ) filters = ListField( child=FilterSerializer(), label="Filters", help_text="Additional filter criteria.", required=False ) def validate(self, data): """Custom validation. Ensure that all columns are properties to models in the path to the chosen model. Ensure that all filters are on properties to models in the path to the chosen model. :param data: Data to validate :type data: dict """ model_set = set([t[0] for t in registry.path(data['model'])]) model_set.add(data['model']) column_errors = OrderedDict() for i, c in enumerate(data.get('columns', [])): if c['model'] not in model_set: column_errors[i] = ( 'Model {} is not in path of {}' .format(c['model'], data['model']) ) if column_errors: raise ValidationError({'columns': column_errors}) filter_errors = OrderedDict() for i, f in enumerate(data.get('filters', [])): if f['model'] not in model_set: filter_errors[i] = ( 'Model {} not in path of {}' .format(f['model'], data['model']) ) if filter_errors: raise ValidationError({'filters': filter_errors}) return data def form_data(self): """Describes web client form associated with this serializer. :returns: List of dict objects :rtype: list """ return [ { 'name': 'time', 'label': self.fields['time'].label, 'help_text': self.fields['time'].help_text, 'required': self.fields['time'].required, 'component': 'Time', 'many': False }, { 'name': 'model', 'label': self.fields['model'].label, 'help_text': self.fields['model'].help_text, 'required': self.fields['model'].required, 'default': self.fields['model'].default, 'component': 'Model' }, { 'name': 'columns', 'label': self.fields['columns'].label, 'help_text': self.fields['columns'].help_text, 'min_length': self.fields['columns'].min_length, 'component': 'ModelProperty', 'many': True, 'watches': 'model' }, { 'name': 'filters', 'label': self.fields['filters'].label, 'help_text': self.fields['filters'].help_text, 'component': 'Filter', 'many': True, 'watches': 'model' } ]
class SourceSerializer(NotEmptySerializer): """Serializer for the Source model.""" name = CharField(required=True, max_length=64) source_type = ValidStringChoiceField(required=False, choices=Source.SOURCE_TYPE_CHOICES) port = IntegerField(required=False, min_value=0, allow_null=True) hosts = CustomJSONField(required=True) options = SourceOptionsSerializer(required=False, many=False) credentials = CredentialsField(many=True, queryset=Credential.objects.all()) class Meta: """Metadata for the serializer.""" model = Source fields = '__all__' # pylint: disable=too-many-branches,too-many-statements @transaction.atomic def create(self, validated_data): """Create a source.""" name = validated_data.get('name') check_for_existing_name(Source.objects, name, _(messages.SOURCE_NAME_ALREADY_EXISTS % name)) if 'source_type' not in validated_data: error = {'source_type': [_(messages.SOURCE_TYPE_REQ)]} raise ValidationError(error) source_type = validated_data.get('source_type') credentials = validated_data.pop('credentials') hosts_list = validated_data.pop('hosts', None) port = None if 'port' in validated_data: port = validated_data['port'] options = validated_data.pop('options', None) if source_type == Source.NETWORK_SOURCE_TYPE: if credentials: for cred in credentials: SourceSerializer.check_credential_type(source_type, cred) if port is None: validated_data['port'] = 22 elif source_type == Source.VCENTER_SOURCE_TYPE: if port is None: validated_data['port'] = 443 if hosts_list and len(hosts_list) != 1: error = {'hosts': [_(messages.VC_ONE_HOST)]} raise ValidationError(error) elif hosts_list and '[' in hosts_list[0]: error = {'hosts': [_(messages.VC_ONE_HOST)]} raise ValidationError(error) if credentials and len(credentials) > 1: error = {'credentials': [_(messages.VC_ONE_CRED)]} raise ValidationError(error) elif credentials and len(credentials) == 1: SourceSerializer.check_credential_type(source_type, credentials[0]) elif source_type == Source.SATELLITE_SOURCE_TYPE: if port is None: validated_data['port'] = 443 if hosts_list and len(hosts_list) != 1: error = {'hosts': [_(messages.SAT_ONE_HOST)]} raise ValidationError(error) elif hosts_list and '[' in hosts_list[0]: error = {'hosts': [_(messages.VC_ONE_HOST)]} raise ValidationError(error) if credentials and len(credentials) > 1: error = {'credentials': [_(messages.SAT_ONE_CRED)]} raise ValidationError(error) elif credentials and len(credentials) == 1: SourceSerializer.check_credential_type(source_type, credentials[0]) source = Source.objects.create(**validated_data) if options: if source_type == Source.SATELLITE_SOURCE_TYPE: if options.get('ssl_cert_verify') is None: options['ssl_cert_verify'] = True if (source_type == Source.VCENTER_SOURCE_TYPE and options.get('ssl_cert_verify') is None): options['ssl_cert_verify'] = True if source_type == Source.NETWORK_SOURCE_TYPE and \ bool(options): invalid_options = ', '.join([key for key in options.keys()]) error = { 'options': [ _(messages.NET_SSL_OPTIONS_NOT_ALLOWED % invalid_options) ] } raise ValidationError(error) options = SourceOptions.objects.create(**options) options.save() source.options = options elif not options and source_type == Source.SATELLITE_SOURCE_TYPE: options = SourceOptions() options.ssl_cert_verify = True options.save() source.options = options elif not options and source_type == Source.VCENTER_SOURCE_TYPE: options = SourceOptions() options.ssl_cert_verify = True options.save() source.options = options source.hosts = json.dumps(hosts_list) for credential in credentials: source.credentials.add(credential) source.save() return source @transaction.atomic def update(self, instance, validated_data): """Update a source.""" # If we ever add optional fields to Source, we need to # add logic here to clear them on full update even if they are # not supplied. name = validated_data.get('name') check_for_existing_name(Source.objects, name, _(messages.SOURCE_NAME_ALREADY_EXISTS % name), search_id=instance.id) if 'source_type' in validated_data: error = {'source_type': [_(messages.SOURCE_TYPE_INV)]} raise ValidationError(error) source_type = instance.source_type credentials = validated_data.pop('credentials', None) hosts_list = validated_data.pop('hosts', None) options = validated_data.pop('options', None) if source_type == Source.NETWORK_SOURCE_TYPE: if credentials: for cred in credentials: SourceSerializer.check_credential_type(source_type, cred) elif source_type == Source.VCENTER_SOURCE_TYPE: if hosts_list and len(hosts_list) != 1: error = {'hosts': [_(messages.VC_ONE_HOST)]} raise ValidationError(error) elif hosts_list and '[' in hosts_list[0]: error = {'hosts': [_(messages.VC_ONE_HOST)]} raise ValidationError(error) if credentials and len(credentials) > 1: error = {'credentials': [_(messages.VC_ONE_CRED)]} raise ValidationError(error) elif credentials and len(credentials) == 1: SourceSerializer.check_credential_type(source_type, credentials[0]) elif source_type == Source.SATELLITE_SOURCE_TYPE: if hosts_list and len(hosts_list) != 1: error = {'hosts': [_(messages.SAT_ONE_HOST)]} raise ValidationError(error) elif hosts_list and '[' in hosts_list[0]: error = {'hosts': [_(messages.VC_ONE_HOST)]} raise ValidationError(error) if credentials and len(credentials) > 1: error = {'credentials': [_(messages.SAT_ONE_CRED)]} raise ValidationError(error) elif credentials and len(credentials) == 1: SourceSerializer.check_credential_type(source_type, credentials[0]) for name, value in validated_data.items(): setattr(instance, name, value) instance.save() # If hosts_list was not supplied and this is a full update, # then we should already have raised a ValidationError before # this point, so it's safe to use hosts_list as an indicator # of whether to replace the hosts. if hosts_list: hosts_data = json.dumps(hosts_list) instance.hosts = hosts_data # credentials is safe to use as a flag for the same reason as # hosts_data above. if credentials: instance.credentials.set(credentials) if options: if instance.options is None: if source_type == Source.NETWORK_SOURCE_TYPE and \ bool(options): invalid_options = ', '.join( [key for key in options.keys()]) error = { 'options': [ _(messages.NET_SSL_OPTIONS_NOT_ALLOWED % invalid_options) ] } raise ValidationError(error) options = SourceOptions.objects.create(**options) options.save() instance.options = options else: self.update_options(options, instance.options) instance.save() return instance @staticmethod def update_options(options, instance_options): """Update the incoming options overlapping the existing options. :param options: the passed in options :param instance_options: the existing options """ ssl_protocol = options.pop('ssl_protocol', None) ssl_cert_verify = options.pop('ssl_cert_verify', None) disable_ssl = options.pop('disable_ssl', None) if ssl_protocol is not None: instance_options.ssl_protocol = ssl_protocol if ssl_cert_verify is not None: instance_options.ssl_cert_verify = ssl_cert_verify if disable_ssl is not None: instance_options.disable_ssl = disable_ssl instance_options.save() @staticmethod def check_credential_type(source_type, credential): """Look for existing credential with same type as the source. :param source_type: The source type :param credential: The credential to obtain """ if credential.cred_type != source_type: error = {'source_type': [_(messages.SOURCE_CRED_WRONG_TYPE)]} raise ValidationError(error) @staticmethod def validate_name(name): """Validate the name of the Source.""" if not isinstance(name, str) or not name.isprintable(): raise ValidationError(_(messages.SOURCE_NAME_VALIDATION)) return name # pylint: disable=too-many-locals, too-many-branches, too-many-statements @staticmethod def validate_hosts(hosts): """Make sure the hosts list is present.""" hosts_list = json.loads(hosts) if not isinstance(hosts_list, list): raise ValidationError(_(messages.SOURCE_HOST_MUST_BE_JSON_ARRAY)) if not hosts_list: raise ValidationError(_(messages.SOURCE_HOSTS_CANNOT_BE_EMPTY)) for host_value in hosts_list: if not isinstance(host_value, str): raise ValidationError( _(messages.SOURCE_HOST_MUST_BE_JSON_ARRAY)) # Regex for octet, CIDR bit range, and check # to see if it is like an IP/CIDR octet_regex = r'(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])' bit_range = r'(3[0-2]|[1-2][0-9]|[0-9])' relaxed_ip_pattern = r'[0-9]*\.[0-9]*\.[0-9\[\]:]*\.[0-9\[\]:]*' relaxed_cidr_pattern = r'[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\/[0-9]*' greedy_subset = r'[0-9]*' range_subset = r'[0-9]*-[0-9]*' relaxed_invalid_ip_range = [ r'{0}\.{0}\.{0}\.{0}-{0}\.{0}\.{0}\.{0}'.format(greedy_subset), r'{0}\.{0}\.{0}\.{1}'.format(greedy_subset, range_subset), r'{0}\.{0}\.{1}\.{0}'.format(greedy_subset, range_subset), r'{0}\.{1}\.{0}\.{0}'.format(greedy_subset, range_subset), r'{1}\.{0}\.{0}\.{0}'.format(greedy_subset, range_subset), r'{1}\.{1}\.{0}\.{0}'.format(greedy_subset, range_subset), r'{1}\.{0}\.{1}\.{0}'.format(greedy_subset, range_subset), r'{1}\.{0}\.{0}\.{1}'.format(greedy_subset, range_subset), r'{1}\.{1}\.{0}\.{1}'.format( greedy_subset, range_subset), r'{1}\.{0}\.{1}\.{1}'.format( greedy_subset, range_subset), r'{0}\.{0}\.{0}\.{0}'.format(range_subset), r'{0}\.{1}\.{0}\.{1}'.format(greedy_subset, range_subset), r'{0}\.{1}\.{1}\.{0}'.format(greedy_subset, range_subset) ] # type IP: 192.168.0.1 # type CIDR: 192.168.0.0/16 # type RANGE 1: 192.168.0.[1:15] # type RANGE 2: 192.168.[2:18].1 # type RANGE 3: 192.168.[2:18].[4:46] ip_regex_list = [ r'^{0}\.{0}\.{0}\.{0}$'.format(octet_regex), r'^{0}\.{0}\.{0}\.{0}\/{1}$'.format(octet_regex, bit_range), r'^{0}\.{0}\.{0}\.\[{0}:{0}\]$'.format(octet_regex), r'^{0}\.{0}\.\[{0}:{0}\]\.{0}$'.format(octet_regex), r'^{0}\.{0}\.\[{0}:{0}\]\.\[{0}:{0}\]$'.format(octet_regex) ] # type HOST: abcd # type HOST NUMERIC RANGE: abcd[2:4].foo.com # type HOST ALPHA RANGE: abcd[a:f].foo.com host_regex_list = [ r'[a-zA-Z0-9-\.]+', r'[a-zA-Z0-9-\.]*\[[0-9]+:[0-9]+\]*[a-zA-Z0-9-\.]*', r'[a-zA-Z0-9-\.]*\[[a-zA-Z]{1}:[a-zA-Z]{1}\][a-zA-Z0-9-\.]*' ] normalized_hosts = [] host_errors = [] for host_range in hosts_list: result = None ip_match = re.match(relaxed_ip_pattern, host_range) cidr_match = re.match(relaxed_cidr_pattern, host_range) invalid_ip_range_match = [ re.match(invalid_ip_range, host_range) for invalid_ip_range in relaxed_invalid_ip_range ] is_likely_ip = ip_match and ip_match.end() == len(host_range) is_likely_cidr = cidr_match and cidr_match.end() == len(host_range) is_likely_invalid_ip_range = any(invalid_ip_range_match) if is_likely_invalid_ip_range: err_message = _(messages.NET_INVALID_RANGE_FORMAT % (host_range, )) result = ValidationError(err_message) elif is_likely_ip or is_likely_cidr: # This is formatted like an IP or CIDR # (e.g. #.#.#.# or #.#.#.#/#) for reg in ip_regex_list: match = re.match(reg, host_range) if match and match.end() == len(host_range): result = host_range break if result is None or is_likely_cidr: # Attempt to convert CIDR to ansible range if is_likely_cidr: try: normalized_cidr = SourceSerializer \ .cidr_to_ansible(host_range) result = normalized_cidr except ValidationError as validate_error: result = validate_error else: err_message = _(messages.NET_INVALID_RANGE_CIDR % (host_range, )) result = ValidationError(err_message) else: # Possibly a host_range addr for reg in host_regex_list: match = re.match(reg, host_range) if match and match.end() == len(host_range): result = host_range break if result is None: err_message = _(messages.NET_INVALID_HOST % (host_range, )) result = ValidationError(err_message) if isinstance(result, ValidationError): host_errors.append(result) elif result is not None: normalized_hosts.append(result) else: # This is an unexpected case. Allow/log for analysis normalized_hosts.append(host_range) logging.warning('%s did not match a pattern or produce error', host_range) if len(host_errors) is 0: return normalized_hosts else: error_message = [error.detail.pop() for error in host_errors] raise ValidationError(error_message) # pylint: disable=too-many-locals @staticmethod def cidr_to_ansible(ip_range): """Convert an IP address range from CIDR to Ansible notation. :param ip_range: the IP range, as a string :returns: the IP range, as an Ansible-formatted string :raises NotCIDRException: if ip_range doesn't look similar to CIDR notation. If it does look like CIDR but isn't quite right, print out error messages and exit. """ # In the case of an input error, we want to distinguish between # strings that are "CIDR-like", so the user probably intended to # use CIDR and we should give them a CIDR error message, and not # at all CIDR-like, in which case we tell the caller to parse it a # different way. cidr_like = r'[0-9\.]*/[0-9]+' if not re.match(cidr_like, ip_range): err_msg = _(messages.NET_NO_CIDR_MATCH % (ip_range, str(cidr_like))) raise ValidationError(err_msg) try: base_address, prefix_bits = ip_range.split('/') except ValueError: err_msg = _(messages.NET_CIDR_INVALID % (ip_range, )) raise ValidationError(err_msg) prefix_bits = int(prefix_bits) if prefix_bits < 0 or prefix_bits > 32: err_msg = _(messages.NET_CIDR_BIT_MASK % { 'ip_range': ip_range, 'prefix_bits': prefix_bits }) raise ValidationError(err_msg) octet_strings = base_address.split('.') if len(octet_strings) != 4: err_msg = _(messages.NET_FOUR_OCTETS % (ip_range, )) raise ValidationError(err_msg) octets = [None] * 4 for i in range(4): if not octet_strings[i]: err_msg = _(messages.NET_EMPTY_OCTET % (ip_range, )) raise ValidationError(err_msg) val = int(octet_strings[i]) if val < 0 or val > 255: # pylint: disable=too-many-locals err_msg = _(messages.NET_CIDR_RANGE % { 'ip_range': ip_range, 'octet': val }) raise ValidationError(err_msg) octets[i] = val ansible_out = [None] * 4 for i in range(4): # "prefix_bits" is the number of high-order bits we want to # keep for the whole CIDR range. "mask" is the number of # low-order bits we want to mask off. Here prefix_bits is for # the whole IP address, but mask_bits is just for this octet. if prefix_bits <= i * 8: ansible_out[i] = '[0:255]' elif prefix_bits >= (i + 1) * 8: ansible_out[i] = str(octets[i]) else: # The number of bits of this octet that we want to # preserve this_octet_bits = prefix_bits - 8 * i assert 0 < this_octet_bits < 8 # mask is this_octet_bits 1's followed by (8 - # this_octet_bits) 0's. mask = -1 << (8 - this_octet_bits) lower_bound = octets[i] & mask upper_bound = lower_bound + ~mask ansible_out[i] = '[{0}:{1}]'.format(lower_bound, upper_bound) return '.'.join(ansible_out) @staticmethod def validate_port(port): """Validate the port.""" if not port: pass elif port < 0 or port > 65536: raise ValidationError(_(messages.NET_INVALID_PORT)) return port @staticmethod def validate_credentials(credentials): """Make sure the credentials list is present.""" if not credentials: raise ValidationError(_(messages.SOURCE_MIN_CREDS)) return credentials
class PageParser(Parser): offset = IntegerField(default=0) # noqa limit = IntegerField(default=100) # noqa
class PkAndGradeDictField(DictField): pk = IntegerField() grade = DecimalField(max_digits=7, decimal_places=4)
class PubSubMessageSerializer(Serializer): type = CharField() data = DictField() version = IntegerField(default=0)
class LaneSerializer(ModelSerializer): pk = IntegerField() pool_name = SerializerMethodField() read_length_name = SerializerMethodField() index_i7_show = SerializerMethodField() index_i5_show = SerializerMethodField() equal_representation = SerializerMethodField() quality_check = CharField(required=False) request = SerializerMethodField() protocol = SerializerMethodField() class Meta: list_serializer_class = LaneListSerializer model = Lane fields = ('pk', 'name', 'pool', 'pool_name', 'read_length_name', 'index_i7_show', 'index_i5_show', 'equal_representation', 'loading_concentration', 'phix', 'quality_check', 'request', 'protocol') extra_kwargs = { 'name': { 'required': False }, 'pool': { 'required': False }, } def get_request(self, obj): requests = [] records = obj.pool.libraries.all() or obj.pool.samples.all() for record in records: for req in record.request.all(): requests.append(req.name) if len(requests) == 1 or len(set(requests)) == 1: return requests[0] else: return ";".join(requests) def get_protocol(self, obj): protocols = [] records = obj.pool.libraries.all() or obj.pool.samples.all() for record in records: protocols.append(record.library_protocol.name) if len(protocols) == 1 or len(set(protocols)) == 1: return protocols[0] else: return ";".join(protocols) def get_pool_name(self, obj): return obj.pool.name def get_read_length_name(self, obj): read_lengths = [] i = 0 records = obj.pool.libraries.all() or obj.pool.samples.all() for record in records: print(record.library_protocol.name) read_lengths.append(str(record.read_length.name)) if len(read_lengths) == 1 or len(set(read_lengths)) == 1: return read_lengths[0] else: return ";".join(read_lengths) def get_index_i7_show(self, obj): '''we show the actual index instead of a yes/no entry''' records = obj.pool.libraries.all() or obj.pool.samples.all() idx = [] contains_i7 = False for record in records: if str(record.index_i7) != "": contains_i7 = True break #idx.append(str(record.index_i7)) #if len(idx) == 1 or len(set(idx)) == 1: # return idx[0] #else: # return ";".join(idx) if contains_i7: return "Yes" else: return "" def get_index_i5_show(self, obj): '''we show the actual index instead of a yes/no entry''' records = obj.pool.libraries.all() or obj.pool.samples.all() idx = [] contains_i5 = False for record in records: if str(record.index_i5) != "": contains_i5 = True break #idx.append(str(record.index_i5)) #if len(idx) == 1 or len(set(idx)) == 1: # return idx[0] #else: # return ";".join(idx) if contains_i5: return "Yes" else: return "" #return None def get_equal_representation(self, obj): records = list( itertools.chain(obj.pool.libraries.all(), obj.pool.samples.all())) ern = [x.equal_representation_nucleotides for x in records].count(True) return len(records) == ern
class Rate(EntitySerializer): carrier_name = CharField(required=True, help_text="The rate's carrier") carrier_id = CharField( required=True, help_text="The targeted carrier's name (unique identifier)") currency = CharField(required=True, help_text="The rate monetary values currency code") service = CharField( required=False, allow_blank=True, allow_null=True, help_text="The carrier's rate (quote) service", ) discount = FloatField( required=False, allow_null=True, help_text="The monetary amount of the discount on the rate", ) base_charge = FloatField( default=0.0, help_text=""" The rate's monetary amount of the base charge.<br/> This is the net amount of the rate before additional charges """, ) total_charge = FloatField( default=0.0, help_text=""" The rate's monetary amount of the total charge.<br/> This is the gross amount of the rate after adding the additional charges """, ) duties_and_taxes = FloatField( required=False, allow_null=True, help_text="The monetary amount of the duties and taxes if applied", ) transit_days = IntegerField( required=False, allow_null=True, help_text="The estimated delivery transit days") extra_charges = Charge( many=True, required=False, default=[], help_text="list of the rate's additional charges", ) meta = PlainDictField(required=False, allow_null=True, help_text="provider specific metadata") carrier_ref = CharField( required=False, allow_blank=True, allow_null=True, help_text="The system carrier configuration id", ) test_mode = BooleanField( required=True, help_text= "Specified whether it was created with a carrier in test mode", )
class OwnerSerializer(Serializer): email = EmailField(required=False) id = IntegerField(required=False)
class Tages_Time_Serializer_Num(ModelSerializer): num = IntegerField() class Meta: model = Tags_Time fields = ['id', 'tag_time', 'num']
class Task2Serializer(TaskSerializerBase): coefficient_matrix = MatrixField(child=IntegerField()) constant_terms_vector = ListField(child=IntegerField())
class HistoricalObjectSerializer(ModelSerializer): """Common serializer attributes for Historical models.""" id = IntegerField(source='history_id') date = DateTimeField(source='history_date') event = SerializerMethodField() changeset = PrimaryKeyRelatedField(source='history_changeset', read_only=True) object_id = HistoricalObjectField() archived_representation = SerializerMethodField() EVENT_CHOICES = { '+': 'created', '~': 'changed', '-': 'deleted', } def get_event(self, obj): return self.EVENT_CHOICES[obj.history_type] @classmethod def get_fields_extra(cls): extra = deepcopy(cls.Meta.fields_extra) archive_extra = cls.Meta.archive_extra extra['id']['resource'] = archive_extra['history_resource'] history_resource_singular = archive_extra.get( 'history_resource_singular') if history_resource_singular: extra['id']['singular'] = history_resource_singular object_resource = archive_extra['object_resource'] extra['object_id']['resource'] = object_resource singular = archive_extra.get('singular', object_resource[:-1]) extra['object_id']['name'] = singular extra['archived_representation']['resource'] = object_resource extra['archived_representation']['is_archive_of'] = cls.ArchivedObject extra['archived_representation']['name'] = object_resource return extra def get_archived_representation(self, obj): serializer = self.ArchivedObject(obj) raw_data = serializer.data data = OrderedDict() links = OrderedDict() fields = serializer.Meta.fields fields_extra = getattr(serializer.Meta, 'fields_extra', {}) for name in fields: field_extra = fields_extra.get(name, {}) archive = field_extra.get('archive', 'include') if archive == 'include': link = field_extra.get('link') if link is None: # Archived attribute data[name] = raw_data[name] elif link == 'self': # Archived self-id data['id'] = str(raw_data[name]) elif link == 'to_one': value = getattr(obj, name + '_id') if value is not None: value = str(value) links[name] = value else: assert link == 'from_many', 'Unhandled link "%s"' % link related = getattr(obj, name) links[name] = [str(rel.pk) for rel in related] elif archive == 'history_id': links[name] = str(obj.history_id) else: assert archive == 'omit', ( 'Unknown value "%s" for fields_extra["%s"]["archive"]' % (archive, name)) data['links'] = links return data class Meta: fields = ('id', 'date', 'event', 'changeset', 'object_id', 'archived_representation') fields_extra = { 'id': { 'link': 'self', 'archived_resource': True, }, 'changeset': { 'link': 'to_one', 'resource': 'changesets', }, 'object_id': { 'link': 'to_one', 'archived_resource': True, }, 'archived_representation': { 'archived_resource': True, }, }
def __new__(metacls: type, cls_name: str, bases: Tuple, attrs: Dict[str, Any]) -> "ExtendedSerializerMetaclass": required_fields_on_create = required_fields_on_update = () required_fields_on_create_any = required_fields_on_update_any = () # Inject `_pk` field attrs.update(_pk=IntegerField( write_only=True, min_value=0, help_text=( "This *write-only* field is used for differentiating between " "`create` and `update` operations of nested serializers. And " "must refer to a valid primary key for the relevant nested " "serializer model to indicate that the operation on nested " "serializer is an `update` of the object referred by the given " "primary key. Otherwise, a `create` operation is performed."), )) try: Meta = attrs["Meta"] except KeyError: raise ValueError( f"No `Meta` class defined on {cls_name}.") from None try: meta_fields = Meta.fields except AttributeError: raise ValueError( f'No "fields" defined on `Meta` class of {cls_name}.' ) from None # When `Meta.fields` is `__all__`, the `_pk` field is # added via `get_default_field_names` method (see below) if meta_fields != ALL_FIELDS: Meta.fields = tuple(meta_fields) + ("_pk", ) # `NestedCreateUpdateMixin` should be first superclass bases = (NestedCreateUpdateMixin, ) + bases cls = super().__new__(metacls, cls_name, bases, attrs) # type: ignore # Add `_pk` to "default_fields" when `Meta.fields` is `__all__` if meta_fields == ALL_FIELDS: get_default_field_names_orig = cls.get_default_field_names def get_default_field_names(obj, *args, **kwargs): default_fields = get_default_field_names_orig( obj, *args, **kwargs) return default_fields + ["_pk"] cls.get_default_field_names = get_default_field_names # This will be used to check the existence of defined # `Meta.non_required_fields` and `Meta.common_field_params` # in `Meta.fields` before setting meta_fields_set = set( Meta.fields) if Meta.fields != ALL_FIELDS else set() try: extra_kwargs = Meta.extra_kwargs except AttributeError: extra_kwargs = Meta.extra_kwargs = {} # `non_required_fields` try: non_required_fields = Meta.non_required_fields # Defaults to `fields` except AttributeError: # TODO: Populate fields when `Meta.fields == '__all__'` non_required_fields = ( ) if Meta.fields == ALL_FIELDS else Meta.fields non_required_fields = tuple(non_required_fields) + ("_pk", ) for field in non_required_fields: if field in cls._declared_fields: field_obj = cls._declared_fields[field] setattr(field_obj, "required", False) # `Field` class saves a reference on # `_kwargs` and use in `__deepcopy__` # (see `fields.Field.__deepcopy__`) field_obj._kwargs["required"] = False else: # For explicitly declared `Meta.fields`, check # if the field exists there if (Meta.fields == ALL_FIELDS) or (field in meta_fields_set): extra_kwargs.setdefault(field, {}).update(required=False) # `common_field_params` common_field_params = getattr(Meta, "common_field_params", {}) if isinstance(common_field_params, Mapping): for fields, params_dict in common_field_params.items(): for field in fields: if field in cls._declared_fields: field_obj = cls._declared_fields[field] for key, value in params_dict.items(): setattr(field_obj, key, value) field_obj._kwargs[key] = value else: # For explicitly declared `Meta.fields`, check # if the field exists there if (Meta.fields == ALL_FIELDS) or (field in meta_fields_set): extra_kwargs.setdefault(field, {}).update(params_dict) required_fields_on_create = Meta.__dict__.get( "required_fields_on_create", required_fields_on_create) required_fields_on_update = Meta.__dict__.get( "required_fields_on_update", required_fields_on_update) required_fields_on_create_any = Meta.__dict__.get( "required_fields_on_create_any", required_fields_on_create_any) required_fields_on_update_any = Meta.__dict__.get( "required_fields_on_update_any", required_fields_on_update_any) common_create_fields: Set[Any] = set(required_fields_on_create) & set( required_fields_on_create_any) if common_create_fields: raise ValueError(f'"{", ".join(common_create_fields)}" set inside ' "both Meta.required_fields_on_create and " "Meta.required_fields_on_create_any") common_update_fields: Set[Any] = set(required_fields_on_update) & set( required_fields_on_update_any) if common_update_fields: raise ValueError(f'"{", ".join(common_update_fields)}" set inside ' "both Meta.required_fields_on_update and " "Meta.required_fields_on_update_any") # Keep a reference to the original `is_valid` method is_valid_orig = getattr(cls, "is_valid", None) def is_valid(obj, raise_exception: bool = False) -> Optional[bool]: """Custom `is_valid` method to perform the `required_fields_*` checks for create and update operations. """ if hasattr(obj, "initial_data"): if isinstance(obj.initial_data, Mapping): data = obj.initial_data errors: Dict[str, List] = {} if obj.instance or "_pk" in data: required_fields = required_fields_on_update required_fields_any = required_fields_on_update_any else: required_fields = required_fields_on_create required_fields_any = required_fields_on_create_any # `required_fields_on_create`/`required_fields_on_update` for field in required_fields: if field not in data: update_error_dict(errors, field, "This field is required.") # `required_fields_on_create_any`/`required_fields_on_update_any` if required_fields_any: for field in required_fields_any: if field in data: break else: update_error_dict( errors, NON_FIELD_ERRORS_KEY, (f'At least one of "{", ".join(required_fields_any)}" ' "is required."), ) if errors: if raise_exception: raise ValidationError(errors) return False return is_valid_orig(obj, raise_exception=raise_exception) cls.is_valid = is_valid return cls
class LeaderboardRequestSerializer(Serializer): start = IntegerField(min_value=0) count = IntegerField(min_value=1)
class Answer1Serializer(AnswerSerializerBase): x = IntegerField() y = IntegerField() z = IntegerField()
class AnnualIncomeSerializer(ModelSerializer): id = IntegerField(read_only=False) class Meta: model = AnnualIncome fields = '__all__'