PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/qualitio/organizations/forms.py

https://github.com/qualitio/qualitio
Python | 174 lines | 152 code | 21 blank | 1 comment | 3 complexity | 0f56772d399c2866974cf2ae18896ed4 MD5 | raw file
  1. from django import forms
  2. from django.contrib.auth import models as auth
  3. from django.forms.models import modelformset_factory, inlineformset_factory
  4. from qualitio import store
  5. from qualitio import execute
  6. from qualitio import glossary
  7. from qualitio import core
  8. from qualitio import THREAD
  9. from qualitio.organizations import models
  10. class OrganizationProfileForm(core.BaseModelForm):
  11. class Meta(core.BaseModelForm.Meta):
  12. model = models.Organization
  13. fields = ("name", "slug", "homepage", "description", "googleapps_domain")
  14. widgets = {
  15. 'name': forms.TextInput(attrs={'readonly': 'readonly'}),
  16. 'slug': forms.TextInput(attrs={'readonly': 'readonly'})
  17. }
  18. class OrganizationGoogleAppsSetupForm(core.BaseModelForm):
  19. # helper field for handling googleapp additional configuration
  20. callback = forms.CharField(widget=forms.HiddenInput, required=False)
  21. slug = forms.RegexField(
  22. regex="^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$",
  23. label="subdomain (part of url address)",
  24. error_messages={
  25. 'invalid': 'subdomain may only conatin characters from set: 0-9, a-z and - (hypen)'
  26. })
  27. class Meta(core.BaseModelForm.Meta):
  28. model = models.Organization
  29. fields = ("name", "homepage", "slug", "description", "googleapps_domain")
  30. widgets = {
  31. 'googleapps_domain': forms.HiddenInput(attrs={'readonly': 'readonly'})
  32. }
  33. class OrganizationMemberForm(core.BaseModelForm):
  34. class Meta(core.BaseModelForm.Meta):
  35. model = models.OrganizationMember
  36. fields = ("role",)
  37. class NewMemberForm(core.BaseForm):
  38. email = forms.EmailField(required=True, label="E-mail address")
  39. def clean_email(self):
  40. email = self.cleaned_data.get('email')
  41. if auth.User.objects.filter(
  42. organization_member__organization=THREAD.organization,
  43. email=self.cleaned_data.get('email')
  44. ):
  45. raise forms.ValidationError("User already in organization.")
  46. return email
  47. class NewUserForm(core.BaseModelForm):
  48. password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
  49. password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput,
  50. help_text = "Enter the same password as above, for verification.")
  51. class Meta(core.BaseModelForm.Meta):
  52. model = auth.User
  53. fields = ("email",)
  54. def clean_email(self):
  55. email = self.cleaned_data.get('email')
  56. if auth.User.objects.filter(username=email).exists():
  57. raise forms.ValidationError("email")
  58. return email
  59. def clean_password2(self):
  60. password1 = self.cleaned_data.get("password1", "")
  61. password2 = self.cleaned_data["password2"]
  62. if password1 != password2:
  63. raise forms.ValidationError("The two password fields didn't match.")
  64. return password2
  65. def save(self, commit=True):
  66. user = super(NewUserForm, self).save(commit=False)
  67. user.set_password(self.cleaned_data["password1"])
  68. if commit:
  69. user.save()
  70. return user
  71. class ProjectForm(core.BaseModelForm):
  72. class Meta(core.BaseModelForm.Meta):
  73. model = models.Project
  74. fields = ("name", "homepage", "description")
  75. def __init__(self, *args, **kwargs):
  76. self.organization = kwargs.pop('organization', None)
  77. super(ProjectForm, self).__init__(*args, **kwargs)
  78. self.instance.organization = self.organization
  79. def clean(self):
  80. name = self.cleaned_data.get('name')
  81. qs = models.Project.objects.filter(name=name, organization=self.organization)
  82. qs = qs.exclude(pk=self.instance.pk)
  83. if qs.exists():
  84. raise forms.ValidationError('Project with name "%s" already exists in "%s" organization.' % (
  85. name, self.organization.name))
  86. return self.cleaned_data
  87. class ProjectUserForm(core.BaseForm):
  88. username = forms.CharField()
  89. def clean_username(self):
  90. data = self.cleaned_data['username']
  91. try:
  92. user = auth.User.objects.get(username=data)
  93. except auth.User.DoesNotExist:
  94. raise forms.ValidationError("User does not exist.")
  95. return data
  96. class OrganizationUsersFormSet(core.BaseInlineFormSet):
  97. def clean(self):
  98. super(OrganizationUsersFormSet, self).clean()
  99. active_members = [member for member in self.cleaned_data\
  100. if member['role'] < models.OrganizationMember.INACTIVE]
  101. if self.instance.payment.strategy.users < len(active_members):
  102. raise forms.ValidationError(
  103. ("Your current plan is %s and maximum number of users is %s.<br/>" +\
  104. "Change your plan to increase the number of users.")
  105. % (self.instance.payment, self.instance.payment.strategy.users)
  106. )
  107. OrganizationUsersForm = inlineformset_factory(models.Organization,
  108. models.OrganizationMember,
  109. form=OrganizationMemberForm,
  110. formset=OrganizationUsersFormSet,
  111. extra=0, can_delete=True)
  112. OrganizationProjectsForm = modelformset_factory(models.Project,
  113. form=ProjectForm,
  114. formset=core.BaseModelFormSet,
  115. extra=0, can_delete=False)
  116. ProjectTestCaseStatusFormSet = inlineformset_factory(models.Project,
  117. store.TestCaseStatus,
  118. formset=core.BaseInlineFormSet,
  119. extra=0)
  120. ProjectTestRunStatusFormSet = inlineformset_factory(models.Project,
  121. execute.TestRunStatus,
  122. formset=core.BaseInlineFormSet,
  123. extra=0)
  124. ProjectTestCaseRunStatusFormSet = inlineformset_factory(models.Project,
  125. execute.TestCaseRunStatus,
  126. formset=core.BaseInlineFormSet,
  127. extra=0)
  128. ProjectGlossaryLanguageFormSet = inlineformset_factory(models.Project,
  129. glossary.Language,
  130. formset=core.BaseInlineFormSet,
  131. extra=0)