openbackloggery/collection/core/models.py

92 lines
2.6 KiB
Python
Raw Normal View History

from datetime import datetime
from django.db import models
from django.utils.translation import ugettext as _
class Collection(models.Model):
2017-08-24 20:36:55 +00:00
name = models.CharField(max_length=255, verbose_name=_('name'))
def __str__(self):
return '%s' % self.name
class Meta:
abstract = True
ordering = ['name']
class Item(models.Model):
TARGET_MODEL = None
TARGET_VERBOSE_NAME = None
# status choices
CREATED = 'created'
STATUS_CHOICES = (
(CREATED, _('New')),
)
DEFAULT_CHOICE = CREATED
2017-08-24 20:36:55 +00:00
name = models.CharField(max_length=255, verbose_name=_('name'))
def __str__(self):
return '%s' % self.name
@classmethod
def on_class_prepared(cls):
"""
Add new field 'collection' which is a link to TARGET_MODEL.
Add new field 'status' which is a list of choices. With CREATED.
"""
target_field = models.ForeignKey(
cls.TARGET_MODEL,
related_name=cls.RELATED_TARGET_NAME,
2017-08-24 20:36:55 +00:00
verbose_name=_(cls.TARGET_VERBOSE_NAME))
target_field.contribute_to_class(cls, 'collection')
status_field = models.CharField(
max_length=30,
choices=Item.STATUS_CHOICES + cls.STATUS_CHOICES,
2017-08-24 20:36:55 +00:00
default=cls.DEFAULT_CHOICE,
verbose_name=_('status'))
status_field.contribute_to_class(cls, 'status')
class Meta:
abstract = True
ordering = ['name']
class Timeline(models.Model):
"""
Keep changes on collection.
For an example, a game cretion date. Or when you completed a game.
"""
TARGET_MODEL = None
2017-08-24 20:36:55 +00:00
TARGET_VERBOSE_NAME = None
STATUS_CHOICES = Item.STATUS_CHOICES
DEFAULT_CHOICE = Item.CREATED
date = models.DateField(default=datetime.now, verbose_name=_('date'))
@classmethod
def on_class_prepared(cls):
"""
Add new field 'item' which is a link to TARGET_MODEL
"""
2017-08-24 20:36:55 +00:00
target_field = models.ForeignKey(
cls.TARGET_MODEL,
verbose_name=_(cls.TARGET_VERBOSE_NAME))
target_field.contribute_to_class(cls, 'item')
status_field = models.CharField(
max_length=30,
choices=Item.STATUS_CHOICES + cls.STATUS_CHOICES,
2017-08-24 20:36:55 +00:00
default=cls.DEFAULT_CHOICE,
verbose_name=_('status'))
status_field.contribute_to_class(cls, 'status')
def __str__(self):
return '%s: %s - %s' % (
self.date.strftime('%Y-%m-%d'),
self.status,
self.item)
class Meta:
abstract = True
ordering = ('-date',)