Jonas Havelka
3 years ago
2 changed files with 0 additions and 219 deletions
@ -1,125 +0,0 @@ |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
import datetime |
|||
import os |
|||
import random |
|||
#import argparse |
|||
from optparse import make_option |
|||
|
|||
|
|||
from django.core.management.base import BaseCommand |
|||
from django.core.management import call_command |
|||
from django.conf import settings |
|||
from django.db import transaction |
|||
import django.contrib.auth |
|||
from django.utils.encoding import force_unicode |
|||
|
|||
from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Nastaveni |
|||
from seminar.testutils import create_test_data |
|||
from seminar import ovvpfile |
|||
|
|||
User = django.contrib.auth.get_user_model() |
|||
|
|||
|
|||
class Command(BaseCommand): |
|||
help = "Import (add / notice changes) schools from a ovvp-format file (skoly.csv)" |
|||
|
|||
option_list = BaseCommand.option_list + ( |
|||
make_option('-n', '--dry_run', dest='dry_run', default=False, |
|||
action='store_true', help="No changes to DB."), |
|||
) |
|||
# def add_arguments(self, parser): |
|||
# parser.add_argument('file', nargs='?', type=argparse.FileType('r', encoding='utf8'), default=sys.stdin) |
|||
# parser.add_argument('-n', '--dry_run', dest='dry_run', default=False, |
|||
# action='store_true', type=bool, help="No changes to DB.") |
|||
|
|||
def school_diffs(self, ovvpskola, dbskola): |
|||
def compare(ovvpcol, dbcol, t=unicode): |
|||
v1 = t(dbskola.__getattribute__(dbcol)) |
|||
v2 = ovvpskola[ovvpcol] |
|||
if unicode(v1) != unicode(v2): |
|||
return "%s: '%s'->'%s', " % (ovvpcol, v1, v2, ) |
|||
return "" |
|||
|
|||
diff = "" |
|||
diff += compare('name','nazev') |
|||
diff += compare('street','ulice') |
|||
diff += compare('town','mesto') |
|||
diff += compare('postcode','psc') |
|||
return diff |
|||
|
|||
def handle(self, *args, **options): |
|||
assert len(args) == 1 |
|||
|
|||
filename = args[0] |
|||
self.stdout.write( |
|||
'Parsing OVVP/OPMK-format file \'%s\' ...' % (filename, ) |
|||
) |
|||
with open(filename, "r") as f: |
|||
o = ovvpfile.parse(f) |
|||
|
|||
assert o.headers['version'] == '1' |
|||
self.stdout.write('Read %d schools with columns: %s' % (len(o.rows), o.columns, )) |
|||
self.stdout.write('Export created: %s' % (o.headers.get('date', 'N/A'), )) |
|||
assert 'id-aesop' in o.columns |
|||
|
|||
same = 0 |
|||
modified = 0 |
|||
new = 0 |
|||
|
|||
with transaction.atomic(): |
|||
for skola in o.rows: |
|||
aesop_id = 'aesop:%s' % (skola['id-aesop'], ) |
|||
found = Skola.objects.filter(aesop_id=aesop_id) |
|||
assert len(found) <= 1 |
|||
|
|||
if found: |
|||
fs = found[0] |
|||
diff = self.school_diffs(skola, fs) |
|||
if diff: |
|||
modified += 1 |
|||
self.stdout.write(u"M %11s %s" % (aesop_id, diff, )) # TODO |
|||
else: |
|||
same += 1 |
|||
if int(skola['is-SS']) > int(fs.je_ss): |
|||
fs.je_ss = True |
|||
fs.save() |
|||
|
|||
if int(skola['is-ZS']) > int(fs.je_zs): |
|||
fs.je_zs = True |
|||
fs.save() |
|||
|
|||
else: |
|||
new += 1 |
|||
# Name duplicates? |
|||
by_name = Skola.objects.filter(nazev=skola['name'], mesto=skola['town'], ulice=skola['street']) |
|||
if len(by_name) > 0: |
|||
self.stdout.write(u"W Same [name, street, town] for %s and %s (%s, %s, %s)" % ( |
|||
by_name[0].aesop_id, aesop_id, skola['name'], skola['street'], skola['town'], )) |
|||
else: |
|||
self.stdout.write(u"+ %11s %s, %s, %s" % (aesop_id, skola['name'], skola['town'], skola['country'], )) |
|||
if not options['dry_run']: |
|||
newskola = Skola.objects.create( |
|||
aesop_id=aesop_id, nazev=skola['name'], kratky_nazev=skola['name'], izo=skola.get('id-izo', ''), |
|||
ulice=skola['street'], mesto=skola['town'], psc=skola['postcode'], stat=skola['country'], |
|||
je_zs=int(skola['is-ZS']), je_ss=int(skola['is-SS']), |
|||
) |
|||
|
|||
self.stdout.write("Result: %d same, %d different, %d new schools" % (same, modified, new, )) |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
@ -1,94 +0,0 @@ |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
import datetime |
|||
import random |
|||
import django.contrib.auth |
|||
from unittest import TestCase |
|||
from django.test import Client |
|||
from django.core.urlresolvers import reverse, resolve |
|||
from django.core.management import call_command |
|||
|
|||
from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Soustredeni, Nastaveni |
|||
from seminar.testutils import create_test_data |
|||
from seminar import ovvpfile |
|||
from seminar import utils |
|||
|
|||
class SeminarBasicTests(TestCase): |
|||
def setUp(self): |
|||
create_test_data(size=2) |
|||
self.client = Client() |
|||
|
|||
def tearDown(self): |
|||
call_command('flush', noinput=True, verbosity=0, interactive=False) |
|||
self.cleint = None |
|||
|
|||
def test_rocniky(self): |
|||
r19 = Rocnik.objects.get(rocnik=21) |
|||
self.assertEqual(r19.roman(), 'XXI') |
|||
|
|||
def test_render_cislo_e2e(self): |
|||
cs = Cislo.objects.all() |
|||
for c in cs[:4]: |
|||
url = c.verejne_url() |
|||
r = self.client.get(url) |
|||
assert r.status_code == 200 |
|||
assert len(r.content) >= 100 |
|||
# TODO: Validate cntent as HTML |
|||
|
|||
def test_render_problem_e2e(self): |
|||
ps = Problem.objects.all() |
|||
for p in ps[:4]: |
|||
url = p.verejne_url() |
|||
r = self.client.get(url) |
|||
assert r.status_code == 200 |
|||
assert len(r.content) >= 100 |
|||
# TODO: Validate cntent as HTML |
|||
|
|||
def test_export_e2e(self): |
|||
i_url = '/aesop-export/index.csv' |
|||
i_r = self.client.get(i_url) |
|||
assert i_r.status_code == 200 |
|||
ls = i_r.content.strip().split('\n') |
|||
|
|||
for u in [ls[0], ls[-1]]: |
|||
ex_r = self.client.get('/aesop-export/' + u) |
|||
assert ex_r.status_code == 200 |
|||
assert len(ex_r.content) >= 100 |
|||
o = ovvpfile.parse(ex_r.content) |
|||
assert o.headers['version'] == '1' |
|||
|
|||
def test_admin_url(self): |
|||
for m in [Skola, Resitel, Rocnik, Cislo, Problem, Reseni, Nastaveni]: |
|||
o = m.objects.first() |
|||
url = o.admin_url() |
|||
assert url |
|||
view = resolve(url) |
|||
assert view |
|||
|
|||
def test_verejne_url(self): |
|||
for m in [Rocnik, Cislo, Problem]: |
|||
p = Problem.objects.first() |
|||
url = p.verejne_url() |
|||
assert url |
|||
view = resolve(url) |
|||
assert view |
|||
|
|||
def test_ovvpfile(self): |
|||
filetext = "H1\ta\nH2\tb\tc\n\nx\ty\tz\n0\t1\t2\n3\t4\t5\n" |
|||
o = ovvpfile.parse(filetext) |
|||
assert len(o.headers) == 2 |
|||
assert o.headers['H2'] == 'b\tc' |
|||
|
|||
assert o.columns == ['x','y','z'] |
|||
assert len(o.rows) == 2 |
|||
assert o.rows[0]['z'] == '2' |
|||
|
|||
t = o.to_string() |
|||
assert t == filetext |
|||
|
|||
def test_roman(self): |
|||
for i in [0, 1, 23, 2015, 1999, 42, 4, 400, 78, 4321, 8765, 999]: |
|||
r = utils.roman(i) |
|||
fr = utils.from_roman(r) |
|||
assert fr == i |
|||
|
Loading…
Reference in new issue