/src/baruwa/lists/forms.py

https://github.com/patrickstalvord/baruwa · Python · 151 lines · 103 code · 24 blank · 24 comment · 27 complexity · b3ffd3a0d1fb963aad8a651ecb770955 MD5 · raw file

  1. #
  2. # Baruwa - Web 2.0 MailScanner front-end.
  3. # Copyright (C) 2010-2011 Andrew Colin Kissa <andrew@topdog.za.net>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. #
  19. # vim: ai ts=4 sts=4 et sw=4
  20. #
  21. from django import forms
  22. from django.template.defaultfilters import force_escape
  23. from django.utils.translation import ugettext as _
  24. try:
  25. from django.forms.fields import email_re
  26. except ImportError:
  27. from django.core.validators import email_re
  28. from baruwa.utils.regex import DOM_RE, IPV4_RE, USER_RE, IPV4_NET_OR_RANGE_RE
  29. LIST_TYPES = (
  30. ('1', 'Whitelist'),
  31. ('2', 'Blacklist'),
  32. )
  33. class ListAddForm(forms.Form):
  34. """ListAddForm"""
  35. list_type = forms.ChoiceField(choices=LIST_TYPES)
  36. from_address = forms.CharField(widget=forms.TextInput(
  37. attrs={'size': '50'}))
  38. to_address = forms.CharField(required=False)
  39. def __init__(self, request=None, *args, **kwargs):
  40. super(ListAddForm, self).__init__(*args, **kwargs)
  41. self.request = request
  42. if not request.user.is_superuser:
  43. addresses = request.session['user_filter']['addresses']
  44. load_addresses = [(address, address) for address in addresses]
  45. self.fields['to_address'] = forms.ChoiceField(
  46. choices=load_addresses)
  47. def clean_to_address(self):
  48. to_address = self.cleaned_data['to_address']
  49. if not email_re.match(to_address):
  50. raise forms.ValidationError(
  51. _('%(email)s provide a valid e-mail address') %
  52. {'email': force_escape(to_address)})
  53. if to_address not in self.request.session['user_filter']['addresses'] \
  54. and not self.request.user.is_superuser():
  55. raise forms.ValidationError(
  56. _("The address: %(email)s does not belong to you.") %
  57. {'email': force_escape(to_address)})
  58. return to_address
  59. def clean_from_address(self):
  60. from_address = self.cleaned_data['from_address']
  61. from_address = from_address.strip()
  62. if (not email_re.match(from_address) and not DOM_RE.match(from_address)
  63. and not IPV4_RE.match(from_address) and not
  64. IPV4_NET_OR_RANGE_RE.match(from_address)):
  65. raise forms.ValidationError(_("Provide either a valid IPv4, email,"
  66. " Domain address, or IPv4 network or range"))
  67. return from_address
  68. def clean(self):
  69. "check for duplicates, implemented coz of mysql utf8 bug"
  70. cleaned_data = self.cleaned_data
  71. from_address = cleaned_data.get("from_address")
  72. to_address = cleaned_data.get("to_address")
  73. from baruwa.lists.models import List
  74. list_objs = List.objects.filter(from_address=from_address,
  75. to_address=to_address)
  76. if list_objs:
  77. raise forms.ValidationError(_("The list item already exists"))
  78. return cleaned_data
  79. class AdminListAddForm(ListAddForm):
  80. """AdminListAddForm"""
  81. user_part = forms.CharField(required=False)
  82. def __init__(self, *args, **kwargs):
  83. super(AdminListAddForm, self).__init__(*args, **kwargs)
  84. if not self.request.user.is_superuser:
  85. addresses = self.request.session['user_filter']['addresses']
  86. load_addresses = [(address, address) for address in addresses]
  87. self.fields['to_address'] = forms.ChoiceField(
  88. choices=load_addresses)
  89. else:
  90. self.fields['to_address'].widget = forms.TextInput(
  91. attrs={'size': '24'})
  92. def clean_to_address(self):
  93. """clean_to_address"""
  94. to_address = self.cleaned_data['to_address']
  95. try:
  96. to_address = to_address.strip()
  97. except AttributeError:
  98. pass
  99. if not self.request.user.is_superuser:
  100. addresses = self.request.session['user_filter']['addresses']
  101. if to_address not in addresses:
  102. raise forms.ValidationError(
  103. _("The address: %(addr)s does not belong to you.")
  104. % {'addr': force_escape(to_address)})
  105. if to_address != "" and not to_address is None:
  106. if not DOM_RE.match(to_address):
  107. raise forms.ValidationError(_("Provide either a valid domain"))
  108. else:
  109. to_address = 'any'
  110. return to_address
  111. def clean_user_part(self):
  112. """clean_user_part"""
  113. user_part = self.cleaned_data['user_part']
  114. if user_part == '' or user_part is None:
  115. user_part = 'any'
  116. else:
  117. user_part = user_part.strip()
  118. if not USER_RE.match(user_part):
  119. raise forms.ValidationError(
  120. _('provide a valid user part of the email address'))
  121. return user_part
  122. class FilterForm(forms.Form):
  123. query_type = forms.ChoiceField(
  124. choices=((1, 'containing'), (2, 'excluding')))
  125. search_for = forms.CharField(required=False)
  126. class ListDeleteForm(forms.Form):
  127. list_item = forms.CharField(widget=forms.HiddenInput)