1 from django.db import models
2 from django.core.urlresolvers import reverse
3 from django.contrib.auth.models import User
4 from django.utils.text import normalize_newlines
5 from django.utils.translation import ugettext_lazy as _
6 from threadedcomments.models import ThreadedComment
7 from xerxes.tools.text import rewrap
8 from string import strip
10 # Create your models here.
11 class Character(models.Model):
21 created = models.DateField(auto_now_add=1, verbose_name = _("Creation time"))
22 modified = models.DateField(auto_now=1, verbose_name = _("Modification time"))
23 name = models.CharField(max_length=255, verbose_name = _("Name"))
24 status = models.CharField(max_length=2, choices=STATUS_CHOICES, default='N', verbose_name = _("Status"))
25 player = models.ForeignKey(User, verbose_name = _("Player"))
26 contacts = models.ManyToManyField('self', blank = True)
27 type = models.CharField(max_length=2, choices=TYPE_CHOICES, verbose_name=_("Type"))
29 def __unicode__(self):
32 def get_absolute_url(self):
33 return reverse('influences_influence_detail', kwargs={'object_id' : self.pk})
36 verbose_name = _("Character")
37 verbose_name_plural = _("Characters")
39 class Influence(models.Model):
42 ('U', _('Under discussion')),
43 ('P', _('Processing')),
46 created = models.DateField(auto_now_add=1, verbose_name = _("Creation time"))
47 modified = models.DateField(auto_now=1, verbose_name = _("Modification time"))
49 initiator = models.ForeignKey(Character, verbose_name = _("Initiator"), related_name='initiated_influences')
50 other_contacts = models.CharField(max_length=255, blank = True, verbose_name = _("Other Contacts"))
51 other_characters = models.ManyToManyField(Character, blank = True, verbose_name = _("Involved characters"), related_name='influences_involved_in')
52 summary = models.CharField(max_length=255, verbose_name = _("Summary"))
53 description = models.TextField(verbose_name = _("Description"))
54 todo = models.TextField(blank=True, verbose_name = _("Todo"))
55 status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='N', verbose_name = _("Status"))
56 longterm = models.BooleanField(default=False, verbose_name = _("Long term"))
58 result = models.TextField(blank=True,verbose_name = _("Result"))
60 def __unicode__(self):
63 def get_absolute_url(self):
64 return reverse('influences_influence_detail', kwargs={'object_id' : self.pk})
66 def get_comments(self, private):
68 Gets the comments that have been made on this Influence. Each
69 comment gets its reply_form attribute set to a Form appropriate
70 for replying to the comment.
72 If private is True, private comments are included in this list.
74 def quote_reply(comment):
75 regex = "^([ >]*)(.*)$"
76 text = rewrap(normalize_newlines(comment.comment), 72, regex)
77 return "\n".join(["> " + l for l in text.split("\n")])
79 # Import here to prevent dependency loop, since forms depends on
81 from forms import get_influence_comment_form
84 comments = ThreadedComment.objects.get_tree(self)
86 comments = ThreadedComment.public.get_tree(self)
88 # Annotate each comment with a proper reply form
89 for comment in comments:
90 initial = { 'comment' : quote_reply(comment) }
91 prefix = "reply-to-%s" % (comment.pk)
92 FormClass = get_influence_comment_form(private, comment)
93 comment.reply_form = FormClass(initial=initial,
99 """ Returns the Characters and contacts (strings) involved """
100 chars = list(self.other_characters.all())
101 if (self.other_contacts):
102 chars.extend(map(strip,self.other_contacts.split(',')))
106 def related_players(self):
107 """ Returns all players to this Influence (ie, the players of the
108 initiator or involved characters). Returns a dict where the
109 players (User objects) are keys and a list of Character objects
110 for which this player is related is the value.
112 players = {self.initiator.player : [self.initiator]}
113 for char in self.other_characters.all():
114 # Add this character to the player's list of characters for
115 # this Influence, creating a new list if this is the first
117 chars = players.get(char.player, [])
119 players[char.player] = chars
123 verbose_name = _("Influence")
124 verbose_name_plural = _("Influences")
126 # vim: set sts=4 sw=4 expandtab: