1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2010-2012 Cidadania S. Coop. Galega
5 # This file is part of e-cidadania.
7 # e-cidadania is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # e-cidadania is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with e-cidadania. If not, see <http://www.gnu.org/licenses/>.
20 from django
.db
import models
21 from django
.utils
.translation
import ugettext_lazy
as _
22 from django
.contrib
.auth
.models
import User
23 from django
.core
.exceptions
import ValidationError
25 from apps
.thirdparty
.tagging
.fields
import TagField
26 from apps
.thirdparty
.tagging
.models
import Tag
27 from core
.spaces
.models
import Space
28 from apps
.ecidadania
.proposals
.models
import *
32 ('users', _('Users')),
33 ('fixed', _('Fixed')),
34 ('none', _('No ponderation'))
38 class Poll(models
.Model
):
41 Data model for Polls. It stores the question and some data like the space
42 and dates. The most important field is "participants". It allows us to
43 limit the times a user can vote in a Poll, using it with the vote field in
46 .. versionadded:: 0.1.5
49 question
= models
.CharField(_('Question'), max_length
=200,
50 help_text
=_('Max: 200 characters'))
51 pub_date
= models
.DateTimeField(_('Date'), auto_now_add
=True)
52 poll_lastup
= models
.DateTimeField(_('Last update'), auto_now
=True)
53 author
= models
.ForeignKey(User
, verbose_name
=_('Author'), blank
=True,
54 null
=True, help_text
=_('Change the user that will figure as the \
55 author'), related_name
='poll-author')
56 participants
= models
.ManyToManyField(User
, blank
=True, null
=True)
57 space
= models
.ForeignKey(Space
, verbose_name
=_('Publish in'), blank
=True,
58 null
=True, help_text
=_('If you want to post to the index leave this \
60 poll_tags
= TagField(help_text
=_('Insert here relevant words related with \
62 start_date
= models
.DateField(_('Start date'), blank
=True, null
=True)
63 end_date
= models
.DateField(_('End date'), blank
=True, null
=True)
67 ('view', 'Can view the poll'),
70 def __unicode__(self
):
73 def set_tags(self
, tags
):
74 Tag
.objects
.update_tags(self
, tags
)
76 def get_tags(self
, tags
):
77 return Tag
.objects
.get_for_object(self
)
80 def get_absolute_url(self
):
81 if self
.space
is not None:
82 return ('view-polls', (), {
83 'space_url': self
.space
.url
,
84 'poll_id': str(self
.id)})
86 return ('view-polls', (), {
87 'poll_id': str(self
.id)})
90 if self
.start_date
> self
.end_date
:
91 raise ValidationError('The start date can not be after the end date.')
94 class Choice(models
.Model
):
95 poll
= models
.ForeignKey(Poll
)
96 choice_text
= models
.CharField(_('Choice'), max_length
=200, blank
=True, null
=True, help_text
=_('Enter choice to be voted upon'))
97 # votes = models.IntegerField(blank=True, null=True, default='0')
98 votes
= models
.ManyToManyField(User
, blank
=True, null
=True)
101 def get_absolute_url(self
):
102 if self
.space
is not None:
103 return ('view-polls', (), {
104 'space_url': self
.space
.url
,
105 'poll_id': str(self
.id)})
107 return ('view-polls', (), {
108 'poll_id': str(self
.id)})
111 class Voting(models
.Model
):
112 title
= models
.CharField(_('Title'), max_length
=200, unique
=True)
113 description
= models
.TextField(_('Description'), blank
=True, null
=True)
115 space
= models
.ForeignKey(Space
, blank
=True, null
=True)
116 date
= models
.DateTimeField(_('Date created'), auto_now_add
=True)
117 date_mod
= models
.DateTimeField(_('Last update'), auto_now
=True)
118 author
= models
.ForeignKey(User
, blank
=True, null
=True)
119 start_date
= models
.DateField(_('Start date'), blank
=True, null
=True)
120 end_date
= models
.DateField(_('End date'), blank
=True, null
=True)
121 ponderation
= models
.CharField(_('Ponderation'), max_length
=3, null
=True,
122 blank
=True, choices
=PONDERATIONS
)
124 proposalsets
= models
.ManyToManyField(ProposalSet
, blank
=True, null
=True)
126 proposals
= models
.ManyToManyField(Proposal
, blank
=True, null
=True, limit_choices_to
={'proposalset__isnull': True})
130 ('view', 'Can view the voting'),
134 def get_absolute_url(self
):
135 if self
.space
is not None:
136 return ('view-votings', (), {
137 'space_url': self
.space
.url
,
138 'voting_id': str(self
.id)})
140 return ('view-votings', (), {
141 'voting_id': str(self
.id)})
144 if self
.start_date
> self
.end_date
:
145 raise ValidationError('The start date can not be after the end date.')
148 class ConfirmVote(models
.Model
):
151 Intent data model. Intent stores the reference of a user-token when a user
152 asks entering in a restricted space.
154 .. versionadded: 0.1.5
156 user
= models
.ForeignKey(User
, blank
=True, null
=True)
157 proposal
= models
.ForeignKey(Proposal
, blank
=True, null
=True)
158 token
= models
.CharField(max_length
=32, blank
=True, null
=True)
159 requested_on
= models
.DateTimeField(auto_now_add
=True)
161 def get_approve_url(self
):
162 site
= Site
.objects
.all()[0]
163 return "http://%s%svote/approve/%s" % (site
.domain
, self
.proposal
.get_absolute_url(), self
.token
)