diff --git a/seminar/export.py b/seminar/export.py new file mode 100644 index 00000000..7a15c845 --- /dev/null +++ b/seminar/export.py @@ -0,0 +1,97 @@ +import datetime +from django.shortcuts import get_object_or_404, render +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse +from django.views import generic +from .models import Problem, Cislo, Reseni, VysledkyKCislu, Nastaveni, Rocnik + +class ExportIndexView(generic.base.TemplateView): + template_name = 'seminar/export_index.csv' + content_type = 'text/plain; charset=utf-8' + def get_context_data(self, **kwargs): + context = super(ExportIndexView, self).get_context_data(**kwargs) + context['exports'] = [] + for r in Rocnik.objects.all(): + if r.verejna_cisla(): + url = reverse('seminar.export.rocnik', kwargs={'prvni_rok': r.prvni_rok}) + context['exports'].append(url.split('/')[-1]) + return context + + +class ExportRocnikView(generic.DetailView): + slug_field = 'prvni_rok' + slug_url_kwarg = 'prvni_rok' + model = Rocnik + template_name = 'seminar/export_rocnik.csv' + content_type = 'text/plain; charset=utf-8' + def get_context_data(self, **kwargs): + context = super(ExportRocnikView, self).get_context_data(**kwargs) + + rocnik = context['rocnik'] + cislo = rocnik.posledni_verejne_cislo() + sloupce = ['id', 'name', 'surname', + 'gender', 'born', 'email', 'end-year', + 'street', 'town', 'postcode', 'country', + 'spam-flag', 'spam-date', 'school', 'school-name', + 'points', 'rank',] + radky = [] + + vysledky = VysledkyKCislu.objects.filter(cislo = cislo).select_related("resitel").all() + + posledni_body = 100000 + posledni_poradi = 0 + for vi in range(len(vysledky)): + v = vysledky[vi] + rd = {} + + rd['name'] = v.resitel.jmeno + rd['surname'] = v.resitel.prijmeni + rd['id'] = v.resitel.id + + rd['gender'] = 'M' if v.resitel.pohlavi_muz else 'F' + if v.resitel.datum_narozeni: + rd['born'] = v.resitel.datum_narozeni.strftime("%Y-%m-%d") + else: + rd['born'] = '' + rd['email'] = v.resitel.email + rd['end-year'] = v.resitel.rok_maturity + + # TODO(gavento): Adresa skoly, kdyz preferuje zasilani tam? + rd['street'] = v.resitel.ulice + rd['town'] = v.resitel.mesto + rd['postcode'] = v.resitel.psc + rd['country'] = v.resitel.stat + + if v.resitel.datum_souhlasu_zasilani: + rd['spam-flag'] = 'Y' + rd['spam-date'] = v.resitel.datum_souhlasu_zasilani.strftime("%Y-%m-%d") + else: + rd['spam-flag'] = '' + rd['spam-date'] = '' + + if v.resitel.skola: + rd['school'] = v.resitel.skola.aesop_id + rd['school-name'] = v.resitel.skola.nazev + else: + rd['school'] = '' + rd['school-name'] = 'Skola neni znama' + + if posledni_body > v.body_celkem: + posledni_body = v.body_celkem + posledni_poradi = vi + 1 + rd['rank'] = posledni_poradi + rd['points'] = v.body_celkem + + r = [] + for c in sloupce: + r.append(rd.pop(c)) + assert len(rd) == 0 + radky.append(r) + + context['date'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + context['cislo'] = cislo + context['sloupce'] = sloupce + context['radky'] = radky + return context + + diff --git a/seminar/models.py b/seminar/models.py index 2162c004..5bc6933e 100644 --- a/seminar/models.py +++ b/seminar/models.py @@ -14,6 +14,13 @@ from django.core.urlresolvers import reverse from django_countries.fields import CountryField from solo.models import SingletonModel +class SeminarModel(models.Model): + + class Meta: + abstract = True + + + # # Mělo by být částečně vytaženo z Aesopa # viz https://ovvp.mff.cuni.cz/wiki/aesop/export-skol. @@ -163,7 +170,13 @@ class Rocnik(models.Model): return force_unicode(self.rocnik) def verejna_cisla(self): - return [c for c in self.cisla.all() if c.verejne()] + vc = [c for c in self.cisla.all() if c.verejne()] + vc.sort(key=lambda c: c.cislo) + return vc + + def posledni_verejne_cislo(self): + vc = self.verejna_cisla() + return vc[-1] if vc else None def druhy_rok(self): return self.prvni_rok + 1 diff --git a/seminar/templates/seminar/export_index.csv b/seminar/templates/seminar/export_index.csv new file mode 100644 index 00000000..edb833cf --- /dev/null +++ b/seminar/templates/seminar/export_index.csv @@ -0,0 +1,2 @@ +{% for e in exports %}{{ e }} +{% endfor %} diff --git a/seminar/templates/seminar/export_rocnik.csv b/seminar/templates/seminar/export_rocnik.csv new file mode 100644 index 00000000..1087a432 --- /dev/null +++ b/seminar/templates/seminar/export_rocnik.csv @@ -0,0 +1,11 @@ +version 1 +event MaM.rocnik +year {{ rocnik.prvni_rok }} +date {{ date }} +id-scope main +id-generation 1 +comment MaM-Web export aktivnich resitelu rocniku {{ rocnik }} do cisla {{ cislo.kod }} + +{% for c in sloupce %}{{ c }} {% endfor %} +{% for r in radky %}{% for c in r %}{{ c }} {% endfor %} +{% endfor %} diff --git a/seminar/urls.py b/seminar/urls.py index 62d4bfe9..44aad6af 100644 --- a/seminar/urls.py +++ b/seminar/urls.py @@ -1,9 +1,12 @@ from django.conf.urls import patterns, url -from . import views +from . import views, export urlpatterns = patterns('', url(r'^rocnik/(?P\d+)/$', views.RocnikView.as_view(), name='seminar.rocnik'), url(r'^cislo/(?P\d+)/$', views.CisloView.as_view(), name='seminar.cislo'), url(r'^problem/(?P\d+)/$', views.ProblemView.as_view(), name='seminar.problem'), url(r'^zadani/$', views.AktualniZadaniView, name='seminar.aktualni_zadani'), + + url(r'^aesop-export/mam-rocnik-(?P\d+)\.csv$', export.ExportRocnikView.as_view(), name='seminar.export.rocnik'), + url(r'^aesop-export/index.csv$', export.ExportIndexView.as_view(), name='seminar.export.index'), )