def create_news(): result = news_schema.load(request.get_json()) if result.errors: return result.errors, 400 news = News(**result.data) db.session.add(news) db.session.commit() return news.to_json().data, 200
def delete(id): user = auth.service.get_user() if user.is_authorized(): news = News.get(id) if news: db.session.delete(news) db.session.commit() return jsonify({'status': 'ok'}) return jsonify({'status': 'fail'})
def save(): v = Validator(request.form) v.fields('id').integer(nullable=True) v.field('title').required() v.field('text').required() v.field('category_id').integer(nullable=True) user = auth.service.get_user() if not user.is_authorized(): abort(403) if v.is_valid(): data = v.valid_data news = News.get(data.id) if news: news.title = data.title news.text = data.text else: news = News() news.title = data.title news.text = data.text news.author = user category = NewsCategory.get(data.category_id) news.category = category tags = data.list('tag') existing_tags = {tag.name: tag for tag in NewsTag.get_tags(tags)} tags = {tag: NewsTag(name=tag) for tag in tags} tags.update(existing_tags) news.tags = list(tags.values()) is_new = True if user.id is not None else False db.session.add(news) db.session.commit() if is_new: send_news_notification.delay(news.id, news.title) return jsonify({'status': 'ok', 'news': news.as_dict()}) return jsonify({'status': 'fail', 'errors': v.errors})
def news_form(id=None): news = News.get(id) or News() categories = defaultdict(list) for c in NewsCategory.all(): categories[c.parent_id].append(c) return render_template('news/form.html', **{'news': news, 'categories': categories})
def list_all_by_tag(id): tag = NewsTag.get(id) news = News.all_by_tag(tag) if tag else [] return render_template('news/all.html', **{'tag': tag, 'news': news})
def list_all(): news = News.all() return render_template('news/all.html', **{'news': news})