From ce1af464ed1771ecbb6efea0cb64ce49bad084bd Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Sat, 7 Oct 2023 23:27:49 +0200 Subject: [PATCH] Add: auth app --- archivist/apps/authentication/__init__.py | 0 archivist/apps/authentication/admin.py | 3 ++ archivist/apps/authentication/config.py | 6 +++ .../authentication/migrations/__init__.py | 0 archivist/apps/authentication/models.py | 3 ++ archivist/apps/authentication/tests.py | 3 ++ archivist/apps/authentication/views.py | 51 +++++++++++++++++++ 7 files changed, 66 insertions(+) create mode 100644 archivist/apps/authentication/__init__.py create mode 100644 archivist/apps/authentication/admin.py create mode 100644 archivist/apps/authentication/config.py create mode 100644 archivist/apps/authentication/migrations/__init__.py create mode 100644 archivist/apps/authentication/models.py create mode 100644 archivist/apps/authentication/tests.py create mode 100644 archivist/apps/authentication/views.py diff --git a/archivist/apps/authentication/__init__.py b/archivist/apps/authentication/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/archivist/apps/authentication/admin.py b/archivist/apps/authentication/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/archivist/apps/authentication/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/archivist/apps/authentication/config.py b/archivist/apps/authentication/config.py new file mode 100644 index 0000000..4661d88 --- /dev/null +++ b/archivist/apps/authentication/config.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AuthConfig(AppConfig): + name = 'apps.auth' + label = 'apps_auth' diff --git a/archivist/apps/authentication/migrations/__init__.py b/archivist/apps/authentication/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/archivist/apps/authentication/models.py b/archivist/apps/authentication/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/archivist/apps/authentication/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/archivist/apps/authentication/tests.py b/archivist/apps/authentication/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/archivist/apps/authentication/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/archivist/apps/authentication/views.py b/archivist/apps/authentication/views.py new file mode 100644 index 0000000..d974983 --- /dev/null +++ b/archivist/apps/authentication/views.py @@ -0,0 +1,51 @@ +# Create your views here. +from django.shortcuts import render, redirect +from django.contrib.auth import authenticate, login +from .forms import LoginForm, SignUpForm + + +def login_view(request): + form = LoginForm(request.POST or None) + + msg = None + + if request.method == "POST": + + if form.is_valid(): + username = form.cleaned_data.get("username") + password = form.cleaned_data.get("password") + user = authenticate(username=username, password=password) + if user is not None: + login(request, user) + return redirect("/") + else: + msg = 'Invalid credentials' + else: + msg = 'Error validating the form' + + return render(request, "accounts/login.html", {"form": form, "msg": msg}) + + +def register_user(request): + msg = None + success = False + + if request.method == "POST": + form = SignUpForm(request.POST) + if form.is_valid(): + form.save() + username = form.cleaned_data.get("username") + raw_password = form.cleaned_data.get("password1") + user = authenticate(username=username, password=raw_password) + + msg = 'User created - please login.' + success = True + + # return redirect("/login/") + + else: + msg = 'Form is not valid' + else: + form = SignUpForm() + + return render(request, "accounts/register.html", {"form": form, "msg": msg, "success": success})