Skip to content

Commit

Permalink
Merge pull request #26 from Ayshyama/auth-back-phil
Browse files Browse the repository at this point in the history
Sign in and sign up backend
  • Loading branch information
Serg-f committed Nov 24, 2023
2 parents f26fecd + 872ec90 commit 7e885b9
Show file tree
Hide file tree
Showing 72 changed files with 1,715 additions and 241 deletions.
18 changes: 17 additions & 1 deletion app_accounts/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
from django.contrib import admin

# Register your models here.
from app_accounts.models import CustomUser


@admin.register(CustomUser)
class CustomUserAdmin(admin.ModelAdmin):
list_display = ('email', 'username', 'first_name', 'last_name', 'is_staff', 'is_active')
list_filter = ('is_staff', 'is_active')
search_fields = ('email', 'username', 'first_name', 'last_name')
ordering = ('email',)
readonly_fields = ('date_joined', 'last_login')
fieldsets = (
(None, {'fields': ('email', 'username', 'password')}),
('Personal info', {'fields': ('first_name', 'last_name')}),
('Permissions',
{'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
('Important dates', {'fields': ('date_joined', 'last_login')}),
)
29 changes: 29 additions & 0 deletions app_accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,32 @@ def create_user_progress(sender, instance, action, reverse, model, pk_set, **kwa

# Connect the signal
m2m_changed.connect(create_user_progress, sender=CustomUser.exercises_done.through)

'''
Signal Receiver:
- The @receiver decorator is used to register the function create_user_progress as
a signal receiver for the m2m_changed signal, specifically for changes to the
exercises_done many-to-many field in the CustomUser model.
- The sender argument specifies the model class that sends the signal, which is the
intermediary model that Django automatically creates to manage the many-to-many
relationship between CustomUser and Exercise.
- The signal is triggered whenever the exercises_done field is changed, which can
happen in a variety of ways, including when a new exercise is added to the set or
when an existing exercise is removed from the set.
- The reverse argument is set to True when the change is made on the reverse side of
the relation, which is the Exercise model in this case.
- The pk_set argument is a set of primary keys for the related objects that were added
to the many-to-many field.
- The **kwargs argument allows the function to accept arbitrary keyword arguments.
- When a CustomUser instance has exercises added to its exercises_done set, the create_user_progress function is triggered.
- If the action is post_add (which occurs after new items have been added to the many-to-many field) and the change is not
happening on the reverse side of the relation, the function iterates over the primary key set of added exercises and
creates a new UserProgress instance for each one, associating the exercise with the user.
Signal Connection:
- Although the @receiver decorator already connects the create_user_progress function to the m2m_changed signal,
the last line explicitly connects the signal again, which is redundant and not necessary given the use of the decorator.
This setup allows the application to automatically track user progress on exercises without requiring additional code to
manage the creation of UserProgress instances whenever exercises are added to a user's exercises_done set. It leverages
Django's signals framework to hook into the lifecycle of the database operations.
'''
7 changes: 7 additions & 0 deletions app_accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from . import views

urlpatterns = [
path('loginn/', views.UserLoginView.as_view(), name='account_login'),
path('signupp/', views.UserSignupView.as_view(), name='account_signup'),
]
25 changes: 24 additions & 1 deletion app_accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
from django.shortcuts import render
from allauth.account.views import LoginView, SignupView

# Create your views here.

class UserLoginView(LoginView):
template_name = 'account/login_signup.html'
success_url = '/'

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['is_login'] = True
context['form_login'] = context.pop('form')
context['form_signup'] = SignupView.form_class()
return context


class UserSignupView(SignupView):
template_name = 'account/login_signup.html'
success_url = '/'

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['is_login'] = False
context['form_login'] = LoginView.form_class()
context['form_signup'] = context.pop('form')
return context
16 changes: 16 additions & 0 deletions eduproject_config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# Third party apps
"allauth",
"allauth.account",
"allauth.socialaccount",
# Local apps
"app_base",
"app_accounts",
"app_exercises",
Expand All @@ -47,6 +52,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"allauth.account.middleware.AccountMiddleware",
]

ROOT_URLCONF = "eduproject_config.urls"
Expand All @@ -67,6 +73,14 @@
},
]

AUTHENTICATION_BACKENDS = [
# Needed to log in by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',

# `allauth` specific authentication methods, such as login by email
'allauth.account.auth_backends.AuthenticationBackend',
]

WSGI_APPLICATION = "eduproject_config.wsgi.application"

# Database
Expand Down Expand Up @@ -125,3 +139,5 @@
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

AUTH_USER_MODEL = "app_accounts.CustomUser"

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
5 changes: 4 additions & 1 deletion eduproject_config/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from django.contrib import admin
from django.urls import path, include


urlpatterns = [
path("admin/", admin.site.urls),
path("", include("app_base.urls")),

path('user/', include('allauth.urls')),
path("user/", include("app_accounts.urls")),
]

14 changes: 14 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
asgiref==3.7.2
certifi==2023.11.17
cffi==1.16.0
charset-normalizer==3.3.2
cryptography==41.0.5
defusedxml==0.7.1
Django==4.2.5
django-allauth==0.58.2
idna==3.4
oauthlib==3.2.2
Pillow==10.1.0
psycopg2-binary==2.9.9
pycparser==2.21
PyJWT==2.8.0
python3-openid==3.2.0
requests==2.31.0
requests-oauthlib==1.3.1
sqlparse==0.4.4
typing_extensions==4.8.0
tzdata==2023.3
urllib3==2.1.0
Loading

0 comments on commit 7e885b9

Please sign in to comment.