Skip to content

Commit

Permalink
Run pre-commit hooks on all files
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Nayak <[email protected]>
  • Loading branch information
nishant-nayak committed Nov 9, 2023
1 parent 372f52d commit 4ea2cb2
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 73 deletions.
52 changes: 39 additions & 13 deletions corpus/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,51 @@
from django.contrib.auth.admin import UserAdmin

from .models import User
from .forms import CorpusChangeForm

# Register your models here.


class CorpusUserAdmin(UserAdmin):
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal Info', {'fields': ('first_name', 'last_name', 'phone_no', 'gender')}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
('Important dates', {'fields': ('last_login', 'date_joined')}),
(None, {"fields": ("email", "password")}),
(
"Personal Info",
{"fields": ("first_name", "last_name", "phone_no", "gender")},
),
(
"Permissions",
{
"fields": (
"is_active",
"is_staff",
"is_superuser",
"groups",
"user_permissions",
)
},
),
("Important dates", {"fields": ("last_login", "date_joined")}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'first_name', 'last_name', 'phone_no', 'gender', 'password1', 'password2'),
}),
(
None,
{
"classes": ("wide",),
"fields": (
"email",
"first_name",
"last_name",
"phone_no",
"gender",
"password1",
"password2",
),
},
),
)
list_display = ('email', 'first_name', 'last_name', 'phone_no', 'gender')
search_fields = ('email', 'first_name', 'last_name', 'phone_no')
ordering = ('email',)
list_display = ("email", "first_name", "last_name", "phone_no", "gender")
search_fields = ("email", "first_name", "last_name", "phone_no")
ordering = ("email",)


admin.site.register(User, CorpusUserAdmin)
admin.site.register(User, CorpusUserAdmin)
4 changes: 2 additions & 2 deletions corpus/accounts/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
default_auto_field = "django.db.models.BigAutoField"
name = "accounts"
5 changes: 3 additions & 2 deletions corpus/accounts/backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend


class CorpusAuthBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
UserModel = get_user_model()
Expand All @@ -12,10 +13,10 @@ def authenticate(self, request, username=None, password=None, **kwargs):
if user.check_password(password):
return user
return None

def get_user(self, user_id):
UserModel = get_user_model()
try:
return UserModel.objects.get(pk=user_id)
except UserModel.DoesNotExist:
return None
return None
29 changes: 20 additions & 9 deletions corpus/accounts/forms.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, AuthenticationForm
from .models import User, GENDERS
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.forms import UserChangeForm
from django.contrib.auth.forms import UserCreationForm

from .models import GENDERS
from .models import User


class CorpusCreationForm(UserCreationForm):
phone_no = forms.CharField(required=True)
gender = forms.ChoiceField(choices=GENDERS, required=True)

error_css_class = "text-sm text-error"

class Meta:
model = User
fields = [
Expand All @@ -17,18 +22,21 @@ class Meta:
"gender",
"email",
"password1",
"password2"
"password2",
]

def __init__(self, *args, **kwargs):
super(CorpusCreationForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs["class"] = "mt-1 block w-full rounded-md border-base-800 text-black shadow-sm focus:border-primary focus:ring focus:ring-primary-200 focus:ring-opacity-50"
visible.field.widget.attrs[
"class"
] = "mt-1 block w-full rounded-md border-base-800 text-black shadow-sm focus:border-primary focus:ring focus:ring-primary-200 focus:ring-opacity-50" # noqa: E501


class CorpusChangeForm(UserChangeForm):
phone_no = forms.CharField(required=True)
gender = forms.ChoiceField(choices=GENDERS, required=True)

class Meta:
model = User
fields = [
Expand All @@ -39,14 +47,17 @@ class Meta:
"email",
]


class CorpusLoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super(CorpusLoginForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs["class"] = "mt-1 block w-full rounded-md border-base-800 text-black shadow-sm focus:border-primary focus:ring focus:ring-primary-200 focus:ring-opacity-50"
visible.field.widget.attrs[
"class"
] = "mt-1 block w-full rounded-md border-base-800 text-black shadow-sm focus:border-primary focus:ring focus:ring-primary-200 focus:ring-opacity-50" # noqa: E501

def clean_username(self):
username = self.cleaned_data.get('username')
username = self.cleaned_data.get("username")
if username:
username = username.lower()
return username
return username
122 changes: 101 additions & 21 deletions corpus/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,120 @@
# Generated by Django 4.2.4 on 2023-09-07 15:06

from django.db import migrations, models
import django.utils.timezone
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
("auth", "0012_alter_user_first_name_max_length"),
]

operations = [
migrations.CreateModel(
name='User',
name="User",
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('phone_no', models.CharField(max_length=13, unique=True)),
('gender', models.CharField(choices=[('M', 'Male'), ('F', 'Female'), ('O', 'Other'), ('N', 'Prefer not to disclose')], max_length=1)),
('email', models.EmailField(max_length=254, unique=True)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("password", models.CharField(max_length=128, verbose_name="password")),
(
"last_login",
models.DateTimeField(
blank=True, null=True, verbose_name="last login"
),
),
(
"is_superuser",
models.BooleanField(
default=False,
help_text="Designates that this user has all permissions without explicitly assigning them.",
verbose_name="superuser status",
),
),
(
"first_name",
models.CharField(
blank=True, max_length=150, verbose_name="first name"
),
),
(
"last_name",
models.CharField(
blank=True, max_length=150, verbose_name="last name"
),
),
(
"is_staff",
models.BooleanField(
default=False,
help_text="Designates whether the user can log into this admin site.",
verbose_name="staff status",
),
),
(
"is_active",
models.BooleanField(
default=True,
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
verbose_name="active",
),
),
(
"date_joined",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="date joined"
),
),
("phone_no", models.CharField(max_length=13, unique=True)),
(
"gender",
models.CharField(
choices=[
("M", "Male"),
("F", "Female"),
("O", "Other"),
("N", "Prefer not to disclose"),
],
max_length=1,
),
),
("email", models.EmailField(max_length=254, unique=True)),
(
"groups",
models.ManyToManyField(
blank=True,
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
related_name="user_set",
related_query_name="user",
to="auth.group",
verbose_name="groups",
),
),
(
"user_permissions",
models.ManyToManyField(
blank=True,
help_text="Specific permissions for this user.",
related_name="user_set",
related_query_name="user",
to="auth.permission",
verbose_name="user permissions",
),
),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
"verbose_name": "user",
"verbose_name_plural": "users",
"abstract": False,
},
),
]
14 changes: 8 additions & 6 deletions corpus/accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _

# Create your models here.
Expand All @@ -12,17 +12,18 @@
("N", "Prefer not to disclose"),
]


class UserManager(BaseUserManager):
def create_user(self, email, password, **kwargs):
if not email:
raise ValueError(_("Email must be set."))

email = self.normalize_email(email)
user = self.model(email=email, **kwargs)
user.set_password(password)
user.save()
return user

def create_superuser(self, email, password, **kwargs):
kwargs.setdefault("is_staff", True)
kwargs.setdefault("is_superuser", True)
Expand All @@ -33,10 +34,11 @@ def create_superuser(self, email, password, **kwargs):
if kwargs.get("is_superuser") is not True:
raise ValueError(_("Superuser must have is_superuser=True."))
return self.create_user(email, password, **kwargs)

def users(self):
return self.filter(is_active=True)


class User(AbstractUser):
username = None
phone_no = models.CharField(max_length=13, unique=True, blank=False, null=False)
Expand All @@ -49,4 +51,4 @@ class User(AbstractUser):
objects = UserManager()

def __str__(self):
return f"{self.first_name} {self.last_name}"
return f"{self.first_name} {self.last_name}"
9 changes: 6 additions & 3 deletions corpus/accounts/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from django.urls import path
from .views import signup, signin, signout

from .views import signin
from .views import signout
from .views import signup

urlpatterns = [
path("signup/", signup, name="accounts_signup"),
path("login/", signin, name="accounts_signin"),
path("logout/", signout, name="accounts_signout")
]
path("logout/", signout, name="accounts_signout"),
]
Loading

0 comments on commit 4ea2cb2

Please sign in to comment.