-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
- Loading branch information
1 parent
b3b24b3
commit 7d57c90
Showing
9 changed files
with
51 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from django import forms | ||
from django.contrib.auth.forms import UserCreationForm | ||
|
||
from .models import CustomUser | ||
|
||
|
||
# Form for user registration | ||
class CustomUserCreationForm(UserCreationForm): | ||
class Meta(UserCreationForm.Meta): | ||
model = CustomUser | ||
fields = UserCreationForm.Meta.fields + ('role',) # Add role field | ||
fields = UserCreationForm.Meta.fields + ("role",) # Add role field |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from django.urls import path | ||
from .views import register, user_login, profile | ||
|
||
from .views import profile, register, user_login | ||
|
||
urlpatterns = [ | ||
path('register/', register, name='register'), | ||
path('login/', user_login, name='login'), | ||
path('profile/', profile, name='profile'), | ||
path("register/", register, name="register"), | ||
path("login/", user_login, name="login"), | ||
path("profile/", profile, name="profile"), | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,52 @@ | ||
from .forms import CustomUserCreationForm, LoginForm | ||
from django.shortcuts import redirect, render | ||
from django.contrib.auth.decorators import login_required | ||
from django.contrib.auth import authenticate, login, logout | ||
from django.contrib.auth.decorators import user_passes_test | ||
def is_admin(user): | ||
return user.role == 'admin' | ||
@user_passes_test(is_admin) | ||
def admin_dashboard(request): | ||
return render(request, 'authentication/admin_dashboard.html') | ||
|
||
|
||
def is_admin(user): | ||
return user.role == "admin" | ||
|
||
|
||
from django.shortcuts import render, redirect | ||
from django.contrib.auth import login, authenticate, logout | ||
from django.contrib.auth.decorators import login_required | ||
from .forms import CustomUserCreationForm, LoginForm | ||
@user_passes_test(is_admin) | ||
def admin_dashboard(request): | ||
return render(request, "authentication/admin_dashboard.html") | ||
|
||
|
||
def register(request): | ||
if request.method == 'POST': | ||
if request.method == "POST": | ||
form = CustomUserCreationForm(request.POST) | ||
if form.is_valid(): | ||
user = form.save() | ||
login(request, user) | ||
return redirect('home') | ||
return redirect("home") | ||
else: | ||
form = CustomUserCreationForm() | ||
return render(request, 'authentication/register.html', {'form': form}) | ||
return render(request, "authentication/register.html", {"form": form}) | ||
|
||
|
||
def user_login(request): | ||
if request.method == 'POST': | ||
if request.method == "POST": | ||
form = LoginForm(request.POST) | ||
if form.is_valid(): | ||
username = form.cleaned_data.get('username') | ||
password = form.cleaned_data.get('password') | ||
username = form.cleaned_data.get("username") | ||
password = form.cleaned_data.get("password") | ||
user = authenticate(request, username=username, password=password) | ||
if user is not None: | ||
login(request, user) | ||
return redirect('home') | ||
return redirect("home") | ||
else: | ||
return render(request, 'authentication/login.html', {'error': 'Invalid credentials'}) | ||
return render( | ||
request, | ||
"authentication/login.html", | ||
{"error": "Invalid credentials"}, | ||
) | ||
else: | ||
form = LoginForm() | ||
return render(request, 'authentication/login.html', {'form': form}) | ||
return render(request, "authentication/login.html", {"form": form}) | ||
|
||
|
||
@login_required | ||
def profile(request): | ||
return render(request, 'authentication/profile.html', {'user': request.user}) | ||
return render(request, "authentication/profile.html", {"user": request.user}) |