Added soapbox and archive forum access levels. #313
[zzz-spline-forum.git] / splinext / forum / model / __init__.py
1 from datetime import datetime
2
3 from sqlalchemy import and_, Column, ForeignKey, Index
4 from sqlalchemy.orm import relation
5 from sqlalchemy.types import DateTime, Enum, Integer, Unicode
6
7 from spline.model.meta import TableBase
8 from splinext.users import model as users_model
9
10
11 ### Core
12
13 class Forum(TableBase):
14 __tablename__ = 'forums'
15 id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
16 name = Column(Unicode(133), nullable=False)
17 access_level = Column(Enum(u'normal', u'soapbox', u'archive', name='forums_access_level'), nullable=False, default=u'normal', server_default=u'normal')
18
19 def can_create_thread(self, user):
20 """Returns True ifff the named user can make a new thread in this
21 forum.
22 """
23 if not user.can('forum:create-thread'):
24 return False
25
26 if self.access_level == u'soapbox' and \
27 not user.can('forum:override-soapbox'):
28 return False
29
30 if self.access_level == u'archive' and \
31 not user.can('forum:override-archive'):
32 return False
33
34 return True
35
36 class Thread(TableBase):
37 __tablename__ = 'threads'
38 id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
39 forum_id = Column(Integer, ForeignKey('forums.id'), nullable=False)
40 icon = Column(Unicode(32), nullable=True)
41 subject = Column(Unicode(133), nullable=False)
42 post_count = Column(Integer, nullable=False, default=0, index=True)
43
44 def specific_post(self, position):
45 """Returns post number `position` in this thread.
46
47 Positions are one-indexed. Negative indexes are allowed.
48 """
49
50 # Handle negative indexes
51 if position < 0:
52 position = self.post_count + position + 1
53
54 return self.posts.filter_by(position=position).one()
55
56 def can_create_post(self, user):
57 """Returns True ifff the named user can make a new post in this thread.
58 """
59 if not user.can('forum:create-post'):
60 return False
61
62 if self.forum.access_level == u'archive' and \
63 not user.can('forum:override-archive'):
64 return False
65
66 return True
67
68 class Post(TableBase):
69 __tablename__ = 'posts'
70 id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
71 thread_id = Column(Integer, ForeignKey('threads.id'), nullable=False)
72 position = Column(Integer, nullable=False)
73 author_user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
74 posted_time = Column(DateTime, nullable=False, index=True, default=datetime.now)
75 content = Column(Unicode(5120), nullable=False)
76
77 Index('thread_position', Post.thread_id, Post.position, unique=True)
78
79
80 # XXX sort by time, how?
81 Forum.threads = relation(Thread, order_by=Thread.id.desc(), lazy='dynamic', backref='forum')
82
83 Thread.posts = relation(Post, order_by=Post.position.asc(), lazy='dynamic', backref='thread')
84 Thread.first_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == 1), uselist=False)
85 Thread.last_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == Thread.post_count), uselist=False)
86
87 Post.author = relation(users_model.User, backref='posts')