views.py 15 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.core.mail import send_mail
11
from django.contrib import messages
12
from django.contrib.auth.models import User
stef's avatar
stef committed
13
from django.utils.translation import ugettext_lazy as _
stef's avatar
stef committed
14
from django.db.models import Count
stef's avatar
stef committed
15
from models import Violation, Attachment, Comment, Confirmation, COUNTRIES, STATUS, Operator
stef's avatar
stef committed
16 17
from tempfile import mkstemp
from datetime import datetime
18
import hashlib, os, re, json
19 20
from random import randint
from email.mime.text import MIMEText
stef's avatar
stef committed
21
from email.header import Header
22 23
from urlparse import urljoin
from BeautifulSoup import BeautifulSoup, Comment as BComment
stef's avatar
stef committed
24
from operator import itemgetter
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

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
50

51
def activate(request):
52 53 54
    try:
        v=Violation.objects.get(activationid=request.GET.get('key','asdf'))
    except:
55 56
        messages.add_message(request, messages.INFO, unicode(_('Thank you, this key has been already activated')))
        return HttpResponseRedirect('/') # Redirect after POST
57 58 59
    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
60
        details='\n'.join(["%s: %s" % (k.capitalize(), val) for k,val in v.__dict__.items() if not k.startswith('_') and val])
61 62 63 64 65 66 67 68 69

        msg = {'from': 'nnmon@respectmynet.eu',
               'to': to,
               'subject': 'NNMon submission approval'.encode("Utf-8"),
               'body': "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)
               }
        send_mail(msg['subject'], msg['body'], msg['from'], msg['to'], fail_silently=False)

70 71
        v.activationid=actid
        v.save()
Benjamin Sonntag's avatar
Benjamin Sonntag committed
72
        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"))
73 74
    return HttpResponseRedirect('/') # Redirect after POST

75
def moderate(request):
76 77 78
    try:
        v=Violation.objects.get(activationid=request.GET.get('key','asdf'))
    except:
79 80
        messages.add_message(request, messages.INFO, unicode(_('Thank you, this key has been already activated')))
        return HttpResponseRedirect('/') # Redirect after POST
81 82 83 84
    if not v:
        messages.add_message(request, messages.INFO, _('No such key'))
        return HttpResponseRedirect('/') # Redirect after POST
    if request.GET.get('action','')=='approve':
stef's avatar
stef committed
85
        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
86

87 88 89
        msg = {'from': 'nnmon@respectmynet.eu',
               'to': [v.comment_set.get().submitter_email],
               'subject': _('NNMon submission approved').encode("Utf-8"),
stef's avatar
stef committed
90
               'body': _("Your report has been approved.\nTo see it, please visit: %s/%s") %
stef's avatar
stef committed
91
                      (settings.ROOT_URL or 'http://localhost:8001/', v.get_absolute_url())
92 93 94
               }
        send_mail(msg['subject'], msg['body'], msg['from'], msg['to'], fail_silently=False)

95
        if settings.TWITTER_API:
stef's avatar
stef committed
96
            try:
stef's avatar
stef committed
97
                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
98 99
            except:
                pass
100 101
        v.activationid=''
        v.save()
102
        return redirect(v) # Redirect after POST to violation url
103 104 105 106 107 108
    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
109 110 111
def confirm(request, id, name=None):
    if name:
        if Confirmation.objects.filter(email=name, violation=id).count()==0:
stef's avatar
stef committed
112 113
            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)
114 115 116
            try:
                c=Confirmation(key=actid, email=name, violation=Violation.objects.get(pk=id))
            except:
stef's avatar
stef committed
117
                return HttpResponse(unicode(_('Thank you, this has been already confirmed')))
stef's avatar
stef committed
118
            c.save()
stef's avatar
stef committed
119
        return HttpResponse(unicode(_('Thank you for your confirmation')))
stef's avatar
stef committed
120 121 122
    try:
        c = get_object_or_404(Confirmation, key=id)
    except:
123 124
        messages.add_message(request, messages.INFO, unicode(_("Thank you, this has been already confirmed")))
        return HttpResponseRedirect('/') # Redirect after POST
stef's avatar
stef committed
125 126
    c.key=''
    c.save()
stef's avatar
stef committed
127
    messages.add_message(request, messages.INFO, unicode(_('Thank you for verifying your confirmation')))
128
    return HttpResponseRedirect('/') # Redirect after POST
stef's avatar
stef committed
129

stef's avatar
stef committed
130
def sendverifymail(service,to,body):
stef's avatar
stef committed
131
    actid = hashlib.sha1(''.join([chr(randint(32, 122)) for x in range(12)])).hexdigest()
132 133 134
    msg = {'from': 'nnmon@respectmynet.eu',
           'to': [to.encode("Utf-8")],
           'subject': _('NNMon submission verification').encode("Utf-8"),
stef's avatar
stef committed
135
           'body': body % (settings.ROOT_URL or 'http://localhost:8001/', service, actid),
136 137 138
           }
    send_mail(msg['subject'], msg['body'], msg['from'], msg['to'], fail_silently=False)

stef's avatar
stef committed
139 140
    return actid

stef's avatar
stef committed
141
def add(request):
stef's avatar
stef committed
142 143 144
    if request.method == 'POST':
        form = AddViolation(request.POST)
        if form.is_valid():
stef's avatar
stef committed
145 146
            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)
147
            operator, created = Operator.objects.get_or_create(name=form.cleaned_data['operator'])
stef's avatar
stef committed
148 149
            v=Violation(
                country = form.cleaned_data['country'],
150
                operator_ref = operator,
stef's avatar
stef committed
151 152
                contract = form.cleaned_data['contract'],
                resource = form.cleaned_data['resource'],
stef's avatar
stef committed
153
                resource_name = form.cleaned_data['resource_name'],
stef's avatar
stef committed
154 155 156 157
                type = form.cleaned_data['type'],
                media = form.cleaned_data['media'],
                temporary = form.cleaned_data['temporary'],
                contractual = form.cleaned_data['contractual'],
158
                contract_excerpt = sanitizeHtml(form.cleaned_data['contract_excerpt']),
159 160
                loophole = form.cleaned_data['loophole'],
                activationid = actid
stef's avatar
stef committed
161 162
                )
            v.save()
stef's avatar
stef committed
163 164
            #c=Confirmation(key='', email=form.cleaned_data['email'], violation=v)
            #c.save()
stef's avatar
stef committed
165 166
            c = Comment(
                comment=form.cleaned_data['comment'],
167 168
                submitter_email=form.cleaned_data['email'],
                submitter_name=form.cleaned_data['nick'],
stef's avatar
stef committed
169
                consent=form.cleaned_data['consent'],
stef's avatar
stef committed
170 171 172 173 174
                timestamp=datetime.now(),
                violation=v,
                )
            c.save()
            for f in request.FILES.getlist('attachments[]'):
175
                a=Attachment(comment=c, name=f.name, type=f.content_type)
176 177 178 179 180
                m = hashlib.sha256()
                for chunk in f.chunks():
                    m.update(chunk)
                sname=m.hexdigest()
                a.storage.save(sname,f)
stef's avatar
stef committed
181
                a.save()
182

183
            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
184 185
            return HttpResponseRedirect('/') # Redirect after POST
    else:
stef's avatar
stef committed
186
        form = AddViolation()
stef's avatar
stef committed
187

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

195 196 197 198 199 200
# XXX obsoleted by API
#def ajax(request, country=None, operator=None):
#    if not operator:
#        return HttpResponse(json.dumps(sorted(list(set([x.operator for x in Violation.objects.filter(country=country,activationid='')])))))
#    else:
#        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
201 202

def index(request):
203
    v_list = Violation.objects.filter(activationid='',featuredcase__isnull=False).order_by('id').reverse()[:3]
stef's avatar
stef committed
204
    form = AddViolation()
stef's avatar
stef committed
205
    reports=sorted([(i['total'],i['id'])
stef's avatar
stef committed
206
                     for i in Violation.objects.values('id').filter(activationid='').exclude(state__in=['closed', 'ooscope', 'duplicate']).annotate(total=Count('confirmation'))],
stef's avatar
stef committed
207
                    reverse=True)
208 209 210
#    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
211
    confirms=sorted([(i['total'],i['country'])
stef's avatar
stef committed
212
                     for i in Violation.objects.values('country').filter(activationid='').exclude(state__in=['closed', 'ooscope', 'duplicate']).annotate(total=Count('confirmation'))],
stef's avatar
stef committed
213
                    reverse=True)
214 215
    operators=sorted([(i['total'],i['operator_ref__name'])
                     for i in Violation.objects.values('operator_ref__name').filter(activationid='').exclude(state__in=['closed', 'ooscope', 'duplicate']).annotate(total=Count('confirmation'))],
stef's avatar
stef committed
216
                     reverse=True)
stef's avatar
stef committed
217 218 219 220

    return render_to_response(
        'index.html',
        { 'form': form,
stef's avatar
stef committed
221 222 223
          '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
224
                     ],
stef's avatar
stef committed
225 226
          'violations': v_list },
        context_instance=RequestContext(request))
227 228 229 230

def filter_violations(request, country, operator=None):
    if not operator:
        violations = Violation.objects.filter(activationid='', country=country)
231
        if not violations.count():
232
            violations = Violation.objects.filter(activationid='', operator_ref__name=country)
233
    else:
234
        violations = Violation.objects.filter(activationid='', country=country, operator_ref__name=operator)
235
    if not request.GET.get('all'):
stef's avatar
stef committed
236
        violations = violations.exclude(state__in=['duplicate', 'closed'])
237 238
    return render_to_response('list.html',
                              { "violations": violations,
239
                                "country": country,
240 241
                                "status": STATUS },
                              context_instance=RequestContext(request))
stef's avatar
stef committed
242 243

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

def view(request,id):
    v = get_object_or_404(Violation, pk=id)
261 262
    if v.activationid:
        raise Http404
stef's avatar
stef committed
263
    return render_to_response('view.html', { 'v': v, },context_instance=RequestContext(request))
stef's avatar
stef committed
264

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

stef's avatar
stef committed
273 274 275 276
def lookup(request):
    if request.method == 'GET':
        form = SearchViolation(request.GET)
        if form.is_valid():
277 278
            v=form.search()
            res=json.dumps(sorted([(x.object.id,x.object.resource_name) for x in v],reverse=True))
stef's avatar
stef committed
279 280
            return HttpResponse(res)
    return HttpResponse('')
281 282 283 284 285 286 287 288 289 290 291 292 293 294

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
295 296 297 298 299 300 301 302 303 304

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