forms.py 5.4 KB
Newer Older
stef's avatar
stef committed
1 2
from django import forms
from django.conf import settings
3
from django.utils.translation import ugettext as _
4 5
from haystack.forms import SearchForm
from haystack.query import SearchQuerySet
jc's avatar
jc committed
6
from bt.models import COUNTRIES, RESOURCES, TYPES, MEDIA
stef's avatar
stef committed
7
from bt.multifile import MultiFileField
8
from operator import itemgetter
stef's avatar
stef committed
9
from captcha.fields import CaptchaField
stef's avatar
stef committed
10

jc's avatar
jc committed
11

stef's avatar
stef committed
12
class AdvancedEditor(forms.Textarea):
Okhin's avatar
Okhin committed
13
    class Media:
14
        js = (settings.MEDIA_URL + '/js/tinymce/tiny_mce.js', )
stef's avatar
stef committed
15

Okhin's avatar
Okhin committed
16 17 18
        def __init__(self, language=None, attrs=None):
            self.language = language or settings.LANGUAGE_CODE[:2]
            self.attrs = {'class': 'advancededitor'}
jc's avatar
jc committed
19 20
            if attrs:
                self.attrs.update(attrs)
Okhin's avatar
Okhin committed
21
            super(AdvancedEditor, self).__init__(attrs)
stef's avatar
stef committed
22

jc's avatar
jc committed
23

24
class AddViolation(forms.Form):
Okhin's avatar
Okhin committed
25
    resource_name = forms.CharField(required=True, max_length=4096, label=_('Please describe the discrimination'), help_text=_("What service or site, or person is unavailable or seems artificially slowed down. e.g. VoIP, p2p, filesharing, specific websites, etc."))
26
    country = forms.ChoiceField(required=True, choices=(('', ''), ) + tuple(sorted(COUNTRIES, key=itemgetter(1))), label=_("Country"), help_text=_('EU member state where the discrimination is reported.'))
Okhin's avatar
Okhin committed
27 28
    operator = forms.CharField(required=True, max_length=256, label=_("Operator"), help_text=_('The ISP or operator providing the Internet service.'))
    contract = forms.CharField(required=True, max_length=256, label=_("Contract"), help_text=_('The specific contract at the ISP provider. (please be as specific as possible)'))
29
    media = forms.ChoiceField(required=True, choices=tuple(sorted(MEDIA, key=itemgetter(1))), label=_('Is the Internet connection over mobile or fixed line?'))
Okhin's avatar
Okhin committed
30 31 32 33 34
    comment = forms.CharField(required=True, widget=AdvancedEditor(), label=_('Please describe the symptoms you are experiencing.'))
    email = forms.EmailField(required=True, label=_('Email (set this to enable saving)'), help_text=_("We need your email to validate your report. Your email address is obligatory, but we will never use your personal data for anything else than checking the submission. (see next for an optional exception)"))
    consent = forms.BooleanField(required=False, label=_("I want to help further"), help_text=_("We need your consent to contact you for clarifications regarding your report. This is optional, but helps us improve the quality of the reports. Thanks!"))
    nick = forms.CharField(required=False, label=_("Name or nickname"), help_text=_("We need some name to display that instead of an email address."))
    attachments = MultiFileField(required=False, label=_("Attach screenshot, document or any other relevant information."))
35
    resource = forms.ChoiceField(required=False, choices=RESOURCES, label=_('What is the affected resource type. (optional)'))
36 37 38 39
    temporary = forms.BooleanField(required=False, label=_('Is the discrimination only temporary, e.g. due to network overload? (optional)'))
    loophole = forms.BooleanField(required=False, label=_('Is there another offer provided by this Operator which removes this discrimination? (optional)'))
    contractual = forms.BooleanField(required=False, label=_('The discrimination is described in the subscribers contract.'))
    contract_excerpt = forms.CharField(required=False, widget=AdvancedEditor(), label=_('Please copy the relevant section describing the discrimination from the user contract. (optional)'))
40
    type = forms.ChoiceField(required=True, choices=tuple(TYPES), label=_('How would you describe the discrimination?'))
Okhin's avatar
Okhin committed
41
    captcha = CaptchaField(label=_("In order to protect against spam, please fill in the result of the following calculation. (note the + and the * are somewhat confusing)"))
42

43

44
class SearchViolation(SearchForm):
45
    country = forms.ChoiceField(required=False, choices=(('', ''),) + tuple(sorted(COUNTRIES, key=itemgetter(1))), label=_("Country"), help_text=_('EU member state where the discrimination is reported.'))
Okhin's avatar
Okhin committed
46 47
    operator = forms.CharField(required=False, max_length=256, label=_("Operator"), help_text=_('The ISP or operator providing the Internet service.'))
    contract = forms.CharField(required=False, max_length=256, label=_("Contract"), help_text=_('The specific contract at the ISP provider. (please be as specific as possible)'))
48
    media = forms.ChoiceField(required=False, choices=(('', ''), ) + tuple(sorted(MEDIA, key=itemgetter(1))), label=_('Is the Internet connection over mobile or fixed line?'))
49

Okhin's avatar
Okhin committed
50 51 52
    def search(self):
        # By default, the search field is q. So let's check if it's empty
        if not self.cleaned_data['q']:
53
            sqs = SearchQuerySet().all().exclude(old=True)
Okhin's avatar
Okhin committed
54
        else:
55
            sqs = super(SearchViolation, self).search().exclude(old=True)
56

Okhin's avatar
Okhin committed
57 58
        if not self.is_valid():
            return self.no_query_found()
59

Okhin's avatar
Okhin committed
60 61
        if self.cleaned_data['operator']:
            sqs = sqs.filter(operator__icontains=self.cleaned_data['operator'])
62

Okhin's avatar
Okhin committed
63 64
        if self.cleaned_data['contract']:
            sqs = sqs.filter(contract__icontains=self.cleaned_data['contract'])
65

Okhin's avatar
Okhin committed
66 67
        if self.cleaned_data['media']:
            sqs = sqs.filter(media=self.cleaned_data['media'])
68

Okhin's avatar
Okhin committed
69 70
        if self.cleaned_data['country']:
            sqs = sqs.filter(country=self.cleaned_data['country'])
71

Okhin's avatar
Okhin committed
72
        return sqs
Okhin's avatar
Okhin committed
73

jc's avatar
jc committed
74

Okhin's avatar
Okhin committed
75 76
class QuickSearchViolation(forms.Form):
    query = forms.CharField(required='True', max_length=256, label=_("Search"), help_text=_('Search for an existing violation'))