first comit

This commit is contained in:
2024-02-23 10:30:02 +00:00
commit ddeb07d0ba
12482 changed files with 1857507 additions and 0 deletions

0
accounts/__init__.py Executable file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

3
accounts/admin.py Executable file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
accounts/apps.py Executable file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'

94
accounts/forms.py Normal file
View File

@@ -0,0 +1,94 @@
from django import forms
from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, UsernameField, PasswordResetForm, SetPasswordForm, UserChangeForm
from django.utils.translation import gettext_lazy as _
from django.conf import settings
class RegistrationForm(UserCreationForm):
def validate_email_domain(email):
for domain in settings.ALLOWED_EMAIL_DOMAINS:
#if "@rawcut.tv" in email or "@tomos.tv" in email or "@themainframe.co.uk" in email:
if domain in email:
break
else:
raise ValidationError(
_('%(email)s - %(domain)s not allowed'),
params={'email': email, 'domain':domain},
)
password1 = forms.CharField(
label=_("Password"),
widget=forms.PasswordInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Password'}),
)
password2 = forms.CharField(
label=_("Confirm Password"),
widget=forms.PasswordInput(attrs={
'class': 'form-control form-control-user', 'placeholder': 'Confirm Password'
}),
)
username= forms.CharField(
widget=forms.TextInput(attrs={
'class': 'form-control form-control-user',
'placeholder': 'Username'
}),
)
email= forms.EmailField(required=True, validators=[validate_email_domain],
widget=forms.EmailInput(attrs={
'class': 'form-control form-control-user',
'placeholder': 'Email',
'name': 'email'
})
)
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
def delete(self):
user = super(RegistrationForm, self).save(commit=False)
user.delete()
return 'User deleted'
class LoginForm(AuthenticationForm):
username = forms.EmailField(widget=forms.EmailInput(attrs={
'class': 'form-control form-control-user',
'placeholder': 'Email',
"autocomplete": "email"
}))
password = forms.CharField(
label=_("Your Password"),
strip=False,
widget=forms.PasswordInput(attrs={
"class": "form-control form-control-user",
"placeholder": "Password",
"autocomplete":"current-password"
}))
class UserPasswordResetForm(PasswordResetForm):
email = forms.EmailField(widget=forms.EmailInput(attrs={
'class': 'form-control form-control-user',
'placeholder': 'Email'
}))
class UserSetPasswordForm(SetPasswordForm):
new_password1 = forms.CharField(max_length=50, widget=forms.PasswordInput(attrs={
'class': 'form-control form-control-user', 'placeholder': 'New Password'
}), label="New Password")
new_password2 = forms.CharField(max_length=50, widget=forms.PasswordInput(attrs={
'class': 'form-control form-control-user', 'placeholder': 'Confirm New Password'
}), label="Confirm New Password")
class UserPasswordChangeForm(PasswordChangeForm):
old_password = forms.CharField(max_length=50, widget=forms.PasswordInput(attrs={
'class': 'form-control form-control-user', 'placeholder': 'Old Password'
}), label='Old Password')
new_password1 = forms.CharField(max_length=50, widget=forms.PasswordInput(attrs={
'class': 'form-control form-control-user', 'placeholder': 'New Password'
}), label="New Password")
new_password2 = forms.CharField(max_length=50, widget=forms.PasswordInput(attrs={
'class': 'form-control form-control-user', 'placeholder': 'Confirm New Password'
}), label="Confirm New Password")

20
accounts/middleware.py Normal file
View File

@@ -0,0 +1,20 @@
from django.shortcuts import redirect
from hrm.models import employee
class permissionEnforceMiddleware():
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if request.path.startswith('/manager'):
user = employee.objects.get(employee__id=request.user.id)
groups = user.groups.all()
for group in groups:
adminGroups = ['manager', 'hr', 'smt']
if any(grp in group.name.lower() for grp in adminGroups):
response = self.get_response(request)
return response
return redirect('403')
else:
response = self.get_response(request)
return response

View File

3
accounts/models.py Executable file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
accounts/tests.py Executable file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

13
accounts/urls.py Normal file
View File

@@ -0,0 +1,13 @@
from django.urls import path
from accounts import views
app_name = 'accounts'
urlpatterns = [
#path('', views.index, name='index'),
path('sign_in', views.sign_in, name='sign_in'),
path('sign_up', views.sign_up, name='sign_up'),
path('user_sign_up', views.user_sign_up, name='user_sign_up'),
path('logout', views.logout_view, name='logout'),
]

63
accounts/views.py Executable file
View File

@@ -0,0 +1,63 @@
from django.shortcuts import render, redirect
from django.urls import reverse
from accounts.forms import *
import json
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.contrib.auth.hashers import make_password
from django.contrib.auth import logout,login,authenticate
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
from django.conf import settings
from custom_user.models import User
def create_session(request, username):
print("logging in")
print(username)
user=User.objects.get(email=username)
login(request, user)
return HttpResponseRedirect(reverse('index'))
def logout_view(request):
logout(request)
return redirect(reverse('accounts:sign_in'))
def sign_in(request):
if request.method == 'POST':
form = LoginForm(request, data=request.POST)
context={'form':form, 'invalid': True}
if form.is_valid():
username=form.cleaned_data.get('username')
password=form.cleaned_data.get('password')
user=authenticate(username=username,password=password)
if user:
return create_session(request,user.email)
else:
print(f"form is invalid, {form.errors}")
return render(request, 'sign-in.html', {'form': LoginForm})
def sign_up(request):
if request.user.is_authenticated:
return redirect(reverse('index'))
return render(request, 'sign-up.html', {'form': LoginForm})
def user_sign_up(request):
if request.method == 'POST':
data = json.loads(request.POST.get('data', ''))
try:
validate_email(data['email'])
except ValidationError as e:
print("bad email, details:", e)
message = 'Email Validation error'
messageTitle = 'Error!'
messageStatus = 'text-bg-danger'
response = JsonResponse({'messageTitle':messageTitle, 'message': message, 'messageStatus':messageStatus})
return response
password = make_password(data['password'])
user = User.objects.create(email = data['email'], password=password)
login(request, user)
return JsonResponse({'no_errors':True})
else:
return render(request, 'sign-up.html', {'form': LoginForm})