Browse Source

Pohádka: model a admin

* migrace
remotes/origin/temata
Matěj Kocián 9 years ago
parent
commit
5ddf810a2f
  1. 44
      seminar/admin.py
  2. 34
      seminar/migrations/0039_pohadka.py
  3. 42
      seminar/models.py

44
seminar/admin.py

@ -11,7 +11,7 @@ from django.db import models
from django.contrib.auth.models import User
from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Nastaveni, Soustredeni, Soustredeni_Ucastnici, Soustredeni_Organizatori, Novinky, Organizator, Prispevek
from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Nastaveni, Soustredeni, Soustredeni_Ucastnici, Soustredeni_Organizatori, Novinky, Organizator, Prispevek, Pohadka
from autocomplete_light import shortcuts as autocomplete_light
@ -296,6 +296,39 @@ class ReseniAdmin(VersionAdmin):
admin.site.register(Reseni, ReseniAdmin)
### Pohádka
class PohadkaAdminForm(forms.ModelForm):
class Meta:
model = Pohadka
exclude = []
autor = UserModelChoiceField(User.objects.filter(is_staff=True))
uloha = forms.ModelChoiceField(Problem.objects.filter(typ=Problem.TYP_ULOHA))
class PohadkaAdmin(VersionAdmin):
form = PohadkaAdminForm
def get_kod_ulohy(self, obj):
return obj.uloha.kod_v_rocniku()
get_kod_ulohy.short_description = u'Kód úlohy'
def get_rocnik(self, obj):
return obj.uloha.cislo_zadani.rocnik.rocnik
get_rocnik.short_description = u'Ročník'
list_display = ['__str__', 'get_rocnik', 'get_kod_ulohy', 'uloha', 'autor', 'timestamp']
get_form = get_form_predvypln_autora
class PohadkaKProblemuInline(admin.TabularInline):
form = PohadkaAdminForm
model = Pohadka
exclude = []
extra = 0
admin.site.register(Pohadka, PohadkaAdmin)
### Problem
from autocomplete_light.contrib.taggit_field import TaggitField, TaggitWidget
@ -348,7 +381,14 @@ create_modeladmin(ProblemNavrhAdmin, Problem, 'ProblemNavrh', verbose_name=u'Pro
class ProblemZadanyAdmin(ProblemAdmin):
list_display = ['nazev', 'typ', 'autor', 'opravovatel', 'kod', 'cislo_zadani', 'pocet_reseni', 'verejne']
list_filter = ['typ', 'zamereni', 'cislo_zadani__cislo', 'cislo_zadani__rocnik']
inlines = [ReseniKProblemuInline]
def get_inline_instances(self, request, obj=None):
if obj and obj.typ == Problem.TYP_ULOHA:
inlines = [ReseniKProblemuInline, PohadkaKProblemuInline]
else:
inlines = [ReseniKProblemuInline]
return [inline(self.model, self.admin_site) for inline in inlines]
def get_queryset(self, request):
qs = super(ProblemZadanyAdmin, self).get_queryset(request)

34
seminar/migrations/0039_pohadka.py

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
from django.conf import settings
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('seminar', '0038_change_meta_prispevek'),
]
operations = [
migrations.CreateModel(
name='Pohadka',
fields=[
('id', models.AutoField(serialize=False, primary_key=True)),
('text', models.TextField(verbose_name='Text poh\xe1dky')),
('pred', models.BooleanField(default=True, verbose_name='P\u0159ed \xfalohou')),
('timestamp', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Vytvo\u0159eno', editable=False, blank=True)),
('autor', models.ForeignKey(verbose_name=b'Autor poh\xc3\xa1dky', to=settings.AUTH_USER_MODEL)),
('uloha', models.ForeignKey(related_name='pohadky', verbose_name='\xdaloha', to='seminar.Problem')),
],
options={
'ordering': ['uloha__cislo_zadani', 'uloha__kod', '-pred'],
'db_table': 'seminar_pohadky',
'verbose_name': 'Poh\xe1dka',
'verbose_name_plural': 'Poh\xe1dky',
},
),
]

42
seminar/models.py

@ -585,6 +585,48 @@ class PrilohaReseni(SeminarModelBase):
def __str__(self):
return force_unicode(self.soubor)
@python_2_unicode_compatible
class Pohadka(SeminarModelBase):
u"""Kus pohádky před/za úlohou v čísle"""
class Meta:
db_table = 'seminar_pohadky'
verbose_name = u'Pohádka'
verbose_name_plural = u'Pohádky'
ordering = ['uloha__cislo_zadani', 'uloha__kod', '-pred']
# Interní ID
id = models.AutoField(primary_key=True)
text = models.TextField(u'Text pohádky')
uloha = models.ForeignKey(
Problem,
verbose_name=u'Úloha',
related_name='pohadky'
)
# Kusů pohádky je v čísle obvykle o 1 více, než úloh. Jeden bude za úlohou
# místo před ní.
pred = models.BooleanField(u'Před úlohou', default=True)
autor = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name="Autor pohádky"
)
timestamp = models.DateTimeField(
u'Vytvořeno',
default=timezone.now,
blank=True,
editable=False
)
def __str__(self):
uryvek = self.text if len(self.text) < (50-3) else self.text[:50]+"..."
return force_unicode(uryvek)
@reversion.register(ignore_duplicate_revisions=True)
class Prispevek(SeminarModelBase):
problem = models.ForeignKey(Problem, verbose_name='Problém') # TODO autokompleet

Loading…
Cancel
Save