Coverage for sites/comments_site/comments_database/forms.py: 44%

30 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-06-19 08:36 +0000

1from django import forms 

2from django.contrib.auth.forms import UserChangeForm as BaseUserChangeForm 

3from django.contrib.auth.forms import UserCreationForm as BaseUserCreationForm 

4 

5from ptf.site_register import SITE_REGISTER 

6 

7from .models import User 

8 

9 

10def check_website_data(form: forms.BaseForm, data: dict): 

11 url = data.get("url") 

12 mathdoc_site = data.get("mathdoc_site") 

13 if url and not mathdoc_site: 

14 form.add_error("mathdoc_site", "Mathdoc site must be checked when filling the URL") 

15 # If the user to create is flagged as a mathdoc website, its username must be 

16 # an entry in the site_register 

17 if mathdoc_site: 

18 username = data.get("username") 

19 if username not in SITE_REGISTER: 

20 form.add_error( 

21 "username", 

22 "The username MUST match an entry in site_register.py when mathdoc site is checked", 

23 ) 

24 

25 

26class UserCreationForm(BaseUserCreationForm): 

27 class Meta: 

28 model = User 

29 fields = ("email", "mathdoc_site", "url") 

30 

31 def clean(self) -> dict: 

32 cleaned_data = super().clean() 

33 check_website_data(self, cleaned_data) 

34 return cleaned_data 

35 

36 

37class UserChangeForm(BaseUserChangeForm): 

38 class Meta: 

39 model = User 

40 fields = ("email", "mathdoc_site", "url") 

41 

42 def clean(self) -> dict: 

43 cleaned_data = super().clean() 

44 check_website_data(self, cleaned_data) 

45 return cleaned_data