from django.shortcuts import get_object_or_404, render from django.views import generic from django.conf import settings from django.http import HttpResponseForbidden from django.db.models import Count,Q from .utils import send_email_notification_komentar from .models import Oprava,Komentar,KorekturovanePDF, Organizator class KorekturyListView(generic.ListView): model = KorekturovanePDF queryset = KorekturovanePDF.objects.annotate( k_oprave_cnt=Count('oprava',distinct=True,filter=Q(oprava__status=Oprava.STATUS.K_OPRAVE)), opraveno_cnt=Count('oprava',distinct=True,filter=Q(oprava__status=Oprava.STATUS.OPRAVENO)), neni_chyba_cnt=Count('oprava',distinct=True,filter=Q(oprava__status=Oprava.STATUS.NENI_CHYBA)), k_zaneseni_cnt=Count('oprava',distinct=True,filter=Q(oprava__status=Oprava.STATUS.K_ZANESENI)), ) template_name = 'korektury/seznam.html' class KorekturyAktualniListView(KorekturyListView): def get_queryset(self, *args, **kwargs): queryset=super().get_queryset() queryset=queryset.exclude(status="zastarale") return queryset def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['selected'] = 'aktualni' return context class KorekturyZastaraleListView(KorekturyListView): def get_queryset(self, *args, **kwargs): queryset=super().get_queryset() queryset=queryset.filter(status="zastarale").order_by("-cas") return queryset def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['selected'] = 'zastarale' return context class KorekturySeskupeneListView(KorekturyAktualniListView): template_name = 'korektury/seskupeny_seznam.html' # {% regroup %} potřebuje dostat správně setříděné názvy, aby nedělal duplikáty def get_queryset(self, *args, **kwargs): qs = super().get_queryset(**kwargs) return reversed(sorted(qs, key=lambda it: it.cislo_a_tema)) ### Korektury class KorekturyView(generic.TemplateView): model = Oprava template_name = 'korektury/korekturovatko/htmlstrana.html' def setup(self, request, *args, **kwargs): super().setup(request, *args, **kwargs) self.pdf_id = self.kwargs["pdf"] self.pdf = get_object_or_404(KorekturovanePDF, id=self.pdf_id) def post(self, request, *args, **kwargs): 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(osoba__user=autor_user).first() if not autor: return HttpResponseForbidden() if not scroll: scroll = 0 action = q.get('action') if (action == ''): # Přidej x = int(q.get('x')) y = int(q.get('y')) text = q.get('txt') strana = int(q.get('img-id')[4:]) op = Oprava(x=x,y=y, strana=strana, pdf=self.pdf) op.save() kom = Komentar(oprava=op,autor=autor,text=text) kom.save() send_email_notification_komentar(op, autor, request) elif (action == 'del'): id = int(q.get('id')) op = Oprava.objects.get(id=id) for k in Komentar.objects.filter(oprava=op): k.delete() op.delete() elif action in Oprava.STATUS.values: id = int(q.get('id')) op = Oprava.objects.get(id=id) op.status = action op.save() elif (action == '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() send_email_notification_komentar(op, autor, request) elif (action == '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 == 'del-comment'): id = int(q.get('id')) kom = Komentar.objects.get(id=id) kom.delete() elif (action == 'set-state'): status = q.get('state') assert status in KorekturovanePDF.STATUS.values self.pdf.status = status self.pdf.save() context = self.get_context_data() context['scroll'] = scroll context['autor'] = autor return render(request, 'korektury/korekturovatko/htmlstrana.html', context) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['pdf'] = self.pdf context['img_prefix'] = self.pdf.get_prefix() context['img_path'] = settings.KOREKTURY_IMG_DIR context['img_indexes'] = range(self.pdf.stran) opravy = Oprava.objects.filter(pdf=self.pdf_id) zasluhy = {} for o in opravy: 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['k_oprave_cnt'] = opravy.filter(status='k_oprave').count() context['opraveno_cnt'] = opravy.filter(status='opraveno').count() context['neni_chyba_cnt'] = opravy.filter(status='neni_chyba').count() context['k_zaneseni_cnt'] = opravy.filter(status='k_zaneseni').count() context['opravy'] = opravy context['zasluhy'] = zasluhy return context