Added filter functionality and combined both base.html
This commit is contained in:
+43
-1
@@ -1,5 +1,6 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.forms import AuthenticationForm
|
||||
from django.contrib.auth.views import PasswordChangeView
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.forms import CharField
|
||||
from django.forms.models import ModelForm
|
||||
@@ -18,7 +19,6 @@ class RegisterForm(ModelForm):
|
||||
|
||||
field_order = ['username', 'email', 'password1', 'password2', 'phone_number', 'company', 'line', 'user_type', 'company_permissions', 'employee_permissions', 'is_active']
|
||||
|
||||
|
||||
class Meta:
|
||||
model = get_user_model()
|
||||
fields = ['username', 'email', 'password1', 'password2', 'phone_number', 'company', 'line', 'user_type', 'company_permissions', 'employee_permissions', 'is_active']
|
||||
@@ -52,3 +52,45 @@ class RegisterForm(ModelForm):
|
||||
user.save()
|
||||
return user
|
||||
|
||||
class UserEditForm(ModelForm):
|
||||
password1 = CharField(required=False, label='Password', widget=PasswordInput)
|
||||
password2 = CharField(required=False, label='Confirm Password', widget=PasswordInput)
|
||||
|
||||
field_order = ['username', 'email', 'password1', 'password2', 'phone_number', 'company', 'line', 'user_type', 'company_permissions', 'employee_permissions', 'is_active']
|
||||
|
||||
class Meta:
|
||||
model = get_user_model()
|
||||
fields = ['username', 'email', 'password1', 'password2', 'phone_number', 'company', 'line', 'user_type', 'company_permissions', 'employee_permissions', 'is_active']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['user_type'].widget.attrs['readonly'] = True
|
||||
self.fields['company_permissions'].widget.attrs['disabled'] = True
|
||||
self.fields['employee_permissions'].widget.attrs['disabled'] = True
|
||||
|
||||
if self.data.get('user_type') in ( 'EM', 'BS'):
|
||||
self.fields['company_permissions'].required = False
|
||||
if self.data.get('user_type') in ('CL', 'CA', 'BS'):
|
||||
self.fields['company_permissions'].required = False
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
password1 = cleaned_data.get('password1')
|
||||
password2 = cleaned_data.get('password2')
|
||||
|
||||
if password1 and password2 and password1 != password2:
|
||||
raise ValidationError("Passwords don't match")
|
||||
return cleaned_data
|
||||
|
||||
def save(self, commit=True):
|
||||
user = super().save(commit=False)
|
||||
user.set_password(self.cleaned_data['password1'])
|
||||
if commit:
|
||||
user.save()
|
||||
return user
|
||||
|
||||
|
||||
class UserChangePasswordForm(PasswordChangeView):
|
||||
old_password = CharField(widget=PasswordInput())
|
||||
new_password = CharField(widget=PasswordInput())
|
||||
confirm_password = CharField(widget=PasswordInput())
|
||||
@@ -0,0 +1,44 @@
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def create_permissions(apps, schema_editor):
|
||||
EmployeePermission = apps.get_model('accounts', 'EmployeePermission')
|
||||
ClientPermission = apps.get_model('accounts', 'ClientPermission')
|
||||
|
||||
employee_permissions = [
|
||||
(1, 'can_manage_containers', 'Can manage containers'),
|
||||
(2, 'can_view_reports', 'Can view reports'),
|
||||
(3, 'can_handle_operations', 'Can handle operations'),
|
||||
]
|
||||
for _id, codename, name in employee_permissions:
|
||||
EmployeePermission.objects.create(id=_id, codename=codename, name=name)
|
||||
|
||||
client_permissions = [
|
||||
(1, 'can_view_booking', 'Can view booking'),
|
||||
(2, 'can_manage_booking', 'Can book container'),
|
||||
(3, 'can_view_preinfo', 'Can view preinfo'),
|
||||
(4, 'can_manage_preinfo', 'Can manage preinfo'),
|
||||
(5, 'can_view_payment', 'Can view payment'),
|
||||
(6, 'can_manage_payment', 'Can manage payment'),
|
||||
(7, 'can_manage_company_users', 'Can manage company users'),
|
||||
]
|
||||
for _id, codename, name in client_permissions:
|
||||
ClientPermission.objects.create(id=_id, codename=codename, name=name)
|
||||
|
||||
|
||||
def reverse_permissions(apps, schema_editor):
|
||||
EmployeePermission = apps.get_model('accounts', 'EmployeePermission')
|
||||
ClientPermission = apps.get_model('accounts', 'ClientPermission')
|
||||
|
||||
EmployeePermission.objects.all().delete()
|
||||
ClientPermission.objects.all().delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('accounts', '0007_auto_20250725_1920'), # Adjust this to your last migration
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(create_permissions, reverse_permissions),
|
||||
]
|
||||
@@ -0,0 +1,29 @@
|
||||
from django.db import migrations
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.hashers import make_password
|
||||
|
||||
def create_superuser(apps, schema_editor):
|
||||
User = apps.get_model('accounts', 'DepotUser')
|
||||
if not User.objects.filter(email=settings.ADMIN_USER_EMAIL).exists():
|
||||
user = User.objects.create(
|
||||
email=settings.ADMIN_USER_EMAIL,
|
||||
username=settings.ADMIN_USER_NAME,
|
||||
is_staff=True,
|
||||
is_superuser=True,
|
||||
is_active=True,
|
||||
user_type='AD',
|
||||
password=make_password(settings.ADMIN_USER_PASSWORD)
|
||||
)
|
||||
|
||||
def reverse_superuser(apps, schema_editor):
|
||||
User = apps.get_model('accounts', 'DepotUser')
|
||||
User.objects.filter(email=settings.ADMIN_USER_EMAIL).delete()
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('accounts', '0008_populate_permissions'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(create_superuser, reverse_superuser),
|
||||
]
|
||||
+4
-3
@@ -3,11 +3,12 @@ from django.contrib.auth import views as auth_views
|
||||
from accounts import views
|
||||
|
||||
urlpatterns = [
|
||||
path('login/', views.DepotLoginView.as_view(), name='login'),
|
||||
path('relogin/', auth_views.logout_then_login, name='relogin'),
|
||||
path('user/', include([
|
||||
path('', include([
|
||||
path('', views.UserListView.as_view(), name='user_list'),
|
||||
path('register/', views.RegisterView.as_view(), name='user_register'),
|
||||
path('login/', views.DepotLoginView.as_view(), name='login'),
|
||||
path('relogin/', auth_views.logout_then_login, name='relogin'),
|
||||
path('change-password/', views.UserChangePasswordView.as_view(), name='change_password'),
|
||||
path('<int:pk>/update/', views.UserUpdateView.as_view(), name='user_update'),
|
||||
path('<int:pk>/active/', views.UserActiveView.as_view(), name='user_active'),
|
||||
])),
|
||||
|
||||
+13
-2
@@ -8,7 +8,7 @@ from django.views import View
|
||||
from django.views.generic import TemplateView, FormView, ListView, UpdateView
|
||||
from rest_framework.generics import get_object_or_404
|
||||
|
||||
from accounts.forms import LoginForm, RegisterForm
|
||||
from accounts.forms import LoginForm, RegisterForm, UserChangePasswordForm
|
||||
from accounts.models import DepotUser
|
||||
|
||||
from django.contrib.auth.decorators import login_required, user_passes_test
|
||||
@@ -106,6 +106,11 @@ class UserListView(ListView):
|
||||
queryset = super().get_queryset()
|
||||
user = self.request.user
|
||||
|
||||
data_filter = self.request.GET.get('filter')
|
||||
|
||||
if data_filter != 'all':
|
||||
queryset = queryset.filter(is_active=True)
|
||||
|
||||
# Filter users based on permissions
|
||||
if user.is_superuser:
|
||||
return queryset.all()
|
||||
@@ -184,4 +189,10 @@ class UserActiveView(LoginRequiredMixin, View):
|
||||
|
||||
target_user.is_active = not target_user.is_active
|
||||
target_user.save()
|
||||
return JsonResponse({'success': True, 'is_active': target_user.is_active})
|
||||
return JsonResponse({'success': True, 'is_active': target_user.is_active})
|
||||
|
||||
|
||||
class UserChangePasswordView(LoginRequiredMixin, View):
|
||||
template_name = 'registration/change_password.html'
|
||||
form_class = UserChangePasswordForm
|
||||
success_url = reverse_lazy('home')
|
||||
|
||||
Reference in New Issue
Block a user