forms.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from django import forms
  2. from django.contrib.auth.models import Group
  3. from django.forms import ModelForm
  4. from tickets.models import Task, TaskList, TicketType
  5. from django.conf import settings
  6. from SharixAdmin.models import SharixUser
  7. class ListCreateForm(ModelForm):
  8. def __init__(self, user, *args, **kwargs):
  9. super(ListCreateForm, self).__init__(*args, **kwargs)
  10. self.fields["group"].queryset = Group.objects.filter(user=user)
  11. self.fields["group"].widget.attrs = {
  12. "id": "id_group",
  13. "class": "custom-select mb-3",
  14. "name": "group",
  15. }
  16. self.fields["name"].widget.attrs = {
  17. "id": "id_name",
  18. "class": "form-control",
  19. "name": "group",
  20. }
  21. class Meta:
  22. model = TaskList
  23. exclude = ["slug"]
  24. class TicketForm(ModelForm):
  25. def __init__(self, user, *args, **kwargs):
  26. super().__init__(*args, **kwargs)
  27. task_list = kwargs.get("initial").get("task_list")
  28. members = task_list.group.user_set.all()
  29. #print(user)
  30. #print(task_list.group)
  31. #members = SharixUser.objects.filter(groups__name=task_list.group)
  32. #print(members)
  33. self.fields["assigned_to"].queryset = members
  34. self.fields["assigned_to"].label_from_instance = lambda obj: "%s (%s)" % (
  35. obj.get_full_name(),
  36. obj.username,
  37. )
  38. self.fields["assigned_to"].widget.attrs = {"class": "custom-select"}
  39. self.fields["type"].widget.attrs = {"class": "custom-select"}
  40. self.fields["title"].widget.attrs = {"class": "form-control"}
  41. self.fields["note"].widget.attrs = {"class": "form-control"}
  42. if kwargs.get("initial").get("type_disabled") == True:
  43. self.fields["type"].disabled = True
  44. due_date = forms.DateField(widget=forms.DateInput(attrs={"type": "date", "class": "form-control"}))
  45. def clean_created_by(self):
  46. """Keep the existing created_by regardless of anything coming from the submitted form.
  47. If creating a new task, then created_by will be None, but we set it before saving."""
  48. return self.instance.created_by
  49. class Meta:
  50. model = Task
  51. fields = ["type", "title", "note", "due_date", "assigned_to", "created_by"]
  52. class SearchForm(forms.Form):
  53. q = forms.CharField(widget=forms.widgets.TextInput(attrs={"size": 35}))