61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
from django.db import models
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from ..validators import LowerAlphanumericValidator
|
|
|
|
BOOTSTRAP_CONTEXT_CHOICES = (
|
|
('default', 'default'),
|
|
('primary', 'primary'),
|
|
('success', 'success'),
|
|
('info', 'info'),
|
|
('warning', 'warning'),
|
|
('danger', 'danger'),
|
|
)
|
|
DEFAULT_EVENT_STATI = {
|
|
'void': (0, _(u'Ungültig'), None),
|
|
'draft': (10, _(u'Entwurf'), 'info'),
|
|
'submitted': (30, _(u'Eingereicht'), 'danger'),
|
|
'accepted': (50, _(u'Freigegeben'), 'warning'),
|
|
'publishing': (70, _(u'Veröffentlichung'), 'warning'),
|
|
'published': (80, _(u'Veröffentlicht'), 'success'),
|
|
'expired': (100, _(u'Ausgelaufen'), None),
|
|
}
|
|
|
|
|
|
def get_event_status(code):
|
|
try:
|
|
obj = EventStatus.objects.get(code=code)
|
|
except EventStatus.DoesNotExist as e:
|
|
if code not in DEFAULT_EVENT_STATI:
|
|
raise e
|
|
severity = DEFAULT_EVENT_STATI[code][0]
|
|
label = DEFAULT_EVENT_STATI[code][1]
|
|
obj = EventStatus(code=code, severity=severity, label=label)
|
|
if DEFAULT_EVENT_STATI[code][2]:
|
|
obj.bootstrap_context = DEFAULT_EVENT_STATI[code][2]
|
|
obj.save()
|
|
return obj
|
|
|
|
|
|
class EventStatus(models.Model):
|
|
code = models.CharField(primary_key=True, max_length=254, validators=[LowerAlphanumericValidator])
|
|
severity = models.IntegerField(unique=True)
|
|
label = models.CharField(unique=True, max_length=254)
|
|
bootstrap_context = models.CharField(max_length=20, blank=True, choices=BOOTSTRAP_CONTEXT_CHOICES)
|
|
|
|
class Meta:
|
|
verbose_name = _(u'Veranstaltungsstatus')
|
|
verbose_name_plural = _(u'Veranstaltungsstati')
|
|
ordering = ['severity']
|
|
|
|
def __unicode__(self):
|
|
return u'{label} ({severity} {code})'.format(code=self.code,
|
|
severity=self.severity,
|
|
label=self.label)
|
|
|
|
def get_bootstrap_label(self):
|
|
context = self.bootstrap_context or 'default'
|
|
return u'<span class="label label-{context}">{label}</span>'.format(context=context,
|
|
label=self.label)
|