Posts can be attached to any model.
[TownSquare.git] / townsquare / controllers / channel.py
blob3d85269b2cb01d6d658a9c78f70e6602b4b8724c
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 townsquare.lib.base import BaseController, render
12 log = logging.getLogger(__name__)
14 #mapper(Channel, Post, account.Account)
16 class ChannelController(BaseController):
18 def view(self, idname):
19 c.ch = g.db.col(Channel).find_one(_id=idname)
20 #TODO: 404 if not found
21 #TODO: pagination
22 c.ch.posts = g.db.col(Post).find(_host_classname='Channel', _host_id=c.ch._id)
23 return render('/channel/view.mako')
25 def list(self):
26 c.item_list = g.db.col(Channel).find()
27 return render('/channel/list.mako')
29 def pick_single(self):
30 """
31 """
32 #TODO: use the parameter 'return'
33 # use COOKIE to store selection
34 return 'eee'
36 def pick_multi(self):
37 return 'aaa'
39 def create(self):
40 #TODO: check settings, role and acl
41 u = user.get_current_user();
42 if u is None:
43 redirect_to(user.create_login_url(''))
44 c.field_errors = []
45 form_valid = False
46 if (len(request.POST)):
47 c.title = request.POST.getone('title').strip()
48 if len(c.title) == 0:
49 c.field_errors.append('title')
50 c.content = request.POST.getone('content').strip()
51 if len(c.content) == 0:
52 c.field_errors.append('content')
53 form_valid = len(c.field_errors) == 0
55 if form_valid:
56 nd = g.db.col(Channel).find().count()
57 disc = g.db.col(Channel).new()
58 disc.creator_id = u.id
59 disc.name = c.title
60 #TODO: create slug
61 disc.idname = "channel_%i" % nd
62 disc.description = c.content
63 disc.save()
64 #TODO: redirect to the newly created channel
65 redirect_to(disc.url)
67 return render('/channel/edit.mako')