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
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
)
22 return render('/post/404.mako')
23 cs
= g
.db
.col(Post
).find(parent_id
=c
.post
._id
)
25 #c.post.children = g.db.col(Post).find(parent_id=c.post._id)
27 c
.post
.children
= cs
.sort(**order
)
28 return render('/post/view.mako')
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();
38 redirect_to(user
.create_login_url(''))
41 if idname
is None or len(idname
) is 0:
43 try: c
.host_classname
= request
.GET
.getone('host_classname').strip()
45 try: c
.host_id
= request
.GET
.getone('host_id').strip()
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')
57 c
.title
= request
.POST
.getone('title').strip()
59 #if len(c.title) == 0:
60 # c.field_errors.append('title')
61 #TODO: not channel. could be attached to anything
63 c
.channels
= request
.POST
.getone('channels').strip()
66 c
.host_classname
= request
.POST
.getone('host_classname').strip()
69 c
.host_id
= request
.POST
.getone('host_id').strip()
72 c
.parent
= request
.POST
.getone('parent').strip()
75 c
.post_id
= request
.POST
.getone('post_id').strip()
78 form_valid
= len(c
.field_errors
) == 0
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
85 post
= g
.db
.col(Post
).new()
86 post
.creator_id
= u
.id
93 #if hasattr(c,'channels'):
95 # chIds = c.channels.split(' ')
99 # ch = g.db.col(Channel).find_one(_id=ci)
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
113 post
.parent_id
= c
.parent
116 post
.content
= c
.content
118 #TODO: catch exceptions
121 success_redirect
= ''
123 success_redirect
= request
.POST
.getone('success_redirect')
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
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
142 c
.redir
= request
.GET
.getone('redir')
145 return render('/post/edit.mako')
147 def delete(self
, idname
):
148 #TODO: check settings, role and acl
149 u
= user
.get_current_user();
151 redirect_to(user
.create_login_url(''))
153 p
= g
.db
.col(Post
).find_one(_id
=idname
)
154 #TODO: catch exceptions
158 return 'delete %s' % str(idname
)