Skip to content

Commit 3ec547e

Browse files
committed
Adding new To Do app
1. Authentication - uses django auth User for new user registration and login. 2. Items - uses sqllite3 to perform CRUD operation on users tasks/notes. 3. Temaplates - contaions html files
1 parent 9b0f29a commit 3ec547e

35 files changed

+1028
-0
lines changed

To-Do_app/Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
django = "*"
8+
9+
[dev-packages]
10+
11+
[requires]
12+
python_version = "3.10"

To-Do_app/Pipfile.lock

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

To-Do_app/ToDo_app/Authentication/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AuthenticationConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'Authentication'

To-Do_app/ToDo_app/Authentication/migrations/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.urls import path
2+
from .views import (
3+
RegistrationView,
4+
UserLoginView,
5+
UserLogoutView,
6+
)
7+
8+
urlpatterns = [
9+
# django always checks for cross site forgery so we add csrf_exempt to let other sites send reuest to this url
10+
path("registration", RegistrationView.as_view(), name="registration"),
11+
path("userlogin", UserLoginView.as_view(), name="login"),
12+
path("userlogout", UserLogoutView.as_view(), name="logout"),
13+
]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import json
2+
from django.shortcuts import render, redirect
3+
from django.views import View
4+
from django.http import JsonResponse
5+
from django.contrib.auth.models import User
6+
from django.db import transaction
7+
from django.contrib import messages
8+
from django.contrib.auth import login, logout, authenticate
9+
10+
11+
# Create your views here.
12+
class RegistrationView(View):
13+
def get(self, request):
14+
messages.get_messages(request)
15+
return render(request, "authentication/register.html")
16+
17+
def post(self, request):
18+
19+
# get user data
20+
data = request.POST
21+
username = data.get("username")
22+
email = data.get("email")
23+
password = data.get("password")
24+
25+
context = {"fieldValues": request.POST}
26+
27+
# validate
28+
if not User.objects.filter(username=username).exists():
29+
if not User.objects.filter(email=email).exists():
30+
if len(password) < 8:
31+
messages.error(request, "Password should be 8 charecter long")
32+
return render(request, "authentication/register.html", context)
33+
else:
34+
# new nuser creation process
35+
try:
36+
# adding user to data base
37+
user = User.objects.create_user(username=username, email=email)
38+
user.set_password(password)
39+
user.save()
40+
transaction.commit()
41+
messages.success(request, "Account created")
42+
except Exception as e:
43+
messages.error(request, f"Server - Database warning {str(e)}")
44+
transaction.rollback()
45+
else:
46+
messages.error(request, "Email already exists")
47+
return render(request, "authentication/register.html", context)
48+
else:
49+
messages.error(request, "User name already exists")
50+
return render(request, "authentication/register.html", context)
51+
52+
return redirect("login")
53+
54+
55+
class UserLoginView(View):
56+
def get(self, request):
57+
return render(request, "authentication/login.html")
58+
59+
def post(self, request):
60+
data = request.POST
61+
username = data.get("username")
62+
password = data.get("password")
63+
64+
if username and password:
65+
user = authenticate(username=username, password=password)
66+
if user:
67+
if user.is_active:
68+
login(request, user)
69+
messages.success(request, "Logged in successfully.")
70+
return redirect("index") # Redirect to the expenses page
71+
else:
72+
messages.error(request, "Your account is not active.")
73+
else:
74+
messages.error(request, "Invalid username or password.")
75+
else:
76+
messages.error(request, "Please provide both username and password.")
77+
78+
return redirect("login")
79+
80+
81+
class UserLogoutView(View):
82+
def get(self, request):
83+
logout(request)
84+
messages.success(request, "Logout successfully.")
85+
return redirect("login")

0 commit comments

Comments
 (0)