説明なし

forms.py 3.7KB

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