Jonas Havelka
1 year ago
13 changed files with 2 additions and 236 deletions
@ -1,3 +0,0 @@ |
|||
""" |
|||
Obsahuje vše, co se týká aesopu (exportu, který po nás vyžaduje OPMK). |
|||
""" |
@ -1,8 +0,0 @@ |
|||
""" |
|||
Soubor sloužící k pojmenování a jiným nastavením djangovské aplikace. |
|||
""" |
|||
from django.apps import AppConfig |
|||
|
|||
|
|||
class AesopConfig(AppConfig): |
|||
name = 'aesop' |
@ -1,30 +0,0 @@ |
|||
from django.http import HttpResponse |
|||
from django.utils.encoding import force_text |
|||
|
|||
|
|||
class OvvpFile: |
|||
def __init__(self): |
|||
# { header: value, ... } |
|||
self.headers = {} |
|||
# [ 'column-name', ... ] |
|||
self.columns = [] |
|||
# [ { column: value, ...}, ...] |
|||
self.rows = [] |
|||
|
|||
def to_lines(self): |
|||
# header |
|||
for hk in sorted(self.headers.keys()): |
|||
yield f'{hk}\t{self.headers[hk]}\n' |
|||
yield '\n' |
|||
# columns |
|||
yield '\t'.join(self.columns) + '\n' |
|||
# rows |
|||
for r in self.rows: |
|||
yield '\t'.join([force_text(r[c]) for c in self.columns]) + '\n' |
|||
|
|||
def to_string(self): |
|||
return ''.join(self.to_lines()) |
|||
|
|||
# Pozn: tohle je ta jediná funkce, která se reálně používá… |
|||
def to_HttpResponse(self): |
|||
return HttpResponse(self.to_string(), content_type='text/plain; charset=utf-8') |
@ -1,27 +0,0 @@ |
|||
""" |
|||
Soubor sloužící jako „router“, tj. zde se definují url adresy a na co ukazují: |
|||
|
|||
- ``aesop-export/mam-rocnik-<int:prvni_rok>.csv`` (seminar_export_rocnik) :class:`~aesop.views.ExportRocnikView` |
|||
- ``aesop-export/mam-sous-<str:datum_zacatku>.csv`` (seminar_export_sous) :class:`~aesop.views.ExportSousView` |
|||
- ``aesop-export/index.csv`` (seminar_export_index) :class:`~aesop.views.ExportIndexView` |
|||
""" |
|||
from django.urls import path |
|||
from aesop import views |
|||
|
|||
urlpatterns = [ |
|||
path( |
|||
'aesop-export/mam-rocnik-<int:prvni_rok>.csv', |
|||
views.ExportRocnikView.as_view(), |
|||
name='seminar_export_rocnik' |
|||
), |
|||
path( |
|||
'aesop-export/mam-sous-<str:datum_zacatku>.csv', |
|||
views.ExportSousView.as_view(), |
|||
name='seminar_export_sous' |
|||
), |
|||
path( |
|||
'aesop-export/index.csv', |
|||
views.ExportIndexView.as_view(), |
|||
name='seminar_export_index' |
|||
), |
|||
] |
@ -1,16 +0,0 @@ |
|||
import datetime |
|||
|
|||
from django.utils.encoding import force_text |
|||
|
|||
from aesop.ovvpfile import OvvpFile |
|||
|
|||
|
|||
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 |
@ -1,101 +0,0 @@ |
|||
""" |
|||
Soubor sloužící k deklaraci jednotlivých „views“ (nejčastěji funkce beroucí request |
|||
a vracející :func:`django.shortcuts.render` respektive nějakou response, nebo |
|||
třídy většinou rozšiřující nějakou třídu z :mod:`django.views.generic`) |
|||
""" |
|||
import django |
|||
from django.shortcuts import get_object_or_404 |
|||
from django.http import HttpResponse |
|||
from django.urls import reverse |
|||
from django.views import generic |
|||
from django.utils.encoding import force_text |
|||
|
|||
from .utils import default_ovvpfile |
|||
from seminar.models import Rocnik, Soustredeni |
|||
from vysledkovky import utils |
|||
from seminar.utils import aktivniResitele |
|||
|
|||
class ExportIndexView(generic.View): |
|||
def get(self, request): |
|||
ls = [] |
|||
for r in Rocnik.objects.filter(exportovat = True): |
|||
url = reverse('seminar_export_rocnik', kwargs={'prvni_rok': r.prvni_rok}) |
|||
ls.append(url.split('/')[-1]) |
|||
for s in Soustredeni.objects.filter(exportovat = True): |
|||
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 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, exportovat=True) |
|||
|
|||
akce = {Soustredeni.TYP_JARNI: 'MaM.sous.jaro', |
|||
Soustredeni.TYP_PODZIMNI: 'MaM.sous.podzim', |
|||
Soustredeni.TYP_VIKEND: 'MaM.vikend', |
|||
}[s.typ] |
|||
|
|||
of = default_ovvpfile(akce, 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() |
|||
|
|||
# POZOR! Předělání na nový model neotestováno v reálu (ale zase jen drobné změny) |
|||
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, exportovat=True) |
|||
cislo = rocnik.posledni_zverejnena_vysledkovka_cislo() |
|||
resitele = aktivniResitele(cislo, True) |
|||
slovnik_body = utils.secti_body_za_rocnik(cislo, resitele, False) |
|||
setrizeni_resitele, body = utils.setrid_resitele_a_body(slovnik_body) |
|||
|
|||
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',] |
|||
|
|||
resitele_slovnik = {} |
|||
for r in resitele: |
|||
resitele_slovnik[r.id] = r |
|||
|
|||
# počítání pořadí řešitelů |
|||
posledni_body = 100000 |
|||
posledni_poradi = 0 |
|||
for i in range(len(setrizeni_resitele)): |
|||
rd = resitele_slovnik[setrizeni_resitele[i]].export_row() |
|||
|
|||
if posledni_body > body[i]: |
|||
posledni_body = body[i] |
|||
posledni_poradi = i + 1 |
|||
rd['rank'] = posledni_poradi |
|||
rd['points'] = body[i] |
|||
|
|||
of.rows.append(rd) |
|||
|
|||
return of.to_HttpResponse() |
Loading…
Reference in new issue