Add a Influence.related_players property.
authorMatthijs Kooijman <matthijs@stdin.nl>
Fri, 16 Jan 2009 21:16:34 +0000 (22:16 +0100)
committerMatthijs Kooijman <matthijs@stdin.nl>
Fri, 16 Jan 2009 21:16:34 +0000 (22:16 +0100)
This is a derived property that lists all the players that are involved
in an Influence, together with their Chracters.

influences/models.py

index c513c1a5c307e3f29b9365c12252fac42e8acc78..2c626266fde1312aa9b6445bd66ac647c6e4b112 100644 (file)
@@ -101,6 +101,23 @@ class Influence(models.Model):
         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")