def validate(self, data): """ Validate file by size and by all checksums provided. Args: data (:class:`django.http.QueryDict`): QueryDict mapping Artifact model fields to their values Raises: :class:`rest_framework.exceptions.ValidationError`: When the expected file size or any of the checksums don't match their actual values. """ if 'size' in data: if data['file'].size != int(data['size']): raise serializers.ValidationError(_("The size did not match actual size of file.")) else: data['size'] = data['file'].size for algorithm in hashlib.algorithms_guaranteed: if algorithm in models.Artifact.DIGEST_FIELDS: digest = data['file'].hashers[algorithm].hexdigest() if algorithm in data and digest != data[algorithm]: raise serializers.ValidationError(_("The %s checksum did not match.") % algorithm) else: data[algorithm] = digest if algorithm in UNIQUE_ALGORITHMS: validator = UniqueValidator(models.Artifact.objects.all(), message=_("{0} checksum must be " "unique.").format(algorithm)) validator.field_name = algorithm validator.instance = None validator(digest) return data
class UserRegSerializer(serializers.ModelSerializer): code = serializers.CharField(required=True, write_only=True, max_length=4, min_length=4, label="验证码", error_messages={ "blank": "请输入验证码", "required": "请输入验证码", "max_length": "验证码格式错误", "min_length": "验证码格式错误" }, help_text="验证码") username = serializers.CharField(label="用户名", help_text="用户名", required=True, allow_blank=False, validators=[ UniqueValidator( queryset=User.objects.all(), message="用户已经存在") ]) password = serializers.CharField( style={'input_type': 'password'}, help_text="密码", label="密码", write_only=True, ) # 调用父类的create方法,该方法会返回当前model的实例化对象即user。 # 前面是将父类原有的create进行执行,后面是加入自己的逻辑 def create(self, validated_data): user = super(UserRegSerializer, self).create(validated_data=validated_data) user.set_password(validated_data["password"]) user.save() return user def validate_code(self, code): # get与filter的区别: get有两种异常,一个是有多个,一个是一个都没有。 # try: # verify_records = VerifyCode.objects.get(mobile=self.initial_data["username"], code=code) # except VerifyCode.DoesNotExist as e: # pass # except VerifyCode.MultipleObjectsReturned as e: # pass # 验证码在数据库中是否存在,用户从前端post过来的值都会放入initial_data里面,排序(最新一条)。 verify_records = VerifyCode.objects.filter( mobile=self.initial_data["username"]).order_by("-add_time") if verify_records: # 获取到最新一条 last_record = verify_records[0] # 有效期为五分钟。 five_mintes_ago = datetime.now() - timedelta( hours=0, minutes=5, seconds=0) if five_mintes_ago > last_record.add_time: raise serializers.ValidationError("验证码过期") if last_record.code != code: raise serializers.ValidationError("验证码错误") else: raise serializers.ValidationError("验证码错误") # 不加字段名的验证器作用于所有字段之上。attrs是字段 validate之后返回的总的dict def validate(self, attrs): attrs["mobile"] = attrs["username"] del attrs["code"] return attrs class Meta: model = User fields = ("username", "code", "mobile", "password")
class UserSignUpSerializer(serializers.Serializer): """User signup serializers. Handle signup data validation and user/profile creation. """ email = serializers.EmailField( validators=[UniqueValidator(queryset=User.objects.all())]) username = serializers.CharField( min_length=4, max_length=20, validators=[UniqueValidator(queryset=User.objects.all())]) # Phone number phone_regex = RegexValidator( regex=r'\+?1?\d{9,15}$', message= "Phone number must be in the format : +999999999 up to 15 digits") phone_number = serializers.CharField(validators=[phone_regex], max_length=17) # Password password = serializers.CharField(min_length=8, max_length=64) password_confirmation = serializers.CharField(min_length=8, max_length=64) # Name first_name = serializers.CharField(min_length=2, max_length=30) last_name = serializers.CharField(min_length=2, max_length=30) def validate(self, data): # Verify passwords match. passwd = data["password"] passwd_conf = data["password_confirmation"] if passwd != passwd_conf: raise serializers.ValidationError("Passwords do not match.") password_validation.validate_password(passwd) return data def create(self, data): data.pop("password_confirmation") user = User.objects.create_user(**data, is_verified=False, is_client=True) profile = Profile.objects.create(user=user) self.send_confirmation_email(user) return user def send_confirmation_email(self, user): """Sending account verification email to given user.""" token = self.gen_verification_token(user) subject = f"Welcome @{user.username}! Verify your account to start using the app." from_email = "Comparte Ride <*****@*****.**>" content = render_to_string("emails/users/account_verification.html", { "token": token, "user": user }) msg = EmailMultiAlternatives(subject, content, from_email, [user.email]) msg.attach_alternative(content, "text/html") msg.send() def gen_verification_token(self, user): """ Generate a JWT that the user can use to verify its account. """ exp_date = timezone.now() + timedelta(days=3) pay_load = { "user": user.username, "exp": int(exp_date.timestamp()), "type": "email_confirmation" } token = jwt.encode(pay_load, settings.SECRET_KEY, algorithm="HS256") return token
class RegistrationSerializer(serializers.Serializer): email = serializers.EmailField( validators=[UniqueValidator(queryset=User.objects.all())]) password = serializers.CharField() company = serializers.CharField(max_length=50)
class UserCreateSerializer(serializers.ModelSerializer): # A field from the user's profile: username = serializers.SlugField( min_length=4, max_length=32, help_text= _('Required. 4-32 characters. Letters, numbers, underscores or hyphens only.' ), validators=[ UniqueValidator(queryset=User.objects.all(), message='has already been taken by other user') ], required=True) password = serializers.CharField(min_length=4, max_length=32, write_only=True, help_text=_('Required. 4-32 characters.'), required=True) email = serializers.EmailField( required=True, validators=[ UniqueValidator(queryset=User.objects.all(), message='has already been taken by other user') ]) bio = serializers.CharField(source='profile.bio', allow_blank=True, default='') name = serializers.CharField(source='profile.name', allow_blank=True, default='', max_length=32) avatar = serializers.URLField(source='profile.avatar', allow_blank=True, default='') status = serializers.CharField(source='profile.status', allow_blank=True, max_length=16, min_length=0, default='') class Meta: model = User fields = ('username', 'name', 'email', 'password', 'bio', 'avatar', 'status') def create(self, validated_data): profile_data = validated_data.pop('profile', None) username = validated_data['username'] email = validated_data['email'] password = validated_data['password'] user = User(username=username, email=email) user.set_password(password) user.save() avatar = profile_data.get('avatar') or None if not avatar: avatar = 'https://api.adorable.io/avatar/200/' + username profile = UserProfile(user=user, bio=profile_data.get('bio') or 'No bio', avatar=avatar, name=profile_data.get('name') or username, status=profile_data.get('status') or 'Member') profile.save() return user
class WcpUserCreateSerializer(UserCreateSerializer): email = serializers.EmailField( required=True, validators=[UniqueValidator(queryset=User.objects.all())])
class DistributionSerializer(ModelSerializer, BasePathOverlapMixin): """ The Serializer for the Distribution model. The serializer deliberately omits the the `publication` and `repository_version` field due to plugins typically requiring one or the other but not both. To include the ``publication`` field, it is recommended plugins define the field:: publication = DetailRelatedField( required=False, help_text=_("Publication to be served"), view_name_pattern=r"publications(-.*/.*)?-detail", queryset=models.Publication.objects.exclude(complete=False), allow_null=True, ) To include the ``repository_version`` field, it is recommended plugins define the field:: repository_version = RepositoryVersionRelatedField( required=False, help_text=_("RepositoryVersion to be served"), allow_null=True ) Additionally, the serializer omits the ``remote`` field, which is used for pull-through caching feature and only by plugins which use publications. Plugins implementing a pull-through caching should define the field in their derived serializer class like this:: remote = DetailRelatedField( required=False, help_text=_('Remote that can be used to fetch content when using pull-through caching.'), queryset=models.Remote.objects.all(), allow_null=True ) """ pulp_href = DetailIdentityField( view_name_pattern=r"distributions(-.*/.*)-detail") pulp_labels = LabelsField(required=False) base_path = serializers.CharField(help_text=_( 'The base (relative) path component of the published url. Avoid paths that \ overlap with other distribution base paths (e.g. "foo" and "foo/bar")' ), ) base_url = BaseURLField( source="base_path", read_only=True, help_text= _("The URL for accessing the publication as defined by this distribution." ), ) content_guard = DetailRelatedField( required=False, help_text=_("An optional content-guard."), view_name_pattern=r"contentguards(-.*/.*)?-detail", queryset=models.ContentGuard.objects.all(), allow_null=True, ) name = serializers.CharField( help_text=_("A unique name. Ex, `rawhide` and `stable`."), validators=[ UniqueValidator(queryset=models.BaseDistribution.objects.all()), UniqueValidator(queryset=models.Distribution.objects.all()), ], ) repository = DetailRelatedField( required=False, help_text=_( "The latest RepositoryVersion for this Repository will be served." ), view_name_pattern=r"repositories(-.*/.*)?-detail", queryset=models.Repository.objects.all(), allow_null=True, ) class Meta: abstract = True model = models.BaseDistribution fields = ModelSerializer.Meta.fields + ( "base_path", "base_url", "content_guard", "pulp_labels", "name", "repository", ) def validate(self, data): super().validate(data) publication_in_data = "publication" in data repository_version_in_data = "repository_version" in data publication_in_instance = self.instance.publication if self.instance else None repository_version_in_instance = self.instance.repository_version if self.instance else None if publication_in_data and repository_version_in_data: error = True elif publication_in_data and repository_version_in_instance: error = True elif publication_in_instance and repository_version_in_data: error = True else: error = False if error: msg = _( "Only one of the attributes 'publication' and 'repository_version' may be used " "simultaneously.") raise serializers.ValidationError(msg) return data
class SharedOrganizationListCreateSerializer(serializers.ModelSerializer): # OVERRIDE THE MODEL FIELDS AND ENFORCE THE FOLLOWING CUSTOM VALIDATION RULES. schema_name = serializers.CharField( required=True, allow_blank=False, validators=[ UniqueValidator(queryset=SharedOrganization.objects.all()) ], ) name = serializers.CharField( required=True, allow_blank=False, ) alternate_name = serializers.CharField( required=True, allow_blank=False, ) timezone_name = serializers.CharField( required=True, allow_blank=False, ) police_report_url = serializers.CharField( required=False, allow_blank=True, ) default_position = PointField(required=False) default_zoom = serializers.IntegerField(required=False, ) class Meta: model = SharedOrganization fields = ( 'id', 'created_at', 'last_modified_at', 'alternate_name', 'description', 'name', # 'url', 'timezone_name', 'police_report_url', # Postal Address 'country', 'city', 'province', 'street_number', 'street_name', 'apartment_unit', 'street_type', 'street_type_other', 'street_direction', 'postal_code', 'default_position', 'default_zoom', # Contact Info 'email', 'phone', 'website_url', 'facebook_url', 'twitter_url', 'instagram_url', 'youtube_url', # Tenancy 'schema_name') def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """ queryset = queryset.prefetch_related() return queryset def create(self, validated_data): """ Override the `create` function to add extra functinality. """ #----------------------------- # Create our `Tenant` object. #----------------------------- django_rq.enqueue(create_organization_func, validated_data) # Return our output return validated_data
class BaseDistributionSerializer(ModelSerializer): """ The Serializer for the BaseDistribution model. The serializer deliberately omits the "remote" field, which is used for pull-through caching only. Plugins implementing pull-through caching will have to add the field in their derived serializer class like this:: remote = DetailRelatedField( required=False, help_text=_('Remote that can be used to fetch content when using pull-through caching.'), queryset=models.Remote.objects.all(), allow_null=True ) """ pulp_href = DetailIdentityField( view_name_pattern=r"distributions(-.*/.*)-detail") pulp_labels = LabelsField(required=False) base_path = serializers.CharField( help_text= _('The base (relative) path component of the published url. Avoid paths that \ overlap with other distribution base paths (e.g. "foo" and "foo/bar")' ), validators=[ UniqueValidator(queryset=models.BaseDistribution.objects.all()) ], ) base_url = BaseURLField( source="base_path", read_only=True, help_text= _("The URL for accessing the publication as defined by this distribution." ), ) content_guard = DetailRelatedField( required=False, help_text=_("An optional content-guard."), view_name_pattern=r"contentguards(-.*/.*)?-detail", queryset=models.ContentGuard.objects.all(), allow_null=True, ) name = serializers.CharField( help_text=_("A unique name. Ex, `rawhide` and `stable`."), validators=[ UniqueValidator(queryset=models.BaseDistribution.objects.all()) ], ) class Meta: abstract = True model = models.BaseDistribution fields = ModelSerializer.Meta.fields + ( "base_path", "base_url", "content_guard", "pulp_labels", "name", ) def _validate_path_overlap(self, path): # look for any base paths nested in path search = path.split("/")[0] q = Q(base_path=search) for subdir in path.split("/")[1:]: search = "/".join((search, subdir)) q |= Q(base_path=search) # look for any base paths that nest path q |= Q(base_path__startswith="{}/".format(path)) qs = models.BaseDistribution.objects.filter(q) if self.instance is not None: qs = qs.exclude(pk=self.instance.pk) match = qs.first() if match: raise serializers.ValidationError(detail=_( "Overlaps with existing distribution '{}'").format(match.name)) return path def validate_base_path(self, path): self._validate_relative_path(path) return self._validate_path_overlap(path)
class RoleSerializer(serializers.ModelSerializer): """Serializer for the Role model.""" uuid = serializers.UUIDField(read_only=True) name = serializers.CharField( required=True, max_length=150, validators=[UniqueValidator(queryset=Role.objects.all())] ) display_name = serializers.CharField(required=False, max_length=150, allow_blank=True) description = serializers.CharField(allow_null=True, required=False) access = AccessSerializer(many=True) policyCount = serializers.IntegerField(read_only=True) accessCount = serializers.IntegerField(read_only=True) applications = serializers.SerializerMethodField() system = serializers.BooleanField(read_only=True) platform_default = serializers.BooleanField(read_only=True) created = serializers.DateTimeField(read_only=True) modified = serializers.DateTimeField(read_only=True) class Meta: """Metadata for the serializer.""" model = Role fields = ( "uuid", "name", "display_name", "description", "access", "policyCount", "accessCount", "applications", "system", "platform_default", "created", "modified", ) def get_applications(self, obj): """Get the list of applications in the role.""" return obtain_applications(obj) def create(self, validated_data): """Create the role object in the database.""" name = validated_data.pop("name") display_name = validated_data.pop("display_name", name) description = validated_data.pop("description", None) access_list = validated_data.pop("access") role = Role.objects.create(name=name, description=description, display_name=display_name) role.save() for access_item in access_list: resource_def_list = access_item.pop("resourceDefinitions") access_obj = Access.objects.create(**access_item, role=role) access_obj.save() for resource_def_item in resource_def_list: res_def = ResourceDefinition.objects.create(**resource_def_item, access=access_obj) res_def.save() return role def update(self, instance, validated_data): """Update the role object in the database.""" if instance.system: key = "role.update" message = "System roles may not be updated." error = {key: [_(message)]} raise serializers.ValidationError(error) access_list = validated_data.pop("access") instance.name = validated_data.get("name", instance.name) instance.display_name = validated_data.get("display_name", instance.display_name) instance.description = validated_data.get("description", instance.description) instance.save() instance.access.all().delete() for access_item in access_list: resource_def_list = access_item.pop("resourceDefinitions") access_obj = Access.objects.create(**access_item, role=instance) access_obj.save() for resource_def_item in resource_def_list: res_def = ResourceDefinition.objects.create(**resource_def_item, access=access_obj) res_def.save() instance.save() return instance
class UserRegSerializer(serializers.ModelSerializer): """用户信息详情序列化类""" #code字段新声明 code = serializers.CharField(write_only=True, required=True, min_length=4, max_length=4, help_text='短信验证码', label='验证码', error_messages={ 'blank': '请输入验证码', 'required': '该字段是必填项', 'max_length': '验证码长度是4位', 'min_length': '验证码长度是4位', }) # 自定义验证字段 username = serializers.CharField(required=True, allow_blank=False, label='用户名', help_text='用户名', validators=[ UniqueValidator( queryset=User.objects.all(), message='用户已存在') ]) password = serializers.CharField(write_only=True, required=True, label='密码', help_text='密码', style={'input_type': 'password'}) def validate_code(self, code): #validate_empty_values函数用于验证单个字段 verify_codes = VerifyCode.objects.filter( mobile=self.initial_data['username']).order_by( '-add_time') # self.initial_data 为用户前端传过来的所有值 if verify_codes: last_record = verify_codes[0] five_mintue_ago = datetime.now() - timedelta( hours=0, minutes=5, seconds=0) # 获取五分钟前的时间 if five_mintue_ago > last_record.add_time: raise serializers.ValidationError('验证码过期', code=code) elif last_record.code != code: raise serializers.ValidationError('验证码错误') # return code #code只用于验证,不需要保存到数据库中 else: #没有查到该手机号对应的验证码 raise serializers.ValidationError('手机号验证码不存在') def validate(self, attrs): #validate函数用于验证所有字段 attrs['mobile'] = attrs['username'] del attrs['code'] #删除User表不存在的code字段 return attrs #attrs将作为参数传递到.create() class Meta: #生成ModelSerializer字段 model = User fields = ( "username", "code", "mobile", "password", ) #username是Django自带必填字段,与mobile保持一致
class SignUpSerializer(serializers.ModelSerializer): username = serializers.CharField( required=True, allow_blank=False, allow_null=False, style={'placeholder': 'Username'}, validators=[ UniqueValidator(queryset=User.objects.all(), message='This Username is already in use!', lookup='exact') ]) first_name = serializers.CharField(required=True, style={'placeholder': 'First Name'}) last_name = serializers.CharField(required=True, style={'placeholder': 'Last Name'}) email = serializers.EmailField( required=True, allow_blank=False, allow_null=False, style={'placeholder': 'Email'}, validators=[ UniqueValidator(queryset=User.objects.all(), message='This Email is already in use!', lookup='exact') ]) phone_number = serializers.CharField( required=True, allow_blank=False, allow_null=False, style={'placeholder': 'Phone Number'}, validators=[ UniqueValidator(queryset=User.objects.all(), message='This Phone Number is already in use!', lookup='exact') ]) password = serializers.CharField(style={ 'input_type': 'password', 'placeholder': 'Password' }, required=True, allow_blank=False, allow_null=False) confirm_password = serializers.CharField(style={ 'input_type': 'password', 'placeholder': 'Confirm Password' }, required=True) class Meta: model = User fields = [ 'url', 'username', 'first_name', 'last_name', 'email', 'phone_number', 'password', 'confirm_password' ] def validate_phone_number(self, phone_number): if len(phone_number) > 10: raise serializers.ValidationError( "Please enter a valid Phone number") elif len(phone_number) < 10: raise serializers.ValidationError( "Please enter a valid Phone number") elif phone_number.isdigit(): return phone_number else: raise serializers.ValidationError( "Please enter a valid Phone number") def validate(self, email): try: match = User.objects.get(email=email) except User.DoesNotExist: return data raise serializers.ValidationError('This email is already in use') def validate(self, data): """ function for password validation :param data: :return: """ password = data.get('password') confirm_password = data.get('confirm_password') if password != confirm_password: raise serializers.ValidationError("Password didn't matched ") if len(password) < 6: raise serializers.ValidationError( "Password of minimum 6 digits is required") else: return data
class UserSerializer(serializers.ModelSerializer): username = serializers.CharField( required=True, allow_blank=False, allow_null=False, style={'placeholder': 'Username'}, validators=[ UniqueValidator(queryset=User.objects.all(), message='This Username is already in use!', lookup='exact') ]) first_name = serializers.CharField(required=True, style={'placeholder': 'First Name'}) last_name = serializers.CharField(required=True, style={'placeholder': 'Last Name'}) email = serializers.EmailField( required=True, allow_blank=False, allow_null=False, style={'placeholder': 'Email'}, validators=[ UniqueValidator(queryset=User.objects.all(), message='This Email is already in use!', lookup='exact') ]) phone_number = serializers.CharField( required=True, allow_blank=False, allow_null=False, style={'placeholder': 'Phone Number'}, validators=[ UniqueValidator(queryset=User.objects.all(), message='This Phone Number is already in use!', lookup='exact') ]) password = serializers.CharField(style={ 'input_type': 'password', 'placeholder': 'Password' }, required=True, allow_blank=False, allow_null=False) confirm_password = serializers.CharField(style={ 'input_type': 'password', 'placeholder': 'Confirm Password' }, required=True) class Meta: model = User fields = [ 'url', 'username', 'first_name', 'last_name', 'email', 'phone_number', 'password', 'confirm_password' ]
class UserSerializer(serializers.ModelSerializer): last_login = serializers.DateTimeField(required=False, read_only=True) date_joined = serializers.DateTimeField(required=False, read_only=True) email = serializers.EmailField( required=True, validators=[UniqueValidator(queryset=User.objects.all())]) username = serializers.CharField(required=False) first_name = serializers.CharField(required=True) last_name = serializers.CharField(required=True) is_worker = serializers.BooleanField(required=False, write_only=True) is_requester = serializers.BooleanField(required=False, write_only=True) class Meta: model = models.User validators = [ EqualityValidator(fields=['password1', 'password2']), LengthValidator('password1', 8), ] fields = ('id', 'username', 'first_name', 'last_name', 'email', 'last_login', 'date_joined', 'is_worker', 'is_requester') def __init__(self, validate_non_fields=False, **kwargs): super(UserSerializer, self).__init__(**kwargs) self.validate_non_fields = validate_non_fields def validate_username(self, value): user = User.objects.filter(username=value) if user: raise ValidationError("Username needs to be unique.") return value def create(self, **kwargs): username = '' validated_username = self.validated_data['first_name'].lower( ) + '.' + self.validated_data['last_name'].lower() username_check = User.objects.filter( username=validated_username).count() if username_check == 0 and len( validated_username) <= settings.USERNAME_MAX_LENGTH: username = validated_username else: username = get_next_unique_id(User, 'username', validated_username) if len(username) > settings.USERNAME_MAX_LENGTH: if len(self.validated_data['email'] ) <= settings.USERNAME_MAX_LENGTH: username = self.validated_data['email'] else: username = uuid.uuid4().hex[:settings.USERNAME_MAX_LENGTH] user = User.objects.create_user(username, self.validated_data.get('email'), self.initial_data.get('password1')) if settings.EMAIL_ENABLED: user.is_active = 0 user.first_name = self.validated_data['first_name'] user.last_name = self.validated_data['last_name'] user.save() user_profile = models.UserProfile() user_profile.user = user user_profile.save() user_financial_account = models.FinancialAccount() user_financial_account.owner = user_profile user_financial_account.type = 'general' user_financial_account.save() if self.validated_data.get('is_requester', True): requester = models.Requester() requester.profile = user_profile requester.alias = username requester.save() requester_financial_account = models.FinancialAccount() requester_financial_account.owner = user_profile requester_financial_account.type = 'requester' requester_financial_account.save() has_profile_info = self.validated_data.get( 'is_requester', False) or self.validated_data.get( 'is_worker', False) if self.validated_data.get('is_worker', False) or not has_profile_info: worker = models.Worker() worker.profile = user_profile worker.alias = username worker.save() worker_financial_account = models.FinancialAccount() worker_financial_account.owner = user_profile worker_financial_account.type = 'worker' worker_financial_account.save() if settings.EMAIL_ENABLED: salt = hashlib.sha1(str( random.random()).encode('utf-8')).hexdigest()[:5] if isinstance(username, str): username = username.encode('utf-8') activation_key = hashlib.sha1(salt.encode('utf-8') + username).hexdigest() registration_model = models.RegistrationModel() registration_model.user = User.objects.get(id=user.id) registration_model.activation_key = activation_key send_activation_email_sendgrid( email=user.email, host=self.context['request'].get_host(), activation_key=activation_key) registration_model.save() return user def change_password(self): from django.contrib.auth import authenticate as auth_authenticate if 'password' not in self.initial_data: raise ValidationError("Current password needs to be provided") user = auth_authenticate(username=self.instance.username, password=self.initial_data['password']) if user is not None: self.instance.set_password(self.initial_data['password1']) self.instance.save() else: raise ValidationError("Old password is incorrect.") def authenticate(self, request): from django.contrib.auth import authenticate as auth_authenticate # self.redirect_to = request.POST.get('next', '') #to be changed, POST does not contain any data username = request.data.get('username', '') password = request.data.get('password', '') email_or_username = username # match with username if not email if not re.match(r"[^@]+@[^@]+\.[^@]+", email_or_username): username = email_or_username else: user = get_model_or_none(User, email=email_or_username) if user is not None: username = user.username user = auth_authenticate(username=username, password=password) if user is not None: if user.is_active: oauth2_utils = Oauth2Utils() client = oauth2_utils.create_client(request, user) response_data = dict() response_data["client_id"] = client.client_id response_data["client_secret"] = client.client_secret response_data["username"] = user.username response_data["email"] = user.email response_data["first_name"] = user.first_name response_data["last_name"] = user.last_name response_data["date_joined"] = user.date_joined response_data["last_login"] = user.last_login response_data["is_requester"] = hasattr( request.user.userprofile, 'requester') response_data["is_worker"] = hasattr(request.user.userprofile, 'worker') return response_data, status.HTTP_201_CREATED else: raise AuthenticationFailed(_('Account is not activated yet.')) else: raise AuthenticationFailed(_('Username or password is incorrect.')) def change_username(self, **kwargs): from django.contrib.auth import authenticate as auth_authenticate if 'password' not in self.initial_data: raise ValidationError("Current password needs to be provided") if 'username' not in self.initial_data: raise ValidationError("New username needs to be provided") user = auth_authenticate(username=self.instance.username, password=self.initial_data['password']) if user is not None: self.instance.username = self.initial_data['username'] self.instance.save() else: raise ValidationError("Username or password is incorrect.") def send_forgot_password(self, **kwargs): user = kwargs['user'] salt = hashlib.sha1(str( random.random()).encode('utf-8')).hexdigest()[:5] username = user.username reset_key = hashlib.sha1(str(salt + username).encode('utf-8')).hexdigest() password_reset = get_model_or_none(models.PasswordResetModel, user_id=user.id) if password_reset is None: password_reset = models.PasswordResetModel() password_reset.user = user password_reset.reset_key = reset_key if settings.EMAIL_ENABLED: password_reset.save() send_password_reset_email(email=user.email, host=self.context['request'].get_host(), reset_key=reset_key) def reset_password(self, **kwargs): """ Resets the password if requested by the user. """ if len(kwargs['password']) < 8: raise ValidationError( "New password must be at least 8 characters long") if not kwargs['reset_model']: raise ValidationError("Invalid email or reset key") user = get_model_or_none(User, id=kwargs['reset_model'].user_id, email=self.context['request'].data.get( 'email', 'NO_EMAIL')) if not user: raise ValidationError("Invalid email or reset key") user.set_password(kwargs['password']) user.save() kwargs['reset_model'].delete() return {"message": "Password reset successfully"}, status.HTTP_200_OK def ignore_reset_password(self, **kwargs): kwargs['reset_model'].delete() return {"message": "Ignored"}, status.HTTP_204_NO_CONTENT
class UserRegSerializer(serializers.ModelSerializer): #返回前端write_only做序列化时 不会拿他来做序列化 也就不会报错因为下面有个 del attrs["code"] code = serializers.CharField(required=True, write_only=True, max_length=4, min_length=4, label="验证码", error_messages={ "blank": "请输入验证码", "required": "请输入验证码", "max_length": "验证码格式错误", "min_length": "验证码格式错误" }, help_text="验证码") username = serializers.CharField(label="用户名", help_text="用户名", required=True, allow_blank=False, validators=[ UniqueValidator( queryset=User.objects.all(), message="用户已经存在") ]) #write_only不设置的话就会返回出来 password = serializers.CharField( style={'input_type': 'password'}, help_text="密码", label="密码", write_only=True, ) # def create(self, validated_data): # user = super(UserRegSerializer, self).create(validated_data=validated_data) # user.set_password(validated_data["password"]) # user.save() # return user def validate_code(self, code): # try: # verify_records = VerifyCode.objects.get(mobile=self.initial_data["username"], code=code) # except VerifyCode.DoesNotExist as e: # pass # except VerifyCode.MultipleObjectsReturned as e: # pass verify_records = VerifyCode.objects.filter( mobile=self.initial_data["username"]).order_by("-add_time") if verify_records: last_record = verify_records[0] five_mintes_ago = datetime.now() - timedelta( hours=0, minutes=5, seconds=0) if five_mintes_ago > last_record.add_time: raise serializers.ValidationError("验证码过期") if last_record.code != code: raise serializers.ValidationError("验证码错误") else: raise serializers.ValidationError("验证码错误") def validate(self, attrs): attrs["mobile"] = attrs["username"] del attrs["code"] return attrs class Meta: model = User fields = ("username", "code", "mobile", "password")
class UserSignUpSerializer(serializers.Serializer): """ User sign up serializer. Handle sign up data validation and user/profile creation. """ email = serializers.EmailField( validators=[UniqueValidator(queryset=User.objects.all())]) # username username = serializers.CharField( min_length=4, max_length=20, validators=[UniqueValidator(queryset=User.objects.all())]) # phone number phone_regex = RegexValidator( regex='\+?1?\d{8,15}$', message= 'Phone number must be enterd in the format: +50769237843. Up to 15 digits allowed.' ) phone_number = serializers.CharField(validators=[phone_regex]) # password password = serializers.CharField(min_length=8, max_length=64) password_confirmation = serializers.CharField(min_length=8, max_length=64) # name first_name = serializers.CharField(min_length=2, max_length=40) last_name = serializers.CharField(min_length=2, max_length=40) def validate(self, data): """ Verify passwords match. """ passwd = data['password'] passwd_conf = data['password_confirmation'] if passwd != passwd_conf: raise serializers.ValidationErrors("Password don't match.") password_validation.validate_password(passwd) return data def create(self, data): """ Handle user/profile creation. """ # delete password conf, not needed data.pop('password_confirmation') # create user and profile user = User.objects.create_user(**data, is_verified=False) Profile.objects.create(user=user) # send email confirmation to user self.send_confirmation_email(user) return user def send_confirmation_email(self, user): """ Send account verification link to given user. """ verification_token = self.gen_verification_token(user) subject = 'Welcome @{}! Verify your account to start using ShareRide.'.format( user.username) from_email = 'ShareRide <*****@*****.**>' content = render_to_string('emails/users/account_verification.html', { 'token': verification_token, 'user': user }) # send email msg = EmailMultiAlternatives(subject, content, from_email, [user.email]) msg.attach_alternative(content, "text/html") msg.send() print("Sending email") def gen_verification_token(self, user): """ Create JWT token that the user can use to verify their account. """ return 'abc'
class UserRegSerializer(serializers.Serializer): code = serializers.CharField(required=True, write_only=True, max_length=4, min_length=4, label='验证码', error_messages={ "blank": "请输入验证码", "required": "请输入验证码", "max_length": "验证码格式错误", "min_length": "验证码格式错误" }, help_text='验证码') username = serializers.CharField(label='用户名', required=True, allow_blank=False, help_text='用户名', validators=[ UniqueValidator( queryset=User.objects.all(), message="用户已经存在") ]) # mobile = models.CharField(null=True, blank=True, max_length=11) password = serializers.CharField(style={'input_type': 'password'}, label='密码', write_only=True, help_text='密码') # 不使用这个 使用信号量, 把数据库的密码转变成密文 # def create(self, validated_data): # user = super(UserRegSerializer, self).create(validated_data=validated_data) # user.set_password(validated_data["password"]) # user.save() # return user def validate_code(self, code): # 用户注册,已post方式提交信息,数据保存在initial_data里 # username是用户注册的手机号码,验证码按照添加的时间排序 verify_records = VerifyCode.objects.filter( mobile=self.initial_data['username']).order_by('-add_time') if verify_records: # 最近的一个验证码 last_record = verify_records[0] # 有效期为5分钟 five_minutes_age = datetime.now() - timedelta( hours=0, minutes=5, seconds=0) if five_minutes_age > last_record.add_time: raise serializers.ValidationError('验证码过期') if last_record.code != code: raise serializers.ValidationError('验证码错误') else: raise serializers.ValidationError('验证码错误') # 所有字段,atter是字段验证合法之后返回的总dict def validate(self, attrs): # 前段没有传mobile到后端,这里添加 attrs['mobile'] = attrs['username'] # code 是自己添加,数据库在没有这个字段,验证完成后删除 del attrs['code'] return attrs class Meta: model = User fields = ('username', 'code', 'mobile', 'password')
class BaseDistributionSerializer(ModelSerializer, BasePathOverlapMixin): """ The Serializer for the BaseDistribution model. The serializer deliberately omits the "remote" field, which is used for pull-through caching only. Plugins implementing pull-through caching will have to add the field in their derived serializer class like this:: remote = DetailRelatedField( required=False, help_text=_('Remote that can be used to fetch content when using pull-through caching.'), queryset=models.Remote.objects.all(), allow_null=True ) """ pulp_href = DetailIdentityField( view_name_pattern=r"distributions(-.*/.*)-detail") pulp_labels = LabelsField(required=False) base_path = serializers.CharField( help_text= _('The base (relative) path component of the published url. Avoid paths that \ overlap with other distribution base paths (e.g. "foo" and "foo/bar")' ), validators=[ UniqueValidator(queryset=models.BaseDistribution.objects.all()) ], ) base_url = BaseURLField( source="base_path", read_only=True, help_text= _("The URL for accessing the publication as defined by this distribution." ), ) content_guard = DetailRelatedField( required=False, help_text=_("An optional content-guard."), view_name_pattern=r"contentguards(-.*/.*)?-detail", queryset=models.ContentGuard.objects.all(), allow_null=True, ) name = serializers.CharField( help_text=_("A unique name. Ex, `rawhide` and `stable`."), validators=[ UniqueValidator(queryset=models.BaseDistribution.objects.all()), UniqueValidator(queryset=models.Distribution.objects.all()), ], ) def __init__(self, *args, **kwargs): """Initialize a BaseDistributionSerializer and emit deprecation warnings""" deprecation_logger.warn( _("BaseDistributionSerializer is deprecated and could be removed as early as " "pulpcore==3.13; use pulpcore.plugin.serializers.DistributionSerializer instead." )) return super().__init__(*args, **kwargs) class Meta: abstract = True model = models.BaseDistribution fields = ModelSerializer.Meta.fields + ( "base_path", "base_url", "content_guard", "pulp_labels", "name", )
def get_field_kwargs(field_name, model_field): """ Creates a default instance of a basic non-relational field. """ kwargs = {} validator_kwarg = list(model_field.validators) # The following will only be used by ModelField classes. # Gets removed for everything else. kwargs['model_field'] = model_field if model_field.verbose_name and needs_label(model_field, field_name): kwargs['label'] = capfirst(model_field.verbose_name) if model_field.help_text: kwargs['help_text'] = model_field.help_text max_digits = getattr(model_field, 'max_digits', None) if max_digits is not None: kwargs['max_digits'] = max_digits decimal_places = getattr(model_field, 'decimal_places', None) if decimal_places is not None: kwargs['decimal_places'] = decimal_places if isinstance(model_field, models.TextField) or (JSONField and isinstance( model_field, JSONField)): kwargs['style'] = {'base_template': 'textarea.html'} if isinstance(model_field, models.AutoField) or not model_field.editable: # If this field is read-only, then return early. # Further keyword arguments are not valid. kwargs['read_only'] = True return kwargs if model_field.has_default() or model_field.blank or model_field.null: kwargs['required'] = False if model_field.null and not isinstance(model_field, models.NullBooleanField): kwargs['allow_null'] = True if model_field.blank and (isinstance(model_field, models.CharField) or isinstance(model_field, models.TextField)): kwargs['allow_blank'] = True if isinstance(model_field, models.FilePathField): kwargs['path'] = model_field.path if model_field.match is not None: kwargs['match'] = model_field.match if model_field.recursive is not False: kwargs['recursive'] = model_field.recursive if model_field.allow_files is not True: kwargs['allow_files'] = model_field.allow_files if model_field.allow_folders is not False: kwargs['allow_folders'] = model_field.allow_folders if model_field.choices: kwargs['choices'] = model_field.choices else: # Ensure that max_value is passed explicitly as a keyword arg, # rather than as a validator. max_value = next( (validator.limit_value for validator in validator_kwarg if isinstance(validator, validators.MaxValueValidator)), None) if max_value is not None and isinstance(model_field, NUMERIC_FIELD_TYPES): kwargs['max_value'] = max_value validator_kwarg = [ validator for validator in validator_kwarg if not isinstance(validator, validators.MaxValueValidator) ] # Ensure that min_value is passed explicitly as a keyword arg, # rather than as a validator. min_value = next( (validator.limit_value for validator in validator_kwarg if isinstance(validator, validators.MinValueValidator)), None) if min_value is not None and isinstance(model_field, NUMERIC_FIELD_TYPES): kwargs['min_value'] = min_value validator_kwarg = [ validator for validator in validator_kwarg if not isinstance(validator, validators.MinValueValidator) ] # URLField does not need to include the URLValidator argument, # as it is explicitly added in. if isinstance(model_field, models.URLField): validator_kwarg = [ validator for validator in validator_kwarg if not isinstance(validator, validators.URLValidator) ] # EmailField does not need to include the validate_email argument, # as it is explicitly added in. if isinstance(model_field, models.EmailField): validator_kwarg = [ validator for validator in validator_kwarg if validator is not validators.validate_email ] # SlugField do not need to include the 'validate_slug' argument, if isinstance(model_field, models.SlugField): validator_kwarg = [ validator for validator in validator_kwarg if validator is not validators.validate_slug ] # IPAddressField do not need to include the 'validate_ipv46_address' argument, if isinstance(model_field, models.GenericIPAddressField): validator_kwarg = [ validator for validator in validator_kwarg if validator is not validators.validate_ipv46_address ] # Our decimal validation is handled in the field code, not validator code. # (In Django 1.9+ this differs from previous style) if isinstance(model_field, models.DecimalField) and DecimalValidator: validator_kwarg = [ validator for validator in validator_kwarg if not isinstance(validator, DecimalValidator) ] # Ensure that max_length is passed explicitly as a keyword arg, # rather than as a validator. max_length = getattr(model_field, 'max_length', None) if max_length is not None and (isinstance(model_field, models.CharField) or isinstance(model_field, models.TextField)): kwargs['max_length'] = max_length validator_kwarg = [ validator for validator in validator_kwarg if not isinstance(validator, validators.MaxLengthValidator) ] # Ensure that min_length is passed explicitly as a keyword arg, # rather than as a validator. min_length = next( (validator.limit_value for validator in validator_kwarg if isinstance(validator, validators.MinLengthValidator)), None) if min_length is not None and isinstance(model_field, models.CharField): kwargs['min_length'] = min_length validator_kwarg = [ validator for validator in validator_kwarg if not isinstance(validator, validators.MinLengthValidator) ] if getattr(model_field, 'unique', False): unique_error_message = model_field.error_messages.get('unique', None) if unique_error_message: unique_error_message = unique_error_message % { 'model_name': model_field.model._meta.verbose_name, 'field_label': model_field.verbose_name } validator = UniqueValidator( queryset=model_field.model._default_manager, message=unique_error_message) validator_kwarg.append(validator) if validator_kwarg: kwargs['validators'] = validator_kwarg return kwargs
class RemoteSerializer(ModelSerializer): """ Every remote defined by a plugin should have a Remote serializer that inherits from this class. Please import from `pulpcore.plugin.serializers` rather than from this module directly. """ pulp_href = DetailIdentityField( view_name_pattern=r"remotes(-.*/.*)-detail", ) name = serializers.CharField( help_text=_("A unique name for this remote."), validators=[UniqueValidator(queryset=models.Remote.objects.all())], ) url = serializers.CharField( help_text="The URL of an external content source.", ) ca_cert = serializers.CharField( help_text= "A string containing the PEM encoded CA certificate used to validate the server " "certificate presented by the remote server. All new line characters must be " "escaped.", required=False, allow_null=True, ) client_cert = serializers.CharField( help_text= "A string containing the PEM encoded client certificate used for authentication. " "All new line characters must be escaped.", required=False, allow_null=True, ) client_key = serializers.CharField( help_text="A PEM encoded private key used for authentication.", required=False, allow_null=True, ) tls_validation = serializers.BooleanField( help_text="If True, TLS peer validation must be performed.", required=False, ) proxy_url = serializers.CharField( help_text="The proxy URL. Format: scheme://user:password@host:port", required=False, allow_null=True, ) username = serializers.CharField( help_text="The username to be used for authentication when syncing.", required=False, allow_null=True, ) password = serializers.CharField( help_text="The password to be used for authentication when syncing.", required=False, allow_null=True, ) pulp_last_updated = serializers.DateTimeField( help_text="Timestamp of the most recent update of the remote.", read_only=True) download_concurrency = serializers.IntegerField( help_text="Total number of simultaneous connections.", required=False, min_value=1) policy = serializers.ChoiceField( help_text="The policy to use when downloading content.", choices=((models.Remote.IMMEDIATE, "When syncing, download all metadata and content now.")), default=models.Remote.IMMEDIATE, ) def validate_url(self, value): """ Check if the 'url' is a ``file://`` path, and if so, ensure it's an ALLOWED_IMPORT_PATH. The ALLOWED_IMPORT_PATH is specified as a Pulp setting. Args: value: The user-provided value for 'url' to be validated. Raises: ValidationError: When the url starts with `file://`, but is not a subfolder of a path in the ALLOWED_IMPORT_PATH setting. Returns: The validated value. """ if not value.lower().startswith("file://"): return value user_path = value[7:] for allowed_path in settings.ALLOWED_IMPORT_PATHS: user_provided_realpath = os.path.realpath(user_path) if user_provided_realpath.startswith(allowed_path): return value raise serializers.ValidationError( _("url '{}' is not an allowed import path").format(value)) class Meta: abstract = True model = models.Remote fields = ModelSerializer.Meta.fields + ( "name", "url", "ca_cert", "client_cert", "client_key", "tls_validation", "proxy_url", "username", "password", "pulp_last_updated", "download_concurrency", "policy", )
class CustomerContactUpdateSerializer(serializers.ModelSerializer): organization_name = serializers.CharField( required=False, allow_blank=True, validators=[ UniqueValidator(queryset=Customer.objects.all()), ], ) organization_type_of = serializers.IntegerField(required=False, ) given_name = serializers.CharField(required=True, allow_blank=False, validators=[]) last_name = serializers.CharField(required=True, allow_blank=False, validators=[]) # We are overriding the `email` field to include unique email validation. email = serializers.EmailField( validators=[ UniqueValidator(queryset=Customer.objects.all()), ], required=False, allow_null=True, allow_blank=True, ) # Custom formatting of our telephone fields. primary_phone = PhoneNumberField(allow_null=False, required=True, source="telephone") primary_phone_type_of = serializers.IntegerField( required=True, validators=[], source="telephone_type_of") secondary_phone = PhoneNumberField(allow_null=True, required=False, source="other_telephone") secondary_phone_type_of = serializers.IntegerField( required=False, validators=[], source="other_telephone_type_of") class Meta: model = Customer fields = ( 'organization_name', 'organization_type_of', 'given_name', 'last_name', 'email', 'primary_phone', 'primary_phone_type_of', 'secondary_phone', 'secondary_phone_type_of', 'is_ok_to_email', 'is_ok_to_text', ) def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """ queryset = queryset.prefetch_related('owner', 'created_by', 'last_modified_by', 'tags', 'comments', 'organization') return queryset def get_organization_name(self, data): if data.get('type_of', None) == COMMERCIAL_CUSTOMER_TYPE_OF_ID: organization_name = data.get('organization_name', None) if organization_name is None: raise serializers.ValidationError( _("Please provide the organization name.")) return data def get_organization_type_of(self, data): if data.get('type_of', None) == COMMERCIAL_CUSTOMER_TYPE_OF_ID: organization_type_of = data.get('organization_type_of', None) if organization_type_of is None: raise serializers.ValidationError( _("Please provide the organization type of.")) return data def validate(self, data): """ Override the validator to provide additional custom validation based on our custom logic. 1. If 'type_of' == Commercial then make sure 'email' was inputted. """ # CASE 1 - Other reason if data.get('type_of', None) == COMMERCIAL_CUSTOMER_TYPE_OF_ID: email = data.get('email', None) if email is None: raise serializers.ValidationError( _("Please provide an email if client is commercial.")) # Return our data. return data def update(self, instance, validated_data): """ Override this function to include extra functionality. """ # Get our inputs. email = validated_data.get('email', instance.email) #----------------------------------------------------------- # Bugfix: Created `SharedUser` object if not created before. #----------------------------------------------------------- if instance.owner is None and email: owner = SharedUser.objects.filter(email=email).first() if owner: instance.owner = owner instance.save() logger.info("BUGFIX: Attached existing shared user to staff.") else: instance.owner = SharedUser.objects.create( first_name=validated_data['given_name'], last_name=validated_data['last_name'], email=email, is_active=True, franchise=self.context['franchise'], was_email_activated=True) instance.save() logger.info( "BUGFIX: Created shared user and attached to staff.") #--------------------------- # Update `SharedUser` object. #--------------------------- if instance.owner: # Update details. instance.owner.first_name = validated_data.get( 'given_name', instance.owner.first_name) instance.owner.last_name = validated_data.get( 'last_name', instance.owner.last_name) if email: instance.owner.email = email instance.owner.username = get_unique_username_from_email(email) # Update the password. password = validated_data.get('password', None) instance.owner.set_password(password) # Save the model to the database. instance.owner.save() logger.info("Updated shared user.") #--------------------------- # Update `Customer` object. #--------------------------- instance.given_name = validated_data.get('given_name', instance.given_name) instance.middle_name = validated_data.get('middle_name', instance.middle_name) instance.last_name = validated_data.get('last_name', instance.last_name) instance.last_modified_by = self.context['last_modified_by'] instance.is_ok_to_email = validated_data.get('is_ok_to_email', instance.is_ok_to_email) instance.is_ok_to_text = validated_data.get('is_ok_to_text', instance.is_ok_to_text) instance.last_modified_by = self.context['last_modified_by'] instance.last_modified_from = self.context['last_modified_from'] instance.last_modified_from_is_public = self.context[ 'last_modified_from_is_public'] instance.organization_name = validated_data.get( 'organization_name', instance.organization_name) instance.organization_type_of = validated_data.get( 'organization_type_of', instance.organization_type_of) instance.email = validated_data.get('email', instance.contact_type) instance.telephone = validated_data.get('telephone', None) instance.telephone_extension = validated_data.get( 'telephone_extension', None) instance.telephone_type_of = validated_data.get( 'telephone_type_of', None) instance.other_telephone = validated_data.get('other_telephone', None) instance.other_telephone_extension = validated_data.get( 'other_telephone_extension', None) instance.other_telephone_type_of = validated_data.get( 'other_telephone_type_of', None) instance.save() logger.info("Updated the customer.") return instance
class UserRegSerializer(serializers.ModelSerializer): code = serializers.CharField(required=True,write_only=True,max_length=4,min_length=4,label="验证码",error_messages={ "blank":"请输入验证码", "required": "请输入验证码", "max_length":"验证码格式错误", "min_length":"验证码格式错误" },help_text="验证码") username = serializers.CharField(label="用户名",required=True,allow_blank=False,validators=[UniqueValidator(queryset=User.objects.all(),message="用户已经存在")]) password = serializers.CharField( style={'input_type':'password'},label="密码",write_only=True, ) def validate_code(self,code): verify_records = VerifyCode.objects.filter(mobile=self.initial_data["username"]).order_by("-add_time") if verify_records: last_record = verify_records[0] five_mintes_ago = datetime.now() - timedelta(hours=0,minutes=5,seconds=0) if five_mintes_ago > last_record.add_time: raise serializers.ValidationError("验证码过期") if last_record.code != code: raise serializers.ValidationError("验证码错误") else: serializers.ValidationError("验证码错误") def validate(self, attrs): attrs["mobile"] = attrs["username"] del attrs["code"] return attrs class Meta: model = User fields = ("username","code","mobile","password")
class UserCreateSerializer(serializers.ModelSerializer): email = serializers.EmailField( required=True, max_length=50, validators=[ UniqueValidator(queryset=User.objects.all(), message="Account with this email already exists") ]) username = serializers.CharField( required=True, max_length=30, validators=[ UniqueValidator(queryset=User.objects.all(), message="This username is already taken") ]) password = serializers.CharField(min_length=8, write_only=True, required=True, max_length=30) first_name = serializers.CharField(min_length=3, required=True, max_length=30) last_name = serializers.CharField(min_length=3, required=True, max_length=30) profile = ProfileSerializer() class Meta: model = User fields = [ 'username', 'email', 'first_name', 'last_name', 'password', 'profile' ] def create(self, validated_data): email = validated_data["email"] username = validated_data["username"] first_name = validated_data["first_name"] last_name = validated_data["last_name"] password = validated_data["password"] profile_data = validated_data.pop("profile") if (not validate_contact_no(None, profile_data["contact_no"])): raise serializers.ValidationError( "account with this contact no. exists") user_profile = Profile(**profile_data) #user is created and saved user = User.objects.create_user(profile=user_profile, email=email, username=username, first_name=first_name, last_name=last_name, password=password) #after user is created and saved,profile is also saved in database user_profile.save() return user
class UserUpdateSerializer(serializers.ModelSerializer): # A field from the user's profile: bio = serializers.CharField(source='profile.bio', allow_blank=True) name = serializers.CharField(source='profile.name', max_length=32, allow_blank=True) avatar = serializers.URLField(source='profile.avatar', allow_blank=True) status = serializers.CharField(source='profile.status', allow_blank=True, default='', min_length=0, max_length=16) current_password = serializers.CharField( write_only=True, allow_blank=True, label=_("Current Password"), help_text=_('Required'), ) new_password = serializers.CharField( allow_blank=True, default='', write_only=True, min_length=4, max_length=32, label=_("New Password"), ) email = serializers.EmailField( allow_blank=True, default='', validators=[ UniqueValidator(queryset=User.objects.all(), message='has already been taken by other user') ]) class Meta: model = User fields = ('username', 'name', 'email', 'current_password', 'new_password', 'bio', 'avatar', 'status') read_only_fields = ('username', ) lookup_field = 'username' def update(self, instance, validated_data): # make sure requesting user provide his current password # e.g if admin 'endiliey' is updating a user 'donaldtrump', # currentPassword must be 'endiliey' password instead of 'donaldtrump' password try: username = self.context.get('request').user.username except: msg = _('Must be authenticated') raise serializers.ValidationError(msg, code='authorization') password = validated_data.get('current_password') validated_data.pop('current_password', None) if not password: msg = _('Must provide current password') raise serializers.ValidationError(msg, code='authorization') user = authenticate(request=self.context.get('request'), username=username, password=password) if not user: msg = _('Sorry, the password you entered is incorrect.') raise serializers.ValidationError(msg, code='authorization') # change password to a new one if it exists new_password = validated_data.get('new_password') or None if new_password: instance.set_password(new_password) validated_data.pop('new_password', None) # Update user profile fields profile_data = validated_data.pop('profile', None) profile = instance.profile for field, value in profile_data.items(): if value: setattr(profile, field, value) # Update user fields for field, value in validated_data.items(): if value: setattr(instance, field, value) profile.save() instance.save() return instance
class EmailSerializer(serializers.Serializer): """serializersa name field for testing our EmailAPI""" name = serializers.CharField( validators=[UniqueValidator(queryset=EmailValidation.objects.all())])
class WatchCreateSerializer(serializers.Serializer): type_of = serializers.IntegerField() tags = serializers.PrimaryKeyRelatedField( many=True, queryset=Tag.objects.all(), allow_null=True, required=False, ) name = serializers.CharField( validators=[ UniqueValidator(queryset=Watch.objects.all()) ], ) description = serializers.CharField() district = serializers.SlugField() street_membership = serializers.JSONField() is_virtual = serializers.BooleanField(allow_null=True,) website_url = serializers.URLField( required=False, allow_blank=True, allow_null=True, ) facebook_url = serializers.URLField( required=False, allow_blank=True, allow_null=True, ) def validate_district(self, value): #TODO: ADD SECURITY SO NON-EXECUTIVES CANNOT ATTACH TO OTHER USERS. if not District.objects.filter(slug=value).exists(): raise serializers.ValidationError("District does not exist") return value def create(self, validated_data): """ Override the `create` function to add extra functinality. """ request = self.context.get('request') type_of = validated_data.get('type_of') name = validated_data.get('name') description = validated_data.get('description') district_slug = validated_data.get('district') tags = validated_data.get('tags') street_membership = validated_data.get('street_membership') is_virtual = validated_data.get('is_virtual', False) facebook_url = validated_data.get('facebook_url', None) if is_virtual == None or is_virtual == "": is_virtual = False district = District.objects.get(slug=district_slug) watch = Watch.objects.create( type_of = type_of, name = name, description = description, district = district, is_virtual = is_virtual, created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, facebook_url=facebook_url, ) # Attached our tags. if tags is not None: if len(tags) > 0: watch.tags.set(tags) logger.info("Attached tag to watch.") # Iterate through all the street addresses and process. for data in street_membership: s = StreetAddressRangeCreateSerializer( data=data, context={ 'request': request, 'watch': watch, } ); s.is_valid(raise_exception=True) s.save() logger.info("Created watch contact.") # ''' # Run in the background the code which will `process` the newly created # watch object. # ''' # django_rq.enqueue( # process_watch_with_slug_func, # request.tenant.schema_name, # watch.user.slug # ) # # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) return watch
class UserRegSerializer(serializers.ModelSerializer): ''' 用户注册 ''' #UserProfile中没有code字段,这里需要自定义一个code序列化字段 code = serializers.CharField(required=True, write_only=True, max_length=4, min_length=4, error_messages={ "blank": "请输入验证码", "required": "请输入验证码", "max_length": "验证码格式错误", "min_length": "验证码格式错误" }, help_text="验证码") #验证用户名是否存在 username = serializers.CharField(label="用户名", help_text="用户名", required=True, allow_blank=False, validators=[ UniqueValidator( queryset=User.objects.all(), message="用户已经存在") ]) password = serializers.CharField(style={'input_type': 'password'}, label='密码', write_only=True) # #密码加密保存 # def create(self, validated_data): # user = super(UserRegSerializer, self).create(validated_data=validated_data) # user.set_password(validated_data["password"]) # user.save() # return user #验证code def validate_code(self, code): # 用户注册,已post方式提交注册信息,post的数据都保存在initial_data里面 #username就是用户注册的手机号,验证码按添加时间倒序排序,为了后面验证过期,错误等 verify_records = VerifyCode.objects.filter( mobile=self.initial_data["username"]).order_by("-add_time") if verify_records: # 最近的一个验证码 last_record = verify_records[0] # 有效期为五分钟。 five_mintes_ago = datetime.now() - timedelta( hours=0, minutes=5, seconds=0) if five_mintes_ago > last_record.add_time: raise serializers.ValidationError("验证码过期") if last_record.code != code: raise serializers.ValidationError("验证码错误") else: raise serializers.ValidationError("验证码错误") # 所有字段。attrs是字段验证合法之后返回的总的dict def validate(self, attrs): #前端没有传mobile值到后端,这里添加进来 attrs["mobile"] = attrs["username"] #code是自己添加得,数据库中并没有这个字段,验证完就删除掉 del attrs["code"] return attrs class Meta: model = User fields = ('username', 'code', 'mobile', 'password')
class UserSerializer(QueryFieldsMixin, serializers.ModelSerializer): id = serializers.ReadOnlyField() adverts = serializers.PrimaryKeyRelatedField(many=True, read_only=True) profile = PKField(read_only=True, source='profile_picture') profile_picture = Base64ImageField(max_length=None, use_url=True, required=False, write_only=True) has_picture = serializers.SerializerMethodField('has_profile_picture', read_only=True) #img_link = serializers.SerializerMethodField('get_photo_url', read_only=True) sold_books = serializers.SerializerMethodField('calculate_sold', read_only=True) bought_books = serializers.SerializerMethodField('calculate_bought', read_only=True) email = serializers.EmailField( required=False, validators=[ UniqueValidator(queryset=User.objects.all(), message='This email is already taken') ]) username = serializers.CharField( required=False, max_length=15, validators=[ UniqueValidator(queryset=User.objects.all(), message='This username is already taken') ]) class Meta: model = User fields = ('id', 'username', 'email', 'adverts', 'profile_picture', 'profile', 'has_picture', 'sold_books', 'bought_books') @staticmethod def setup_eager_loading(queryset, *args): """ Perform necessary eager loading of data. """ #Ta in alla adverts och läs in 'owner och 'responder' i cahce advert_queryset = Advert.objects.select_related( 'responder', 'owner').only('transaction_type', 'state', 'responder', 'owner', 'id') for arg in args: #Kollar om det är OneToOne eller OneToMany relationship if isinstance(User._meta.get_field(arg), type(User._meta.get_field('profile_picture'))): queryset = queryset.select_related(arg) else: if arg == 'adverts': queryset = queryset.prefetch_related( Prefetch("adverts", queryset=advert_queryset)).prefetch_related( 'adverts__responder') elif arg == 'accepted_adverts': queryset = queryset.prefetch_related( Prefetch("accepted_adverts", queryset=advert_queryset)) return queryset def has_profile_picture(self, user): return bool(user.profile_picture.image) def calculate_sold(self, user): sum = len([ sold_advert for sold_advert in user.adverts.all() if sold_advert.transaction_type == 'S' and sold_advert.state == 'I' and sold_advert.responder is not None ]) sum += len([ sold_advert for sold_advert in user.accepted_adverts.all() if sold_advert.transaction_type == 'B' and sold_advert.state == 'I' ]) return sum def calculate_bought(self, user): sum = len([ bought_advert for bought_advert in user.adverts.all() if bought_advert.transaction_type == 'B' and bought_advert.state == 'I' and bought_advert.responder is not None ]) sum += len([ bought_advert for bought_advert in user.accepted_adverts.all() if bought_advert.transaction_type == 'S' and bought_advert.state == 'I' ]) return sum def get_photo_url(self, user): try: profilePicUrl = user.profile_picture.image.url return self.context.get('request').build_absolute_uri( profilePicUrl) except: return None def update(self, user, validated_data): try: image_data = validated_data.pop('profile_picture') except: image_data = None if image_data is not None: userProfile = user.profile_picture userProfile.image = image_data userProfile.save() return super(UserSerializer, self).update(user, validated_data)
class UserSignUpSerializer(serializers.Serializer): """ User sign up serializer. """ email = serializers.EmailField( validators=[UniqueValidator(queryset=User.objects.all())]) username = serializers.CharField( min_length=4, max_length=20, validators=[UniqueValidator(queryset=User.objects.all())]) # Phone number phone_regex = RegexValidator( regex=r'\+?1?\d{9,15}$', message= "Phone number must be entered in the format: +999999999. Up to 15 digits allowed." ) phone_number = serializers.CharField(validators=[phone_regex]) # Password password = serializers.CharField(min_length=8, max_length=64) password_confirmation = serializers.CharField(min_length=8, max_length=64) # Name first_name = serializers.CharField(min_length=2, max_length=30) last_name = serializers.CharField(min_length=2, max_length=30) def validate(self, data): """ Verify Passwords match.""" passwd = data['password'] passwd_conf = data['password_confirmation'] if passwd != passwd_conf: raise serializers.ValidationError("Passwords don´t no match.") password_validation.validate_password(passwd) return data def create(self, data): """ Handle user and profile creation. """ data.pop('password_confirmation') user = User.objects.create_user(**data, is_verified=False, is_client=True) profile = Profile.objects.create(user=user) self.send_confirmation_email(user) return user def send_confirmation_email(self, user): """ Send account verification link to give user. """ verification_token = self.gen_verification_token(user) subject = 'Welcome @{}! Verity your account to start using Comparte Ride'.format( user.username) from_email = 'Comparte Ride <*****@*****.**>' content = render_to_string('emails/users/account_verification.html', { 'token': verification_token, 'user': user }) msg = EmailMultiAlternatives(subject, content, from_email, [user.email]) msg.attach_alternative(content, "text/html") msg.send() def gen_verification_token(self, user): """ Create JWT token that the user can use to verify its account.""" exp_date = timezone.now() + timedelta(days=3) payload = { 'user': user.username, 'exp': int(exp_date.timestamp()), 'type': 'email_confirmation' } token = jwt.encode(payload, settings.SECRET_KEY, algorithm='HS256') return token.decode()
class RegisterSerializer(serializers.ModelSerializer): """Register a user""" username = serializers.CharField( required=True, help_text="Your new username", validators=[UniqueValidator(queryset=DjangoUser.objects.all())], ) email = serializers.EmailField(required=True, help_text="Your e-mail address") password = serializers.CharField(write_only=True, required=True, validators=[validate_password], help_text="Your new password") password2 = serializers.CharField( write_only=True, required=True, help_text="Repeat your new password again") # name changed for the field to work with https://github.com/benchesh/Cookie-Notice-Blocker shrivacy_policy = serializers.BooleanField( write_only=True, required=False, help_text="I have read and I accept" " the privacy policy") recaptcha = ReCaptchaV2Field(required=not settings.NOCAPTCHA) class Meta: model = DjangoUser fields = ('username', 'password', 'password2', 'email', 'recaptcha', 'shrivacy_policy') def validate(self, attrs): if attrs['password'] != attrs['password2']: raise serializers.ValidationError( {"password": "******"}) if not attrs.get('shrivacy_policy', False): raise serializers.ValidationError({ 'shrivacy_policy': "Please accept the privacy policy" " to sign up for Tournesol" }) return attrs def create(self, validated_data): user = None try: user = DjangoUser.objects.create_user( username=validated_data['username'], email=validated_data['email'], password=validated_data['password'], is_active=False, ) user_info = UserInformation.objects.create(user=user) email = VerifiableEmail.objects.create(email=user.email, user=user_info, is_verified=False) # creating preferences for a new user # return redirect(f'/email_show_instructions/{user.email}') except Exception as e: raise serializers.ValidationError({'username': [str(e)]}) create_user_preferences() send_verification_email(email) return user
class RegisterSerializer(serializers.ModelSerializer): """ Second step to register serializer """ email = serializers.EmailField( validators=[ UniqueValidator(queryset=User.objects.all(), message=ACCOUNT_ALREADY_EXIST) ], required=True, ) terms_and_conditions = serializers.BooleanField(required=True) repeat_password = serializers.CharField(required=True) class Meta: model = User fields = ( "email", "first_name", "last_name", "password", "repeat_password", "terms_and_conditions", ) @staticmethod def validate_password(password): """ Validate if the password is correct :param password: password input :return: password """ try: validators.validate_password(password) return password except Exception as e: raise serializers.ValidationError(" ".join(e.messages)) @staticmethod def validate_terms_and_conditions(terms_and_conditions): """ Check if the 'term and conditions' are correct :param terms_and_conditions: documents input :return: documents """ if not terms_and_conditions: raise serializers.ValidationError(TERMS_AND_CONDITIONS) return terms_and_conditions def validate(self, attrs): """ Validate attrs """ password = attrs.get('password') repeat_password = attrs.pop('repeat_password') if password != repeat_password: raise serializers.ValidationError( {'repeat_password': PASSWORDS_DOES_NOT_MATCH}) return attrs def save(self): """ Create new user """ data = {**self.validated_data} email = data.pop("email") password = data.pop("password") user = User.objects.create_user(email=email, password=password, **data) return user