models.py
109 lines
| 2.9 KiB
| text/x-python
|
PythonLexer
/ boards / models.py
|
r0 | from django.db import models | ||
from django.utils import timezone | ||||
|
r4 | |||
|
r22 | from neboard import settings | ||
import thumbs | ||||
|
r4 | NO_PARENT = -1 | ||
NO_IP = '0.0.0.0' | ||||
UNKNOWN_UA = '' | ||||
|
r22 | |||
|
r0 | |||
|
r2 | class PostManager(models.Manager): | ||
|
r22 | def create_post(self, title, text, image=None, parent_id=NO_PARENT, | ||
|
r24 | ip=NO_IP, tags=None): | ||
|
r22 | post = self.create(title=title, | ||
text=text, | ||||
pub_time=timezone.now(), | ||||
parent=parent_id, | ||||
image=image, | ||||
poster_ip=ip, | ||||
|
r25 | poster_user_agent=UNKNOWN_UA, | ||
last_edit_time=timezone.now()) | ||||
|
r0 | |||
|
r24 | if tags: | ||
for tag in tags: | ||||
post.tags.add(tag) | ||||
|
r25 | if parent_id != NO_PARENT: | ||
parent = self.get(id=parent_id) | ||||
parent.last_edit_time=timezone.now() | ||||
parent.save() | ||||
|
r1 | return post | ||
|
r0 | |||
def delete_post(self, post): | ||||
|
r22 | children = self.filter(parent=post.id) | ||
|
r3 | for child in children: | ||
self.delete_post(child) | ||||
|
r2 | post.delete() | ||
|
r0 | def delete_posts_by_ip(self, ip): | ||
|
r22 | posts = self.filter(poster_ip=ip) | ||
|
r1 | for post in posts: | ||
self.delete_post(post) | ||||
|
r0 | |||
|
r22 | def get_threads(self, tag=None): | ||
|
r5 | if tag is None: | ||
|
r22 | threads = self.filter(parent=NO_PARENT) | ||
|
r5 | else: | ||
|
r22 | threads = self.filter(parent=NO_PARENT, tag=tag) | ||
|
r25 | threads = list(threads.order_by('-last_edit_time')) | ||
|
r5 | |||
return threads | ||||
def get_thread(self, opening_post_id): | ||||
|
r22 | opening_post = self.get(id=opening_post_id) | ||
replies = self.filter(parent=opening_post_id) | ||||
|
r5 | |||
|
r17 | thread = [opening_post] | ||
thread.extend(replies) | ||||
return thread | ||||
|
r5 | |||
|
r8 | def exists(self, post_id): | ||
|
r22 | posts = self.filter(id=post_id) | ||
|
r8 | |||
|
r10 | return len(posts) > 0 | ||
|
r8 | |||
|
r5 | |||
|
r4 | 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 | ||||
""" | ||||
|
r22 | name = models.CharField(max_length=100) | ||
|
r4 | |||
|
r8 | |||
|
r2 | class Post(models.Model): | ||
|
r4 | """A post is a message.""" | ||
|
r0 | |||
|
r2 | objects = PostManager() | ||
|
r0 | |||
|
r22 | title = models.CharField(max_length=100) | ||
|
r2 | pub_time = models.DateTimeField() | ||
text = models.TextField() | ||||
|
r22 | image = thumbs.ImageWithThumbsField(upload_to='images/', | ||
blank=True, sizes=((200, 150),)) | ||||
|
r2 | poster_ip = models.IPAddressField() | ||
poster_user_agent = models.TextField() | ||||
parent = models.BigIntegerField() | ||||
|
r7 | tags = models.ManyToManyField(Tag) | ||
|
r25 | last_edit_time = models.DateTimeField() | ||
|
r0 | |||
def __unicode__(self): | ||||
|
r9 | return self.title + ' (' + self.text + ')' | ||
|
r11 | class Admin(models.Model): | ||
|
r9 | """ | ||
Model for admin users | ||||
""" | ||||
|
r22 | name = models.CharField(max_length=100) | ||
password = models.CharField(max_length=100) | ||||
|
r9 | |||
def __unicode__(self): | ||||
return self.name + '/' + '*' * len(self.password) | ||||