From 065461300dbfdd4f0d073d14e196db71000b86ea Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Sat, 14 Mar 2015 17:24:18 +0100 Subject: [PATCH] Added fake/testing data generation --- README.md | 3 + seminar/admin.py | 4 +- seminar/management/__init__.py | 0 seminar/management/commands/.testdata.py.swp | Bin 0 -> 16384 bytes seminar/management/commands/__init__.py | 0 seminar/management/commands/testdata.py | 99 +++++++++++++++++++ seminar/models.py | 8 +- 7 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 seminar/management/__init__.py create mode 100644 seminar/management/commands/.testdata.py.swp create mode 100644 seminar/management/commands/__init__.py create mode 100644 seminar/management/commands/testdata.py diff --git a/README.md b/README.md index 5f6510d2..87f51b71 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,9 @@ Make commands enhanced compared to `./manage.py runserver`. Open [127.0.0.1:8000](127.0.0.1:8000). +* `./manage.py testdata` - create pseudo-random seminar data and admin/admin + user. + * `./manage.py test` - run the tests. diff --git a/seminar/admin.py b/seminar/admin.py index 7b7afdb0..87488893 100644 --- a/seminar/admin.py +++ b/seminar/admin.py @@ -15,7 +15,7 @@ class SkolaAdmin(admin.ModelAdmin): (u'Interní ID', {'fields': ['aesop_id', 'izo'], 'classes': ['collapse']}), (u'Adresa', {'fields': ['ulice', 'mesto', 'psc', 'stat']}), ] - list_display = ['nazev', 'aesop_id', 'mesto'] + list_display = ['nazev', 'aesop_id', 'mesto', 'ulice', 'stat'] search_fields = ['nazev', 'mesto', 'ulice'] admin.site.register(Skola, SkolaAdmin) @@ -93,7 +93,7 @@ class ReseniAdmin(admin.ModelAdmin): (None, {'fields': ['problem', 'resitel', 'body', 'cislo_body', 'timestamp']}), ] list_display = ['problem', 'resitel', 'body', 'timestamp'] - list_filter = ['problem', 'resitel', 'body', 'timestamp'] + list_filter = ['body', 'timestamp'] search_fields = [] inlines = [PrilohaReseniInline] diff --git a/seminar/management/__init__.py b/seminar/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/seminar/management/commands/.testdata.py.swp b/seminar/management/commands/.testdata.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..b6a2d787c34dbf8aa0e5837c380a385c2a65302a GIT binary patch literal 16384 zcmeHOO>7)V6>gU09|Ky1gmOTIEvv`LxIHs=;!VgbjP0<=I&r+Q9g)cWE>c!F6^Ssic*G5V_t6@Z zgknH3a3lk9v^n|oh&J77HmH1T^potPPpus(sxlP=iUGxdVn8vV7*Gr-1{4E|fww0E zk^hADWen|!YZ$F@EzX84td<%FH zm;v5=Kk5QM0=^5p1bhV;0q(vJdz-+k@6|K|m;_D$Z@ov;ehu6MP5|G)`Gy*x10Mta z`7TZS0q_d&65s$gfJxvVPioqCfD^#Ku@LeW@BsKe@O9wxz#p)H(gpqw9{&aqpRW<8 zkL|~f`)$gA1tPSW?XiG+TcQ?tmVrCV_M%#&&Sobj&S~0l)${x?;(gX;p=&G$vgZib zSXu2{@80Sxbe3+`S+wg9mag7h?yg6g3%ag%fqQBQ4Sz1vTGB(&KxAIvMK=vFq-#{hvB=o3rt7ay-=8IID%?3+tx6 zQD=1H+Zz-0W4llpj_lnQW)vD`Akdo{k0a2Uj%lzi=*=;j~y2%z4rrEHA z$g?+&)=lQ}OU43IGq6u?hWu)8qDl5ln@tawVQ_7ItTQ*iaN)DhUcB_&;^iw#SC^l^ zwz9f*{c|^N-qMF!8)GtR}r3IeKbGp6v=otFm7|G36l)r8uK)Kqrz4oP0!vT zx-ue27DhA4XgIwSyWRe7LRfNcmbp4|=P z4k=})*wb~^oM0(&r&x=fC!@fzgcAz3Bt0=SrnEQzMbl@f>4EBGMrb{J=3vHD+bKv6 zn#>HL;l(cwR8M6GpP89DJ2R+cjM{Ygpq_~?jl&G^k9@W1JM$rM&mtacZUT zOrB73gjQqF3DTj9!V8Al%c7=>!+NQ`F2*o{xBn{aL7vgpR~E@Mq1o0O%@&)UK`M3- z>D#fs>_j{J_gNEJNq|SzN+2u`c?b7*VtAKcxW}VaUq$5Mee#4<^Zcz`^K7HR8jX_X zi%#5=q%!CetS@5;fw z$E3A2sCB`S@ivs6E-76IlB~z4vpJ}9G1$MKShuWn8G(CfpkqlOse~Gu1{mD;g=f`d z;RF;Tq!HqLYOR{NX$N7{^|&kW?4i10(&<$0*n2~=5LgREPzj;k72B1+8K zb)_W``3~8#U0(*#QFRVQHUsHmI+>hMW|@4_OvreM7jYtJiB0B6ZguInvh8hk$t;+S zI-7{3hfO6z9TqFPV_U(6X_f4`cmi01${$Hn#CVH zb^_)pDqWacdzQ7^jEG32i1=+do+G-R4GVlrD7050C^esOJbb!wQp9Y=*+Q4~$+B^rSpJG5UpcqgL zC6Lk%}vO30a@C!x0_5MkBF8AlbOZ@^zZHPAOV|PHwQ( zL@LwVbPFQAPj){Nk!)Kk6G_L9)rn%wB<}*>iDK?xzlU~2XpMP@Wqh%NVWmr4S>4#A61uooB#j- literal 0 HcmV?d00001 diff --git a/seminar/management/commands/__init__.py b/seminar/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/seminar/management/commands/testdata.py b/seminar/management/commands/testdata.py new file mode 100644 index 00000000..dfba605f --- /dev/null +++ b/seminar/management/commands/testdata.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- + +import datetime +import os +import random + +from django.core.management.base import NoArgsCommand +from django.core.management import call_command +from django.conf import settings + +class Command(NoArgsCommand): + help = "Clear database and load testing data." + + def handle_noargs(self, **options): + assert settings.DEBUG == True + dbfile = settings.DATABASES['default']['NAME'] + if os.path.exists(dbfile): + os.rename(dbfile, dbfile + '.old') + self.stderr.write('Stara databaze prejmenovana na "%s"' % (dbfile + '.old')) + call_command('migrate', noinput=True) + self.load_testing_data() + + def load_testing_data(self): + from seminar import models as sm + import django.contrib.auth + User = django.contrib.auth.get_user_model() + + self.stderr.write('Vytvarim uzivatele "admin" (heslo "admin")') + + # pevna pseudo-nahodnost + rnd = random.Random(x=42) + + # users + admin = User.objects.create_superuser(username='admin', email='', password='admin') + + self.stderr.write('Vytvarim pseudo-nahodna data') + + orgs = [] + for org in ['anet', 'bara', 'cyril', 'david', 'eva', 'filip']: + o = User.objects.create_user(username=org, password=org) + o.first_name = org.capitalize() + o.save() + orgs.append(o) + + # skoly + sm.Skola.objects.create(mesto = u'Praha', stat='CZ', psc='101 00', ulice=u'Krátká 5', nazev=u'První SŠ') + sm.Skola.objects.create(mesto = u'Praha', stat='CZ', psc='102 00', ulice=u'Dlouhá 5', nazev=u'Druhá SŠ') + sm.Skola.objects.create(mesto = u'Praha', stat='CZ', psc='103 00', ulice=u'Široká 3', nazev=u'Třetí SŠ') + sm.Skola.objects.create(mesto = u'Ostrava', stat='CZ', psc='700 00', ulice=u'Hluboká 42', nazev=u'Hutní gympl') + sm.Skola.objects.create(mesto = u'Humenné', stat='SK', psc='012 34', ulice=u'Pltká 1', nazev=u'Sredná škuola') + + # resitele + for i in range(20): + skola = rnd.choice(sm.Skola.objects.all()) + pohlavi = rnd.randint(0,1) + sm.Resitel.objects.create(skola = skola, datum_prihlaseni = datetime.date(rnd.randint(2002, 2014), rnd.randint(1,12), 1), + jmeno = u'Řešitel' if pohlavi else u'Řešitelka', prijmeni = 'Číslo-%s' % (i), rok_maturity = rnd.randint(2015, 2019), + stat = skola.stat, zasilat = sm.Resitel.ZASILAT_NIKAM, pohlavi_muz = pohlavi) + + # rocniky + for ri in range(17, 22): + r = sm.Rocnik.objects.create(prvni_rok = 1993 + ri, rocnik = ri) + + # cisla + cisel = rnd.randint(4, 6) + cs = {} + for ci in range(1, cisel + 1): + vydano = datetime.date(r.prvni_rok, ci + 6, 1) + deadline = datetime.date(r.prvni_rok, ci + 8, 1) if ci + 2 < cisel else None + c = sm.Cislo.objects.create(rocnik = r, cislo = str(ci), datum_vydani=vydano, datum_deadline=deadline) + cs[ci] = c + + # problemy resene v ci + seq='#ABCDEFGHIJKLMNOPQRSTUVWXYZ' + if ci >= 3: + for pi in range(1, 4): + p = sm.Problem.objects.create(autor = rnd.choice(orgs), cislo_zadani=cs[ci-2], cislo_reseni=cs[ci], + opravovatel = rnd.choice(orgs), kod = str(pi), nazev = u'Dummy úloha %s-%s' % (seq[ci], seq[pi]), + stav = sm.Problem.STAV_ZADANY, typ = sm.Problem.TYP_ULOHA, body = rnd.randint(1, 5)) + + for resi in range(rnd.randint(0, 7)): + res = sm.Reseni.objects.create(problem = p, resitel = rnd.choice(sm.Resitel.objects.all()), + body = rnd.randint(0, p.body), cislo_body = cs[ci]) + + nastaveni = sm.Nastaveni.objects.create(aktualni_rocnik = sm.Rocnik.objects.last(), + aktualni_cislo = sm.Cislo.objects.last()) + + self.stderr.write('Vytvoreno %d uzivatelu, %d skol, %d resitelu, %d rocniku, %d cisel, %d problemu, %d reseni.' % + (User.objects.count(), sm.Skola.objects.count(), sm.Resitel.objects.count(), sm.Rocnik.objects.count(), + sm.Cislo.objects.count(), sm.Problem.objects.count(), sm.Reseni.objects.count())) + + + + + + + + + diff --git a/seminar/models.py b/seminar/models.py index b1a4da36..dbfa5f22 100644 --- a/seminar/models.py +++ b/seminar/models.py @@ -56,7 +56,7 @@ class Skola(models.Model): # ISO 3166-1 dvojznakovy kod zeme velkym pismem (CZ, SK) # Ekvivalentní s CharField(max_length=2, default='CZ', ...) - stat = CountryField(u'kód státu', default='CZ', + stat = CountryField(u'stát', default='CZ', help_text=u'ISO 3166-1 kód zeme velkými písmeny (CZ, SK, ...)') def __str__(self): @@ -85,7 +85,7 @@ class Resitel(models.Model): # Pohlaví. Že ho neznáme se snad nestane (a ušetří to práci při programování) pohlavi_muz = models.BooleanField(u'pohlaví (muž)', default=False) - skola = models.OneToOneField(Skola, blank=True, null=True, verbose_name=u'škola') + skola = models.ForeignKey(Skola, blank=True, null=True, verbose_name=u'škola') # Očekávaný rok maturity a vyřazení z aktivních řešitelů rok_maturity = models.IntegerField(u'rok maturity') @@ -126,7 +126,7 @@ class Resitel(models.Model): # ISO 3166-1 dvojznakovy kod zeme velkym pismem (CZ, SK) # Ekvivalentní s CharField(max_length=2, default='CZ', ...) - stat = CountryField(u'kód státu', default='CZ', + stat = CountryField(u'stát', default='CZ', help_text=u'ISO 3166-1 kód země velkými písmeny (CZ, SK, ...)') def plne_jmeno(self): @@ -242,6 +242,8 @@ class Problem(models.Model): cislo_reseni = models.ForeignKey(Cislo, verbose_name=u'číslo řešení', blank=True, null=True, related_name=u'resene_problemy', help_text=u'Číslo s řešením úlohy, jen pro úlohy') + body = models.IntegerField(u'maximum bodů', blank=True, null=True) + def __str__(self): return force_unicode(u'%s (%s)' % (self.nazev, self.stav))