~widelands-dev/widelands-website/django_staticfiles

99 by Holger Rapp
First draft version of wlpoll, a poll application
1
from django.db import models
281 by Holger Rapp
User can no only vote once on polls
2
from django.contrib.auth.models import User
489.1.16 by franku
run through django1.111 deprecations
3
from django.urls import reverse
99 by Holger Rapp
First draft version of wlpoll, a poll application
4
import datetime
5
438.1.6 by franku
run the script
6
404.2.11 by franku
little changes
7
def closed_date_default():
404.2.26 by franku
fixed creating of polls
8
    return datetime.datetime.now() + datetime.timedelta(days=90)
404.2.11 by franku
little changes
9
438.1.6 by franku
run the script
10
100 by Holger Rapp
Finished simple voting application
11
class PollManager(models.Manager):
438.1.6 by franku
run the script
12
100 by Holger Rapp
Finished simple voting application
13
    def open(self):
404.2.50 by franku
use datetime.now() instead of datetime.now method for querys in polls; fixes also django1.9 warning
14
        return self.all().exclude(closed_date__lte=datetime.datetime.now())
100 by Holger Rapp
Finished simple voting application
15
438.1.6 by franku
run the script
16
99 by Holger Rapp
First draft version of wlpoll, a poll application
17
class Poll(models.Model):
18
    name = models.CharField(max_length=256)
438.1.6 by franku
run the script
19
    pub_date = models.DateTimeField(
20
        'date published', default=datetime.datetime.now)
21
    closed_date = models.DateTimeField('date closed', default=closed_date_default,
404.2.11 by franku
little changes
22
                                       blank=True, null=True)
277 by Holger Rapp
Replaced google chart api through highcharts
23
100 by Holger Rapp
Finished simple voting application
24
    objects = PollManager()
438.1.6 by franku
run the script
25
99 by Holger Rapp
First draft version of wlpoll, a poll application
26
    def total_votes(self):
438.1.6 by franku
run the script
27
        return self.choices.all().aggregate(models.Sum('votes'))['votes__sum']
277 by Holger Rapp
Replaced google chart api through highcharts
28
281 by Holger Rapp
User can no only vote once on polls
29
    def has_user_voted(self, u):
30
        return u.poll_votes.filter(poll=self).count() > 0
31
100 by Holger Rapp
Finished simple voting application
32
    def is_closed(self):
33
        if self.closed_date is None:
34
            return False
35
        return self.closed_date < datetime.datetime.now()
277 by Holger Rapp
Replaced google chart api through highcharts
36
100 by Holger Rapp
Finished simple voting application
37
    def get_absolute_url(self):
489.1.16 by franku
run through django1.111 deprecations
38
        return reverse('wlpoll_detail', kwargs={'pk': self.id})
99 by Holger Rapp
First draft version of wlpoll, a poll application
39
40
    def __unicode__(self):
41
        return self.name
42
438.1.6 by franku
run the script
43
99 by Holger Rapp
First draft version of wlpoll, a poll application
44
class Choice(models.Model):
438.1.6 by franku
run the script
45
    poll = models.ForeignKey(Poll, related_name='choices')
99 by Holger Rapp
First draft version of wlpoll, a poll application
46
    choice = models.CharField(max_length=256)
47
    votes = models.PositiveIntegerField(default=0)
48
49
    def __unicode__(self):
438.1.6 by franku
run the script
50
        return u"%i:%s" % (self.votes, self.choice)
51
281 by Holger Rapp
User can no only vote once on polls
52
53
class Vote(models.Model):
438.1.6 by franku
run the script
54
    user = models.ForeignKey(User, related_name='poll_votes')
281 by Holger Rapp
User can no only vote once on polls
55
    poll = models.ForeignKey(Poll)
56
    choice = models.ForeignKey(Choice)
438.1.6 by franku
run the script
57
    date_voted = models.DateTimeField(
58
        'voted at', default=datetime.datetime.now)