Source

pyweek / pyweek / challenge / models.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
import os

from django.core import validators
from django.db import models, connection, transaction
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

from stripogram import html2text
import datetime

# Maximum number of checksums per entry. Set to None to disable
MAX_CHECKSUMS = 5

# Time before the deadline when we switch to MD5-only
CUTOFF_TIME = 2*60*60

# Number of days allowed to upload against MD5
EXTRA_DAYS = 3

class UTC(datetime.tzinfo):
    def utcoffset(self, dt):
        return datetime.timedelta(0)
    def tzname(self, dt):
        return "UTC"
    def dst(self, dt):
        return datetime.timedelta(0)
UTC = UTC()

def pretty_time_diff(diff):
    minutes = int(diff.seconds / 60)
    hours = int(minutes / 60)
    minutes = minutes % 60

    l = []

    if diff.days > 1:
        l.append('%d days'%diff.days)
    if diff.days == 1:
        l.append('1 day')
    if hours > 1:
        l.append('%d hours'%hours)
    if hours == 1:
        l.append('1 hour')
    if minutes > 1:
        l.append('%d minutes'%minutes)
    if minutes == 1:
        l.append('1 minute')
    if not l:
        l.append('less than 1 minute')
    if len(l) == 1:
        return l[0]
    elif len(l) == 2:
        return '%s and %s'%tuple(l)
    return ', '.join(l[:-1]) + ' and ' + l[-1]

def participation():
    cursor = connection.cursor()

    # generate stats on participation
    data = {
        1: {'users': 155, 'entries': 110, 'finals': 26},
    }
    sql = '''
    select challenge_id, count(distinct(u.user_id))
    from challenge_entry,challenge_entry_users u, challenge_challenge c
    where entry_id=challenge_entry.name
      and c.number=challenge_id
      and challenge_id<1000
      and c.end < now() - interval '1 day'
    group by challenge_id '''
    cursor.execute(sql)
    for row in cursor.fetchall():
        data[row[0]] = {'users': row[1]}

    sql = '''
    select challenge_id, count(*)
      from challenge_entry, challenge_challenge c
     where challenge_id<1000
      and c.number=challenge_id
      and c.end < now() - interval '1 day'
    group by challenge_id'''
    cursor.execute(sql)
    for row in cursor.fetchall():
        data[row[0]]['entries'] = row[1]

    sql = '''
    select challenge_id, count(*)
      from challenge_entry, challenge_challenge c
     where challenge_id<1000
       and has_final=true
       and c.number=challenge_id
       and c.end < now() - interval '1 day'
    group by challenge_id'''
    cursor.execute(sql)
    for row in cursor.fetchall():
        data[row[0]]['finals'] = row[1]

    return data

# Create your models here.
class Challenge(models.Model):
    number = models.IntegerField(primary_key=True)
    title = models.CharField(max_length=100)
    start = models.DateField()
    end = models.DateField()
    motd = models.TextField()
    is_rego_open = models.BooleanField(default=False)
    theme_poll = models.ForeignKey('Poll', null=True, blank=True, related_name='poll_challenge')
    theme = models.CharField(max_length=100, null=True, blank=True, default='')
    torrent_url = models.CharField(max_length=255, null=True, blank=True, default='')

    class Meta:
        ordering = ['start']

    def __repr__(self):
        return '<Challenge %d: %r>' % (self.number, self.title)
    def __str__(self):
        return 'Challenge %d: %r' % (self.number, self.title)
    def __unicode__(self):
        return u'Challenge %d: %s' % (self.number, self.title.decode('utf8', 'replace'))

    def start_utc(self):
        return datetime.datetime(self.start.year, self.start.month,
            self.start.day, 0, 0, 0, 0)
    def end_utc(self):
        return datetime.datetime(self.end.year, self.end.month, self.end.day,
            0, 0, 0, 0)

    def countdown(self):
        now = datetime.datetime.utcnow()
        sd = self.start_utc()
        ed = self.end_utc()
        if now < sd:
            diff = sd - now
            event = 'challenge start'
        elif now < ed:
            diff = ed - now
            event = 'challenge end'
        elif now < ed + datetime.timedelta(14):
            diff = ed - now + datetime.timedelta(14)
            event = 'judging ends'
        else:
            return 'challenge finished'
        return '%s: %s'%(event, pretty_time_diff(diff))

    def summary(self):
        now = datetime.datetime.utcnow()
        sv = self.start_utc() - datetime.timedelta(7)
        sd = self.start_utc()
        ed = self.end_utc()
        if now < sv:
            diff = sd - now
            event = ['The %s challenge starts in'%self.title, '', '']
        elif now < sd:
            diff = sd - now
            event = ['Theme voting; challenge starts in', '',
                '(<a href="/p/%s">vote</a>)'%self.theme_poll.id]
        elif now < ed:
            diff = ed - now
            event = ['"%s" challenge underway;'%self.getTheme(),
                '', 'to go']
        elif now < ed + datetime.timedelta(14):
            diff = ed - now + datetime.timedelta(14)
            event = ['Judging ends in', '', '']
        else:
            return '"%s" challenge is finished'%self.getTheme()

        event[1] = pretty_time_diff(diff)
        return ' '.join(event)

    def setTheme(self):
        # close the poll
        poll = self.theme_poll
        poll.is_open = False
        poll.save()

        # and save off the winner
        self.theme = poll.instant_runoff_winner()
        self.save()
        return self.theme

    def getTheme(self):
        if self.theme:
            return self.theme
        else:
            return '[not chosen yet]'

    def pageTitle(self):
        if self.theme:
            return 'PyWeek &mdash; %s &mdash; %s'%(self.title, self.theme)
        else:
            return 'PyWeek &mdash; %s'%self.title

    def isCompComing(self):
        if self.is_rego_open: return True
        rego_date = self.start_utc() - datetime.timedelta(30)
        now = datetime.datetime.utcnow()
        return now < rego_date

    def isRegoOpen(self):
        if self.is_rego_open: return True
        now = datetime.datetime.utcnow()
        sd = self.start_utc()
        ed = self.end_utc()
        rego_date = sd - datetime.timedelta(30)
        end_rego_date = ed - datetime.timedelta(1)
        return rego_date <= now <= end_rego_date

    def isVotingOpen(self):
        if not self.theme_poll:
            return False
        sd = self.start_utc() - datetime.timedelta(7)
        ed = self.start_utc()
        now = datetime.datetime.utcnow()
        return sd <= now < ed

    def isCompStarted(self):
        sd = self.start_utc()
        now = datetime.datetime.utcnow()
        return now >= sd

    def isCompRunning(self):
        sd = self.start_utc()
        ed = self.end_utc()
        now = datetime.datetime.utcnow()
        return sd <= now <= ed

    def isCompFinished(self):
        ed = self.end_utc()
        now = datetime.datetime.utcnow()
        return now > ed

    def isUploadOpen(self):
        '''File upload is allowed in the following time range:

        from the start date to 24 hours after the end date
        from the end of judging onwards
        '''
        sd = self.start_utc()
        ed = self.end_utc() + datetime.timedelta(1)
        now = datetime.datetime.utcnow()
        end_date = ed + datetime.timedelta(13)
        ret = sd <= now <= ed or now > end_date
        return ret

    def isFinalUploadOpen(self):
        '''FINAL file upload is allowed in the following time range:

        from the start date to 24 hours after the end date
        '''
        sd = self.start_utc()
        ed = self.end_utc() + datetime.timedelta(15)
        now = datetime.datetime.utcnow()
        ret = sd <= now <= ed
        return ret

    def isGraceUploadTime(self):
        '''FINAL file upload during the 24 hours after the end date
        '''
        sd = self.end_utc()
        ed = self.end_utc() + datetime.timedelta(1)
        now = datetime.datetime.utcnow()
        return sd <= now <= ed

    def isRatingOpen(self):
        ed = self.end_utc() + datetime.timedelta(1)
        now = datetime.datetime.utcnow()
        end_date = self.end_utc() + datetime.timedelta(14)
        return ed < now <= end_date

    def isAllDone(self):
        ed = self.end_utc()
        now = datetime.datetime.utcnow()
        end_date = ed + datetime.timedelta(14)
        return now > end_date

    def timetableHTML(self):
        ''' Generate HTML rows for a timodels.Modelble display. '''
        l = []
        now = datetime.datetime.utcnow()
        def add(this, next, event):
            style = None
            if this <= now < next or (this <= now and next is None):
                style = 'active'
                event = event.split()
                event = ' '.join(event[:-1] + ['underway'])
            elif next is not None and now >= next:
                style = 'done'
            date = this.strftime('%A %Y/%m/%d')
            if style:
                l.append('<tr class="%s"><td>%s</td><td>%s</td>'%(style,
                    date, event))
            else:
                l.append('<tr><td>%s</td><td>%s</td>'%(date, event))
        sd = self.start_utc()
        ed = self.end_utc()
        this, next = sd - datetime.timedelta(30), sd - datetime.timedelta(7)
        add(this, next, 'Pre-registration open')
        this, next = next, sd
        add(this, next, 'Theme voting commences')
        this, next = next, ed
        add(this, next, 'Challenge start')
        this, next = next, ed + datetime.timedelta(14)
        add(this, next, 'Challenge end, judging begins')
        add(next, None, 'Judging closes, winners announced')
        return '\n'.join(l)

    def generate_tallies(self):
        # set up the rating tallies table for a challenge
        team = []
        individual = []
        for entry in Entry.objects.filter(challenge=self.number):
            if not entry.has_final: continue
            ratings = entry.tally_ratings()
            ratingtally = RatingTally(
                challenge=self,
                entry=entry,
                individual=not entry.is_team(),
                fun=ratings['fun'],
                innovation=ratings['innovation'],
                production=ratings['production'],
                overall=ratings['overall'],
                nonworking=int(ratings['nonworking']*100),
                disqualify=int(ratings['disqualify']*100),
                respondents=ratings['respondents'],
            )
            ratingtally.save()

            if entry.is_team():
                team.append((int(ratings['overall']*100), entry))
            else:
                individual.append((int(ratings['overall']*100), entry))

        # figure the winners
        team.sort()
        individual.sort()
        for l in (team, individual):
            n = l[-1][0]
            for s, e in l:
                if s == n:
                    e.winner = self
                    e.save()

    def fix_winners(self):
        # set up the rating tallies table for a challenge
        team = []
        individual = []
        for entry in Entry.objects.filter(challenge=self.number):
            if not entry.has_final: continue
            ratingtally = entry.ratingtally_set.all()[0]
            if entry.is_team():
                team.append((int(ratingtally.overall*100), entry))
            else:
                individual.append((int(ratingtally.overall*100), entry))

        # figure the winners
        team.sort()
        individual.sort()
        for l in (team, individual):
            n = l[-1][0]
            for s, e in l:
                if s == n:
                    e.winner = self
                    e.save()

    def individualWinners(self):
        return [e for e in Entry.objects.filter(winner=self.number) if not e.is_team()]

    def teamWinners(self):
        return [e for e in Entry.objects.filter(winner=self.number) if e.is_team()]

class Entry(models.Model):
    name = models.CharField(max_length=15, primary_key=True,
        validators=[validators.validate_slug])
    title = models.CharField(max_length=100)
    game = models.CharField(max_length=100)
    challenge = models.ForeignKey(Challenge, related_name='challenge')
    winner = models.ForeignKey(Challenge, blank=True, null=True, related_name='winner')
    user = models.ForeignKey(User, verbose_name='entry owner', related_name="owner")
    users = models.ManyToManyField(User)
    description = models.TextField()
    is_upload_open = models.BooleanField(default=False)
    has_final = models.BooleanField(default=False)

    class Meta:
        ordering = ['-challenge', 'name']
        verbose_name_plural = "entries"
        unique_together = (("challenge", "name"), ("challenge", "title"))

    def __repr__(self):
        return '<Entry %r>' % (self.name, )
    def __str__(self):
        return 'Entry "%s"' % (self.name, )
    def __unicode__(self):
        return u'Entry "%s"' % (self.name.decode('utf8', 'replace'), )

    def is_team(self):
        return len(self.users.all()) > 1

    def isUploadOpen(self):
        if self.is_upload_open:
            return True
        challenge = self.challenge
        return challenge.isUploadOpen()

    #def has_final(self):
        #return len(self.file_set.filter(is_final__exact=True,
            #is_screenshot__exact=False))

    def may_rate(self, user, challenge=None):
        # determine whether the current user is allowed to rate the entry
        if user.is_anonymous():
            return False
        if user in self.users.all():
            return False
        username = user.username
        if challenge is None:
            challenge = self.challenge
        for e in Entry.objects.filter(challenge=self.challenge, users__username__exact=username):
            if e.has_final:
                return True
        return False

    def has_rated(self, user):
        if user.is_anonymous():
            return False
        return len(self.rating_set.filter(user__username__exact=user.username))

    def tally_ratings(self):
        ratings = self.rating_set.all()
        info = {}
        for rating in 'fun innovation production disqualify nonworking overall'.split():
            info[rating] = 0
        n = 0
        for rating in ratings:
            info['disqualify'] += rating.disqualify
            info['nonworking'] += rating.nonworking
            if rating.nonworking:
                continue
            n += 1
            info['fun'] += rating.fun
            info['innovation'] += rating.innovation
            info['production'] += rating.production
        m = float(len(ratings))
        num_finished = sum([len(e.users.all())
                for e in Entry.objects.filter(challenge=self.challenge)
                    if e.has_final])
        if n:
            for rating in 'fun innovation production'.split():
                info[rating] /= float(n)
            info['disqualify'] /= float(num_finished)
            info['nonworking'] /= float(len(ratings))
            info['participants'] = len(ratings)/float(num_finished)
            info['overall'] = (info['fun'] + info['innovation'] +
                info['production'])/3
        info['respondents'] = n
        return info

RATING_CHOICES = ((1, 'Not at all'), (2,'Below average'),
    (3,'About average'), (4,'Above average'), (5,'Exceptional'))

class Rating(models.Model):
    entry = models.ForeignKey(Entry)
    user = models.ForeignKey(User)
    fun = models.PositiveIntegerField(choices=RATING_CHOICES, default=3)
    innovation = models.PositiveIntegerField(choices=RATING_CHOICES, default=3)
    production = models.PositiveIntegerField(choices=RATING_CHOICES, default=3)
    nonworking = models.BooleanField()
    disqualify = models.BooleanField()
    comment = models.TextField()
    created = models.DateTimeField()

    class Meta:
        get_latest_by = 'created'
        ordering = ['-created']
        unique_together = (("entry", "user"),)

    def __repr__(self):
        return '%r rating %r'%(self.user, self.entry)
    def __str__(self):
        return '%s rating %s'%(self.user, self.entry)
    def __unicode__(self):
        return u'%s rating %s'%(self.user.name.decode('utf8', 'replace'),
            self.entry)

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        super(Rating, self).save()

class RatingTally(models.Model):
    challenge = models.ForeignKey(Challenge)      # convenience
    entry = models.ForeignKey(Entry)
    individual = models.BooleanField()
    fun = models.FloatField()#max_digits=3, decimal_places=2)
    innovation = models.FloatField()#max_digits=3, decimal_places=2)
    production = models.FloatField()#max_digits=3, decimal_places=2)
    overall = models.FloatField()#max_digits=3, decimal_places=2)
    nonworking = models.PositiveIntegerField()
    disqualify = models.PositiveIntegerField()
    respondents = models.PositiveIntegerField()

    class Meta:
        ordering = ['challenge', 'individual', '-overall']
        verbose_name_plural = "RatingTallies"

    def __repr__(self):
        return '%r rating tally'%(self.entry, )
    def __str__(self):
        return '%s rating tally'%(self.entry, )
    def __unicode__(self):
        return u'%s rating tally'%(self.entry,)

class DiaryEntry(models.Model):
    challenge = models.ForeignKey(Challenge, blank=True, null=True)      # convenience
    entry = models.ForeignKey(Entry, blank=True, null=True)
    user = models.ForeignKey(User, related_name='author')
    title = models.CharField(max_length=100)
    content = models.TextField()
    created = models.DateTimeField()
    edited = models.DateTimeField(blank=True, null=True)
    activity = models.DateTimeField()
    actor = models.ForeignKey(User, related_name='actor')
    last_comment = models.ForeignKey('DiaryComment', blank=True, null=True)
    reply_count = models.PositiveIntegerField(default=0)
    sticky = models.BooleanField(default=False)
    is_pyggy = models.BooleanField(default=False)

    class Meta:
        get_latest_by = 'activity'
        ordering = ['-created', 'title']
        verbose_name_plural = "DiaryEntries"

    def __repr__(self):
        return '%r by %r'%(self.title, self.user)
    def __str__(self):
        return '%s by %s'%(self.title, self.user)
    def __unicode__(self):
        return u'%s by %s'%(self.title.decode('utf8', 'replace'),
            self.user.name.decode('utf8', 'replace'))

    def summary(self):
        ''' summary text - remove HTML and truncate '''
        text = html2text(self.content)
        if len(text) > 255:
            text = text[:252] + '...'
        return text

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        if self.activity == None:
            self.activity = datetime.datetime.utcnow()
        super(DiaryEntry, self).save()

    def get_absolute_url(self):
        return reverse("pyweek.challenge.views.message.diary_display", args=[self.id])

class DiaryComment(models.Model):
    challenge = models.ForeignKey(Challenge, blank=True, null=True)
    diary_entry = models.ForeignKey(DiaryEntry)
    user = models.ForeignKey(User)
    content = models.TextField()
    created = models.DateTimeField()
    edited = models.DateTimeField()

    class Meta:
        get_latest_by = 'created'
        ordering = ['created']

    def __repr__(self):
        return 'diary_comment-%r'%self.id
    __str__ = __repr__
    def __unicode__(self):
        return u'diary_comment-%r'%self.id

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        super(DiaryComment, self).save()

class File(models.Model):
    challenge = models.ForeignKey(Challenge)
    entry = models.ForeignKey(Entry)
    user = models.ForeignKey(User)
    thumb_width = models.PositiveIntegerField(default=0)
    def upload_location(instance, filename):
        return os.path.join(str(instance.challenge.number), str(instance.entry.name), filename)
    content = models.FileField(upload_to=upload_location)
    created = models.DateTimeField()
    description = models.CharField(max_length=255)
    is_final = models.BooleanField(default=False)
    is_screenshot = models.BooleanField(default=False)

    class Meta:
        get_latest_by = 'created'
        ordering = ['-created']


    def __repr__(self):
       return 'file for %r (%r)'%(self.entry, self.description)
    def __str__(self):
       return 'file for %s (%s)'%(self.entry, self.description)
    def __unicode__(self):
        return u'file for %s (%s)'%(self.entry.name.decode('utf8', 'replace'),
            self.description.decode('utf8', 'replace'))

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        super(File, self).save()

    def filename(self):
        return os.path.basename(self.content.name)

    def pretty_size(self):
        size = self.content.size
        unitSize= {'': 1, 'K': 1024.0, 'M': 1048576.0, 'G': 1073741824.0}
        if size < 1024:
            unit = ''
        elif size < 1048576:
            unit = 'K'
        elif size < 1073741824:
            unit = 'M'
        else:
            unit = 'G'
        sizeInUnit = size/unitSize[unit]
        return '%0.2f %sbytes'%(sizeInUnit, unit)

class Award(models.Model):
    creator = models.ForeignKey(User)
    created = models.DateTimeField()
    def upload_location(instance, filename):
        return os.path.join('awards', str(instance.creator.id), filename)
    content = models.FileField(upload_to=upload_location)
    description = models.CharField(max_length=255)

    class Meta:
        get_latest_by = 'created'
        ordering = ['-created']

    def __repr__(self):
        return 'award from %r (%r)'%(self.creator, self.description)
    def __str__(self):
        return 'award from %s (%s)'%(self.creator, self.description)
    def __unicode__(self):
        return u'award from %s (%s)'%(self.creator.name.decode('utf8', 'replace'),
            self.description.decode('utf8', 'replace'))

    def filename(self):
        return os.path.basename(self.get_content_filename())

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        super(Award, self).save()


class EntryAward(models.Model):
    creator = models.ForeignKey(User)
    created = models.DateTimeField()
    challenge = models.ForeignKey(Challenge)
    entry = models.ForeignKey(Entry)
    award = models.ForeignKey(Award)

    class Meta:
        get_latest_by = 'created'
        ordering = ['-created']


    def __repr__(self):
        return '%r to %r' % (self.award, self.entry)
    def __str__(self):
        return '%s to %s' % (self.award, self.entry)
    def __unicode__(self):
        return u'%s to %s' % (self.award, self.entry)

    def content(self):
        return self.award.content

    def description(self):
        return self.award.description

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        super(EntryAward, self).save()

class Checksum(models.Model):
    entry = models.ForeignKey(Entry)
    user = models.ForeignKey(User)
    created = models.DateTimeField()
    description = models.CharField(max_length=255)
    md5 = models.CharField(max_length=32, unique=True,
        validators=[validators.RegexValidator(
            '[0-9a-fA-F]{32}','Invalid md5 hash. Should be 32 hex digits')]
        )
    is_final = models.BooleanField(default=False)
    is_screenshot = models.BooleanField(default=False)

    class Meta:
        get_latest_by = 'created'
        ordering = ['-created']


    #def __repr__(self):
        #return 'MD5 hash %r' % (self.md5)
    #def __str__(self):
        #return 'MD5 hash %r' % (self.md5)

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        super(Checksum, self).save()

BEST_TEN = 0
SELECT_MANY = 1
INSTANT_RUNOFF = 2
POLL = 3
POLL_CHOICES = (
    (BEST_TEN, 'Ten Single Votes'),
    (SELECT_MANY, 'Select Many'),
    (INSTANT_RUNOFF, 'Instant-Runoff'),
    (POLL, 'Poll'),
)
class Poll(models.Model):
    challenge = models.ForeignKey(Challenge)
    title = models.CharField(max_length=100)
    description = models.TextField()
    created = models.DateTimeField()
    is_open = models.BooleanField()
    is_hidden = models.BooleanField()
    is_ongoing = models.BooleanField()
    type = models.IntegerField(choices=POLL_CHOICES)

    BEST_TEN=BEST_TEN
    SELECT_MANY=SELECT_MANY
    INSTANT_RUNOFF=INSTANT_RUNOFF
    POLL=POLL
    POLL_CHOICES=POLL_CHOICES

    class Meta:
        get_latest_by = 'created'
        ordering = ['-created']

    def __repr__(self):
        if self.challenge:
            return '<Poll %r challenge %r>' % (self.title, self.challenge)
        else:
            return '<Poll %r>' % (self.title, )
    def __str__(self):
        if self.challenge:
            return '<Poll %s challenge %s>' % (self.title, self.challenge)
        else:
            return '<Poll %s>' % (self.title, )
    def __unicode__(self):
        if self.challenge:
            return u'<Poll %s challenge %s>' % (self.title.decode('utf8',
                'replace'), self.challenge)
        else:
            return u'<Poll %s>' % (self.title.decode('utf8', 'replace'), )

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        super(Poll, self).save()

    def tally(self):
        ''' Figure the results of voting.

        Return [(option, percentage)]
        '''
        tally = {}
        responses = self.response_set.all()
        respondees = set()
        for response in responses:
            respondees.add(response.user_id)
            sum = tally.get(response.option_id, 0)
            if self.type == INSTANT_RUNOFF and response.value == 1:
                tally[response.option_id] = sum + 1
            elif self.type in (BEST_TEN, SELECT_MANY, POLL):
                tally[response.option_id] = sum + 1
        num_voters = len(respondees)

        # figure percentages
        if self.type == INSTANT_RUNOFF:
            for choice, num in tally.items():
                num = (100. * num / num_voters)
                tally[choice] = num
        return num_voters, tally

    def instant_runoff(self):
        '''Take the votes as recorded, and figure a majority winner
        according to instant-runoff rules.

        First choices are tallied. If no candidate has the support of a
        majority of voters, the candidate with the least support is
        eliminated. A second round of counting takes place, with the votes
        of supporters of the eliminated candidate now counting for their
        second choice candidate. After a candidate is eliminated, he or she
        may not receive any more votes.

        This process of counting and eliminating is repeated until one
        candidate has over half the votes. This is equivalent to continuing
        until there is only one candidate left.
        '''
        eliminated = {}

        # obtain a list of the voters
        responses = self.response_set.all()
        voters = {}
        for vote in responses:
            if not voters.has_key(vote.user_id):
                voters[vote.user_id] = {}
            voters[vote.user_id][vote.option_id] = int(vote.value)
        num_voters = len(voters)

        # now give each voter an ordered list of votes
        for voter, votes in voters.items():
            l = [(v,k) for (k,v) in votes.items()]
            l.sort()
            voters[voter] = l

        # now see if someone won
        erk = 0
        while erk < 10:
            erk += 1
            tally = {}
            for votes in voters.values():
                for n, choice in votes:
                    if eliminated.has_key(choice):
                        continue
                    tally[choice] = tally.get(choice, 0) + 1
                    break

            l = [(n,c) for (c,n) in tally.items()]
            l.sort()
            if l[-1][0] > num_voters/2.:
                break

            # eliminate, distribute votes
            eliminated[l[0][1]] = True

        # figure percentages
        for choice, num in tally.items():
            num = (100. * num / num_voters)
            tally[choice] = num

        return num_voters, tally

    def instant_runoff_winner(self):
        n, tally = self.instant_runoff()
        l = [(num, choice) for choice, num in tally.items()]
        l.sort()
        choice = Option.objects.get(pk=l[-1][1])
        return choice.text

class Option(models.Model):
    poll = models.ForeignKey(Poll) #, edit_inline=models.TABULAR, num_in_admin=5, num_extra_on_change=5)
    text = models.CharField(max_length=100)  #, core=True)

    class Meta:
        ordering = ['id']
        unique_together = (("poll", "text"),)

    def __repr__(self):
        return '<Poll %r Option %r>' % (self.poll, self.text)
    def __str__(self):
        return 'Poll %s Option "%s"' % (self.poll, self.text)
    def __unicode__(self):
        return u'Poll %s Option "%s"' % (self.poll, self.text.decode('utf8', 'replace'))

class Response(models.Model):
    poll = models.ForeignKey(Poll)
    option = models.ForeignKey(Option)
    user = models.ForeignKey(User)
    created = models.DateTimeField()
    value = models.IntegerField()

    class Meta:
        get_latest_by = 'created'
        unique_together = (("option", "user"),)

    def __repr__(self):
        if self.value:
            return '%r chose %r (%r)'%(self.user, self.option,
                self.value)
        else:
            return '%r chose %r'%(self.user, self.option)
    __unicode__ = __str__ = __repr__

    def save(self):
        if self.created == None:
            self.created = datetime.datetime.utcnow()
        super(Response, self).save()

class UserProfile(models.Model):
    user = models.ForeignKey(User)
    content = models.TextField()