first commit
This commit is contained in:
50
core/apps/management/forms/user/BaseUserForm.py
Normal file
50
core/apps/management/forms/user/BaseUserForm.py
Normal file
@@ -0,0 +1,50 @@
|
||||
# forms/base.py
|
||||
from django import forms
|
||||
from core.apps.accounts.models import User
|
||||
from core.apps.accounts.choices import RoleChoice
|
||||
|
||||
class BaseUserForm(forms.ModelForm):
|
||||
password = forms.CharField(
|
||||
required=False,
|
||||
widget=forms.PasswordInput(attrs={"class": "form-control"})
|
||||
)
|
||||
role = forms.ChoiceField(
|
||||
choices=[
|
||||
(RoleChoice.MANAGER, "Manager"),
|
||||
(RoleChoice.EMPLOYEE, "Employee"),
|
||||
],
|
||||
widget=forms.Select(attrs={"class": "form-control"})
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = [
|
||||
"phone",
|
||||
"password",
|
||||
"role",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"region",
|
||||
"warehouse",
|
||||
]
|
||||
|
||||
def clean_role(self):
|
||||
role = self.cleaned_data["role"]
|
||||
if role in (RoleChoice.BUSINESSMAN, RoleChoice.SUPERUSER):
|
||||
raise forms.ValidationError("This role cannot be assigned.")
|
||||
return role
|
||||
|
||||
def save(self, commit=True):
|
||||
user = super().save(commit=False)
|
||||
|
||||
password = self.cleaned_data.get("password")
|
||||
if password:
|
||||
user.set_password(password)
|
||||
else:
|
||||
if user.pk:
|
||||
old_user = User.objects.get(pk=user.pk)
|
||||
user.password = old_user.password
|
||||
|
||||
if commit:
|
||||
user.save()
|
||||
return user
|
||||
40
core/apps/management/forms/user/UserCreateFormBusinessman.py
Normal file
40
core/apps/management/forms/user/UserCreateFormBusinessman.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# forms/businessman_create.py
|
||||
from .BaseUserForm import BaseUserForm
|
||||
from core.apps.accounts.models import RoleChoice
|
||||
from core.apps.management.models import Region, Warehouse
|
||||
|
||||
class UserCreateFormBusinessman(BaseUserForm):
|
||||
|
||||
class Meta(BaseUserForm.Meta):
|
||||
fields = [
|
||||
"phone",
|
||||
"password",
|
||||
"role",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"region",
|
||||
"warehouse",
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.fields["password"].required = True
|
||||
self.fields["region"].queryset = Region.objects.all()
|
||||
self.fields["warehouse"].queryset = Warehouse.objects.all()
|
||||
|
||||
def clean(self):
|
||||
cleaned = super().clean()
|
||||
role = cleaned.get("role")
|
||||
warehouse = cleaned.get("warehouse")
|
||||
|
||||
if role == RoleChoice.MANAGER:
|
||||
cleaned["warehouse"] = None
|
||||
|
||||
if role == RoleChoice.EMPLOYEE:
|
||||
if not warehouse:
|
||||
self.add_error("warehouse", "Warehouse is required for employee")
|
||||
else:
|
||||
cleaned["region"] = warehouse.region
|
||||
|
||||
return cleaned
|
||||
@@ -0,0 +1,48 @@
|
||||
# forms/manager_create.py
|
||||
from .BaseUserForm import BaseUserForm
|
||||
from core.apps.accounts.models import RoleChoice
|
||||
from core.apps.management.models import Warehouse
|
||||
|
||||
class UserCreateFormManagerToEmployee(BaseUserForm):
|
||||
class Meta(BaseUserForm.Meta):
|
||||
fields = [
|
||||
"phone",
|
||||
"password",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"warehouse",
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.manager = kwargs.pop("manager")
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# Password is required for creation
|
||||
self.fields["password"].required = True
|
||||
|
||||
# Filter warehouses to manager's region
|
||||
self.fields["warehouse"].queryset = Warehouse.objects.filter(
|
||||
region=self.manager.region
|
||||
)
|
||||
|
||||
# Hide role field, manager can only create employees
|
||||
self.fields.pop("role", None)
|
||||
|
||||
def save(self, commit=True):
|
||||
user = super().save(commit=False)
|
||||
|
||||
# Always set role to EMPLOYEE
|
||||
user.role = RoleChoice.EMPLOYEE
|
||||
|
||||
# Set region from selected warehouse
|
||||
if user.warehouse:
|
||||
user.region = user.warehouse.region
|
||||
|
||||
# Only set password if provided
|
||||
password = self.cleaned_data.get("password")
|
||||
if password:
|
||||
user.set_password(password)
|
||||
|
||||
if commit:
|
||||
user.save()
|
||||
return user
|
||||
31
core/apps/management/forms/user/UserEditFormBusinessman.py
Normal file
31
core/apps/management/forms/user/UserEditFormBusinessman.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# forms/businessman_edit.py
|
||||
from .BaseUserForm import BaseUserForm
|
||||
from core.apps.accounts.models import RoleChoice
|
||||
from core.apps.management.models import Warehouse
|
||||
from django import forms
|
||||
|
||||
|
||||
class UserEditFormBusinessman(BaseUserForm):
|
||||
class Meta(BaseUserForm.Meta):
|
||||
fields = BaseUserForm.Meta.fields
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.fields["warehouse"].queryset = Warehouse.objects.all()
|
||||
|
||||
if self.instance.role == RoleChoice.MANAGER:
|
||||
self.fields["warehouse"].widget = forms.HiddenInput()
|
||||
|
||||
def clean(self):
|
||||
cleaned = super().clean()
|
||||
role = cleaned.get("role")
|
||||
warehouse = cleaned.get("warehouse")
|
||||
|
||||
if role == RoleChoice.MANAGER:
|
||||
cleaned["warehouse"] = None
|
||||
|
||||
if role == RoleChoice.EMPLOYEE and warehouse:
|
||||
cleaned["region"] = warehouse.region
|
||||
|
||||
return cleaned
|
||||
@@ -0,0 +1,38 @@
|
||||
# forms/manager_edit.py
|
||||
from .BaseUserForm import BaseUserForm
|
||||
from core.apps.management.models import Warehouse
|
||||
from core.apps.accounts.choices import RoleChoice
|
||||
|
||||
class UserEditFormManagerToEmployee(BaseUserForm):
|
||||
class Meta(BaseUserForm.Meta):
|
||||
fields = [
|
||||
"phone",
|
||||
"password",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"warehouse",
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.manager = kwargs.pop("manager")
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.fields.pop("role", None)
|
||||
|
||||
# 👇 decision based on edited user's role
|
||||
if self.instance.role == RoleChoice.MANAGER:
|
||||
# editing manager → no warehouse
|
||||
self.fields.pop("warehouse", None)
|
||||
else:
|
||||
# editing employee → show warehouse
|
||||
self.fields["warehouse"].queryset = Warehouse.objects.filter(
|
||||
region=self.manager.region
|
||||
)
|
||||
|
||||
def save(self, commit=True):
|
||||
user = super().save(commit=False)
|
||||
user.region = user.warehouse.region
|
||||
|
||||
if commit:
|
||||
user.save()
|
||||
return user
|
||||
5
core/apps/management/forms/user/__init__.py
Normal file
5
core/apps/management/forms/user/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from .BaseUserForm import *
|
||||
from .UserEditFormBusinessman import *
|
||||
from .UserCreateFormBusinessman import *
|
||||
from .UserCreateFormManagerToEmployee import *
|
||||
from .UserEditFormManagerToEmployee import *
|
||||
Reference in New Issue
Block a user