Prepracovani exportu, doplneni exportu soustredek
This commit is contained in:
parent
584108444f
commit
944afc5974
6 changed files with 114 additions and 85 deletions
|
@ -1,80 +1,92 @@
|
|||
import datetime
|
||||
import datetime, django
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.http import HttpResponse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.views import generic
|
||||
from .models import Problem, Cislo, Reseni, VysledkyKCislu, Nastaveni, Rocnik
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
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'] = []
|
||||
from .models import Problem, Cislo, Reseni, VysledkyKCislu, Nastaveni, Rocnik, Soustredeni
|
||||
from .ovvpfile import OvvpFile
|
||||
|
||||
class ExportIndexView(generic.View):
|
||||
def get(self, request):
|
||||
|
||||
ls = []
|
||||
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
|
||||
ls.append(url.split('/')[-1])
|
||||
for s in Soustredeni.objects.all():
|
||||
if s.ucastnici.count() >= 1:
|
||||
url = reverse('seminar_export_sous', kwargs={'datum_zacatku': s.datum_zacatku.isoformat()})
|
||||
ls.append(url.split('/')[-1])
|
||||
|
||||
return HttpResponse('\n'.join(ls) + '\n', content_type='text/plain; charset=utf-8')
|
||||
|
||||
|
||||
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)
|
||||
def default_ovvpfile(event, rocnik):
|
||||
of = OvvpFile()
|
||||
of.headers['version'] = '1'
|
||||
of.headers['event'] = event
|
||||
of.headers['year'] = force_text(rocnik.prvni_rok)
|
||||
of.headers['date'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
of.headers['id-scope'] = 'mam'
|
||||
of.headers['id-generation'] = '1'
|
||||
return of
|
||||
|
||||
rocnik = context['rocnik']
|
||||
|
||||
class ExportSousView(generic.View):
|
||||
|
||||
def get(self, request, datum_zacatku=None):
|
||||
try:
|
||||
dz = django.utils.dateparse.parse_date(datum_zacatku)
|
||||
except:
|
||||
dz = None
|
||||
if dz is None:
|
||||
raise django.http.Http404()
|
||||
|
||||
s = get_object_or_404(Soustredeni, datum_zacatku=dz)
|
||||
|
||||
of = default_ovvpfile('MaM.sous', s.rocnik)
|
||||
of.headers['x-event-begin'] = s.datum_zacatku.isoformat()
|
||||
of.headers['x-event-end'] = s.datum_konce.isoformat()
|
||||
of.headers['x-event-location'] = s.misto
|
||||
of.headers['comment'] = u'MaM-Web export ucastniku soustredeni v {x-event-location} od {x-event-begin} do {x-event-end}'.format(**of.headers)
|
||||
of.columns = ['id', 'name', 'surname', 'gender', 'email', 'end-year', 'school', 'school-name']
|
||||
|
||||
for u in s.ucastnici.all():
|
||||
of.rows.append(u.export_row())
|
||||
|
||||
return of.to_HttpResponse()
|
||||
|
||||
|
||||
class ExportRocnikView(generic.View):
|
||||
|
||||
def get(self, request, prvni_rok=None):
|
||||
try:
|
||||
pr = int(prvni_rok)
|
||||
except:
|
||||
pr = None
|
||||
if pr is None:
|
||||
raise django.http.Http404()
|
||||
|
||||
rocnik = get_object_or_404(Rocnik, prvni_rok=pr)
|
||||
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").order_by('-body_celkem').all()
|
||||
|
||||
of = default_ovvpfile('MaM.rocnik', rocnik)
|
||||
of.headers['comment'] = u'MaM-Web export aktivnich resitelu rocniku {rocnik} do cisla {cislo}'.format(
|
||||
rocnik=rocnik, cislo=cislo)
|
||||
of.columns = ['id', 'name', 'surname', 'gender', 'born', 'email', 'end-year',
|
||||
'street', 'town', 'postcode', 'country', 'spam-flag', 'spam-date',
|
||||
'school', 'school-name', 'points', 'rank',]
|
||||
|
||||
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'] = str(v.resitel.skola)
|
||||
else:
|
||||
rd['school'] = ''
|
||||
rd['school-name'] = 'Skola neni znama'
|
||||
rd = v.resitel.export_row()
|
||||
|
||||
if posledni_body > v.body_celkem:
|
||||
posledni_body = v.body_celkem
|
||||
|
@ -82,16 +94,8 @@ class ExportRocnikView(generic.DetailView):
|
|||
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
|
||||
of.rows.append(rd)
|
||||
|
||||
return of.to_HttpResponse()
|
||||
|
||||
|
||||
|
|
|
@ -186,6 +186,30 @@ class Resitel(SeminarModelBase):
|
|||
def __str__(self):
|
||||
return force_unicode(self.plne_jmeno())
|
||||
|
||||
def export_row(self):
|
||||
"Slovnik pro pouziti v OVVP exportu"
|
||||
return {
|
||||
'id': self.id,
|
||||
'name': self.jmeno,
|
||||
'surname': self.prijmeni,
|
||||
'gender': 'M' if self.pohlavi_muz else 'F',
|
||||
'born': self.datum_narozeni.isoformat() if self.datum_narozeni else '',
|
||||
'email': self.email,
|
||||
'end-year': self.rok_maturity,
|
||||
|
||||
# TODO(gavento): Adresa skoly, kdyz preferuje zasilani tam?
|
||||
'street': self.ulice,
|
||||
'town': self.mesto,
|
||||
'postcode': self.psc,
|
||||
'country': self.stat,
|
||||
|
||||
'spam-flag': 'Y' if self.datum_souhlasu_zasilani else '',
|
||||
'spam-date': self.datum_souhlasu_zasilani.isoformat() if self.datum_souhlasu_zasilani else '',
|
||||
|
||||
'school': self.skola.aesop_id if self.skola else '',
|
||||
'school-name': str(self.skola) if self.skola else 'Skola neni znama',
|
||||
}
|
||||
|
||||
|
||||
@reversion.register(ignore_duplicate_revisions=True)
|
||||
@python_2_unicode_compatible
|
||||
|
|
|
@ -1,9 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
try:
|
||||
from django.http import HttpResponse
|
||||
from django.utils.encoding import force_text
|
||||
except:
|
||||
force_text = str
|
||||
|
||||
|
||||
class OvvpFile(object):
|
||||
def __init__(self):
|
||||
# { header: value, ... }
|
||||
self.headers = {}
|
||||
# [ 'column-name', ... ]
|
||||
self.columns = []
|
||||
# [ { column: value, ...}, ...]
|
||||
self.rows = []
|
||||
|
||||
def to_lines(self):
|
||||
|
@ -15,11 +25,14 @@ class OvvpFile(object):
|
|||
yield '\t'.join([c for c in self.columns]) + '\n'
|
||||
# rows
|
||||
for r in self.rows:
|
||||
yield '\t'.join([r[c] for c in self.columns]) + '\n'
|
||||
yield '\t'.join([force_text(r[c]) for c in self.columns]) + '\n'
|
||||
|
||||
def to_string(self):
|
||||
return ''.join(self.to_lines())
|
||||
|
||||
def to_HttpResponse(self):
|
||||
return HttpResponse(self.to_string(), content_type='text/plain; charset=utf-8')
|
||||
|
||||
def parse_from(self, source, with_headers=True):
|
||||
"Parse data from file, string or line iterator, overwriting self"
|
||||
if isinstance(source, str) or isinstance(source, unicode):
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
{% for e in exports %}{{ e }}
|
||||
{% endfor %}
|
|
|
@ -1,11 +0,0 @@
|
|||
version 1
|
||||
event MaM.rocnik
|
||||
year {{ rocnik.prvni_rok }}
|
||||
date {{ date }}
|
||||
id-scope mam
|
||||
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 %}
|
Can't render this file because it has a wrong number of fields in line 11.
|
|
@ -16,5 +16,6 @@ urlpatterns = patterns('',
|
|||
url(r'^zadani/aktualni/$', views.AktualniZadaniView, name='seminar_aktualni_zadani'),
|
||||
|
||||
url(r'^aesop-export/mam-rocnik-(?P<prvni_rok>\d+)\.csv$', export.ExportRocnikView.as_view(), name='seminar_export_rocnik'),
|
||||
url(r'^aesop-export/mam-sous-(?P<datum_zacatku>[\d-]+)\.csv$', export.ExportSousView.as_view(), name='seminar_export_sous'),
|
||||
url(r'^aesop-export/index.csv$', export.ExportIndexView.as_view(), name='seminar_export_index'),
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue