annotate gpp/bio/models.py @ 210:77b606e0ad5f

Fix #78: improve the download upload_to path.
author Brian Neal <bgneal@gmail.com>
date Fri, 07 May 2010 02:56:49 +0000
parents b4305e18d3af
children 8c1832b9d815
rev   line source
gremmie@1 1 """
gremmie@1 2 Contains models for the bio application.
gremmie@1 3 I would have picked profile for this application, but that is already taken, apparently.
gremmie@1 4 """
gremmie@1 5
gremmie@1 6 import os.path
gremmie@1 7
gremmie@1 8 from django.db import models
gremmie@1 9 from django.contrib import auth
gremmie@1 10 from django.conf import settings
bgneal@54 11 from django.core.cache import cache
gremmie@1 12
bgneal@128 13 from core.markup import SiteMarkup
bgneal@124 14
gremmie@1 15
bgneal@147 16 (STA_ACTIVE, STA_RESIGNED, STA_REMOVED, STA_SUSPENDED, STA_SPAMMER) = range(5)
bgneal@147 17
bgneal@147 18 USER_STATUS_CHOICES = (
bgneal@147 19 (STA_ACTIVE, "Active"),
bgneal@147 20 (STA_RESIGNED, "Resigned"),
bgneal@147 21 (STA_REMOVED, "Removed"),
bgneal@147 22 (STA_SUSPENDED, "Suspended"),
bgneal@147 23 (STA_SPAMMER, "Spammer"),
bgneal@147 24 )
bgneal@147 25
bgneal@204 26
bgneal@204 27 class Badge(models.Model):
bgneal@204 28 """This model represents badges that users can earn."""
bgneal@204 29 image = models.ImageField(upload_to='badges')
bgneal@204 30 name = models.CharField(max_length=64)
bgneal@204 31 description = models.TextField(blank=True)
bgneal@204 32 order = models.IntegerField()
bgneal@204 33 numeric_id = models.IntegerField(db_index=True)
bgneal@204 34
bgneal@204 35 class Meta:
bgneal@204 36 ordering = ('order', )
bgneal@204 37
bgneal@204 38 def __unicode__(self):
bgneal@204 39 return self.name
bgneal@204 40
bgneal@204 41 def get_absolute_url(self):
bgneal@204 42 return self.image.url
bgneal@204 43
bgneal@204 44 def html(self):
bgneal@204 45 """Returns a HTML img tag representation of the badge."""
bgneal@204 46 if self.image:
bgneal@204 47 return u'<img src="%s" alt="%s" title="%s" />' % (
bgneal@204 48 self.get_absolute_url(), self.name, self.name)
bgneal@204 49 return u''
bgneal@204 50 html.allow_tags = True
bgneal@204 51
bgneal@204 52
gremmie@1 53 def avatar_file_path_for_user(username, filename):
gremmie@1 54 return os.path.join(settings.AVATAR_DIR, 'users', username, filename)
gremmie@1 55
gremmie@1 56 def avatar_file_path(instance, filename):
gremmie@1 57 return avatar_file_path_for_user(instance.user.username, filename)
gremmie@1 58
gremmie@1 59
gremmie@1 60 class UserProfile(models.Model):
gremmie@1 61 """model to represent additional information about users"""
gremmie@1 62
gremmie@1 63 user = models.ForeignKey(auth.models.User, unique=True)
gremmie@1 64 location = models.CharField(max_length=128, blank=True)
gremmie@1 65 birthday = models.DateField(blank=True, null=True,
gremmie@1 66 help_text='Optional; the year is not shown to others')
gremmie@1 67 occupation = models.CharField(max_length=128, blank=True)
gremmie@1 68 interests = models.CharField(max_length=255, blank=True)
gremmie@1 69 profile_text = models.TextField(blank=True)
gremmie@1 70 profile_html = models.TextField(blank=True)
gremmie@1 71 hide_email = models.BooleanField(default=True)
gremmie@1 72 signature = models.TextField(blank=True)
gremmie@1 73 signature_html = models.TextField(blank=True)
gremmie@1 74 avatar = models.ImageField(upload_to=avatar_file_path, blank=True)
bgneal@70 75 time_zone = models.CharField(max_length=64, blank=True,
bgneal@70 76 default='US/Pacific')
bgneal@120 77 use_24_time = models.BooleanField(default=False)
bgneal@96 78 forum_post_count = models.IntegerField(default=0)
bgneal@147 79 status = models.IntegerField(default=STA_ACTIVE,
bgneal@147 80 choices=USER_STATUS_CHOICES)
bgneal@147 81 status_date = models.DateTimeField(auto_now_add=True)
bgneal@204 82 badges = models.ManyToManyField(Badge, through="BadgeOwnership")
gremmie@1 83
gremmie@1 84 def __unicode__(self):
gremmie@1 85 return self.user.username
gremmie@1 86
gremmie@1 87 class Meta:
gremmie@1 88 ordering = ('user__username', )
gremmie@1 89
gremmie@1 90 def save(self, *args, **kwargs):
bgneal@128 91 sm = SiteMarkup()
bgneal@128 92 self.profile_html = sm.convert(self.profile_text)
bgneal@128 93 self.signature_html = sm.convert(self.signature)
gremmie@1 94 super(UserProfile, self).save(*args, **kwargs)
bgneal@54 95 cache.delete('avatar_' + self.user.username)
bgneal@138 96
bgneal@138 97 @models.permalink
bgneal@138 98 def get_absolute_url(self):
bgneal@138 99 return ('bio-view_profile', (), {'username': self.user.username})
bgneal@138 100
bgneal@204 101 def badge_ownership(self):
bgneal@204 102 if hasattr(self, '_badges'):
bgneal@204 103 return self._badges
bgneal@204 104 self._badges = BadgeOwnership.objects.filter(profile=self).select_related(
bgneal@204 105 "badge")
bgneal@204 106 return self._badges
bgneal@204 107
bgneal@138 108
bgneal@138 109 class UserProfileFlag(models.Model):
bgneal@138 110 """This model represents a user flagging a profile as inappropriate."""
bgneal@138 111 user = models.ForeignKey(auth.models.User)
bgneal@138 112 profile = models.ForeignKey(UserProfile)
bgneal@138 113 flag_date = models.DateTimeField(auto_now_add=True)
bgneal@138 114
bgneal@138 115 def __unicode__(self):
bgneal@138 116 return u"%s's profile flagged by %s" % (self.profile.user.username,
bgneal@138 117 self.user.username)
bgneal@138 118
bgneal@138 119 class Meta:
bgneal@138 120 ordering = ('flag_date', )
bgneal@138 121
bgneal@138 122 def get_profile_url(self):
bgneal@138 123 return '<a href="%s">Profile</a>' % self.profile.get_absolute_url()
bgneal@138 124 get_profile_url.allow_tags = True
bgneal@204 125
bgneal@204 126
bgneal@204 127 class BadgeOwnership(models.Model):
bgneal@204 128 """This model represents the ownership of badges by users."""
bgneal@204 129 profile = models.ForeignKey(UserProfile)
bgneal@204 130 badge = models.ForeignKey(Badge)
bgneal@204 131 count = models.IntegerField(default=1)
bgneal@204 132
bgneal@204 133 class Meta:
bgneal@204 134 verbose_name_plural = "badge ownership"
bgneal@204 135 ordering = ('badge__order', )
bgneal@204 136
bgneal@204 137 def __unicode__(self):
bgneal@204 138 if self.count == 1:
bgneal@204 139 return u"%s owns 1 %s" % (self.profile.user.username,
bgneal@204 140 self.badge.name)
bgneal@204 141 else:
bgneal@204 142 return u"%s owns %d %s badges" % (self.profile.user.username,
bgneal@204 143 self.count, self.badge.name)
bgneal@204 144
bgneal@204 145 def badge_count_str(self):
bgneal@204 146 if self.count == 1:
bgneal@204 147 return u"1 %s" % self.badge.name
bgneal@204 148 return u"%d %ss" % (self.count, self.badge.name)