X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=influences%2Fmodels.py;h=4c14f7cb592b7af3cd31a1b09e055f9c909a2ada;hb=d8acd479311086f3372ed61688a7468356198a04;hp=aa64f269fbca96db6bf6d5479775fed9d0465a11;hpb=d2c32e093a5f0bee8f4188cb7921de62051951be;p=matthijs%2Fprojects%2Fxerxes.git diff --git a/influences/models.py b/influences/models.py index aa64f26..4c14f7c 100644 --- a/influences/models.py +++ b/influences/models.py @@ -5,24 +5,30 @@ from django.utils.text import normalize_newlines from django.utils.translation import ugettext_lazy as _ from threadedcomments.models import ThreadedComment from xerxes.tools.text import rewrap +from string import strip # Create your models here. class Character(models.Model): + NEW = 'N' + APPROVED = 'A' STATUS_CHOICES = ( - ('N', _('New')), - ('A', _('Approved')), + (NEW, _('New')), + (APPROVED, _('Approved')), ) + PLAYER = 'P' + NPC = 'N' + CONTACT = 'C' TYPE_CHOICES = ( - ('P', _('Player')), - ('N', _('NPC')), - ('C', _('Contact')), + (PLAYER, _('Player')), + (NPC, _('NPC')), + (CONTACT, _('Contact')), ) created = models.DateField(auto_now_add=1, verbose_name = _("Creation time")) modified = models.DateField(auto_now=1, verbose_name = _("Modification time")) name = models.CharField(max_length=255, verbose_name = _("Name")) - status = models.CharField(max_length=2, choices=STATUS_CHOICES, default='N', verbose_name = _("Status")) + status = models.CharField(max_length=2, choices=STATUS_CHOICES, default=NEW, verbose_name = _("Status")) player = models.ForeignKey(User, verbose_name = _("Player")) - contacts = models.ManyToManyField('self') + contacts = models.ManyToManyField('self', blank = True) type = models.CharField(max_length=2, choices=TYPE_CHOICES, verbose_name=_("Type")) def __unicode__(self): @@ -36,11 +42,15 @@ class Character(models.Model): verbose_name_plural = _("Characters") class Influence(models.Model): + NEW = 'N' + DISCUSSING = 'U' + PROCESSING = 'P' + DONE = 'D' STATUS_CHOICES = ( - ('N', _('New')), - ('U', _('Under discussion')), - ('P', _('Processing')), - ('D', _('Done')), + (NEW, _('New')), + (DISCUSSING, _('Under discussion')), + (PROCESSING, _('Processing')), + (DONE, _('Done')), ) created = models.DateField(auto_now_add=1, verbose_name = _("Creation time")) modified = models.DateField(auto_now=1, verbose_name = _("Modification time")) @@ -50,8 +60,8 @@ class Influence(models.Model): other_characters = models.ManyToManyField(Character, blank = True, verbose_name = _("Involved characters"), related_name='influences_involved_in') summary = models.CharField(max_length=255, verbose_name = _("Summary")) description = models.TextField(verbose_name = _("Description")) - todo = models.TextField(verbose_name = _("Todo")) - status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='N', verbose_name = _("Status")) + todo = models.TextField(blank=True, verbose_name = _("Todo")) + status = models.CharField(max_length=1, choices=STATUS_CHOICES, default=NEW, verbose_name = _("Status")) longterm = models.BooleanField(default=False, verbose_name = _("Long term")) result = models.TextField(blank=True,verbose_name = _("Result")) @@ -93,6 +103,31 @@ class Influence(models.Model): prefix=prefix) return comments + @property + def involved(self): + """ Returns the Characters and contacts (strings) involved """ + chars = list(self.other_characters.all()) + if (self.other_contacts): + chars.extend(map(strip,self.other_contacts.split(','))) + return chars + + @property + def related_players(self): + """ Returns all players to this Influence (ie, the players of the + initiator or involved characters). Returns a dict where the + players (User objects) are keys and a list of Character objects + for which this player is related is the value. + """ + players = {self.initiator.player : [self.initiator]} + for char in self.other_characters.all(): + # Add this character to the player's list of characters for + # this Influence, creating a new list if this is the first + # character. + chars = players.get(char.player, []) + chars.append(char) + players[char.player] = chars + return players + class Meta: verbose_name = _("Influence") verbose_name_plural = _("Influences")