Skip to content

Instantly share code, notes, and snippets.

@meoooh
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoooh/9528352 to your computer and use it in GitHub Desktop.
Save meoooh/9528352 to your computer and use it in GitHub Desktop.
"AttributeError: 'MyUser' object has no attribute 'update'"
class MyUserManager(BaseUserManager):
def create(self, email, name, birthday, sex, password):
return self.create_user(email=email,
password=password,
name=name,
birthday=birthday,
sex=sex,
)
def create_user(self, email, name, birthday, sex, password):
if not email:
raise ValueError(_('email cannot be blank.'))
user = self.model(
email=MyUserManager.normalize_email(email),
name=name,
birthday=birthday,
sex=sex,
is_active=True,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, name, birthday, sex, password):
user = self.create_user(email=email,
password=password,
name=name,
birthday=birthday,
sex=sex,
)
user.is_staff=True
user.is_superuser=True
user.is_admin=True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=254, unique=True, db_index=True)
name = models.CharField(max_length=254)
birthday = models.DateField()
sex = models.SmallIntegerField()
creation = models.DateTimeField(auto_now_add=True)
modification = models.DateTimeField(auto_now=True)
objects = MyUserManager()
is_staff = models.BooleanField(default=False, blank=True,)
is_active = models.BooleanField(default=True, blank=True,)
def get_absolute_url(self):
return reverse('vomuser-detail', args=[str(self.id)])
def __unicode__(self):
return self.email
def has_perm(self, perm, obj=None):
return super(VomUser, self).has_perm(perm, obj)
def has_module_perms(self, app_label):
return super(VomUser, self).has_module_perms(app_label)
def get_short_name(self):
return self.name
def get_username(self):
return self.name
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'birthday', 'sex', ]
class ChangePasswordSerializer(serializers.Serializer):
current_password = serializers.CharField()
new_password = serializers.CharField()
new_password2 = serializers.CharField()
def validate_current_password(self, attrs, source):
# http://goo.gl/5Frj9Z
if not self.object.check_password(attrs["current_password"]):
raise serializers.ValidationError(
_("Current password is not correct"),
code='password_wrong',
)
return attrs
def validate_new_password2(self, attrs, source):
if attrs["new_password"] != attrs["new_password2"]:
raise serializers.ValidationError(
_("This field must be matched by password field."),
code='password_mismatch',
)
return attrs
def restore_object(self, attrs, instance=None):
""" change password """
user = super(ChangePasswordSerializer, self).restore_object(attrs,
instance)
if instance is not None:
instance.set_password(attrs['new_password2'])
return instance
return user
@api_view(['PATCH'])
@permission_classes((custom_permissions.permissions.IsAuthenticated, ))
def changePassword(request):
import ipdb; ipdb.set_trace()
serializer = serializers.ChangePasswordSerializer(request.user,
request.DATA,)
if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment