|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
from django.contrib.auth.decorators import user_passes_test
|
|
|
|
from html.parser import HTMLParser
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
|
|
|
import seminar.models as m
|
|
|
|
import seminar.treelib as t
|
|
|
|
|
|
|
|
staff_member_required = user_passes_test(lambda u: u.is_staff)
|
|
|
|
|
|
|
|
class FirstTagParser(HTMLParser):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.firstTag = None
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def handle_data(self, data):
|
|
|
|
if self.firstTag == None:
|
|
|
|
self.firstTag = data
|
|
|
|
|
|
|
|
def histogram(seznam):
|
|
|
|
d = {}
|
|
|
|
for i in seznam:
|
|
|
|
if i not in d:
|
|
|
|
d[i] = 0
|
|
|
|
d[i] += 1
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
roman_numerals = zip((1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
|
|
|
|
('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I'))
|
|
|
|
|
|
|
|
def roman(num):
|
|
|
|
res = ""
|
|
|
|
for i, n in roman_numerals:
|
|
|
|
res += n * (num // i)
|
|
|
|
num %= i
|
|
|
|
return res
|
|
|
|
|
|
|
|
def from_roman(rom):
|
|
|
|
if not rom:
|
|
|
|
return 0
|
|
|
|
for i, n in roman_numerals:
|
|
|
|
if rom.upper().startswith(n):
|
|
|
|
return i + from_roman(rom[len(n):])
|
|
|
|
raise Exception('Invalid roman numeral: "%s"', rom)
|
|
|
|
|
|
|
|
|
|
|
|
def seznam_problemu():
|
|
|
|
problemy = []
|
|
|
|
|
|
|
|
# Pomocna fce k formatovani problemovych hlasek
|
|
|
|
def prb(cls, msg, objs=None):
|
|
|
|
s = '<b>%s:</b> %s' % (cls.__name__, msg)
|
|
|
|
if objs:
|
|
|
|
s += ' ['
|
|
|
|
for o in objs:
|
|
|
|
try:
|
|
|
|
url = o.admin_url()
|
|
|
|
except:
|
|
|
|
url = None
|
|
|
|
if url:
|
|
|
|
s += '<a href="%s">%s</a>, ' % (url, o.pk, )
|
|
|
|
else:
|
|
|
|
s += '%s, ' % (o.pk, )
|
|
|
|
s = s[:-2] + ']'
|
|
|
|
problemy.append(s)
|
|
|
|
|
|
|
|
# Duplicita jmen
|
|
|
|
jmena = {}
|
|
|
|
for r in m.Resitel.objects.all():
|
|
|
|
j = r.osoba.plne_jmeno()
|
|
|
|
if j not in jmena:
|
|
|
|
jmena[j] = []
|
|
|
|
jmena[j].append(r)
|
|
|
|
for j in jmena:
|
|
|
|
if len(jmena[j]) > 1:
|
|
|
|
prb(m.Resitel, 'Duplicitní jméno "%s"' % (j, ), jmena[j])
|
|
|
|
|
|
|
|
# Data maturity a narození
|
|
|
|
for r in m.Resitel.objects.all():
|
|
|
|
if not r.rok_maturity:
|
|
|
|
prb(m.Resitel, 'Neznámý rok maturity', [r])
|
|
|
|
if r.rok_maturity and (r.rok_maturity < 1990 or r.rok_maturity > datetime.date.today().year + 10):
|
|
|
|
prb(m.Resitel, 'Podezřelé datum maturity', [r])
|
|
|
|
if r.osoba.datum_narozeni and (r.osoba.datum_narozeni.year < 1970 or r.osoba.datum_narozeni.year > datetime.date.today().year - 12):
|
|
|
|
prb(m.Resitel, 'Podezřelé datum narození', [r])
|
|
|
|
# if not r.email:
|
|
|
|
# prb(Resitel, u'Neznámý email', [r])
|
|
|
|
|
|
|
|
## Kontroly konzistence databáze a TreeNodů
|
|
|
|
|
|
|
|
# Články
|
|
|
|
for clanek in m.Clanek.objects.all():
|
|
|
|
# získáme řešení svázané se článkem a z něj node ve stromě
|
|
|
|
reseni = clanek.reseni_set
|
|
|
|
if (reseni.count() != 1):
|
|
|
|
raise ValueError("Článek k sobě má nejedno řešení!")
|
|
|
|
r = reseni.first()
|
|
|
|
clanek_node = r.text_cely # vazba na ReseniNode z Reseni
|
|
|
|
# content type je věc pomáhající rozeznávat různé typy objektů v django-polymorphic
|
|
|
|
# protože isinstance vrátí vždy jen TreeNode
|
|
|
|
# https://django-polymorphic.readthedocs.io/en/stable/migrating.html
|
|
|
|
cislonode_ct = ContentType.objects.get_for_model(m.CisloNode)
|
|
|
|
node = clanek_node
|
|
|
|
while node is not None:
|
|
|
|
node_ct = node.polymorphic_ctype
|
|
|
|
if node_ct == cislonode_ct: # dostali jsme se k CisloNode
|
|
|
|
# zkontrolujeme, že stromové číslo odpovídá atributu
|
|
|
|
# .cislonode je opačná vazba k treenode_ptr, abychom z TreeNode dostali
|
|
|
|
# CisloNode
|
|
|
|
if clanek.cislo != node.cislonode.cislo:
|
|
|
|
prb(m.Clanek, "Číslo otištění uložené u článku nesedí s "
|
|
|
|
"číslem otištění podle struktury treenodů.", [clanek])
|
|
|
|
break
|
|
|
|
node = t.get_parent(node)
|
|
|
|
|
|
|
|
return problemy
|