Created
January 31, 2014 09:51
-
-
Save meoooh/8729216 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 User(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() | |
dateOfRecevingLastQuestion = models.DateField( | |
default=date.today()-timedelta(days=1) | |
) | |
questionOfToday = models.ForeignKey(Question, null=True, blank=True) | |
constellation = models.ForeignKey(Constellation, null=True, blank=True) | |
switch = models.BooleanField(default=False) | |
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 __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', ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserSerializer(serializers.HyperlinkedModelSerializer): | |
password2 = serializers.CharField() | |
class Meta: | |
model = User | |
fields = ('url', 'id', 'email', 'sex', 'birthday', | |
'name', 'password', 'password2') | |
# read_only_fields = ('email',) | |
write_only_fields = ('password',) | |
def __init__(self, *args, **kwargs): | |
# Don't pass the 'fields' arg up to the superclass | |
fields = kwargs.pop('fields', None) | |
# Instantiate the superclass normally | |
super(UserSerializer, self).__init__(*args, **kwargs) | |
if fields: | |
# Drop any fields that are not specified in the `fields` argument. | |
allowed = set(fields) | |
existing = set(self.fields.keys()) | |
for field_name in existing - allowed: | |
self.fields.pop(field_name) | |
def restore_object(self, attrs, instance=None): | |
user = super(UserSerializer, self).restore_object(attrs, instance) | |
if instance is None: | |
user.set_password(attrs['password']) | |
return user | |
def to_native(self, obj): | |
if 'password2' in self.fields: | |
del self.fields['password2'] | |
return super(UserSerializer, self).to_native(obj) | |
def validate_sex(self, attrs, source): | |
if attrs['sex'] not in (0, 1): | |
raise serializers.ValidationError( | |
_("You must choice '1' or '0'. '1' means male and '0' means" + | |
" female. Your input: '%(value)s'"), | |
code='invalid', | |
params={'value': attrs['sex']} | |
) | |
return attrs | |
def validate_password2(self, attrs, source): | |
# http://goo.gl/U7FGWZ | |
if attrs.get('password') == attrs.pop('password2'): | |
return attrs | |
raise serializers.ValidationError( | |
_("This field must be matched by password field"), | |
code='password_mismatch', | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment