view gpp/bio/models.py @ 124:9c18250972d5

Refactored the markdown/smiley logic. Created classes for Markdown and Smilify. No longer call render_to_string() in models.py for various models.
author Brian Neal <bgneal@gmail.com>
date Sat, 14 Nov 2009 04:32:32 +0000
parents f8f4514b806a
children 48621ba5c385
line wrap: on
line source
"""
Contains models for the bio application.
I would have picked profile for this application, but that is already taken, apparently.
"""

import os.path

from django.db import models
from django.contrib import auth
from django.conf import settings
from django.core.cache import cache

from core.markup import Markdown
from smiley import Smilify


def avatar_file_path_for_user(username, filename):
    return os.path.join(settings.AVATAR_DIR, 'users', username, filename)

def avatar_file_path(instance, filename):
    return avatar_file_path_for_user(instance.user.username, filename)


class UserProfile(models.Model):
    """model to represent additional information about users"""

    user = models.ForeignKey(auth.models.User, unique=True)
    location = models.CharField(max_length=128, blank=True)
    birthday = models.DateField(blank=True, null=True,
            help_text='Optional; the year is not shown to others')
    occupation = models.CharField(max_length=128, blank=True)
    interests = models.CharField(max_length=255, blank=True)
    profile_text = models.TextField(blank=True)
    profile_html = models.TextField(blank=True)
    hide_email = models.BooleanField(default=True)
    signature = models.TextField(blank=True)
    signature_html = models.TextField(blank=True)
    avatar = models.ImageField(upload_to=avatar_file_path, blank=True)
    time_zone = models.CharField(max_length=64, blank=True,
            default='US/Pacific')
    use_24_time = models.BooleanField(default=False)
    forum_post_count = models.IntegerField(default=0)

    def __unicode__(self):
        return self.user.username

    class Meta:
        ordering = ('user__username', )

    def save(self, *args, **kwargs):
        md = Markdown()
        sm = Smilify()
        self.profile_html = sm.convert(md.convert(self.profile_text))
        self.signature_html = sm.convert(md.convert(self.signature))
        super(UserProfile, self).save(*args, **kwargs)
        cache.delete('avatar_' + self.user.username)