snipt/snipts/models.py

54 lines
1.6 KiB
Python
Raw Normal View History

2011-10-01 10:23:05 -07:00
from django.template.defaultfilters import slugify
from django.contrib.auth.models import User
from django.db import models
2011-06-05 09:51:56 -07:00
from taggit.managers import TaggableManager
class Snipt(models.Model):
2011-10-01 10:23:05 -07:00
"""An individual Snipt."""
2011-06-05 09:51:56 -07:00
user = models.ForeignKey(User)
title = models.CharField(max_length=255)
2011-10-02 11:02:13 -07:00
description = models.TextField(blank=True, null=True)
slug = models.SlugField(max_length=255, blank=True)
2011-06-05 09:51:56 -07:00
tags = TaggableManager()
lexer = models.CharField(max_length=50)
code = models.TextField()
stylized = models.TextField()
key = models.CharField(max_length=100)
public = models.BooleanField(default=False)
# TODO Set auto_now_add back to True for production!
created = models.DateTimeField(auto_now_add=False, editable=False)
2011-10-02 13:07:47 -07:00
modified = models.DateTimeField(auto_now=False, editable=False)
2011-10-01 10:23:05 -07:00
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)[:50]
2011-10-02 11:02:13 -07:00
return super(Snipt, self).save(*args, **kwargs)
2011-10-01 10:23:05 -07:00
def __unicode__(self):
2011-10-01 10:23:05 -07:00
return self.title
2011-06-05 08:55:27 -07:00
2011-10-01 16:09:59 -07:00
def get_absolute_url(self):
2011-10-02 13:07:47 -07:00
return "/%s/%s/" % (self.user.username, self.slug)
2011-10-01 16:09:59 -07:00
2011-06-05 08:55:27 -07:00
class Comment(models.Model):
"""A comment on a Snipt"""
2011-06-05 09:51:56 -07:00
user = models.ForeignKey(User)
snipt = models.ForeignKey(Snipt)
2011-06-05 08:55:27 -07:00
comment = models.TextField()
# TODO Set auto_now_add back to True for production!
2011-10-02 11:02:13 -07:00
created = models.DateTimeField(auto_now_add=False, editable=False)
2011-10-02 13:07:47 -07:00
modified = models.DateTimeField(auto_now=False, editable=False)
2011-10-01 10:23:05 -07:00
def __unicode__(self):
return u'%s on %s' %(self.user, self.snipt)