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):
16 (APPROVED, _('Approved')),
22 (PLAYER, _('Player')),
24 (CONTACT, _('Contact')),
26 created = models.DateField(auto_now_add=1, verbose_name = _("Creation time"))
27 modified = models.DateField(auto_now=1, verbose_name = _("Modification time"))
28 name = models.CharField(max_length=255, verbose_name = _("Name"))
29 status = models.CharField(max_length=2, choices=STATUS_CHOICES, default=NEW, verbose_name = _("Status"))
30 player = models.ForeignKey(User, verbose_name = _("Player"))
31 contacts = models.ManyToManyField('self', blank = True)
32 type = models.CharField(max_length=2, choices=TYPE_CHOICES, verbose_name=_("Type"))
34 def __unicode__(self):
37 def get_absolute_url(self):
38 return reverse('influences_influence_detail', kwargs={'object_id' : self.pk})
41 verbose_name = _("Character")
42 verbose_name_plural = _("Characters")
44 class Influence(models.Model):
51 (DISCUSSING, _('Under discussion')),
52 (PROCESSING, _('Processing')),
55 created = models.DateField(auto_now_add=1, verbose_name = _("Creation time"))
56 modified = models.DateField(auto_now=1, verbose_name = _("Modification time"))
58 initiator = models.ForeignKey(Character, verbose_name = _("Initiator"), related_name='initiated_influences')
59 other_contacts = models.CharField(max_length=255, blank = True, verbose_name = _("Other Contacts"))
60 other_characters = models.ManyToManyField(Character, blank = True, verbose_name = _("Involved characters"), related_name='influences_involved_in')
61 summary = models.CharField(max_length=255, verbose_name = _("Summary"))
62 description = models.TextField(verbose_name = _("Description"))
63 todo = models.TextField(blank=True, verbose_name = _("Todo"))
64 status = models.CharField(max_length=1, choices=STATUS_CHOICES, default=NEW, verbose_name = _("Status"))
65 longterm = models.BooleanField(default=False, verbose_name = _("Long term"))
67 result = models.TextField(blank=True,verbose_name = _("Result"))
69 def __unicode__(self):
72 def get_absolute_url(self):
73 return reverse('influences_influence_detail', kwargs={'object_id' : self.pk})
75 def get_comments(self, private):
77 Gets the comments that have been made on this Influence. Each
78 comment gets its reply_form attribute set to a Form appropriate
79 for replying to the comment.
81 If private is True, private comments are included in this list.
83 def quote_reply(comment):
84 regex = "^([ >]*)(.*)$"
85 text = rewrap(normalize_newlines(comment.comment), 72, regex)
86 return "\n".join(["> " + l for l in text.split("\n")])
88 # Import here to prevent dependency loop, since forms depends on
90 from forms import get_influence_comment_form
93 comments = ThreadedComment.objects.get_tree(self)
95 comments = ThreadedComment.public.get_tree(self)
97 # Annotate each comment with a proper reply form
98 for comment in comments:
99 initial = { 'comment' : quote_reply(comment) }
100 prefix = "reply-to-%s" % (comment.pk)
101 FormClass = get_influence_comment_form(private, comment)
102 comment.reply_form = FormClass(initial=initial,
108 """ Returns the Characters and contacts (strings) involved """
109 chars = list(self.other_characters.all())
110 if (self.other_contacts):
111 chars.extend(map(strip,self.other_contacts.split(',')))
115 def related_players(self):
116 """ Returns all players to this Influence (ie, the players of the
117 initiator or involved characters). Returns a dict where the
118 players (User objects) are keys and a list of Character objects
119 for which this player is related is the value.
121 players = {self.initiator.player : [self.initiator]}
122 for char in self.other_characters.all():
123 # Add this character to the player's list of characters for
124 # this Influence, creating a new list if this is the first
126 chars = players.get(char.player, [])
128 players[char.player] = chars
132 verbose_name = _("Influence")
133 verbose_name_plural = _("Influences")
135 # vim: set sts=4 sw=4 expandtab: