Deleted post comestics.
[TownSquare.git] / townsquare / controllers / post.py
blobbcdfdb988e68856742f0c4a86902ac8a3d5e1b47
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: check settings, role and acl
36 u = user.get_current_user();
37 if u is None:
38 redirect_to(user.create_login_url(''))
40 c.new_post = False
41 if idname is None or len(idname) is 0:
42 c.new_post = True
43 try: c.host_classname = request.GET.getone('host_classname').strip()
44 except: pass
45 try: c.host_id = request.GET.getone('host_id').strip()
46 except: pass
48 c.field_errors = []
49 form_valid = False
51 if len(request.POST):
52 #TODO: show message if the post has no content property
53 c.content = request.POST.getone('content').strip()
54 if len(c.content) == 0:
55 c.field_errors.append('content')
56 try:
57 c.title = request.POST.getone('title').strip()
58 except: pass
59 #if len(c.title) == 0:
60 # c.field_errors.append('title')
61 #TODO: not channel. could be attached to anything
62 try:
63 c.channels = request.POST.getone('channels').strip()
64 except: pass
65 try:
66 c.host_classname = request.POST.getone('host_classname').strip()
67 except: pass
68 try:
69 c.host_id = request.POST.getone('host_id').strip()
70 except: pass
71 try:
72 c.parent = request.POST.getone('parent').strip()
73 except: pass
74 try:
75 c.post_id = request.POST.getone('post_id').strip()
76 except: pass
78 form_valid = len(c.field_errors) == 0
80 if form_valid:
81 if c.post_id and len(c.post_id) > 0:
82 post = g.db.col(Post).find_one(_id=idname)
83 #TODO: catch exceptions
84 else:
85 post = g.db.col(Post).new()
86 post.creator_id = u.id
88 try:
89 if len(c.title):
90 post.name = c.title
91 except: pass
93 #if hasattr(c,'channels'):
94 # if len(c.channels):
95 # chIds = c.channels.split(' ')
96 # if len(chIds):
97 # post.channels = []
98 # for ci in chIds:
99 # ch = g.db.col(Channel).find_one(_id=ci)
100 # if ch is not None:
101 # post.channels.append(str(ch._id))
103 if len(c.host_classname) and len(c.host_id):
104 cl = globals()[c.host_classname]
105 #TODO: catch exception
106 host = g.db.col(cl).find_one(_id=c.host_id)
107 #TODO: check acl and role
108 if host is not None:
109 post.host = host
111 try:
112 if len(c.parent):
113 post.parent_id = c.parent
114 except: pass
116 post.content = c.content
118 #TODO: catch exceptions
119 post.save()
121 success_redirect = ''
122 try:
123 success_redirect = request.POST.getone('success_redirect')
124 except:
125 pass
126 if len(success_redirect):
127 redirect_to(str(success_redirect))
128 #TODO: message or the post?
129 redirect_to(post.url)
130 #TODO: better notification message
131 return "Saved"
133 if idname and len(idname) is not 0:
134 p = g.db.col(Post).find_one(_id=idname)
135 #TODO: catch exceptions
136 c.post_id = str(p._id)
137 c.content = p.content
138 try:
139 c.title = p.name
140 except: pass
141 try:
142 c.redir = request.GET.getone('redir')
143 except: pass
145 return render('/post/edit.mako')
147 def delete(self, idname):
148 #TODO: check settings, role and acl
149 u = user.get_current_user();
150 if u is None:
151 redirect_to(user.create_login_url(''))
153 p = g.db.col(Post).find_one(_id=idname)
154 #TODO: catch exceptions
156 p.delete(u.id)
158 return 'delete %s' % str(idname)