Coverage for ivatar/tools/forms.py: 79%

38 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-26 00:11 +0000

1# -*- coding: utf-8 -*- 

2""" 

3Classes for our ivatar.tools.forms 

4""" 

5from django import forms 

6from django.utils.translation import gettext_lazy as _ 

7from django.core.exceptions import ValidationError 

8from django.forms.utils import ErrorList 

9 

10from ivatar.settings import AVATAR_MAX_SIZE 

11from ivatar.settings import MIN_LENGTH_URL, MAX_LENGTH_URL 

12from ivatar.settings import MIN_LENGTH_EMAIL, MAX_LENGTH_EMAIL 

13 

14 

15class CheckDomainForm(forms.Form): 

16 """ 

17 Form handling domain check 

18 """ 

19 

20 domain = forms.CharField( 

21 label=_("Domain"), 

22 required=True, 

23 error_messages={"required": _("Cannot check without a domain name.")}, 

24 ) 

25 

26 

27class CheckForm(forms.Form): 

28 """ 

29 Form handling check 

30 """ 

31 

32 mail = forms.EmailField( 

33 label=_("E-Mail"), 

34 required=False, 

35 min_length=MIN_LENGTH_EMAIL, 

36 max_length=MAX_LENGTH_EMAIL, 

37 error_messages={"required": _("Cannot check without a domain name.")}, 

38 ) 

39 

40 openid = forms.CharField( 

41 label=_("OpenID"), 

42 required=False, 

43 min_length=MIN_LENGTH_URL, 

44 max_length=MAX_LENGTH_URL, 

45 error_messages={"required": _("Cannot check without an openid name.")}, 

46 ) 

47 

48 size = forms.IntegerField( 

49 label=_("Size"), 

50 initial=80, 

51 min_value=5, 

52 max_value=AVATAR_MAX_SIZE, 

53 required=True, 

54 ) 

55 

56 default_opt = forms.ChoiceField( 

57 label=_("Default"), 

58 required=False, 

59 widget=forms.RadioSelect, 

60 choices=[ 

61 ("retro", _("Retro style (similar to GitHub)")), 

62 ("robohash", _("Roboter style")), 

63 ("pagan", _("Retro adventure character")), 

64 ("wavatar", _("Wavatar style")), 

65 ("monsterid", _("Monster style")), 

66 ("identicon", _("Identicon style")), 

67 ("mm", _("Mystery man")), 

68 ("mmng", _("Mystery man NextGen")), 

69 ("none", _("None")), 

70 ], 

71 ) 

72 

73 default_url = forms.URLField( 

74 label=_("Default URL"), 

75 min_length=1, 

76 max_length=MAX_LENGTH_URL, 

77 required=False, 

78 ) 

79 

80 def clean(self): 

81 self.cleaned_data = super().clean() 

82 mail = self.cleaned_data.get("mail") 

83 openid = self.cleaned_data.get("openid") 

84 default_url = self.cleaned_data.get("default_url") 

85 default_opt = self.cleaned_data.get("default_opt") 

86 if default_url and default_opt and default_opt != "none": 

87 if "default_url" not in self._errors: 

88 self._errors["default_url"] = ErrorList() 

89 if "default_opt" not in self._errors: 

90 self._errors["default_opt"] = ErrorList() 

91 

92 errstring = _("Only default URL OR default keyword may be specified") 

93 self._errors["default_url"].append(errstring) 

94 self._errors["default_opt"].append(errstring) 

95 if not mail and not openid: 

96 raise ValidationError(_("Either OpenID or mail must be specified")) 

97 return self.cleaned_data 

98 

99 def clean_openid(self): 

100 data = self.cleaned_data["openid"] 

101 return data.lower() 

102 

103 def clean_mail(self): 

104 data = self.cleaned_data["mail"] 

105 return data.lower()