From 8cb73b9890d60356156f71cdf8ac763e94a89053 Mon Sep 17 00:00:00 2001 From: Mohamed Nouffer Date: Mon, 29 Aug 2022 19:55:48 +0530 Subject: [PATCH] added group to custom user --- rog/admin.py | 10 +++++----- rog/migrations/0024_customuser_group.py | 18 ++++++++++++++++++ rog/models.py | 17 +++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 rog/migrations/0024_customuser_group.py diff --git a/rog/admin.py b/rog/admin.py index 62d96b2..25bcf20 100644 --- a/rog/admin.py +++ b/rog/admin.py @@ -18,18 +18,18 @@ class ShopRouteAdmin(LeafletAdminListMixin, LeafletGeoAdminMixin, admin.ModelAdm list_display=['name',] class UserAdminConfig(UserAdmin): - search_fields = ('email',) - list_filter = ('email',) + search_fields = ('email', 'group',) + list_filter = ('email', 'group') ordering = ('email',) - list_display = ('email', 'is_active', 'is_staff',) + list_display = ('email', 'group', 'is_active', 'is_staff',) fieldsets = ( - (None, {'fields':('email',)}), + (None, {'fields':('email', 'group')}), ('Permissions', {'fields':('is_staff', 'is_active',)}), ) add_fieldsets = ( - (None, {'classes':('wide',), 'fields':('email','password1', 'password2')}), + (None, {'classes':('wide',), 'fields':('email', 'group', 'password1', 'password2')}), ) class LocationAdmin(LeafletGeoAdmin): diff --git a/rog/migrations/0024_customuser_group.py b/rog/migrations/0024_customuser_group.py new file mode 100644 index 0000000..7a4c981 --- /dev/null +++ b/rog/migrations/0024_customuser_group.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.9 on 2022-08-29 14:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('rog', '0023_alter_location_cp'), + ] + + operations = [ + migrations.AddField( + model_name='customuser', + name='group', + field=models.CharField(choices=[('G1', '大垣-初心者'), ('G2', '大垣-3時間'), ('G3', '大垣-5時間')], default='G1', max_length=2), + ), + ] diff --git a/rog/models.py b/rog/models.py index c3141ac..29e9014 100644 --- a/rog/models.py +++ b/rog/models.py @@ -35,20 +35,20 @@ db = Db(dbname=env("POSTGRES_DBNAME"), user=env("POSTGRES_USER"), password=env(" class CustomUserManager(BaseUserManager): - def create_user(self, email, password, **other_fields): + def create_user(self, email, password, group, **other_fields): if not email: raise ValueError(_("You must provide an email address")) email = self.normalize_email(email) - user=self.model(email=email, **other_fields) + user=self.model(email=email, group=group, **other_fields) user.set_password(password) user.save() return user - def create_superuser(self, email, password, **other_fields): + def create_superuser(self, email, password, group, **other_fields): other_fields.setdefault('is_staff', True) other_fields.setdefault('is_superuser', True) other_fields.setdefault('is_active', True) @@ -58,7 +58,7 @@ class CustomUserManager(BaseUserManager): if other_fields.get('is_superuser') is not True: raise ValueError(_('Supperuser must assigned to superuser=True')) - return self.create_user(email, password, **other_fields) + return self.create_user(email, password, group, **other_fields) @@ -138,10 +138,19 @@ class GifuAreas(models.Model): class CustomUser(AbstractBaseUser, PermissionsMixin): + class Groups(models.TextChoices): + GB1 = 'G1', '大垣-初心者' + GB2 = 'G2', '大垣-3時間' + GB3 = 'G3', '大垣-5時間' + email = models.EmailField(_("email address"), unique=True) is_staff = models.BooleanField(default=False) is_active = models.BooleanField(default=True) + group = models.CharField(max_length=2, + choices=Groups.choices, + default=Groups.GB1) USERNAME_FIELD = 'email' + REQUIRED_FIELDS = ['group',] objects = CustomUserManager()