|
|
from django.db import models
|
|
|
from django.utils import timezone
|
|
|
|
|
|
NO_PARENT = -1
|
|
|
NO_IP = '0.0.0.0'
|
|
|
UNKNOWN_UA = ''
|
|
|
DIR_IMAGES = 'images'
|
|
|
|
|
|
class PostManager(models.Manager):
|
|
|
|
|
|
def create_post(self, title, text, image = None, parent_id = NO_PARENT,
|
|
|
ip = NO_IP):
|
|
|
post = self.create(title = title,
|
|
|
text = text,
|
|
|
pub_time = timezone.now(),
|
|
|
parent = parent_id,
|
|
|
image = image,
|
|
|
poster_ip = ip,
|
|
|
poster_user_agent = UNKNOWN_UA)
|
|
|
|
|
|
return post
|
|
|
|
|
|
def delete_post(self, post):
|
|
|
children = self.filter(parent = post.id)
|
|
|
for child in children:
|
|
|
self.delete_post(child)
|
|
|
post.delete()
|
|
|
|
|
|
def delete_posts_by_ip(self, ip):
|
|
|
posts = self.filter(poster_ip = ip)
|
|
|
for post in posts:
|
|
|
self.delete_post(post)
|
|
|
|
|
|
def get_threads(self, tag = None):
|
|
|
if tag is None:
|
|
|
threads = self.filter(parent = NO_PARENT)
|
|
|
else:
|
|
|
threads = self.filter(parent = NO_PARENT, tag = tag)
|
|
|
|
|
|
return threads
|
|
|
|
|
|
def get_thread(self, opening_post_id):
|
|
|
opening_post = self.get(opening_post_id)
|
|
|
replies = self.filter(parent = opening_post_id)
|
|
|
|
|
|
return [opening_post, replies]
|
|
|
|
|
|
def exists(self, post_id):
|
|
|
posts = Post.objects.filter(id = post_id)
|
|
|
|
|
|
return len(posts) == 0
|
|
|
|
|
|
def is_admin(self, user, passw):
|
|
|
return Admins.objects.filter(name = user, password = passw) is not None
|
|
|
|
|
|
|
|
|
class Tag(models.Model):
|
|
|
"""
|
|
|
A tag is a text node assigned to the post. The tag serves as a board
|
|
|
section. There can be multiple tags for each message
|
|
|
"""
|
|
|
|
|
|
name = models.CharField(max_length = 100)
|
|
|
|
|
|
|
|
|
class Post(models.Model):
|
|
|
"""A post is a message."""
|
|
|
|
|
|
objects = PostManager()
|
|
|
|
|
|
title = models.CharField(max_length = 100)
|
|
|
pub_time = models.DateTimeField()
|
|
|
text = models.TextField()
|
|
|
image = models.ImageField(upload_to = DIR_IMAGES)
|
|
|
poster_ip = models.IPAddressField()
|
|
|
poster_user_agent = models.TextField()
|
|
|
parent = models.BigIntegerField()
|
|
|
tags = models.ManyToManyField(Tag)
|
|
|
|
|
|
def __unicode__(self):
|
|
|
return self.title + ' (' + self.text + ')'
|
|
|
|
|
|
|
|
|
class Admins(models.Model):
|
|
|
"""
|
|
|
Model for admin users
|
|
|
"""
|
|
|
|
|
|
objects = PostManager()
|
|
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
password = models.CharField(max_length=100)
|
|
|
|
|
|
def __unicode__(self):
|
|
|
return self.name + '/' + '*' * len(self.password)
|
|
|
|
|
|
|