def profile_edit_commit(self, id, name=None): """Save profile changes.""" c.page_user = meta.Session.query(users_model.User).get(id) if not c.page_user: abort(404) # XXX could use some real permissions if c.page_user != c.user: abort(403) c.form = ProfileEditForm(request.params, name=c.page_user.name, ) if not c.form.validate(): return render('/users/profile_edit.mako') c.page_user.name = c.form.name.data meta.Session.add(c.page_user) meta.Session.commit() h.flash('Saved your profile.', icon='tick') redirect( url(controller='users', action='profile', id=c.page_user.id, name=c.page_user.name), code=303, )
def profile_edit_commit(self, id, name=None): """Save profile changes.""" c.page_user = meta.Session.query(users_model.User).get(id) if not c.page_user: abort(404) # XXX could use some real permissions if c.page_user != c.user: abort(403) c.form = ProfileEditForm( request.params, name=c.page_user.name, ) if not c.form.validate(): return render('/users/profile_edit.mako') c.page_user.name = c.form.name.data meta.Session.add(c.page_user) meta.Session.commit() h.flash('Saved your profile.', icon='tick') redirect( url(controller='users', action='profile', id=c.page_user.id, name=c.page_user.name), code=303, )
def logout(self): """Logs the user out.""" if 'user_id' in session: del session['user_id'] session.save() h.flash(u"""Logged out.""", icon='user-silhouette') redirect(url('/'), code=303)
def write_thread_commit(self, forum_id): """Posts a new thread.""" if not c.user.can('forum:create-thread'): abort(403) try: c.forum = meta.Session.query(forum_model.Forum) \ .filter_by(id=forum_id).one() except NoResultFound: abort(404) c.write_thread_form = WriteThreadForm(request.params) # Reshow the form on failure if not c.write_thread_form.validate(): return render('/forum/write_thread.mako') # Otherwise, add the post. c.forum = meta.Session.query(forum_model.Forum) \ .with_lockmode('update') \ .get(c.forum.id) thread = forum_model.Thread( forum_id=c.forum.id, subject=c.write_thread_form.subject.data, post_count=1, ) source = c.write_thread_form.content.data post = forum_model.Post( position=1, author_user_id=c.user.id, raw_content=source, content=spline.lib.markdown.translate(source), ) thread.posts.append(post) c.forum.threads.append(thread) meta.Session.commit() # Redirect to the new thread h.flash( "Contribution to the collective knowledge of the species successfully recorded." ) redirect( url(controller='forum', action='posts', forum_id=forum_id, thread_id=thread.id), code=303, )
def write_thread_commit(self, forum_id): """Posts a new thread.""" if not c.user.can('forum:create-thread'): abort(403) try: c.forum = meta.Session.query(forum_model.Forum) \ .filter_by(id=forum_id).one() except NoResultFound: abort(404) c.write_thread_form = WriteThreadForm(request.params) # Reshow the form on failure if not c.write_thread_form.validate(): return render('/forum/write_thread.mako') # Otherwise, add the post. c.forum = meta.Session.query(forum_model.Forum) \ .with_lockmode('update') \ .get(c.forum.id) thread = forum_model.Thread( forum_id = c.forum.id, subject = c.write_thread_form.subject.data, post_count = 1, ) source = c.write_thread_form.content.data post = forum_model.Post( position = 1, author_user_id = c.user.id, raw_content = source, content = spline.lib.markdown.translate(source), ) thread.posts.append(post) c.forum.threads.append(thread) meta.Session.commit() # Redirect to the new thread h.flash("Contribution to the collective knowledge of the species successfully recorded.") redirect( url(controller='forum', action='posts', forum_id=forum_id, thread_id=thread.id), code=303, )
def login_finish(self): """Step two of logging in; the OpenID provider redirects back here.""" cons = Consumer(session=session, store=self.openid_store) host = request.headers['host'] return_url = url(host=host, controller='accounts', action='login_finish') res = cons.complete(request.params, return_url) if res.status == CANCEL: # I guess.. just.. back to the homepage? h.flash(u"""Login canceled.""", icon='user-silhouette') redirect(url('/')) elif res.status != SUCCESS: return 'Error! %s' % res.message try: # Grab an existing user record, if one exists q = meta.Session.query(users_model.User) \ .filter(users_model.User.openids.any(openid=res.identity_url)) user = q.one() except NoResultFound: # Try to pull a name out of the SReg response sreg_res = SRegResponse.fromSuccessResponse(res) try: username = sreg_res['nickname'] except (KeyError, TypeError): # KeyError if sreg has no nickname; TypeError if sreg is None username = '******' # Create db records user = users_model.User(name=username) meta.Session.add(user) openid = users_model.OpenID(openid=res.identity_url) user.openids.append(openid) meta.Session.commit() # Remember who's logged in, and we're good to go session['user_id'] = user.id session.save() h.flash(u"""Hello, {0}!""".format(user.name), icon='user') redirect(url('/'), code=303)
def write_commit(self, forum_id, thread_id): """Post to a thread.""" if not c.user.can('forum:create-post'): abort(403) try: c.thread = meta.Session.query(forum_model.Thread) \ .filter_by(id=thread_id, forum_id=forum_id).one() except NoResultFound: abort(404) c.write_post_form = WritePostForm(request.params) # Reshow the form on failure if not c.write_post_form.validate(): return render('/forum/write.mako') # Otherwise, add the post. c.thread = meta.Session.query(forum_model.Thread) \ .with_lockmode('update') \ .get(c.thread.id) source = c.write_post_form.content.data post = forum_model.Post( position=c.thread.post_count + 1, author_user_id=c.user.id, raw_content=source, content=spline.lib.markdown.translate(source), ) c.thread.posts.append(post) c.thread.post_count += 1 meta.Session.commit() # Redirect to the thread # XXX probably to the post instead; anchor? depends on paging scheme h.flash('Your uniqueness has been added to our own.') redirect( url(controller='forum', action='posts', forum_id=forum_id, thread_id=thread_id), code=303, )
def write_commit(self, forum_id, thread_id): """Post to a thread.""" if not c.user.can('forum:create-post'): abort(403) try: c.thread = meta.Session.query(forum_model.Thread) \ .filter_by(id=thread_id, forum_id=forum_id).one() except NoResultFound: abort(404) c.write_post_form = WritePostForm(request.params) # Reshow the form on failure if not c.write_post_form.validate(): return render('/forum/write.mako') # Otherwise, add the post. c.thread = meta.Session.query(forum_model.Thread) \ .with_lockmode('update') \ .get(c.thread.id) source = c.write_post_form.content.data post = forum_model.Post( position = c.thread.post_count + 1, author_user_id = c.user.id, raw_content = source, content = spline.lib.markdown.translate(source), ) c.thread.posts.append(post) c.thread.post_count += 1 meta.Session.commit() # Redirect to the thread # XXX probably to the post instead; anchor? depends on paging scheme h.flash('Your uniqueness has been added to our own.') redirect( url(controller='forum', action='posts', forum_id=forum_id, thread_id=thread_id), code=303, )