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: 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();
45 redirect_to(user
.create_login_url(''))
49 if idname
is None or len(idname
) is 0:
51 try: c
.content_type
= request
.GET
.getone('type').strip()
53 #TODO: show as internal error
55 try: c
.host_classname
= request
.GET
.getone('host_classname').strip()
57 try: c
.host_id
= request
.GET
.getone('host_id').strip()
65 try: c
.content_type
= request
.POST
.getone('content_type').strip()
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()
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()
78 try: c
.host_id
= request
.POST
.getone('host_id').strip()
80 try: c
.parent_id
= request
.POST
.getone('parent_id').strip()
82 try: c
.post_id
= request
.POST
.getone('post_id').strip()
85 try: c
.redir
= request
.POST
.getone('redir').strip()
88 form_valid
= len(c
.field_errors
) == 0
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
101 post
= g
.db
.col(Post
).new()
102 post
.content_type
= c
.content_type
103 post
.creator_id
= u
.id
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
120 post
.parent_id
= c
.parent_id
123 post
.content
= c
.content
125 #TODO: catch exceptions
129 redirect_to(str(c
.redir
))
131 #TODO: message or the post?
132 redirect_to(post
.url
)
134 #TODO: better notification message
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
142 c
.post_id
= str(p
._id
)
143 c
.content
= p
.content
144 try: c
.title
= p
.name
146 try: c
.redir
= request
.GET
.getone('redir')
149 return render('/post/edit.mako')
151 def delete(self
, idname
):
152 #TODO: check settings, role and acl
153 u
= user
.get_current_user();
155 redirect_to(user
.create_login_url(''))
157 p
= g
.db
.col(Post
).find_one(_id
=idname
)
158 #TODO: catch exceptions
162 return 'delete %s' % str(idname
)