Example #1
0
 def _update_post_model(self, post, fields, isnew=False):
     if isnew:
         lang = fields['lang'] = self._get_lang(fields['lang'])
         post.author_id = db.ses.query(db.models.User)\
                          .filter_by(name=cherrypy.request.login).one().id
     else:
         lang = post.lang
     fields = self._fields_to_db_models(fields, lang)
     # add the post to the session, even if this is not new, because
     # of the .scalar call in `_fields_to_db_models`
     db.ses.add(post) 
     revision = db.models.PostRevision(title=fields.pop('title'),
                                       abstract=fields.pop('abstract'),
                                       content=fields.pop('content'))
     post.revisions.append(revision)
     if update_model(post, fields):
         message = precautious_commit(db.ses)  # None if everything went ok.
         if message is None:
             clean_empty_metainfo()
             return json.dumps({'id': post.id,
                                'slug': post.slug,
                                'lang': post.lang.code,
                                'public': post.public})
         else:
             raise Exception(message)
     else:
         raise Exception("Unable to update the post model.")
Example #2
0
 def change_visibility(self, postid, public):
     post = self.get_post_by_id(postid)
     if post is None:
         raise Exception("The post does not exist.")
     else:
         post.public = public
         return precautious_commit(db.ses)