Nav apraksta

forms.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from django import forms
  2. from django.contrib.auth import get_user_model
  3. from decimal import Decimal
  4. from django.utils import timezone
  5. from django.contrib.contenttypes.models import ContentType
  6. from .models import (
  7. MaterialCategory,
  8. Material,
  9. ProvidedService,
  10. Customer,
  11. CustomerSite,
  12. )
  13. from markdownfield.widgets import MDEWidget
  14. from orgs.models import UserProfile
  15. class MaterialCategoryForm(forms.ModelForm):
  16. class Meta:
  17. model = MaterialCategory
  18. fields = ["organization", "name"]
  19. class MaterialForm(forms.ModelForm):
  20. class Meta:
  21. model = Material
  22. fields = ["organization", "category", "name", "code", "default_unit"]
  23. class CustomerForm(forms.ModelForm):
  24. class Meta:
  25. model = Customer
  26. fields = ["organization", "name", "email", "phone", "billing_address", "price_list"]
  27. class CustomerSiteForm(forms.ModelForm):
  28. class Meta:
  29. model = CustomerSite
  30. fields = ["customer", "name", "address", "contact_name", "contact_phone", "contact_email"]
  31. class ProvidedServiceForm(forms.ModelForm):
  32. class Meta:
  33. model = ProvidedService
  34. fields = ["title", "description", "body", "display_order", "is_enabled"]
  35. widgets = {
  36. "description": forms.Textarea(attrs={"rows": 3}),
  37. "body": MDEWidget(),
  38. }
  39. # Operational forms ----------------------------------------------------------
  40. User = get_user_model()
  41. class PickupAssignForm(forms.Form):
  42. driver = forms.ModelChoiceField(queryset=User.objects.all(), required=True, label="Assign Driver")
  43. class PickupStatusForm(forms.Form):
  44. status = forms.ChoiceField(choices=(), required=True, label="Set Status")
  45. def __init__(self, *args, **kwargs):
  46. from .models import PickupOrder
  47. super().__init__(*args, **kwargs)
  48. self.fields["status"].choices = PickupOrder.STATUS_CHOICES
  49. class PaymentForm(forms.Form):
  50. amount = forms.DecimalField(max_digits=14, decimal_places=2)
  51. received_at = forms.DateTimeField(required=False, initial=timezone.now)
  52. reference = forms.CharField(max_length=128, required=False)
  53. class DocumentForm(forms.Form):
  54. organization = forms.ModelChoiceField(queryset=None)
  55. file = forms.FileField()
  56. kind = forms.CharField(max_length=64, required=False)
  57. content_type = forms.ModelChoiceField(queryset=ContentType.objects.all(), required=True, label="Attach To Model")
  58. object_id = forms.IntegerField(required=True, label="Object ID")
  59. def __init__(self, *args, **kwargs):
  60. from orgs.models import Organization as Org
  61. super().__init__(*args, **kwargs)
  62. self.fields["organization"].queryset = Org.objects.all()
  63. # User management ------------------------------------------------------------
  64. class UserCreateForm(forms.Form):
  65. username = forms.CharField(max_length=150)
  66. email = forms.EmailField(required=False)
  67. role = forms.ChoiceField(choices=UserProfile.ROLE_CHOICES)
  68. password1 = forms.CharField(widget=forms.PasswordInput)
  69. password2 = forms.CharField(widget=forms.PasswordInput)
  70. def clean_username(self):
  71. User = get_user_model()
  72. username = self.cleaned_data["username"].strip()
  73. if User.objects.filter(username__iexact=username).exists():
  74. raise forms.ValidationError("Username already taken")
  75. return username
  76. def clean(self):
  77. cleaned = super().clean()
  78. p1 = cleaned.get("password1")
  79. p2 = cleaned.get("password2")
  80. if p1 and p2 and p1 != p2:
  81. self.add_error("password2", "Passwords do not match")
  82. return cleaned
  83. class UserEditForm(forms.Form):
  84. email = forms.EmailField(required=False)
  85. role = forms.ChoiceField(choices=UserProfile.ROLE_CHOICES)
  86. password1 = forms.CharField(widget=forms.PasswordInput, required=False)
  87. password2 = forms.CharField(widget=forms.PasswordInput, required=False)
  88. def clean(self):
  89. cleaned = super().clean()
  90. p1 = cleaned.get("password1")
  91. p2 = cleaned.get("password2")
  92. if (p1 or p2) and p1 != p2:
  93. self.add_error("password2", "Passwords do not match")
  94. return cleaned