2011-01-24 02:08:16 +03:00
|
|
|
from django.db import models
|
|
|
|
from django.template.defaultfilters import slugify
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
def uuid_str():
|
|
|
|
return str(uuid.uuid1())
|
|
|
|
|
|
|
|
|
|
|
|
RATING_CHOICES = ((0, 'Awful'),
|
|
|
|
(1, 'Poor'),
|
|
|
|
(2, 'OK'),
|
|
|
|
(3, 'Good'),
|
|
|
|
(4, 'Excellent'))
|
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
MAX_POSTS = 10
|
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
class BlogPost(models.Model):
|
|
|
|
key = models.CharField(primary_key=True, max_length=64, default=uuid_str, editable=False)
|
|
|
|
title = models.CharField(max_length=128)
|
|
|
|
content = models.TextField()
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
slug = models.SlugField(editable=False, default='')
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2011-06-15 17:09:01 +04:00
|
|
|
"""
|
|
|
|
For the purposes of the sandbox, limit the maximum number of stored models.
|
|
|
|
"""
|
2011-01-24 02:08:16 +03:00
|
|
|
self.slug = slugify(self.title)
|
|
|
|
super(self.__class__, self).save(*args, **kwargs)
|
2011-05-16 17:11:36 +04:00
|
|
|
for obj in self.__class__.objects.order_by('-created')[MAX_POSTS:]:
|
2011-03-06 15:26:19 +03:00
|
|
|
obj.delete()
|
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
class Comment(models.Model):
|
|
|
|
blogpost = models.ForeignKey(BlogPost, editable=False, related_name='comments')
|
|
|
|
username = models.CharField(max_length=128)
|
|
|
|
comment = models.TextField()
|
|
|
|
rating = models.IntegerField(blank=True, null=True, choices=RATING_CHOICES, help_text='How did you rate this post?')
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
|