diff --git a/myproject/myapp/__init__.py b/myproject/myapp/__init__.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..2e350f47378ed544a8099ab351d2572b6ec5b622 100644 --- a/myproject/myapp/__init__.py +++ b/myproject/myapp/__init__.py @@ -0,0 +1 @@ +default_app_config = 'myapp.apps.MyappConfig' \ No newline at end of file diff --git a/myproject/myapp/admin.py b/myproject/myapp/admin.py index 8c38f3f3dad51e4585f3984282c2a4bec5349c1e..d7f569c0f677e4cd20cbc82598a44d5475e855ae 100644 --- a/myproject/myapp/admin.py +++ b/myproject/myapp/admin.py @@ -1,3 +1,28 @@ from django.contrib import admin -# Register your models here. + +from django.contrib.auth.admin import UserAdmin as BaseUserAdmin +from django.contrib.auth.models import User +from .models import Profile + +# Define an inline admin descriptor for Profile model +# which acts a bit like a singleton +class ProfileInline(admin.StackedInline): + model = Profile + can_delete = False + verbose_name_plural = 'Profiles' + fk_name = 'user' + +# Define a new User admin +class UserAdmin(BaseUserAdmin): + inlines = (ProfileInline,) + + def get_inline_instances(self, request, obj=None): + if not obj: + return list() + return super(UserAdmin, self).get_inline_instances(request, obj) + +# Re-register UserAdmin +admin.site.unregister(User) +admin.site.register(User, UserAdmin) + diff --git a/myproject/myapp/apps.py b/myproject/myapp/apps.py index c34fb20eb6a166110a0fb57a95512f57e55f38e7..8f930c53ea609de15a686739443c5461fb9d6e5c 100644 --- a/myproject/myapp/apps.py +++ b/myproject/myapp/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class MyappConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'myapp' + + def ready(self): + import myapp.signals diff --git a/myproject/myapp/forms.py b/myproject/myapp/forms.py index 4b0ccd749e58b9f1e5f52c4ba461e6ee00587daa..f1fe8d6159adfb0f8ee6683c8240fb26c0b00e1b 100644 --- a/myproject/myapp/forms.py +++ b/myproject/myapp/forms.py @@ -1,19 +1,22 @@ from django import forms from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User +from django.contrib.auth.forms import AuthenticationForm -class CustomRegistrationForm(UserCreationForm): - #UserCreationForm comes with username, password1, password2 by default - #only email needs to be added for our custom users - email = forms.EmailField() +from .models import Profile - class Meta(UserCreationForm.Meta): - model = User - fields = ["username", "email", "password1", "password2"] +# 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) +# class LoginForm(forms.Form): +# username = forms.CharField() +# password = forms.CharField(widget=forms.PasswordInput) class InstrumentDetectionForm(forms.Form): audio_file = forms.FileField( @@ -27,4 +30,41 @@ class InstrumentDetectionForm(forms.Form): }) ) +class LoginAuthenticationForm(AuthenticationForm): + def __init__(self, *args, **kwargs): + super(LoginAuthenticationForm, self).__init__(*args, **kwargs) + for visible in self.visible_fields(): + visible.field.widget.attrs['class'] = 'block w-full px-3 py-2 mt-1 text-gray-700 bg-white border border-gray-300 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500' + + +class UserRegisterForm(UserCreationForm): + email = forms.EmailField(widget=forms.EmailInput(attrs={ + 'class': 'block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-red-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-red-600 focus:border-red-500 dark:focus:border-red-500 focus:outline-none focus:ring focus:ring-red-500', + 'placeholder': 'Email' + })) + + class Meta: + model = User + fields = ['username', 'email', 'password1', 'password2'] + + def __init__(self, *args, **kwargs): + super(UserRegisterForm, self).__init__(*args, **kwargs) + common_attrs = { + 'class': 'block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-red-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-red-600 focus:border-red-500 dark:focus:border-red-500 focus:outline-none focus:ring focus:ring-red-500', + 'placeholder': '' + } + self.fields['username'].widget.attrs.update({**common_attrs, 'placeholder': 'Username'}) + self.fields['password1'].widget.attrs.update({**common_attrs, 'placeholder': 'Password'}) + self.fields['password2'].widget.attrs.update({**common_attrs, 'placeholder': 'Repeat Password'}) + + +class ProfileForm(forms.ModelForm): + class Meta: + model = Profile + fields = ['user_type'] + widgets = { + 'user_type': forms.Select(attrs={ + 'class': 'block w-full px-3 py-1.5 text-base font-normal text-gray-700 bg-white bg-clip-padding bg-no-repeat border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none', + }), + } diff --git a/myproject/myapp/migrations/0003_profile.py b/myproject/myapp/migrations/0003_profile.py new file mode 100644 index 0000000000000000000000000000000000000000..23249ec7bbad9a2dadbf23e46dff2315a9e2ca24 --- /dev/null +++ b/myproject/myapp/migrations/0003_profile.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.1 on 2024-03-25 00:43 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('myapp', '0002_log'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Profile', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('user_type', models.IntegerField(choices=[(0, 'Basic User'), (1, 'Admin'), (2, 'ML Engineer'), (3, 'Accountant')], default=0)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/myproject/myapp/models.py b/myproject/myapp/models.py index 87e3ccbd78453e60a54fc7ac2318b7d6aa34c06e..7dc56b8baaceda6431a612b5bf489da923978f3a 100644 --- a/myproject/myapp/models.py +++ b/myproject/myapp/models.py @@ -18,7 +18,7 @@ from enum import Enum # for group_name in group_names: # Group.objects.get_or_create(name=group_name) -# assign group permissions +# # assign group permissions # content_type = ContentType.objects.get_for_model(UserTypes) # permission = Permission.objects.create(codename='can_view_user', # name='Can View User', @@ -30,7 +30,21 @@ from enum import Enum # User = get_user_model() # user = User.objects.create_user('username', 'email', 'password') -#names are not necessary - reduces gdpr concerns aswell +# # names are not necessary - reduces gdpr concerns aswell + +class Profile(models.Model): + USER_TYPES = ( + (0, 'Basic User'), + (1, 'Admin'), + (2, 'ML Engineer'), + (3, 'Accountant'), + ) + + user = models.OneToOneField(User, on_delete=models.CASCADE) + user_type = models.IntegerField(choices=USER_TYPES, default=0) + + def __str__(self): + return f'{self.user.username} Profile' class Action(Enum): diff --git a/myproject/myapp/signals.py b/myproject/myapp/signals.py new file mode 100644 index 0000000000000000000000000000000000000000..e1c9ac65ee31df37419bb984cd2e4594c3ba8e85 --- /dev/null +++ b/myproject/myapp/signals.py @@ -0,0 +1,10 @@ +from django.db.models.signals import post_save +from django.contrib.auth.models import User +from django.dispatch import receiver +from .models import Profile + +@receiver(post_save, sender=User) +def create_or_update_user_profile(sender, instance, created, **kwargs): + if created: + Profile.objects.create(user=instance) + instance.profile.save() diff --git a/myproject/myapp/templates/_base.html b/myproject/myapp/templates/_base.html index a2cd93cc5354d8f8490dfea1c98a2756e6c77f73..de172c29c435a0d1e8623b2330c7369567155cc8 100644 --- a/myproject/myapp/templates/_base.html +++ b/myproject/myapp/templates/_base.html @@ -12,6 +12,7 @@ </head> <body> + {% block navbar %} <nav class=" border-gray-200 dark:bg-gray-900"> <div class="max-w-screen-xl flex flex-wrap items-center justify-between mx-auto p-4" @@ -49,53 +50,41 @@ class="font-medium flex flex-col p-4 md:p-0 mt-4 border border-gray-100 rounded-lg bg-gray-50 md:flex-row md:space-x-8 rtl:space-x-reverse md:mt-0 md:border-0 md:bg-white dark:bg-gray-800 md:dark:bg-gray-900 dark:border-gray-700" > <li> - <a - href="/" - class="block py-2 px-3 text-white bg-blue-700 rounded md:bg-transparent md:text-blue-700 md:p-0 dark:text-white md:dark:text-blue-500" - aria-current="page" - >Home</a - > + <a href="{% url 'index' %}" class="block py-2 px-3 {% if request.path == '/' %}text-blue-700{% else %}text-gray-900 dark:text-white{% endif %} rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:p-0 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent">Home</a> </li> {% if user.is_authenticated %} <li> <a href="{% url 'pricing' %}" - 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" + class="block py-2 px-3 {% if request.path == '/pricing/' %}text-blue-700{% else %}text-gray-900 dark:text-white{% endif %} rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:p-0 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" >Tokens</a > </li> <li> <a 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" + class="block py-2 px-3 {% if request.path == '/user/' %}text-blue-700{% else %}text-gray-900 dark:text-white{% endif %} rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:p-0 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" >Dashboard</a > </li> - <form action="{% url 'user_logout' %}" method="post"> + <form action="{% url 'logout' %}" method="post"> {% csrf_token %} <button type="submit">Logout</button> </form> {% else %} <li> - <a - 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 - > + <a href="{% url 'login' %}" class="block py-2 px-3 {% if request.path == '/login/' %}text-blue-700{% else %}text-gray-900 dark:text-white{% endif %} rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:p-0 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent">Login</a> </li> <li> - <a - href="{% url 'register' %}" - 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" - >Register</a - > + <a href="{% url 'register' %}" class="block py-2 px-3 {% if request.path == '/register/' %}text-blue-700{% else %}text-gray-900 dark:text-white{% endif %} rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:p-0 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent">Register</a> </li> {% endif %} </ul> </div> </div> </nav> + {% endblock navbar %} <div class="container mx-auto mt-6"> {% block content %} {% endblock content %} diff --git a/myproject/myapp/templates/index2.html b/myproject/myapp/templates/index2.html index 443f7f2facfa2d50b4caa32a95239ded042cfbad..7cf84cd548c3d740ac54daec2b96a4fb952fd440 100644 --- a/myproject/myapp/templates/index2.html +++ b/myproject/myapp/templates/index2.html @@ -24,7 +24,7 @@ </h2> <p class="mb-8 font-light text-gray-500 lg:mb-16 sm:text-xl dark:text-gray-400"> <a href="{% url 'register' %}" class="inline-flex items-center font-medium text-primary-600 hover:text-primary-800 dark:text-primary-500 dark:hover:text-primary-700">Sign up</a> - or <a href="{% url 'user_login' %}" class="inline-flex items-center font-medium text-primary-600 hover:text-primary-800 dark:text-primary-500 dark:hover:text-primary-700">Login!</a> + or <a href="{% url 'login' %}" class="inline-flex items-center font-medium text-primary-600 hover:text-primary-800 dark:text-primary-500 dark:hover:text-primary-700">Login!</a> Choose one of our exceptionally well priced payment offers and enjoy our service to it's fullest! </p> </div> diff --git a/myproject/myapp/templates/register.html b/myproject/myapp/templates/register.html deleted file mode 100644 index d6cb1d0e32d7131901279ee374a0afb5ee1dfd6f..0000000000000000000000000000000000000000 --- a/myproject/myapp/templates/register.html +++ /dev/null @@ -1,58 +0,0 @@ -{% extends "_base.html"%}{% block content %} -<section class="my-10"> - <div class="flex flex-col items-center justify-center px-6 py-8 mx-auto md:h-screen lg:py-0"> - <div class="w-full bg-white rounded-lg shadow dark:border md:mt-0 sm:max-w-md xl:p-0 dark:bg-gray-800 dark:border-gray-700"> - <div class="p-6 space-y-4 md:space-y-6 sm:p-8"> - <h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white"> - Create an account - </h1> - <form method="POST"> - {% csrf_token %} - {{ form.as_p }} - <br> - <button type="submit">Register</button> - <br> - <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=""> - </div> - <div> - <label for="password" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Password</label> - <input type="password" name="password" id="password" placeholder="••••••••" 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" required=""> - </div> - <div> - <label for="confirm-password" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Confirm password</label> - <input type="confirm-password" name="confirm-password" id="confirm-password" placeholder="••••••••" 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" required=""> - </div> - <div class="flex items-start"> - <div class="flex items-center h-5"> - <input id="terms" aria-describedby="terms" type="checkbox" class="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-primary-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-primary-600 dark:ring-offset-gray-800" required=""> - </div> - <div class="ml-3 text-sm"> - <label for="terms" class="font-light text-gray-500 dark:text-gray-300">I accept the <a class="font-medium text-primary-600 hover:underline dark:text-primary-500" href="#">Terms and Conditions</a></label> - </div> - </div> - <button type="submit" class="w-full text-white bg-primary-600 hover:bg-primary-700 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">Create an account</button> - <p class="text-sm font-light text-gray-500 dark:text-gray-400"> - Already have an account? <a href="#" class="font-medium text-primary-600 hover:underline dark:text-primary-500">Login here</a> - </p> - </form> - -{% endcomment %} diff --git a/myproject/myapp/templates/login.html b/myproject/myapp/templates/registration/login.html similarity index 83% rename from myproject/myapp/templates/login.html rename to myproject/myapp/templates/registration/login.html index bbd3d038acf13d9f84ce685393d7ed8a73f2653c..c609c3cd22a22c581fba83035d3f7eb7789738d2 100644 --- a/myproject/myapp/templates/login.html +++ b/myproject/myapp/templates/registration/login.html @@ -8,13 +8,17 @@ </h1> - <form method="POST"> + <form method="POST" class="space-y-6"> {% csrf_token %} - {{ form.as_p }} - <br> - <button type="submit">Login</button> - <br> - <a href="{% url 'register' %}">New User: Create Account</a> + <div class="space-y-4"> + {{ form.as_p }} + </div> + <button type="submit" class="w-full px-4 py-2 text-white bg-blue-500 rounded-md hover:bg-blue-500 focus:ring-blue-500 focus:ring-offset-blue-200 transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 "> + Login + </button> + <div class="text-center"> + <a href="{% url 'register' %}" class="text-blue-500 hover:text-blue-600">New User? Create Account</a> + </div> </form> </div> diff --git a/myproject/myapp/templates/registration/register.html b/myproject/myapp/templates/registration/register.html new file mode 100644 index 0000000000000000000000000000000000000000..65a24df8ba1e687fde5376588a57a918e7079222 --- /dev/null +++ b/myproject/myapp/templates/registration/register.html @@ -0,0 +1,26 @@ +{% extends "_base.html"%}{% block content %} +<section class="my-10"> + <div class="flex flex-col items-center justify-center px-6 py-8 mx-auto md:h-screen lg:py-0"> + <div class="w-full bg-white rounded-lg shadow dark:border md:mt-0 sm:max-w-md xl:p-0 dark:bg-gray-800 dark:border-gray-700"> + <div class="p-6 space-y-4 md:space-y-6 sm:p-8"> + <h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white"> + Create an account + </h1> + <form method="POST" class="space-y-6"> + {% csrf_token %} + <div class="space-y-4"> + {{ form.as_p }} + </div> + <button type="submit" class="w-full px-4 py-2 text-white bg-blue-500 rounded-md hover:bg-blue-500 focus:ring-blue-500 focus:ring-offset-blue-200 transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 "> + Register + </button> + <div class="text-center"> + <a href="{% url 'login' %}" class="text-blue-500 hover:text-blue-600">Already have an account? Login</a> + </div> + </form> + + </div> + </div> + </div> + </section> +{% endblock content%} diff --git a/myproject/myapp/urls.py b/myproject/myapp/urls.py index 9edf0eede992b47a794b1808eb9d9c90482c0378..8e0269800f768e72ec60ddb8174e9262a0095816 100644 --- a/myproject/myapp/urls.py +++ b/myproject/myapp/urls.py @@ -1,17 +1,20 @@ from django.urls import path -from .views import InstrumentDetectionView, index, users, maintenance, handler404, handler500, register, user_login, terms_conditions, privacy_policy, handling_music_file, pricing, generate_pdf, admin_table +from .views import InstrumentDetectionView, index, users, maintenance, handler404, handler500, terms_conditions, privacy_policy, handling_music_file, pricing, generate_pdf, admin_table from django.contrib.auth import views as auth_views +# Authentication +from .views import RegisterView, CustomLoginView +from django.contrib.auth.views import LoginView, LogoutView + urlpatterns = [ - # path('', index, name='index'), <- uncomment when index/main page will be ready path('', index, name='index'), path('user/', users, name='users'), path('404/', handler404), path('500/', handler500), path('maintenance/', maintenance), - path('register/', register, name='register'), - path('login/', user_login, name='user_login'), + # path('register/', register, name='register'), + # path('login/', user_login, name='user_login'), path('terms_conditions/', terms_conditions, name='terms_conditions'), path('pricay_policy/', privacy_policy, name='privacy_policy'), path('pricing/', pricing, name='pricing'), @@ -23,5 +26,10 @@ urlpatterns = [ path('instrument_detection/', InstrumentDetectionView.as_view(), name='instrument_detection'), path('password_change/', auth_views.PasswordChangeView.as_view(template_name='password_change_form.html'), name='password_change'), path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html'), name='password_change_done'), - path('user_logout/', auth_views.LogoutView.as_view(next_page='index'), name='user_logout') + # path('user_logout/', auth_views.LogoutView.as_view(next_page='index'), name='user_logout') + + # Authentication + path('login/', CustomLoginView.as_view(), name='login'), + path('logout/', LogoutView.as_view(), name='logout'), + path('register/', RegisterView.as_view(), name='register'), ] diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index e5d4d15683ed07534f20a3ea1882ee39a5ad3050..34e3e538fceba7e9c1439b0ccc81bcfa817fbc14 100644 --- a/myproject/myapp/views.py +++ b/myproject/myapp/views.py @@ -11,7 +11,7 @@ from reportlab.pdfgen import canvas import json from datetime import datetime -from .forms import InstrumentDetectionForm, CustomRegistrationForm, LoginForm +from .forms import InstrumentDetectionForm from .models import Log, Action, User from django.http import JsonResponse from django.db import connection @@ -24,6 +24,13 @@ from .serializers import InstrumentDetectionSerializer from .audio_preprocessing import preprocess_audio_for_inference import requests +# Authentication Imports +from django.urls import reverse_lazy +from django.views import generic +from .models import Profile +from .forms import UserRegisterForm, LoginAuthenticationForm +from django.contrib.auth.views import LoginView + logger = logging.getLogger(__name__) def get_log_data(action, status='success', file=None, description=None): @@ -163,43 +170,61 @@ def handler500(request, *args, **kwargs): def maintenance(request): return render(request, 'maintenance.html') -def user_login(request): - if request.method == 'POST': - form = LoginForm(request.POST) +# 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') +# 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 +# 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 +# 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}) +# else: +# form = LoginForm() +# return render(request, 'login.html', {'form': form}) -def register(request): - if request.method == 'POST': - form = CustomRegistrationForm(request.POST) - if form.is_valid(): - form.save() - return redirect('user_login') - else: - form = CustomRegistrationForm() +# def register(request): +# 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') + + +# Authentication +class RegisterView(generic.CreateView): + form_class = UserRegisterForm + success_url = reverse_lazy('login') + template_name = 'registration/register.html' + + def form_valid(self, form): + response = super().form_valid(form) + Profile.objects.create(user=self.object, user_type=0) # Default user type as Basic User + return response + - return render(request, 'register.html', {'form': form}) +class CustomLoginView(LoginView): + authentication_form = LoginAuthenticationForm + template_name = 'registration/login.html' -def user_logout(request): - logout(request) - return redirect('user_login') def terms_conditions(request): return render(request, 'terms_conditions.html') diff --git a/myproject/myproject/settings.py b/myproject/myproject/settings.py index bb27f10d6d8490f0b0f752ea6cecc14b008b24b3..f4f4170d35aea11fc55f2c37a39cc94fb5e10b4c 100644 --- a/myproject/myproject/settings.py +++ b/myproject/myproject/settings.py @@ -48,11 +48,11 @@ STATICFILES_DIRS = [ # Add any other directories containing static files ] -COMPRESS_ROOT = BASE_DIR / 'myapp/static' +# COMPRESS_ROOT = BASE_DIR / 'static' -COMPRESS_ENABLED = True +# COMPRESS_ENABLED = True -STATICFILES_FINDERS = ('compressor.finders.CompressorFinder',) +# STATICFILES_FINDERS = ('compressor.finders.CompressorFinder',) MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', @@ -162,7 +162,7 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.0/howto/static-files/ - +STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = 'static/' # Default primary key field type @@ -173,3 +173,4 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' IMAGE_URL = 'static/src/images/' LOGIN_REDIRECT_URL = '/' +LOGOUT_REDIRECT_URL = '/'