Introduced post types.
[TownSquare.git] / townsquare / controllers / post.py
blob6dd5100239e19943c235edab823799bb4245c549
1 import logging
3 from pylons import request, response, session, tmpl_context as c, app_globals as g
4 from pylons.controllers.util import abort, redirect_to
6 from fma.orm import mapper
7 from townsquare.model.post import Post
8 from townsquare.model.channel import Channel
9 from townsquare.model import user
10 from user import User
11 from townsquare.lib.base import BaseController, render
13 log = logging.getLogger(__name__)
15 #mapper(Channel, Post, account.Account)
17 class PostController(BaseController):
19 def view(self, idname):
20 c.post = g.db.col(Post).find_one(_id=idname)
21 if c.post is None:
22 return render('/post/404.mako')
23 cs = g.db.col(Post).find(parent_id=c.post._id)
24 #cs.sort('_id')
25 #c.post.children = g.db.col(Post).find(parent_id=c.post._id)
26 order = {'_id':1}
27 c.post.children = cs.sort(**order)
28 return render('/post/view.mako')
30 def list(self):
31 c.item_list = g.db.col(Post).find()
32 return render('/post/list.mako')
34 def edit(self, idname=None):
35 #TODO: detect content_type based on properties
36 # oneliner: content only, one line
37 # text: content only, for replies
38 # article: title is a must, optional brief and single pic
39 # question: title is a must, optional content as description
40 # link: title is a must, optional content as description
42 #TODO: check settings, role and acl
43 u = user.get_current_user();
44 if u is None:
45 redirect_to(user.create_login_url(''))
47 c.new_post = False
49 if idname is None or len(idname) is 0:
50 c.new_post = True
51 try: c.content_type = request.GET.getone('type').strip()
52 except:
53 #TODO: show as internal error
54 pass
55 try: c.host_classname = request.GET.getone('host_classname').strip()
56 except: pass
57 try: c.host_id = request.GET.getone('host_id').strip()
58 except: pass
60 c.field_errors = []
61 form_valid = False
63 if len(request.POST):
64 #CHECK: mandatory?
65 try: c.content_type = request.POST.getone('content_type').strip()
66 except: pass
67 #TODO: show message if the post has no content property
68 c.content = request.POST.getone('content').strip()
69 if len(c.content) == 0:
70 c.field_errors.append('content')
71 try: c.title = request.POST.getone('title').strip()
72 except: pass
73 #TODO: This depends on the content type or this affects the content type
74 #if len(c.title) == 0:
75 # c.field_errors.append('title')
76 try: c.host_classname = request.POST.getone('host_classname').strip()
77 except: pass
78 try: c.host_id = request.POST.getone('host_id').strip()
79 except: pass
80 try: c.parent_id = request.POST.getone('parent_id').strip()
81 except: pass
82 try: c.post_id = request.POST.getone('post_id').strip()
83 except: pass
85 try: c.redir = request.POST.getone('redir').strip()
86 except: pass
88 form_valid = len(c.field_errors) == 0
90 if form_valid:
92 if c.post_id and len(c.post_id) > 0:
93 # Editing existing post
95 post = g.db.col(Post).find_one(_id=idname)
96 #TODO: catch exceptions
98 else:
99 # Creating new post
101 post = g.db.col(Post).new()
102 post.content_type = c.content_type
103 post.creator_id = u.id
105 try:
106 if len(c.title):
107 post.name = c.title
108 except: pass
110 if len(c.host_classname) and len(c.host_id):
111 cl = globals()[c.host_classname]
112 #TODO: catch exception
113 host = g.db.col(cl).find_one(_id=c.host_id)
114 #TODO: check acl and role
115 if host is not None:
116 post.host = host
118 try:
119 if len(c.parent_id):
120 post.parent_id = c.parent_id
121 except: pass
123 post.content = c.content
125 #TODO: catch exceptions
126 post.save()
128 if len(c.redir):
129 redirect_to(str(c.redir))
131 #TODO: message or the post?
132 redirect_to(post.url)
134 #TODO: better notification message
135 return "Saved"
137 if idname and len(idname) is not 0:
138 p = g.db.col(Post).find_one(_id=idname)
139 #TODO: catch exceptions
140 try: c.content_type = p.content_type
141 except: pass
142 c.post_id = str(p._id)
143 c.content = p.content
144 try: c.title = p.name
145 except: pass
146 try: c.redir = request.GET.getone('redir')
147 except: pass
149 return render('/post/edit.mako')
151 def delete(self, idname):
152 #TODO: check settings, role and acl
153 u = user.get_current_user();
154 if u is None:
155 redirect_to(user.create_login_url(''))
157 p = g.db.col(Post).find_one(_id=idname)
158 #TODO: catch exceptions
160 p.delete(u.id)
162 return 'delete %s' % str(idname)