2017-08-22 19:02:43 +00:00
|
|
|
from datetime import datetime
|
2018-01-15 17:55:03 +00:00
|
|
|
|
2017-08-22 19:02:43 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
|
|
|
|
class Collection(models.Model):
|
2018-01-16 18:07:39 +00:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255, verbose_name=_('name'), unique=True)
|
2018-01-15 17:55:03 +00:00
|
|
|
shortname = models.CharField(max_length=30, verbose_name=_('shortname'))
|
2017-08-22 19:02:43 +00:00
|
|
|
|
|
|
|
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'
|
2018-01-15 17:55:03 +00:00
|
|
|
STATUS_CHOICES = ((CREATED, _('New')), )
|
2017-08-22 19:02:43 +00:00
|
|
|
DEFAULT_CHOICE = CREATED
|
|
|
|
|
2017-08-24 20:36:55 +00:00
|
|
|
name = models.CharField(max_length=255, verbose_name=_('name'))
|
2018-01-21 11:38:18 +00:00
|
|
|
achievement = models.PositiveIntegerField(blank=True, null=True, verbose_name=_('achievement'))
|
|
|
|
achievement_max = models.PositiveIntegerField(blank=True, null=True, verbose_name=_('out of'))
|
2017-08-22 19:02:43 +00:00
|
|
|
|
|
|
|
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))
|
2017-08-22 19:02:43 +00:00
|
|
|
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'))
|
2017-08-22 19:02:43 +00:00
|
|
|
status_field.contribute_to_class(cls, 'status')
|
2018-01-18 19:31:09 +00:00
|
|
|
# check that no other same item name on same collection exists
|
|
|
|
cls._meta.unique_together = (('collection', 'name'), )
|
2017-08-22 19:02:43 +00:00
|
|
|
|
|
|
|
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
|
2017-08-22 19:02:43 +00:00
|
|
|
STATUS_CHOICES = Item.STATUS_CHOICES
|
|
|
|
DEFAULT_CHOICE = Item.CREATED
|
2017-09-03 08:53:17 +00:00
|
|
|
date = models.DateField(default=datetime.now, verbose_name=_('date'))
|
2017-08-22 19:02:43 +00:00
|
|
|
|
|
|
|
@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(
|
2018-01-15 17:55:03 +00:00
|
|
|
cls.TARGET_MODEL, verbose_name=_(cls.TARGET_VERBOSE_NAME))
|
2017-08-22 19:02:43 +00:00
|
|
|
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'))
|
2017-08-22 19:02:43 +00:00
|
|
|
status_field.contribute_to_class(cls, 'status')
|
|
|
|
|
|
|
|
def __str__(self):
|
2018-01-15 17:55:03 +00:00
|
|
|
return '%s: %s - %s' % (self.date.strftime('%Y-%m-%d'), self.status,
|
|
|
|
self.item)
|
2017-08-22 19:02:43 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2018-01-15 17:55:03 +00:00
|
|
|
ordering = ('-date', )
|