|
|
@ -14,28 +14,19 @@ from django.contrib.auth.models import User |
|
|
|
from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Nastaveni, Soustredeni, Soustredeni_Ucastnici, Novinky, Organizator |
|
|
|
import autocomplete_light |
|
|
|
|
|
|
|
uzivatele = [(user, u"{} ()".format(1)) for user in User.objects.filter(is_staff=True)] |
|
|
|
print(uzivatele) |
|
|
|
|
|
|
|
class UserModelChoiceField(forms.ModelChoiceField): |
|
|
|
u"""Vlastní ModelChoiceField pro uživatele. Zobrazí kromě loginu i jméno. |
|
|
|
""" |
|
|
|
def label_from_instance(self, obj): |
|
|
|
return u"{} ({})".format(obj.get_full_name(), obj.username) |
|
|
|
|
|
|
|
def predvypln_prihlaseneho(*pole): |
|
|
|
u"""Dekorátor pro Adminy. Předvyplní do *polí přihlášeného uživatele. |
|
|
|
def get_form_predvypln_autora(self, request, obj=None, *args, **kwargs): |
|
|
|
u"""get_form fce pro Adminy. Předvyplňí přihlášeného uživatele jako autora. |
|
|
|
""" |
|
|
|
def formfield_for_foreignkey(self, db_field, request, **kwargs): |
|
|
|
if db_field.name in pole: |
|
|
|
kwargs['initial'] = request.user.id |
|
|
|
return super(self.__class__, self).formfield_for_foreignkey( |
|
|
|
db_field, request, **kwargs |
|
|
|
) |
|
|
|
|
|
|
|
def decorator(orig_class): |
|
|
|
orig_class.formfield_for_foreignkey = formfield_for_foreignkey |
|
|
|
return orig_class |
|
|
|
|
|
|
|
return decorator |
|
|
|
form = super(self.__class__, self).get_form(request, *args, **kwargs) |
|
|
|
form.base_fields['autor'].initial = request.user.id |
|
|
|
return form |
|
|
|
|
|
|
|
|
|
|
|
def make_set_action(atribut, hodnota, nazev): |
|
|
@ -303,7 +294,7 @@ class ProblemAdminForm(forms.ModelForm): |
|
|
|
text_org = forms.CharField(widget=CKEditorWidget(), required=False, **field_labels(Problem, 'text_org')) |
|
|
|
zamereni = TaggitField(widget=TaggitWidget('TagAutocomplete'), required=False) |
|
|
|
autor = UserModelChoiceField(User.objects.filter(is_staff=True)) |
|
|
|
opravovatel = UserModelChoiceField(User.objects.filter(is_staff=True)) |
|
|
|
opravovatel = UserModelChoiceField(User.objects.filter(is_staff=True), required=False) |
|
|
|
class Meta: |
|
|
|
model = Problem |
|
|
|
exclude = [] |
|
|
@ -327,7 +318,6 @@ class ProblemAdmin(reversion.VersionAdmin): |
|
|
|
def pocet_reseni(self, obj): |
|
|
|
return obj.pocet_reseni |
|
|
|
|
|
|
|
@predvypln_prihlaseneho('autor') |
|
|
|
class ProblemNavrhAdmin(ProblemAdmin): |
|
|
|
list_display = ['nazev', 'typ', 'zamereni', 'stav', 'autor', 'timestamp'] |
|
|
|
list_filter = ['typ', 'zamereni', 'timestamp', 'stav'] |
|
|
@ -336,9 +326,11 @@ class ProblemNavrhAdmin(ProblemAdmin): |
|
|
|
qs = super(ProblemNavrhAdmin, self).get_queryset(request) |
|
|
|
return qs.filter(stav__in=[Problem.STAV_NAVRH, Problem.STAV_SMAZANY]) |
|
|
|
|
|
|
|
get_form = get_form_predvypln_autora |
|
|
|
|
|
|
|
|
|
|
|
create_modeladmin(ProblemNavrhAdmin, Problem, 'ProblemNavrh', verbose_name=u'Problém (návrh)', verbose_name_plural=u'Problémy (návrhy)') |
|
|
|
|
|
|
|
@predvypln_prihlaseneho('autor') |
|
|
|
class ProblemZadanyAdmin(ProblemAdmin): |
|
|
|
list_display = ['nazev', 'typ', 'autor', 'opravovatel', 'kod', 'cislo_zadani', 'pocet_reseni', 'verejne'] |
|
|
|
list_filter = ['typ', 'cislo_zadani__cislo', 'cislo_zadani__rocnik'] |
|
|
@ -348,6 +340,8 @@ class ProblemZadanyAdmin(ProblemAdmin): |
|
|
|
qs = super(ProblemZadanyAdmin, self).get_queryset(request) |
|
|
|
return qs.filter(stav=Problem.STAV_ZADANY).annotate(pocet_reseni=Count('reseni')) |
|
|
|
|
|
|
|
get_form = get_form_predvypln_autora |
|
|
|
|
|
|
|
create_modeladmin(ProblemZadanyAdmin, Problem, 'ProblemZadany', verbose_name=u'Problém (zadaný)', verbose_name_plural=u'Problémy (zadané)') |
|
|
|
|
|
|
|
#admin.site.register(Problem, ProblemAdmin) |
|
|
@ -405,12 +399,13 @@ def zneverejnit_novinky(modeladmin, request, queryset): |
|
|
|
zneverejnit_novinky.short_description = 'Zneveřejnit vybrané novinky' |
|
|
|
|
|
|
|
|
|
|
|
@predvypln_prihlaseneho('autor') |
|
|
|
class NovinkyAdmin(admin.ModelAdmin): |
|
|
|
form = NovinkyAdminForm |
|
|
|
list_display = ['datum', 'autor', 'text', 'zverejneno', 'obrazek'] |
|
|
|
actions = [zverejnit_novinky, zneverejnit_novinky] |
|
|
|
|
|
|
|
get_form = get_form_predvypln_autora |
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(Novinky, NovinkyAdmin) |
|
|
|
|
|
|
|