Exemple #1
0
def edit_news_category_post():
    v = Validator(request.form)
    v.field("name_category").required()
    if v.is_valid():
        name_category = v.valid_data.name_category
        NewsCategory.rename(request.form.get("category_id"), name_category)
        return jsonify({"status": "ok"})
    return jsonify({"status": "fail", "errors": v.errors})
Exemple #2
0
def edit_news_category_post():
    v = Validator(request.form)
    v.field("name_category").required()
    if v.is_valid():
        name_category = v.valid_data.name_category
        NewsCategory.rename(request.form.get("category_id"), name_category)
        return jsonify({"status": "ok"})
    return jsonify({"status": "fail",
                    "errors": v.errors})
Exemple #3
0
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})
Exemple #4
0
def news_category_list():
    category_list = NewsCategory.get_all()
    return render_template('news_category/news_category_list.html',
                           **{'category_list': category_list})
Exemple #5
0
def delete_category(cat_id):
    NewsCategory.delete(cat_id)
    return redirect(url_for('admin.news_categories'))
Exemple #6
0
def edit_news_category(cat_id):
    category = NewsCategory.get_by_id(cat_id)
    return render_template('admin/news_category/edit_category.html',
                           category=category)
Exemple #7
0
def news_category_list():
    category_list = NewsCategory.get_all()
    return render_template('news_category/news_category_list.html',  **{'category_list': category_list})
Exemple #8
0
def delete_category(cat_id):
    NewsCategory.delete(cat_id)
    return redirect(url_for('admin.news_categories'))
Exemple #9
0
def edit_news_category(cat_id):
    category = NewsCategory.get_by_id(cat_id)
    return render_template('admin/news_category/edit_category.html',
                            category=category)
Exemple #10
0
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})
Exemple #11
0
def list_all_by_category(id):
    category = NewsCategory.get(id)
    return render_template('news/all.html', **{'category': category, 'news': category.news})