From c60a8ab49f2cfd7f8cb838af5dbf498523775fba Mon Sep 17 00:00:00 2001 From: James <james39.smith@live.uwe.ac.uk> Date: Thu, 7 Mar 2024 17:10:44 +0000 Subject: [PATCH] changed entrypoint port to new port. Added forms.py for custom forms(login/registration), briefly fiddled with models.py to implement different user levels - non-functional currently so commented all code out. Updated urls in _base.html. Implemented forms in login and registration. updated urls to work correctly with new url names. added user creation/login code to views.py, added redirect url to settings to utilise redirects upon account creation/login. Removed all references to 'login' to prevent clashing with built in django login() function throughout the entire project --- .gitignore | 4 +++ myproject/entrypoint.sh | 2 +- myproject/myapp/forms.py | 16 +++++++++ myproject/myapp/models.py | 45 +++++++++++++++++++++++ myproject/myapp/templates/_base.html | 4 +-- myproject/myapp/templates/login.html | 29 +++++++++++---- myproject/myapp/templates/register.html | 30 ++++++++++++---- myproject/myapp/urls.py | 13 +++---- myproject/myapp/views.py | 48 ++++++++++++++++++++++--- myproject/myproject/settings.py | 2 ++ 10 files changed, 165 insertions(+), 28 deletions(-) create mode 100644 myproject/myapp/forms.py diff --git a/.gitignore b/.gitignore index 9968480..3a4dc13 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ myproject/node_modules myproject/env myproject/myapp/static/CACHE myproject/myapp/__pycache__ +.DS_Store +myproject/myproject/__pycache__/__init__.cpython-312.pyc +myproject/myproject/__pycache__/settings.cpython-312.pyc +myproject/myproject/__pycache__/urls.cpython-312.pyc diff --git a/myproject/entrypoint.sh b/myproject/entrypoint.sh index 2087de4..e882418 100644 --- a/myproject/entrypoint.sh +++ b/myproject/entrypoint.sh @@ -7,4 +7,4 @@ python manage.py makemigrations echo "Applying migrations" python manage.py migrate -python manage.py runserver 0.0.0.0:5432 \ No newline at end of file +python manage.py runserver 0.0.0.0:8000 \ No newline at end of file diff --git a/myproject/myapp/forms.py b/myproject/myapp/forms.py new file mode 100644 index 0000000..8391495 --- /dev/null +++ b/myproject/myapp/forms.py @@ -0,0 +1,16 @@ +from django import forms +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User + +class CustomRegistrationForm(UserCreationForm): + #UserCreationForm comes with username, password1, password2 by default + #only email needs to be added for our custom users + email = forms.EmailField() + + class Meta(UserCreationForm.Meta): + model = User + fields = ["username", "email", "password1", "password2"] + +class LoginForm(forms.Form): + username = forms.CharField() + password = forms.CharField(widget=forms.PasswordInput) \ No newline at end of file diff --git a/myproject/myapp/models.py b/myproject/myapp/models.py index 3921a6b..5fdd11c 100644 --- a/myproject/myapp/models.py +++ b/myproject/myapp/models.py @@ -1,4 +1,49 @@ from django.db import models +from django.contrib.auth import get_user_model +from django.contrib.auth.models import User, Group, Permission +from django.contrib.contenttypes.models import ContentType + +# class UserTypes(User): +# USER_TYPE_CHOICES = ( +# 0, 'Basic User', +# 1, 'Admin', +# 2, 'ML Engineer', +# 3, 'Accountant' +# ) + +# usertype = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES) # should we declare default=0 here? + +# group_names = ['Basic User', 'Admin', 'ML Engineer', 'Accountant'] +# for group_name in group_names: +# Group.objects.get_or_create(name=group_name) + +# assign group permissions +# content_type = ContentType.objects.get_for_model(UserTypes) +# permission = Permission.objects.create(codename='can_view_user', +# name='Can View User', +# content_type=content_type) +# group = Group.objects.get(name='Admin') +# group.permissions.add(permission) + + +# User = get_user_model() + +# user = User.objects.create_user('username', 'email', 'password') +#names are not necessary - reduces gdpr concerns aswell + + + + + + + + + + + + + + # Usertypes # --------- diff --git a/myproject/myapp/templates/_base.html b/myproject/myapp/templates/_base.html index 91303bc..ed5dab0 100644 --- a/myproject/myapp/templates/_base.html +++ b/myproject/myapp/templates/_base.html @@ -58,7 +58,7 @@ </li> <li> <a - href="{% url 'login' %}" + href="{% url 'user_login' %}" class="block py-2 px-3 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" >Login</a > @@ -79,7 +79,7 @@ </li> <li> <a - href="user" + href="{% url 'users' %}" class="block py-2 px-3 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" >Dashboard</a > diff --git a/myproject/myapp/templates/login.html b/myproject/myapp/templates/login.html index c1836bb..639ef31 100644 --- a/myproject/myapp/templates/login.html +++ b/myproject/myapp/templates/login.html @@ -6,7 +6,26 @@ <h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white"> Sign in to your account </h1> - <form class="space-y-4 md:space-y-6" action="#"> + + + <form method="POST"> + {% csrf_token %} + {{ form.as_p }} + <button type="submit">Login</button> + <a href="{% url 'register' %}">Dont have Account Create</a> + </form> + + </div> + </div> + </div> + </section> +{% endblock content%} + +{% comment %} + + +<form class="space-y-4 md:space-y-6" action="#" method="POST"> + {% csrf_token %} <div> <label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your email</label> <input type="email" name="email" id="email" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="name@company.com" required=""> @@ -31,8 +50,6 @@ Don’t have an account yet? <a href="#" class="font-medium text-primary-600 hover:underline dark:text-primary-500">Sign up</a> </p> </form> - </div> - </div> - </div> - </section> -{% endblock content%} \ No newline at end of file + + +{% endcomment %} \ No newline at end of file diff --git a/myproject/myapp/templates/register.html b/myproject/myapp/templates/register.html index baed31f..5fc8c87 100644 --- a/myproject/myapp/templates/register.html +++ b/myproject/myapp/templates/register.html @@ -6,7 +6,28 @@ <h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white"> Create an account </h1> - <form class="space-y-4 md:space-y-6" action="#"> + <form method="POST"> + {% csrf_token %} + {{ form.as_p }} + + <button type="submit">Register</button> + <a href="{% url 'user_login' %}">Already created an account? Login</a> + </form> + </div> + </div> + </div> + </section> +{% endblock content%} + +{% comment %} +<h1> Register </h1> + + + + + <form class="space-y-4 md:space-y-6" action="#" method="POST"> + {% csrf_token %} + <div> <label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your email</label> <input type="email" name="email" id="email" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="name@company.com" required=""> @@ -32,8 +53,5 @@ Already have an account? <a href="#" class="font-medium text-primary-600 hover:underline dark:text-primary-500">Login here</a> </p> </form> - </div> - </div> - </div> - </section> -{% endblock content%} \ No newline at end of file + +{% endcomment %} diff --git a/myproject/myapp/urls.py b/myproject/myapp/urls.py index 0bfffe4..c2d522b 100644 --- a/myproject/myapp/urls.py +++ b/myproject/myapp/urls.py @@ -1,21 +1,16 @@ from django.urls import path -from .views import index -from .views import users -from .views import maintenance -from .views import handler404 -from .views import handler500 -from .views import register -from .views import login +from .views import index, users, maintenance, handler404, handler500, register, user_login + urlpatterns = [ # path('', index, name='index'), <- uncomment when index/main page will be ready path('', index), - path('user/',users), + path('user/',users, name='users'), path('404/', handler404), path('500/', handler500), path('maintenance/', maintenance), path('register/', register, name='register'), - path('login/', login, name='login'), + path('login/', user_login, name='user_login'), ] diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index 3c8d3d9..9488d61 100644 --- a/myproject/myapp/views.py +++ b/myproject/myapp/views.py @@ -1,6 +1,14 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect from django.template import RequestContext +from django.contrib.auth import authenticate, login, logout +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User +from django.contrib import messages + +from .forms import CustomRegistrationForm, LoginForm + + def index(request): return render(request, 'index.html') @@ -20,8 +28,40 @@ def handler500(request, *args, **kwargs): def maintenance(request): return render(request, 'maintenance.html') -def login(request): - return render(request, 'login.html') +def user_login(request): + if request.method == 'POST': + form = LoginForm(request.POST) + + if form.is_valid(): + username = form.cleaned_data.get('username') + password = form.cleaned_data.get('password') + + user = authenticate(request, username=username, password=password) # Passing request along with username and password + + if user: + login(request, user=user) # Passing request along with user + return redirect('users') + else: + messages.error(request, 'Invalid username or password.') + else: + pass + + else: + form = LoginForm() + return render(request, 'login.html', {'form': form}) + def register(request): - return render(request, 'register.html') \ No newline at end of file + if request.method == 'POST': + form = CustomRegistrationForm(request.POST) + if form.is_valid(): + form.save() + return redirect('user_login') + else: + form = CustomRegistrationForm() + + return render(request, 'register.html', {'form': form}) + +def user_logout(request): + logout(request) + return redirect('user_login') diff --git a/myproject/myproject/settings.py b/myproject/myproject/settings.py index 41a1691..a32ffa3 100644 --- a/myproject/myproject/settings.py +++ b/myproject/myproject/settings.py @@ -154,3 +154,5 @@ STATIC_URL = 'static/' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' IMAGE_URL = 'static/src/images/' + +LOGIN_REDIRECT_URL = '/' -- GitLab