def post(self): title = self.request.get('subject') post = self.request.get('content') slug = self.request.get('slug') cid = self.request.get('cid') error = dict() cid = int(cid) if title and post: if not slug or not valid_slug(slug): slug = slugify(str(title)) slug_in_db = Articles.all().filter('slug =', slug).get() if not slug_in_db or cid == slug_in_db.key().id(): edit = Articles.get_by_id(cid) edit.title = title edit.post = post edit.slug = slug edit.put() # Remove from memcache memcache.flush_all() self.redirect('/articles/' + slug) else: error['slug'] = 'Sorry, this slug is taken. Please enter a unique one' form = {'title': title, 'post': post, 'slug': slug, 'cid': cid, 'error': error, 'logged_in': True} self.render_form(form) else: if not title: error['subject'] = 'Please enter a title' if not post: error['post'] = 'Please enter some content' form = {'title': title, 'post': post, 'slug': slug, 'cid': cid, 'error': error, 'logged_in': True} self.render_form(form)
def api_delete_article(aid): a = Articles.get_by_id(aid) if a is None: raise notfound() a.delete() uploaders.delete_attachment(a.cover_id) comments.delete_comments(aid) return dict(result=True)
def web_get_article(aid): article = Articles.get_by_id(aid) if article is None or article.draft: raise notfound article.reads = counters.incr(aid) article.content = texts.md2html(texts.get(article.content_id)) category = Categories.get_by_id(article.category_id) return dict(article=article, category=category, comments=comments.get_comments(aid))
def api_create_article_comment(aid): u = ctx.user if u is None: raise APIPermissionError() i = ctx.request.input(content='') content = assert_not_empty(i.content, 'content') a = Articles.get_by_id(aid) if a is None: raise notfound() return comments.create_comment('article', aid, content)
def api_update_article(aid): article = Articles.get_by_id(aid) if article is None: raise notfound() i = ctx.request.input() update = False if 'name' in i: article.name = assert_not_empty(i.name, 'name') update = True if 'summary' in i: article.summary = assert_not_empty(i.summary, 'summary') update = True if 'category_id' in i: article.category_id = _check_category_id(i.category_id) update = True if 'tags' in i: article.tags = texts.format_tags(i.tags) update = True # update draft first: if 'draft' in i: if i.draft.lower() == 'true': if not article.draft: # change False to True: article.draft = True if article.publish_time < TIME_FEATURE: article.publish_time = article.publish_time + TIME_FEATURE update = True else: if article.draft: # change True to False: article.draft = False # update publish time: if 'publish_time' in i and i.publish_time.strip(): article.publish_time = time2timestamp(i.publish_time) else: article.publish_time = time.time() update = True if 'content' in i: content = assert_not_empty(i.content, 'content') article.content = content update = True old_cover_id = '' if 'cover' in i: f = i.cover if f: # update cover: old_cover_id = article.cover_id atta = uploaders.upload_cover(article.name, f.file.read()) article.cover_id = atta._id update = True if hasattr(article, 'content'): article.content_id = texts.set(article._id, article.content) if update: article.update() if old_cover_id: uploaders.delete_attachment(old_cover_id) return dict(_id=article._id)
def api_update_article(aid): article = Articles.get_by_id(aid) if article is None: raise notfound() i = ctx.request.input() update = False if 'name' in i: article.name = assert_not_empty(i.name, 'name') update = True if 'summary' in i: article.summary = assert_not_empty(i.summary, 'summary') update = True if 'category_id' in i: article.category_id = _check_category_id(i.category_id) update = True if 'tags' in i: article.tags = texts.format_tags(i.tags) update = True # update draft first: if 'draft' in i: if i.draft.lower()=='true': if not article.draft: # change False to True: article.draft = True if article.publish_time < TIME_FEATURE: article.publish_time = article.publish_time + TIME_FEATURE update = True else: if article.draft: # change True to False: article.draft = False # update publish time: if 'publish_time' in i and i.publish_time.strip(): article.publish_time = time2timestamp(i.publish_time) else: article.publish_time = time.time() update = True if 'content' in i: content = assert_not_empty(i.content, 'content') article.content = content update = True old_cover_id = '' if 'cover' in i: f = i.cover if f: # update cover: old_cover_id = article.cover_id atta = uploaders.upload_cover(article.name, f.file.read()) article.cover_id = atta._id update = True if hasattr(article, 'content'): article.content_id = texts.set(article._id, article.content) if update: article.update() if old_cover_id: uploaders.delete_attachment(old_cover_id) return dict(_id=article._id)
def edit_article(): article = Articles.get_by_id(ctx.request['_id']) if article is None: raise notfound() return dict( \ form_title = u'Edit Article', \ form_action = '/api/articles/%s/update' % article._id, \ _id = article._id, \ name = article.name, \ category_id = article.category_id, \ draft = article.draft, \ publish_time = article.publish_time, \ tags = article.tags, \ summary = article.summary, \ cover_id = article.cover_id, \ content = texts.get(article.content_id), \ categories = _get_categories())