##// END OF EJS Templates
Initial commit. One test doesn't work, missing posting form.
Initial commit. One test doesn't work, missing posting form.

File last commit:

r0:8aafc193 default
r0:8aafc193 default
Show More
models.py
67 lines | 1.7 KiB | text/x-python | PythonLexer
from django.db import models
from django.utils import timezone
from django.conf import settings
class BoardManager(models.Manager):
def create_thread(self, board):
thread = self.create(board=board, posts=0, pub_date=timezone.now())
return thread
def create_post(self, title, text, thread, image):
post = self.create(title=title, text=text, pub_date=timezone.now(),
thread=thread, image=image, poster_ip='0.0.0.0',
poster_user_agent='')
thread.posts = thread.posts + 1
return post
def delete_post(self, post):
thread = post.thread
post.delete()
thread.posts = thread.posts - 1
if (thread.is_empty):
thread.delete()
def delete_posts_by_ip(self, ip):
posts = self.filter(poster_ip=ip)
for post in posts:
self.delete_post(post)
class Board(models.Model):
name = models.CharField(max_length=10)
description = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Thread(models.Model):
objects = BoardManager()
board = models.ForeignKey(Board)
posts = models.IntegerField()
pub_date = models.DateTimeField()
def __unicode__(self):
return str(self.id)
def is_dead(self):
return self.posts >= settings.MAX_POSTS_PER_THREAD
def is_empty(self):
return self.posts == 0
class Post(models.Model):
objects = BoardManager()
title = models.CharField(max_length=100)
pub_date = models.DateTimeField()
text = models.TextField()
thread = models.ForeignKey(Thread)
image = models.ImageField(upload_to='images/src/')
poster_ip = models.IPAddressField()
poster_user_agent = models.TextField()
def __unicode__(self):
return self.title + ' (' + self.text + ')'