|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from django.shortcuts import get_object_or_404, render
|
|
|
|
from django.views import generic
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpResponseForbidden,HttpResponse
|
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.core.context_processors import csrf
|
|
|
|
|
|
|
|
from .models import Oprava,Komentar,KorekturovanePDF, Organizator
|
|
|
|
from .forms import OpravaForm
|
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import unicodedata
|
|
|
|
|
|
|
|
class KorekturyHelpView(generic.TemplateView):
|
|
|
|
template_name = 'korektury/help.html'
|
|
|
|
|
|
|
|
class KorekturyListView(generic.ListView):
|
|
|
|
model = KorekturovanePDF
|
|
|
|
template_name = 'korektury/seznam.html'
|
|
|
|
|
|
|
|
### Korektury
|
|
|
|
class KorekturyBaseView(generic.TemplateView):
|
|
|
|
model = Oprava
|
|
|
|
template_name = 'korektury/opraf.html'
|
|
|
|
form_class = OpravaForm
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class(request.POST)
|
|
|
|
q = request.POST
|
|
|
|
scroll = q.get('scroll')
|
|
|
|
|
|
|
|
# prirazeni autora podle prihlaseni
|
|
|
|
autor_user = request.user
|
|
|
|
# pokud existuje ucet (user), ale neni to organizator = 403
|
|
|
|
autor = Organizator.objects.filter(user=autor_user).first()
|
|
|
|
if not autor:
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
|
|
if not scroll:
|
|
|
|
scroll = 0
|
|
|
|
|
|
|
|
action = q.get('action')
|
|
|
|
if (action == u''): # Přidej
|
|
|
|
x = int(q.get('x'))
|
|
|
|
y = int(q.get('y'))
|
|
|
|
text = q.get('txt')
|
|
|
|
strana = int(q.get('img-id')[4:])
|
|
|
|
pdf = KorekturovanePDF.objects.get(id=q.get('pdf'))
|
|
|
|
|
|
|
|
op = Oprava(x=x,y=y, autor=autor, text=text, strana=strana,pdf = pdf)
|
|
|
|
op.save()
|
|
|
|
self.send_email_notification_komentar(op, autor, text)
|
|
|
|
elif (action == u'del'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
op.delete()
|
|
|
|
elif (action == u'update'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
text = q.get('txt')
|
|
|
|
op.autor = autor
|
|
|
|
op.text = text
|
|
|
|
op.save()
|
|
|
|
elif (action == u'undone'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
op.status = op.STATUS_K_OPRAVE
|
|
|
|
op.save()
|
|
|
|
elif (action == u'done'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
op.status = op.STATUS_OPRAVENO
|
|
|
|
op.save()
|
|
|
|
elif (action == u'wontfix'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
op.status = op.STATUS_NENI_CHYBA
|
|
|
|
op.save()
|
|
|
|
elif (action == u'comment'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
text = q.get('txt')
|
|
|
|
kom = Komentar(oprava=op,autor=autor,text=text)
|
|
|
|
kom.save()
|
|
|
|
self.send_email_notification_komentar(op, autor, text)
|
|
|
|
elif (action == u'update-comment'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
kom = Komentar.objects.get(id=id)
|
|
|
|
text = q.get('txt')
|
|
|
|
kom.text = text
|
|
|
|
kom.autor = autor
|
|
|
|
kom.save()
|
|
|
|
elif (action == u'del-comment'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
kom = Komentar.objects.get(id=id)
|
|
|
|
kom.delete()
|
|
|
|
elif (action == u'delall'):
|
|
|
|
pdf = KorekturovanePDF.objects.filter(id=q.get('pdf'))
|
|
|
|
checked = q.get('yes')
|
|
|
|
if checked:
|
|
|
|
opravy = Oprava.objects.filter(pdf=pdf)
|
|
|
|
komentare = Komentar.objects.filter(oprava=opravy)
|
|
|
|
opravy.delete()
|
|
|
|
komentare.delete()
|
|
|
|
elif (action == u'set-state'):
|
|
|
|
pdf = KorekturovanePDF.objects.get(id=q.get('pdf'))
|
|
|
|
if (q.get('state') == u'adding'):
|
|
|
|
pdf.status = pdf.STATUS_PRIDAVANI
|
|
|
|
elif (q.get('state') == u'comitting'):
|
|
|
|
pdf.status = pdf.STATUS_ZANASENI
|
|
|
|
elif (q.get('state') == u'deprecated'):
|
|
|
|
pdf.status = pdf.STATUS_ZASTARALE
|
|
|
|
pdf.save()
|
|
|
|
context = self.get_context_data()
|
|
|
|
context['scroll'] = scroll
|
|
|
|
context['autor'] = autor
|
|
|
|
return render(request, 'korektury/opraf.html',context)
|
|
|
|
|
|
|
|
def send_email_notification_komentar(self, oprava, autor, text):
|
|
|
|
''' Rozesle e-mail pri pridani komentare,
|
|
|
|
ktery obsahuje text komentare.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# parametry e-mailu
|
|
|
|
odkaz = "https://mam.mff.cuni.cz/korektury/{}/".format(oprava.pdf.pk)
|
|
|
|
from_email = 'korekturovatko@mam.mff.cuni.cz'
|
|
|
|
subject = u'Nová korektura od {} v {}'.format(autor,
|
|
|
|
oprava.pdf.nazev)
|
|
|
|
text = u"Text komentáře:\n\n{}\n\n=== Konec textu komentáře ===\n\
|
|
|
|
\nodkaz do korekturovátka: {}\n\
|
|
|
|
\nVaše korekturovátko\n".format(text, odkaz)
|
|
|
|
|
|
|
|
# Prijemci e-mailu
|
|
|
|
emails = set()
|
|
|
|
# e-mail autora korektury
|
|
|
|
email = oprava.autor.user.email
|
|
|
|
if email:
|
|
|
|
emails.add(email)
|
|
|
|
|
|
|
|
# nalezeni e-mailu na autory komentaru
|
|
|
|
for komentar in oprava.komentar_set.all():
|
|
|
|
email_komentujiciho = komentar.autor.user.email
|
|
|
|
if email_komentujiciho:
|
|
|
|
emails.add(email_komentujiciho)
|
|
|
|
|
|
|
|
# zodpovedny org
|
|
|
|
if oprava.pdf.org:
|
|
|
|
email_zobpovedny = oprava.pdf.org.user.email
|
|
|
|
if email_zobpovedny:
|
|
|
|
emails.add(email_zobpovedny)
|
|
|
|
|
|
|
|
# odstran e-mail autora opravy
|
|
|
|
email = autor.user.email
|
|
|
|
if email:
|
|
|
|
emails.discard(email)
|
|
|
|
|
|
|
|
if not settings.SEND_EMAIL_NOTIFICATIONS:
|
|
|
|
print "Poslal bych upozornění na tyto adresy: ", " ".join(emails)
|
|
|
|
return
|
|
|
|
|
|
|
|
send_mail(subject, text, from_email, list(emails))
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(KorekturyBaseView,self).get_context_data(**kwargs)
|
|
|
|
pdf = get_object_or_404(KorekturovanePDF, id=self.kwargs['pdf'])
|
|
|
|
context['pdf'] = pdf
|
|
|
|
context['img_name'] = os.path.split(pdf.pdf.path)[1].split('.')[0]
|
|
|
|
context['img_path'] = settings.KOREKTURY_IMG_DIR
|
|
|
|
context['img_indexes'] = range(pdf.stran)
|
|
|
|
context['form_oprava'] = OpravaForm()
|
|
|
|
opravy = Oprava.objects.filter(pdf=self.kwargs['pdf'])
|
|
|
|
zasluhy = {}
|
|
|
|
for o in opravy:
|
|
|
|
if o.autor in zasluhy:
|
|
|
|
zasluhy[o.autor]+=1
|
|
|
|
else:
|
|
|
|
zasluhy[o.autor]=1
|
|
|
|
o.komentare = o.komentar_set.all()
|
|
|
|
for k in o.komentare:
|
|
|
|
if k.autor in zasluhy:
|
|
|
|
zasluhy[k.autor]+=1
|
|
|
|
else:
|
|
|
|
zasluhy[k.autor]=1
|
|
|
|
zasluhy = [{'autor':jmeno, 'pocet':pocet} for (jmeno,pocet) in zasluhy.items()]
|
|
|
|
zasluhy.sort(key=lambda z:z['pocet'],reverse=True)
|
|
|
|
|
|
|
|
strany = set(o.strana for o in opravy)
|
|
|
|
opravy_na_stranu = [{'strana': s, 'op_id': opravy.filter(strana=s)} for s in strany]
|
|
|
|
context['opravy_strany'] = opravy_na_stranu
|
|
|
|
|
|
|
|
context['opravy'] = opravy
|
|
|
|
context['zasluhy'] = zasluhy
|
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self,form):
|
|
|
|
return super(KorekturyView,self).form_valid(form)
|
|
|
|
|
|
|
|
### Korektury
|
|
|
|
class KorekturyView(generic.TemplateView):
|
|
|
|
model = Oprava
|
|
|
|
template_name = 'korektury/opraf.html'
|
|
|
|
form_class = OpravaForm
|
|
|
|
|
|
|
|
def get(self,request,*args,**kwargs):
|
|
|
|
pdf = get_object_or_404(KorekturovanePDF, id=self.kwargs['pdf'])
|
|
|
|
context = {}
|
|
|
|
context['pdf'] = pdf.id
|
|
|
|
context['img_name'] = os.path.split(pdf.pdf.path)[1].split('.')[0]
|
|
|
|
context['img_path'] = settings.KOREKTURY_IMG_DIR
|
|
|
|
context['img_indexes'] = range(pdf.stran)
|
|
|
|
|
|
|
|
opravy = Oprava.objects.filter(pdf=self.kwargs['pdf'])
|
|
|
|
strany = set(o.strana for o in opravy)
|
|
|
|
opravy_na_stranu = [{'strana': s, 'op_id': map(lambda o:o.id,opravy.filter(strana=s))} for s in strany]
|
|
|
|
context['opravy_strany'] = opravy_na_stranu
|
|
|
|
|
|
|
|
opravy = list(opravy.values())
|
|
|
|
for o in opravy:
|
|
|
|
o["autor"] = str(Organizator.objects.filter(id=(o[u"autor_id"]))[0])
|
|
|
|
context['opravy'] = opravy
|
|
|
|
return HttpResponse(json.dumps(context),content_type="application/json")
|
|
|
|
|
|
|
|
|
|
|
|
class KorekturyRequestView(generic.TemplateView):
|
|
|
|
model = Oprava
|
|
|
|
template_name = 'korektury/opraf.html'
|
|
|
|
form_class = OpravaForm
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class(request.POST)
|
|
|
|
q = request.POST
|
|
|
|
scroll = q.get('scroll')
|
|
|
|
|
|
|
|
# prirazeni autora podle prihlaseni
|
|
|
|
autor_user = request.user
|
|
|
|
# pokud existuje ucet (user), ale neni to organizator = 403
|
|
|
|
autor = Organizator.objects.filter(user=autor_user).first()
|
|
|
|
if not autor:
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
|
|
if not scroll:
|
|
|
|
scroll = 0
|
|
|
|
|
|
|
|
action = q.get('action')
|
|
|
|
if (action == u''): # Přidej
|
|
|
|
x = int(q.get('x'))
|
|
|
|
y = int(q.get('y'))
|
|
|
|
text = q.get('txt')
|
|
|
|
strana = int(q.get('img-id')[4:])
|
|
|
|
pdf = KorekturovanePDF.objects.get(id=q.get('pdf'))
|
|
|
|
|
|
|
|
op = Oprava(x=x,y=y, autor=autor, text=text, strana=strana,pdf = pdf)
|
|
|
|
op.save()
|
|
|
|
self.send_email_notification_komentar(op, autor, text)
|
|
|
|
elif (action == u'del'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
op.delete()
|
|
|
|
elif (action == u'update'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
text = q.get('txt')
|
|
|
|
op.autor = autor
|
|
|
|
op.text = text
|
|
|
|
op.save()
|
|
|
|
elif (action == u'undone'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
op.status = op.STATUS_K_OPRAVE
|
|
|
|
op.save()
|
|
|
|
elif (action == u'done'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
op.status = op.STATUS_OPRAVENO
|
|
|
|
op.save()
|
|
|
|
elif (action == u'wontfix'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
op.status = op.STATUS_NENI_CHYBA
|
|
|
|
op.save()
|
|
|
|
elif (action == u'comment'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
op = Oprava.objects.get(id=id)
|
|
|
|
text = q.get('txt')
|
|
|
|
kom = Komentar(oprava=op,autor=autor,text=text)
|
|
|
|
kom.save()
|
|
|
|
self.send_email_notification_komentar(op, autor, text)
|
|
|
|
elif (action == u'update-comment'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
kom = Komentar.objects.get(id=id)
|
|
|
|
text = q.get('txt')
|
|
|
|
kom.text = text
|
|
|
|
kom.autor = autor
|
|
|
|
kom.save()
|
|
|
|
elif (action == u'del-comment'):
|
|
|
|
id = int(q.get('id'))
|
|
|
|
kom = Komentar.objects.get(id=id)
|
|
|
|
kom.delete()
|
|
|
|
context = self.get_context_data()
|
|
|
|
context['scroll'] = scroll
|
|
|
|
context['autor'] = autor
|
|
|
|
return render(request, 'korektury/opraf.html',context)
|
|
|
|
|
|
|
|
def send_email_notification_komentar(self, oprava, autor, text):
|
|
|
|
''' Rozesle e-mail pri pridani komentare,
|
|
|
|
ktery obsahuje text komentare.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# parametry e-mailu
|
|
|
|
odkaz = "https://mam.mff.cuni.cz/korektury/{}/".format(oprava.pdf.pk)
|
|
|
|
from_email = 'korekturovatko@mam.mff.cuni.cz'
|
|
|
|
subject = u'Nová korektura od {} v {}'.format(autor,
|
|
|
|
oprava.pdf.nazev)
|
|
|
|
text = u"Text komentáře:\n\n{}\n\n=== Konec textu komentáře ===\n\
|
|
|
|
\nodkaz do korekturovátka: {}\n\
|
|
|
|
\nVaše korekturovátko\n".format(text, odkaz)
|
|
|
|
|
|
|
|
# Prijemci e-mailu
|
|
|
|
emails = set()
|
|
|
|
# e-mail autora korektury
|
|
|
|
email = oprava.autor.user.email
|
|
|
|
if email:
|
|
|
|
emails.add(email)
|
|
|
|
|
|
|
|
# nalezeni e-mailu na autory komentaru
|
|
|
|
for komentar in oprava.komentar_set.all():
|
|
|
|
email_komentujiciho = komentar.autor.user.email
|
|
|
|
if email_komentujiciho:
|
|
|
|
emails.add(email_komentujiciho)
|
|
|
|
|
|
|
|
# zodpovedny org
|
|
|
|
if oprava.pdf.org:
|
|
|
|
email_zobpovedny = oprava.pdf.org.user.email
|
|
|
|
if email_zobpovedny:
|
|
|
|
emails.add(email_zobpovedny)
|
|
|
|
|
|
|
|
# odstran e-mail autora opravy
|
|
|
|
email = autor.user.email
|
|
|
|
if email:
|
|
|
|
emails.discard(email)
|
|
|
|
|
|
|
|
if not settings.SEND_EMAIL_NOTIFICATIONS:
|
|
|
|
print "Poslal bych upozornění na tyto adresy: ", " ".join(emails)
|
|
|
|
return
|
|
|
|
|
|
|
|
send_mail(subject, text, from_email, list(emails))
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(KorekturyRequestView,self).get_context_data(**kwargs)
|
|
|
|
pdf = get_object_or_404(KorekturovanePDF, id=self.kwargs['pdf'])
|
|
|
|
context['pdf'] = pdf
|
|
|
|
context['img_name'] = os.path.split(pdf.pdf.path)[1].split('.')[0]
|
|
|
|
context['img_path'] = settings.KOREKTURY_IMG_DIR
|
|
|
|
context['img_indexes'] = range(pdf.stran)
|
|
|
|
context['form_oprava'] = OpravaForm()
|
|
|
|
opravy = Oprava.objects.filter(pdf=self.kwargs['pdf'])
|
|
|
|
zasluhy = {}
|
|
|
|
for o in opravy:
|
|
|
|
if o.autor in zasluhy:
|
|
|
|
zasluhy[o.autor]+=1
|
|
|
|
else:
|
|
|
|
zasluhy[o.autor]=1
|
|
|
|
o.komentare = o.komentar_set.all()
|
|
|
|
for k in o.komentare:
|
|
|
|
if k.autor in zasluhy:
|
|
|
|
zasluhy[k.autor]+=1
|
|
|
|
else:
|
|
|
|
zasluhy[k.autor]=1
|
|
|
|
zasluhy = [{'autor':jmeno, 'pocet':pocet} for (jmeno,pocet) in zasluhy.items()]
|
|
|
|
zasluhy.sort(key=lambda z:z['pocet'],reverse=True)
|
|
|
|
|
|
|
|
strany = set(o.strana for o in opravy)
|
|
|
|
opravy_na_stranu = [{'strana': s, 'op_id': opravy.filter(strana=s)} for s in strany]
|
|
|
|
context['opravy_strany'] = opravy_na_stranu
|
|
|
|
|
|
|
|
context['opravy'] = opravy
|
|
|
|
context['zasluhy'] = zasluhy
|
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self,form):
|
|
|
|
return super(KorekturyView,self).form_valid(form)
|
|
|
|
|