from forms import AddViolation from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext from django.core.files import File from django.conf import settings from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.exceptions import ObjectDoesNotExist from django.contrib import messages from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ from models import Violation, Attachment, Comment, Confirmation from tempfile import mkstemp from datetime import datetime import hashlib, os, re, json, smtplib from random import randint from email.mime.text import MIMEText from urlparse import urljoin from BeautifulSoup import BeautifulSoup, Comment as BComment def sanitizeHtml(value, base_url=None): rjs = r'[\s]*(.{1,7})?'.join(list('javascript:')) rvb = r'[\s]*(.{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') def activate(request): try: v=Violation.objects.get(activationid=request.GET.get('key','asdf')) except: return HttpResponse(_("Thank you, this has been already activated")) 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')] msg = MIMEText(_("A new report was submitted. To approve click here: %s/moderate/?key=%s\n") % (settings.ROOT_URL or 'http://localhost:8001/', actid)) msg['Subject'] = _('NNMon submission approval') msg['From'] = 'nnmon@nnmon.lqdn.fr' msg['To'] = ', '.join(to) s = smtplib.SMTP('localhost') s.sendmail('nnmon@nnmon.lqdn.fr', to, msg.as_string()) s.quit() v.activationid=actid v.save() messages.add_message(request, messages.INFO, _('Thank you for verifying your submission. It will be listed shortly, after verifying it\'s a valid report.')) return HttpResponseRedirect('/') # Redirect after POST def moderate(request): try: v=Violation.objects.get(activationid=request.GET.get('key','asdf')) except: return HttpResponse(_("Thank you, this has been already activated")) if not v: messages.add_message(request, messages.INFO, _('No such key')) return HttpResponseRedirect('/') # Redirect after POST if request.GET.get('action','')=='approve': if settings.TWITTER_API: try: settings.TWITTER_API.PostUpdate(_("New infringement reported for %s (%s) %s") % (v.operator, v.country, v.contract)) except: pass v.activationid='' v.save() messages.add_message(request, messages.INFO, _('Thank you for approving the submission.')) return HttpResponseRedirect('/') # Redirect after POST 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)) def confirm(request, id, name=None): if name: if Confirmation.objects.filter(email=name, violation=id).count()==0: actid=sendverifymail('confirm/',name) try: c=Confirmation(key=actid, email=name, violation=Violation.objects.get(pk=id)) except: return HttpResponse(_("Thank you, this has been already confirmed")) c.save() return HttpResponse(_('