|
@ -1,15 +1,17 @@ |
|
|
# coding:utf-8 |
|
|
# coding:utf-8 |
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404, render |
|
|
from django.shortcuts import get_object_or_404, render |
|
|
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden |
|
|
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden, JsonResponse |
|
|
from django.core.urlresolvers import reverse |
|
|
from django.core.urlresolvers import reverse |
|
|
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist |
|
|
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist |
|
|
from django.views import generic |
|
|
from django.views import generic |
|
|
from django.utils.translation import ugettext as _ |
|
|
from django.utils.translation import ugettext as _ |
|
|
from django.http import Http404 |
|
|
from django.http import Http404 |
|
|
from django.db.models import Q |
|
|
from django.db.models import Q |
|
|
|
|
|
from django.views.decorators.csrf import ensure_csrf_cookie |
|
|
|
|
|
from django.contrib.auth import authenticate, login |
|
|
|
|
|
|
|
|
from .models import Problem, Cislo, Reseni, Nastaveni, Rocnik, Soustredeni, Organizator, Resitel, Novinky, Soustredeni_Ucastnici |
|
|
from .models import Problem, Cislo, Reseni, Nastaveni, Rocnik, Soustredeni, Organizator, Resitel, Novinky, Soustredeni_Ucastnici, Pohadka |
|
|
from .models import VysledkyZaCislo, VysledkyKCisluZaRocnik, VysledkyKCisluOdjakziva |
|
|
from .models import VysledkyZaCislo, VysledkyKCisluZaRocnik, VysledkyKCisluOdjakziva |
|
|
from . import utils |
|
|
from . import utils |
|
|
|
|
|
|
|
@ -21,6 +23,10 @@ import shutil |
|
|
import os |
|
|
import os |
|
|
from django.conf import settings |
|
|
from django.conf import settings |
|
|
import unicodedata |
|
|
import unicodedata |
|
|
|
|
|
import json |
|
|
|
|
|
import traceback |
|
|
|
|
|
import sys |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def verejna_temata(rocnik): |
|
|
def verejna_temata(rocnik): |
|
|
"""Vrací queryset zveřejněných témat v daném ročníku. |
|
|
"""Vrací queryset zveřejněných témat v daném ročníku. |
|
@ -487,3 +493,133 @@ def StavDatabazeView(request): |
|
|
'jmena_muzu': utils.histogram([r.jmeno for r in muzi]), |
|
|
'jmena_muzu': utils.histogram([r.jmeno for r in muzi]), |
|
|
'jmena_zen': utils.histogram([r.jmeno for r in zeny]), |
|
|
'jmena_zen': utils.histogram([r.jmeno for r in zeny]), |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ensure_csrf_cookie |
|
|
|
|
|
def LoginView(request): |
|
|
|
|
|
"""Pro přihlášení při nahrávání z texu""" |
|
|
|
|
|
q = request.POST |
|
|
|
|
|
# nastavení cookie csrftoken |
|
|
|
|
|
if not q: |
|
|
|
|
|
return JsonResponse({"ok": 1}) |
|
|
|
|
|
|
|
|
|
|
|
if "username" in q: |
|
|
|
|
|
username = q["username"] |
|
|
|
|
|
password = q["password"] |
|
|
|
|
|
user = authenticate(username=username, password=password) |
|
|
|
|
|
if user is not None and user.is_staff: |
|
|
|
|
|
login(request, user) |
|
|
|
|
|
return JsonResponse({"ok": 1}) |
|
|
|
|
|
else: |
|
|
|
|
|
return JsonResponse({"error": "Neplatné uživatelské jméno nebo heslo"}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ensure_csrf_cookie |
|
|
|
|
|
def TexUploadView(request): |
|
|
|
|
|
|
|
|
|
|
|
def uloz_soubory(files, rocnik, cislo): |
|
|
|
|
|
for filename, f in files: |
|
|
|
|
|
path = "{}/{}/{}/{}".format( |
|
|
|
|
|
settings.CISLO_IMG_DIR, |
|
|
|
|
|
rocnik, |
|
|
|
|
|
cislo, |
|
|
|
|
|
filename |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
adresar = os.path.dirname(path) |
|
|
|
|
|
if not os.path.exists(adresar): |
|
|
|
|
|
os.makedirs(adresar) |
|
|
|
|
|
|
|
|
|
|
|
with open(path, "wb+") as fout: |
|
|
|
|
|
for chunk in f.chunks(): |
|
|
|
|
|
fout.write(chunk) |
|
|
|
|
|
|
|
|
|
|
|
q = request.POST |
|
|
|
|
|
# nastavení cookie csrftoken |
|
|
|
|
|
if not q: |
|
|
|
|
|
return JsonResponse({"ok": 1}) |
|
|
|
|
|
|
|
|
|
|
|
# Odchytíme všechny výjimky a traceback pošleme v odpovědi |
|
|
|
|
|
try: |
|
|
|
|
|
meta = json.loads(q["meta"]) |
|
|
|
|
|
html = q["html"] |
|
|
|
|
|
|
|
|
|
|
|
if meta["typ"] in ["uloha", "serial", "reseni"]: |
|
|
|
|
|
|
|
|
|
|
|
# Uložíme soubory |
|
|
|
|
|
if meta["typ"] != "reseni": |
|
|
|
|
|
c = meta["cislo"] |
|
|
|
|
|
else: |
|
|
|
|
|
# Řešení má nastavené číslo svojí úlohy, ale obrázky jsou |
|
|
|
|
|
# ukládány do čísla, kde řešení vyšlo |
|
|
|
|
|
c = meta["cislo_reseni"] |
|
|
|
|
|
uloz_soubory(request.FILES.items(), meta["rocnik"], c) |
|
|
|
|
|
|
|
|
|
|
|
# Zjistíme typ ukládaného problému |
|
|
|
|
|
typy = { |
|
|
|
|
|
"uloha": Problem.TYP_ULOHA, |
|
|
|
|
|
"serial": Problem.TYP_SERIAL, |
|
|
|
|
|
"reseni": Problem.TYP_ULOHA, |
|
|
|
|
|
} |
|
|
|
|
|
problem_typ = typy[meta["typ"]] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Pokud už problém existuje, vytáhneme jej z db a upravíme |
|
|
|
|
|
|
|
|
|
|
|
# Pokud ročník/číslo ještě neexistuje, vyhodí to výjimku -> |
|
|
|
|
|
# číslo/ročník se musí založit ručně v adminu |
|
|
|
|
|
rocnik = Rocnik.objects.get(rocnik=meta["rocnik"]) |
|
|
|
|
|
cislo = Cislo.objects.get(rocnik=rocnik, cislo=meta["cislo"]) |
|
|
|
|
|
|
|
|
|
|
|
existujici = Problem.objects.filter( |
|
|
|
|
|
typ=problem_typ, |
|
|
|
|
|
stav=Problem.STAV_ZADANY, |
|
|
|
|
|
cislo_zadani=cislo, |
|
|
|
|
|
kod=meta["kod"] |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
problem = None |
|
|
|
|
|
if existujici: |
|
|
|
|
|
problem = existujici[0] |
|
|
|
|
|
# Jinak vytvoříme nový |
|
|
|
|
|
else: |
|
|
|
|
|
problem = Problem( |
|
|
|
|
|
typ=problem_typ, |
|
|
|
|
|
stav=Problem.STAV_ZADANY, |
|
|
|
|
|
kod=meta["kod"], |
|
|
|
|
|
cislo_zadani=cislo |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if meta["typ"] == "reseni": |
|
|
|
|
|
problem.text_reseni = html |
|
|
|
|
|
#při nahrávání řešení už původní zadání atd. neměníme |
|
|
|
|
|
else: |
|
|
|
|
|
problem.text_zadani = html |
|
|
|
|
|
problem.nazev = meta["nazev"] |
|
|
|
|
|
problem.body = meta["body"] |
|
|
|
|
|
|
|
|
|
|
|
problem.save() |
|
|
|
|
|
|
|
|
|
|
|
# Vrátíme id dané úlohy, aby se k ní dala případně připojit pohádka |
|
|
|
|
|
return JsonResponse({"db_id": problem.id}) |
|
|
|
|
|
|
|
|
|
|
|
elif meta["typ"] == "pohadka": |
|
|
|
|
|
uloha = Problem.objects.get(typ=Problem.TYP_ULOHA, pk=meta["uloha"]) |
|
|
|
|
|
|
|
|
|
|
|
# Pokud už příslušná pohádka existuje, jen ji upravíme |
|
|
|
|
|
existujici = Pohadka.objects.filter(uloha=uloha, pred=meta["pred"]) |
|
|
|
|
|
pohadka = None |
|
|
|
|
|
if existujici: |
|
|
|
|
|
pohadka = existujici[0] |
|
|
|
|
|
else: |
|
|
|
|
|
pohadka = Pohadka(uloha=uloha, pred=meta["pred"]) |
|
|
|
|
|
pohadka.text = q["html"] |
|
|
|
|
|
pohadka.save() |
|
|
|
|
|
|
|
|
|
|
|
return JsonResponse({"db_id": pohadka.id}) |
|
|
|
|
|
|
|
|
|
|
|
except Exception, e: |
|
|
|
|
|
# Pošleme zpátky traceback, ať uživatel ví, v čem je problém |
|
|
|
|
|
tb = "".join(traceback.format_exception(type(e), e, sys.exc_info()[2])) |
|
|
|
|
|
return JsonResponse({"error": tb}) |
|
|