def post(self, request): form = self.form_class(request.POST) if form.is_valid(): user = request.user pw = form.cleaned_data['password2'] user.set_password(pw) password_changed(pw) user.save() user = authenticate(email=user.email, password=pw) login(request, user) pk = request.user.pk if hasattr(user, 'h_admin'): return HttpResponseRedirect( reverse('h_admin:profile', args=[pk])) elif hasattr(user, 'patient'): return HttpResponseRedirect( reverse('accounts:patientprofile', args=[pk])) elif hasattr(user, 'doctor'): return HttpResponseRedirect( reverse('doctor:profile', args=[pk])) elif hasattr(user, 'nurse'): return HttpResponseRedirect(reverse('nurse:profile', args=[pk])) return render(request, self.template_name, {'form': form})
def save(self, *args, **kwargs): if not self.displayName: self.displayName = self.username super().save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def save(self, *args, **kwargs): if self._password is not None: password_validation.password_changed(self._password, self) self._password = None if not self.id: self.id = generate_id() self.date_joined = timezone.now() if not self.username: self.username = self.create_unique_username() # using your function as above or anything else success = False failures = 0 while not success: try: super(User, self).save(*args, **kwargs) except IntegrityError: failures += 1 if failures > 5: # or some other arbitrary cutoff point at which things are clearly wrong raise KeyError else: # looks like a collision, try another random value self.id = generate_id() else: success = True else: super(User, self).save(*args, **kwargs)
def save(self, *args, **kwargs): self.username = generate_random_username() self.full_clean() super(MyUser, self).save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def save(self, commit=True): password = self.cleaned_data["new_password1"] self.user.set_password(password) password_validation.password_changed(password, self.user) if commit: self.user.save() return self.user
def save(self, commit=True): password = self.cleaned_data["new_password1"] self.user.set_password(password) password_validation.password_changed(password, self.user) if commit: self.user.save() return self.user
def save(self, *args, **kwargs): if self.mobile or self.email: super().save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None else: raise Exception('mobile and email is none')
def save(self, *args, **kwargs): self.check_group() super().save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def validate(self, password, user): if user.check_password(password): raise ValidationError( _("Your password must be different from old password"), code='password_no_change', ) else: password_changed(password)
def save(self, *args, **kwargs): if not self.pk: super().save(*args, **kwargs) email, domain = self.email.split('@') Profile.objects.create(user=self, username=email) else: cache.delete(f'user{self.pk}') super().save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def test_password_changed_with_custom_validator(self): class Validator: def password_changed(self, password, user): self.password = password self.user = user user = object() validator = Validator() password_changed('password', user=user, password_validators=(validator,)) self.assertIs(validator.user, user) self.assertEqual(validator.password, 'password')
def save(self, *args, **kwargs): # unique_url, access_tokenが存在しない場合は生成 if len(self.unique_url) == 0: self.unique_url = self.generate_unique_url() if self.access_token is None: self.access_token = self.generate_access_token() # 公開期限日が設定されていない場合、回答期限日と同一であると設定 if self.expired_at and not self.finished_at: self.finished_at = self.expired_at super().save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def patch(self, request): body = json.loads(request.body) user = User.objects.get(id=request.user.id) if 'password' in body and request.user.check_password( body['password']): # should consider validation if 'alpaca_key_id' in body: user.alpaca_key_id = body['alpaca_key_id'] if not body['alpaca_key_id'] or len( body['alpaca_key_id']) > 64: return Response( { 'alpaca_key_id': "Must not be empty or longer than 64 characters" }, status=status.HTTP_400_BAD_REQUEST) if 'alpaca_secret_key' in body: user.alpaca_secret_key = body['alpaca_secret_key'] if not body['alpaca_secret_key'] or len( body['alpaca_secret_key']) > 64: return Response( { 'alpaca_secret_key': "Must not be empty or longer than 64 characters" }, status=status.HTTP_400_BAD_REQUEST) if 'new_email' in body: try: validate_email(body['new_email']) user.email = body['new_email'] except Exception as e: return Response({'new_email': e.messages[0]}, status=status.HTTP_400_BAD_REQUEST) if 'new_password' in body: try: v = validate_password(password=body['new_password'], user=request.user) user.set_password(str(body['new_password'])) password_changed(password=body['new_password'], user=request.user) except Exception as e: return Response({'new_password': e.messages[0]}, status=status.HTTP_400_BAD_REQUEST) user.save() return Response({'message': 'User data updated successfully.'}, status=status.HTTP_200_OK) else: return Response({'password': '******'}, status=status.HTTP_401_UNAUTHORIZED)
def create(self, validated_data): password = validated_data['password'] try: validate_password(password) except Exception as e: raise ValidationError({'password': e}) user = get_user_model().objects.create( email=validated_data['email'], username=validated_data['username'], ) user.set_password(password) user.save() password_changed(password, user) return user
def post(self, request): ''' 处理用户对于```/accounts/change_password```的post请求 处理用户的修改密码的请求 @param in POST old_password<str>: 旧密码 new_password<str>: 新密码 @return in JSON code<int>: 返回代码 可能值及其含义 | 返回值 | 含义 | | ------ | ------------------ | | 200 | 修改密码成功 | | 400 | 旧的密码错误 | | 401 | 未登录 | | 420 | 新的密码不符合要求 | msg<dict>: 返回代码相应的解释 ''' old_password = request.POST.get('old_password') new_password = request.POST.get('new_password') try: password_changed(new_password, request.user) except ValidationError as e: return JsonResponse({'code': 420, 'msg': e.messages}) user = authenticate(request, username=request.user.username, password=old_password) if user: user.set_password(new_password) user.save() return JsonResponse({ 'code': 200, 'msg': [_('Change password successfully')] }) else: return JsonResponse({ 'code': 400, 'msg': [_('The old password is not correct')] })
def save(self, *args, **kwargs): if self.get_in_date is None: self.get_in_date = datetime.today() if self.get_out_date is not None: if self.get_out_date > self.get_in_date: self.experience = str( (self.get_out_date.year - self.get_in_date.year) * 12 + (self.get_out_date.month - self.get_in_date.month)) + ' мес' else: if datetime.today().strftime( '%Y-%m-%d') > self.get_in_date.strftime('%Y-%m-%d'): self.experience = str( (datetime.today().year - self.get_in_date.year) * 12 + (datetime.today().month - self.get_in_date.month)) + ' мес' else: self.experience = '0 мес' super().save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def save(self, *args, **kwargs): # Custom primary key if not self.pk: self.pk = generate_short_uuid4() # Generate Password and a valid activation token if not self.activation_key: self.activation_key = generate_activation_token(self) if self._password is not None: password_changed(self._password, self) self._password = None # Referral Code Signature if not self.referral_code: signer = signing.Signer() ref = signer.sign(self.pk) self.referral_code = ref.split(":")[1] return super(AbstractUser, self).save(*args, **kwargs)
def test_password_changed(self): self.assertIsNone(password_changed("password"))
def save(self, *args, **kwargs): super(AbstractBaseUser, self).save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def save(self, *args, **kwargs): super(AbstractBaseUser, self).save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def save(self, *args, **kwargs): super().save(*args, **kwargs) if self._otp is not None: password_validation.password_changed(self._otp, self) self._otp = None
def test_password_changed(self): self.assertIsNone(password_changed("password"))
def update(self, instance, validated_data): """Only change password in PUT method""" password_changed(validated_data.password, instance) return self.list(instance.pk)
def save(self, *args, **kwargs): if self.password is not None: password_validation.password_changed(self.password, self) if not self.is_superuser: self.set_password(self.password) super().save(*args, **kwargs)
def save(self, *args, **kwargs): super(Corporator, self).save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
def test_password_changed(self): self.assertIsNone(password_changed('password'))
"""
def save(self, *args, **kwargs): # self.slug = self.slug or slugify(self.username) super().save(*args, **kwargs) if self._password is not None: password_validation.password_changed(self._password, self) self._password = None
======= >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 def __str__(self): return self.get_username() def clean(self): setattr(self, self.USERNAME_FIELD, self.normalize_username(self.get_username())) def save(self, *args, **kwargs): <<<<<<< HEAD super(AbstractBaseUser, self).save(*args, **kwargs) ======= super().save(*args, **kwargs) >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 if self._password is not None: password_validation.password_changed(self._password, self) self._password = None def natural_key(self): return (self.get_username(),) @property def is_anonymous(self): """ Always return False. This is a way of comparing User objects to anonymous users. """ <<<<<<< HEAD return CallableFalse ======= return False