views.py 15.9 KB
Newer Older
stef's avatar
stef committed
1
from forms import AddViolation, SearchViolation
stef's avatar
stef committed
2
from django.http import HttpResponse, HttpResponseRedirect, Http404
3
from django.shortcuts import render_to_response, get_object_or_404, redirect
4
from django.template import RequestContext, loader, Context
stef's avatar
stef committed
5
from django.core.files import File
6
from django.core.servers.basehttp import FileWrapper
stef's avatar
stef committed
7
from django.conf import settings
stef's avatar
stef committed
8
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
9
from django.core.exceptions import ObjectDoesNotExist
10
from django.contrib import messages
11
from django.contrib.auth.models import User
stef's avatar
stef committed
12
from django.utils.translation import ugettext_lazy as _
stef's avatar
stef committed
13
from django.db.models import Count
14
from models import Violation, Attachment, Comment, Confirmation, COUNTRIES, STATUS
stef's avatar
stef committed
15
16
from tempfile import mkstemp
from datetime import datetime
17
18
19
import hashlib, os, re, json, smtplib
from random import randint
from email.mime.text import MIMEText
stef's avatar
stef committed
20
from email.header import Header
21
22
from urlparse import urljoin
from BeautifulSoup import BeautifulSoup, Comment as BComment
stef's avatar
stef committed
23
from operator import itemgetter
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

def sanitizeHtml(value, base_url=None):
    rjs = r'[\s]*(&#x.{1,7})?'.join(list('javascript:'))
    rvb = r'[\s]*(&#x.{1,7})?'.join(list('vbscript:'))
    re_scripts = re.compile('(%s)|(%s)' % (rjs, rvb), re.IGNORECASE)
    validTags = 'p i strong b u a h1 h2 h3 pre br img'.split()
    validAttrs = 'href src width height'.split()
    urlAttrs = 'href src'.split() # Attributes which should have a URL
    soup = BeautifulSoup(value)
    for comment in soup.findAll(text=lambda text: isinstance(text, BComment)):
        # Get rid of comments
        comment.extract()
    for tag in soup.findAll(True):
        if tag.name not in validTags:
            tag.hidden = True
        attrs = tag.attrs
        tag.attrs = []
        for attr, val in attrs:
            if attr in validAttrs:
                val = re_scripts.sub('', val) # Remove scripts (vbs & js)
                if attr in urlAttrs:
                    val = urljoin(base_url, val) # Calculate the absolute url
                tag.attrs.append((attr, val))

    return soup.renderContents().decode('utf8')
stef's avatar
stef committed
49

50
def activate(request):
51
52
53
    try:
        v=Violation.objects.get(activationid=request.GET.get('key','asdf'))
    except:
54
55
        messages.add_message(request, messages.INFO, unicode(_('Thank you, this key has been already activated')))
        return HttpResponseRedirect('/') # Redirect after POST
56
57
58
    if v:
        actid = hashlib.sha1(''.join([chr(randint(32, 122)) for x in range(12)])).hexdigest()
        to=[x.email for x in User.objects.filter(groups__name='moderator')]
stef's avatar
stef committed
59
60
61
        details='\n'.join(["%s: %s" % (k.capitalize(), val) for k,val in v.__dict__.items() if not k.startswith('_') and val])
        msg = MIMEText("A new report was submitted. To approve click here: %s/moderate/?key=%s\n\nDetails follow:\n%s\n%s" %
                       (settings.ROOT_URL or 'http://localhost:8001/', actid, details, v.comment_set.get().comment), _charset="utf-8")
stef's avatar
stef committed
62
        msg['Subject'] = 'NNMon submission approval'.encode("Utf-8")
stef's avatar
stef committed
63
        msg['From'] = 'nnmon@respectmynet.eu'
64
65
        msg['To'] = ', '.join(to)
        s = smtplib.SMTP('localhost')
stef's avatar
stef committed
66
        s.sendmail('nnmon@respectmynet.eu', to, msg.as_string())
67
68
69
        s.quit()
        v.activationid=actid
        v.save()
Benjamin Sonntag's avatar
Benjamin Sonntag committed
70
        messages.add_message(request, messages.INFO, _('Thank you for verifying your submission. It will be listed shortly, after we\'ve checked that the report is valid.').encode("Utf-8"))
71
72
    return HttpResponseRedirect('/') # Redirect after POST

73
def moderate(request):
74
75
76
    try:
        v=Violation.objects.get(activationid=request.GET.get('key','asdf'))
    except:
77
78
        messages.add_message(request, messages.INFO, unicode(_('Thank you, this key has been already activated')))
        return HttpResponseRedirect('/') # Redirect after POST
79
80
81
82
    if not v:
        messages.add_message(request, messages.INFO, _('No such key'))
        return HttpResponseRedirect('/') # Redirect after POST
    if request.GET.get('action','')=='approve':
83
        messages.add_message(request, messages.INFO, _('Thank you for approving the <a href="%s">submission</a>.' % v.get_absolute_url))
stef's avatar
stef committed
84

85
        msg = MIMEText(_("Your report has been approved.\nTo see it, please visit: %s%s") % (settings.ROOT_URL or 'http://localhost:8001/', v.get_absolute_url()), _charset="utf-8")
stef's avatar
stef committed
86
87
88
89
90
91
        msg['Subject'] = Header(_('NNMon submission approved').encode("Utf-8"), 'utf-8')
        msg['From'] = 'nnmon@respectmynet.eu'
        msg['To'] = v.comment_set.get().submitter_email
        s = smtplib.SMTP('localhost')
        s.sendmail('nnmon@respectmynet.eu', [msg['To']], msg.as_string())
        s.quit()
92
        if settings.TWITTER_API:
stef's avatar
stef committed
93
            try:
stef's avatar
stef committed
94
                settings.TWITTER_API.PostUpdate("New #NetNeutrality violation reported for %s (%s) %s %s/%s" % (v.operator, v.country, v.contract, settings.ROOT_URL or 'http://localhost:8001/', v.id))
stef's avatar
stef committed
95
96
            except:
                pass
97
98
        v.activationid=''
        v.save()
99
        return redirect(v) # Redirect after POST to violation url
100
101
102
103
104
105
    if request.GET.get('action','')=='delete':
        v.delete()
        messages.add_message(request, messages.INFO, _('Thank you for deleting the submission.'))
        return HttpResponseRedirect('/') # Redirect after POST
    return render_to_response('view.html', { 'v': v, 'key': request.GET.get('key') },context_instance=RequestContext(request))

stef's avatar
stef committed
106
107
108
def confirm(request, id, name=None):
    if name:
        if Confirmation.objects.filter(email=name, violation=id).count()==0:
stef's avatar
stef committed
109
110
            msg=_("Thank you for confirming a case. To finalize your confirmation please validate using your confirmation key.\nYour confirmation key is %s/%s%s")
            actid=sendverifymail('confirm/',name, msg)
111
112
113
            try:
                c=Confirmation(key=actid, email=name, violation=Violation.objects.get(pk=id))
            except:
stef's avatar
stef committed
114
                return HttpResponse(unicode(_('Thank you, this has been already confirmed')))
stef's avatar
stef committed
115
            c.save()
stef's avatar
stef committed
116
        return HttpResponse(unicode(_('Thank you for your confirmation')))
stef's avatar
stef committed
117
118
119
    try:
        c = get_object_or_404(Confirmation, key=id)
    except:
120
121
        messages.add_message(request, messages.INFO, unicode(_("Thank you, this has been already confirmed")))
        return HttpResponseRedirect('/') # Redirect after POST
stef's avatar
stef committed
122
123
    c.key=''
    c.save()
stef's avatar
stef committed
124
    messages.add_message(request, messages.INFO, unicode(_('Thank you for verifying your confirmation')))
125
    return HttpResponseRedirect('/') # Redirect after POST
stef's avatar
stef committed
126

stef's avatar
stef committed
127
def sendverifymail(service,to,msg):
stef's avatar
stef committed
128
    actid = hashlib.sha1(''.join([chr(randint(32, 122)) for x in range(12)])).hexdigest()
stef's avatar
stef committed
129
    msg = MIMEText(msg % (settings.ROOT_URL or 'http://localhost:8001/', service, actid), _charset="utf-8")
stef's avatar
stef committed
130
    msg['Subject'] = Header(_('NNMon submission verification').encode("Utf-8"), 'utf-8')
stef's avatar
stef committed
131
    msg['From'] = 'nnmon@respectmynet.eu'
stef's avatar
stef committed
132
    msg['To'] = Header(to.encode("Utf-8"), 'utf-8')
stef's avatar
stef committed
133
    s = smtplib.SMTP('localhost')
stef's avatar
stef committed
134
    s.sendmail('nnmon@respectmynet.eu', [to], msg.as_string())
stef's avatar
stef committed
135
136
137
    s.quit()
    return actid

stef's avatar
stef committed
138
def add(request):
stef's avatar
stef committed
139
140
141
    if request.method == 'POST':
        form = AddViolation(request.POST)
        if form.is_valid():
stef's avatar
stef committed
142
143
            msg=_("Thank you for submitting a new report. To finalize your submission please confirm using your validation key.\nYour verification key is %s/%s%s\nPlease note that reports are moderated, it might take some time before your report appears online. Thank you for your patience.")
            actid=sendverifymail('activate?key=',form.cleaned_data['email'], msg)
stef's avatar
stef committed
144
145
146
147
148
            v=Violation(
                country = form.cleaned_data['country'],
                operator = form.cleaned_data['operator'],
                contract = form.cleaned_data['contract'],
                resource = form.cleaned_data['resource'],
stef's avatar
stef committed
149
                resource_name = form.cleaned_data['resource_name'],
stef's avatar
stef committed
150
151
152
153
                type = form.cleaned_data['type'],
                media = form.cleaned_data['media'],
                temporary = form.cleaned_data['temporary'],
                contractual = form.cleaned_data['contractual'],
154
                contract_excerpt = sanitizeHtml(form.cleaned_data['contract_excerpt']),
155
156
                loophole = form.cleaned_data['loophole'],
                activationid = actid
stef's avatar
stef committed
157
158
                )
            v.save()
stef's avatar
stef committed
159
160
            #c=Confirmation(key='', email=form.cleaned_data['email'], violation=v)
            #c.save()
stef's avatar
stef committed
161
162
            c = Comment(
                comment=form.cleaned_data['comment'],
163
164
                submitter_email=form.cleaned_data['email'],
                submitter_name=form.cleaned_data['nick'],
stef's avatar
stef committed
165
                consent=form.cleaned_data['consent'],
stef's avatar
stef committed
166
167
168
169
170
                timestamp=datetime.now(),
                violation=v,
                )
            c.save()
            for f in request.FILES.getlist('attachments[]'):
171
                a=Attachment(comment=c, name=f.name, type=f.content_type)
172
173
174
175
176
                m = hashlib.sha256()
                for chunk in f.chunks():
                    m.update(chunk)
                sname=m.hexdigest()
                a.storage.save(sname,f)
stef's avatar
stef committed
177
                a.save()
178

179
            messages.add_message(request, messages.INFO, _('Thank you for submitting this report, you will receive a verification email immediately, if not check your spam folder.'))
stef's avatar
stef committed
180
181
            return HttpResponseRedirect('/') # Redirect after POST
    else:
stef's avatar
stef committed
182
        form = AddViolation()
stef's avatar
stef committed
183

184
    v_list = Violation.objects.filter(activationid='',featuredcase__isnull=False).order_by('id').reverse()[:3]
stef's avatar
stef committed
185
    return render_to_response(
186
        'index.html',
187
188
        { 'form': form,
          'violations': v_list },
stef's avatar
stef committed
189
        context_instance=RequestContext(request))
stef's avatar
stef committed
190

191
192
def ajax(request, country=None, operator=None):
    if not operator:
193
        return HttpResponse(json.dumps(sorted(list(set([x.operator for x in Violation.objects.filter(country=country,activationid='')])))))
194
    else:
195
        return HttpResponse(json.dumps(sorted(list(set([x.contract for x in Violation.objects.filter(country=country,activationid='',operator=operator)])))))
stef's avatar
stef committed
196
197

def index(request):
198
    v_list = Violation.objects.filter(activationid='',featuredcase__isnull=False).order_by('id').reverse()[:3]
stef's avatar
stef committed
199
    form = AddViolation()
stef's avatar
stef committed
200
    reports=sorted([(i['total'],i['id'])
stef's avatar
stef committed
201
                     for i in Violation.objects.values('id').filter(activationid='').exclude(state__in=['closed', 'ooscope', 'duplicate']).annotate(total=Count('confirmation'))],
stef's avatar
stef committed
202
                    reverse=True)
203
204
205
#    countries=sorted([(i['total'],i['country'])
#                      for i in Violation.objects.values('country').filter(activationid='').annotate(total=Count('country'))],
#                     reverse=True)
stef's avatar
stef committed
206
    confirms=sorted([(i['total'],i['country'])
stef's avatar
stef committed
207
                     for i in Violation.objects.values('country').filter(activationid='').exclude(state__in=['closed', 'ooscope', 'duplicate']).annotate(total=Count('confirmation'))],
stef's avatar
stef committed
208
209
                    reverse=True)
    operators=sorted([(i['total'],i['operator'])
stef's avatar
stef committed
210
                     for i in Violation.objects.values('operator').filter(activationid='').exclude(state__in=['closed', 'ooscope', 'duplicate']).annotate(total=Count('confirmation'))],
stef's avatar
stef committed
211
                     reverse=True)
stef's avatar
stef committed
212
213
214
215

    return render_to_response(
        'index.html',
        { 'form': form,
stef's avatar
stef committed
216
217
218
          'stats': [ (_('Total confirmed reports'), len([i for i,z in reports if i>0])),
                     (_('Countries with some confirmed reports'), len([i for i,z in confirms if i>0])),
                     (_('Operators with some confirmed reports'), len([i for i,z in operators if i>0])),
stef's avatar
stef committed
219
                     ],
stef's avatar
stef committed
220
221
          'violations': v_list },
        context_instance=RequestContext(request))
222
223
224
225

def filter_violations(request, country, operator=None):
    if not operator:
        violations = Violation.objects.filter(activationid='', country=country)
226
227
        if not violations.count():
            violations = Violation.objects.filter(activationid='', operator=country)
228
229
    else:
        violations = Violation.objects.filter(activationid='', country=country, operator=operator)
230
    if not request.GET.get('all'):
stef's avatar
stef committed
231
        violations = violations.exclude(state__in=['duplicate', 'closed'])
stef's avatar
stef committed
232
233
234
235
    countries=sorted([(i['total'],i['country'])
                      for i in Violation.objects.values('country').filter(activationid='',country=country).exclude(state__in=['duplicate', 'closed']).annotate(total=Count('country'))],
                     reverse=True)
    countryweights=json.dumps([{'iso2': y, 'w': x} for x, y in countries])
236
237
    return render_to_response('list.html',
                              { "violations": violations,
stef's avatar
stef committed
238
                               "country": country,
239
240
                                "status": STATUS },
                              context_instance=RequestContext(request))
stef's avatar
stef committed
241
242

def list_violations(request):
243
    violations = Violation.objects.filter(activationid='')
244
    if not request.GET.get('all'):
stef's avatar
stef committed
245
        violations = violations.exclude(state__in=['duplicate', 'closed'])
stef's avatar
stef committed
246
    countries=sorted([(i['total'],i['country'])
stef's avatar
stef committed
247
                      for i in Violation.objects.values('country').filter(activationid='').exclude(state__in=['duplicate', 'closed']).annotate(total=Count('country'))],
stef's avatar
stef committed
248
                     reverse=True)
stef's avatar
stef committed
249
    countryweights=json.dumps([{'iso2': y, 'w': x} for x, y in countries])
stef's avatar
stef committed
250
    #countrycolors=json.dumps(dict([(c.lower(),"#ff%02x00" % (w*768/(countries[0][0]+1)%256)) for w,c in countries]))
stef's avatar
stef committed
251
    #confirms=sorted([(i['total'],i['country'])
stef's avatar
stef committed
252
253
    #                 for i in Violation.objects.values('country').filter(activationid='').annotate(total=Count('confirmation'))
    #                 if i['total']>1],
stef's avatar
stef committed
254
255
256
    #                reverse=True)
    return render_to_response('list.html',
                              {"violations": violations,
stef's avatar
stef committed
257
                               "countries": dict([(y,x) for x,y in countries]),
stef's avatar
stef committed
258
                               "countryweights": countryweights,
259
                               "status": STATUS,},
stef's avatar
stef committed
260
261
                               #"confirms": confirms,},
                              context_instance=RequestContext(request))
262
263
264

def view(request,id):
    v = get_object_or_404(Violation, pk=id)
265
266
    if v.activationid:
        raise Http404
stef's avatar
stef committed
267
    return render_to_response('view.html', { 'v': v, },context_instance=RequestContext(request))
stef's avatar
stef committed
268

269
270
271
272
def get_attach(request,id):
    f = get_object_or_404(Attachment, pk=id)
    wrapper = FileWrapper(f.storage)
    response=HttpResponse(wrapper, mimetype=f.type)
273
    response['Content-Disposition'] = 'attachment; filename="%s"' % f.name
274
275
276
    response['Content-Length'] = f.storage.size
    return response

stef's avatar
stef committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
def lookup(request):
    if request.method == 'GET':
        form = SearchViolation(request.GET)
        if form.is_valid():
            v=Violation.objects.filter(
                country = form.cleaned_data['country'],
                operator = form.cleaned_data['operator'],
                contract = form.cleaned_data['contract'],
                media = form.cleaned_data['media'],
                activationid = ''
                )
            res=json.dumps(sorted([(x.id,x.resource_name) for x in v],reverse=True))
            return HttpResponse(res)
    return HttpResponse('')
291
292
293
294
295
296
297
298
299
300
301
302
303
304

def ascsv(request):
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=respectmynet.csv'

    res=[]
    for v in Violation.objects.filter(activationid=''):
        res.append((v.state, v.country, v.operator, v.contract, v.resource, v.resource_name, v.type, v.media, v.temporary, v.contractual, v.contract_excerpt, v.loophole, v.editorial,v.comment_set.get().comment))
    t = loader.get_template('csv.tmpl')
    c = Context({
        'data': res,
    })
    response.write(t.render(c))
    return response
305
306
307
308
309
310
311
312
313
314

from sheet import save_ods
def asods(request):
    response = HttpResponse(mimetype='application/vnd.oasis.opendocument.spreadsheet')
    response['Content-Disposition'] = 'attachment; filename=respectmynet-ec_berec_tm_questionnaire.ods'
    save_ods()
    f=open('/tmp/ec_berec_tm_questionnaire.ods','r')
    response.write(f.read())
    f.close()
    return response