Give each comment form a unique prefix.
[matthijs/projects/xerxes.git] / influences / models.py
1 from django.db import models
2 from django.contrib.auth.models import User
3 from django.utils.translation import ugettext_lazy as _
4 from threadedcomments.models import ThreadedComment
5
6 # Create your models here.
7 class Character(models.Model):
8     STATUS_CHOICES = (
9         ('N', _('New')),
10         ('A', _('Approved')),
11     )
12     created     = models.DateField(auto_now_add=1, verbose_name = _("Creation time"))
13     modified    = models.DateField(auto_now=1, verbose_name = _("Modification time"))
14     name        = models.CharField(max_length=255, verbose_name = _("Name"))
15     status      = models.CharField(max_length=2, choices=STATUS_CHOICES, default='N', verbose_name = _("Status"))
16     player      = models.ForeignKey(User, verbose_name = _("Player"))
17
18     def __str__(self):
19         return self.name
20
21     class Meta:
22         verbose_name = _("Character")
23         verbose_name_plural = _("Characters")
24
25 class Influence(models.Model):
26     STATUS_CHOICES = (
27         ('N', _('New')),
28         ('U', _('Under discussion')),
29         ('P', _('Processing')),
30         ('D', _('Done')),
31     )
32     created     = models.DateField(auto_now_add=1, verbose_name = _("Creation time"))
33     modified    = models.DateField(auto_now=1, verbose_name = _("Modification time"))
34     
35     character   = models.ForeignKey(Character, verbose_name = _("Character"))
36     contact     = models.CharField(max_length=255, verbose_name = _("Contact Name"))
37     summary     = models.CharField(max_length=255, verbose_name = _("Summary"))
38     description = models.TextField(verbose_name = _("Description"))
39     status      = models.TextField(max_length=1, choices=STATUS_CHOICES, default='N', verbose_name = _("Status"))
40     longterm    = models.BooleanField(default=False, verbose_name = _("Long term"))
41
42     result      = models.TextField(blank=True,verbose_name = _("Result"))
43
44     def __str__(self):
45         return self.summary
46
47     def get_comments(self, private):
48         """
49         Gets the comments that have been made on this Influence. Each
50         comment gets its reply_form attribute set to a Form appropriate
51         for replying to the comment.
52         
53         If private is True, private comments are included in this list.
54         """
55         def quote_reply(comment):
56             return "\n".join(["> " + l for l in comment.comment.split("\n")])
57
58         # Import here to prevent dependency loop, since forms depends on
59         # models as well
60         from forms import get_influence_comment_form
61
62         if private:
63             comments = ThreadedComment.objects.get_tree(self)
64         else:
65             comments = ThreadedComment.public.get_tree(self)
66
67         # Annotate each comment with a proper reply form
68         for comment in comments:
69             initial = { 'comment' : quote_reply(comment) }
70             prefix = "reply-to-%s" % (comment.pk)
71             FormClass = get_influence_comment_form(private, comment)
72             comment.reply_form = FormClass(initial=initial,
73                                            prefix=prefix)
74         return comments
75
76     class Meta:
77         verbose_name = _("Influence")
78         verbose_name_plural = _("Influences")
79
80 # vim: set sts=4 sw=4 expandtab: