def addRecipe():
    if 'email' not in session:
        return redirect(url_for('login'))

    recForm = AddArticleForm()
    # Temporarily passing this message object in order to display an added message under the form.
    message = ""

    if request.method == 'POST':
        if not recForm.validate():
            return render_template('newrecipe.html',
                                   recForm=recForm,
                                   message=message)
        else:

            # The Recipe constructor adds each ingredient in this tuple as a child which handles the many-to-many relationship
            ingredients = recForm.recipeingredients.data
            newRec = Recipe(recForm.recipetitle.data, recForm.recipedesc.data,
                            ingredients, session['email'])

            # No Try/Catch here because the db is not configured to require unique titles
            db.session.add(newRec)
            db.session.commit()
            message = "Recipe added: " + newRec.recipetitle
            return redirect(url_for('index'))

    elif request.method == 'GET':
        return render_template('newrecipe.html',
                               recForm=recForm,
                               message=message)
Beispiel #2
0
def add_article():
    form = AddArticleForm()
    if request.method == 'POST':
        sql = "insert into articles (user_id, title, article) values (%s, %s, %s)"
        try:
            title = form.title.data
            text = form.content.data
            g.cur.execute(sql, [session['user_id'], title, text])
            g.db.commit()
        except Exception as e:
            mylogger.error(str(e))
            g.db.rollback()
            abort(401)
        #print(text)
        # do something interesting with the Markdown text
        # allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', \
        #'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul','h1', 'h2', 'h3', 'p','img']
        # html_body=markdown(text,output_format='html')
        # html_body=bleach.clean(html_body,tags=allowed_tags,strip=True)
        # html_body=bleach.linkify(html_body)
        #
        # pagedown = html_body
        # print(pagedown)

    return render_template('addarticle.html', form=form)
Beispiel #3
0
def front_add_article(request):
    # 添加文章
    # 1.请求页面时,我们需要获取所有的标签和分类
    # 2.提交修改后的页面时,检查分类和标签在数据库中是否存在,
    # 如果不存在则新建,否则存入
    # 3.文章与标签是多对多关系,与其他内容不一起存
    # 4.文章与分类不是是多对多关系
    if request.method == 'GET':
        tags = TagModel.objects.all()
        categorys = CategoryModel.objects.all()
        context = {'tags': tags, 'categorys': categorys}
        return render(request, 'front_add_article.html', context=context)
    else:
        form = AddArticleForm(request.POST)
        if form.is_valid():
            user = request.user
            tag_ids = request.POST.getlist('tags[]')

            title = form.cleaned_data.get('title')
            thumbnail = form.cleaned_data.get('thumbnail')
            content = form.cleaned_data.get('content')
            category_id = form.cleaned_data.get('category')

            category = CategoryModel.objects.filter(pk=category_id).first()

            article_model = ArticleModel(username=user,
                                         title=title,
                                         thumbnail=thumbnail,
                                         content=content,
                                         category=category)
            article_model.save()
            # 文章与标签的多对多的保存
            # 如果选择了标签,则将选择的标签以此存入,否则不用存
            if tag_ids:
                for tag_id in tag_ids:
                    tag_model = TagModel.objects.filter(pk=tag_id).first()
                    if tag_model:
                        article_model.tags.add(tag_model)
            return myjson.json_result()
        else:
            return render(request, 'front_add_article.html',
                          {'error': form.get_error()})
Beispiel #4
0
def add_article(request):
    if request.method == 'GET':
        categorys = CategoryModel.objects.all()
        tags = TagModel.objects.all()
        data = {'categorys': categorys, 'tags': tags}
        return render(request, 'cms_add_article.html', data)
    else:
        form = AddArticleForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data.get('title')
            category = form.cleaned_data.get('category')
            desc = form.cleaned_data.get('desc')
            thumbnail = form.cleaned_data.get('thumbnail')
            content_html = form.cleaned_data.get('content_html')
            tags = request.POST.getlist('tags[]')
            user = request.user

            categoryModel = CategoryModel.objects.filter(pk=category).first()

            articleModel = ArticleModel(title=title,
                                        desc=desc,
                                        thumbnail=thumbnail,
                                        content_html=content_html,
                                        author=user,
                                        category=categoryModel)
            articleModel.save()
            #添加tags的多对多

            if tags:
                for tag in tags:
                    tagModel = TagModel.objects.filter(pk=int(tag)).first()
                    if tagModel:
                        articleModel.tags.add(tagModel)

            return xtjson.json_result(data={})

        else:
            return form.get_error_response()
Beispiel #5
0
def front_add_article(request):
    if request.method == 'GET':
        categorys = Categorys.objects.all()
        tags = Tags.objects.all()
        context = {'categorys': categorys, 'tags': tags}
        if hasattr(request, 'front_user'):
            context['front_user'] = request.front_user
        return render(request, 'front_add_article.html', context)
    else:
        form = AddArticleForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data.get('title')
            subtitle = form.cleaned_data.get('subtitle')
            data = request.POST
            tags = request.POST.getlist('tags[]')
            context = {}
            for k, v in data.iteritems():
                context[k] = v
            category_name = context['category']  #获取分类
            categoryModel = Categorys.objects.filter(
                name=category_name).first()
            current_user = request.front_user
            article_content = context['textarea_content']
            articleModel = Articles(title=title,
                                    subtitle=subtitle,
                                    categorys=categoryModel,
                                    author=current_user,
                                    content=article_content)
            articleModel.save()
            for tag in tags:
                tagModel = Tags.objects.filter(pk=tag).first()
                articleModel.tags.add(tagModel)
            data = {'code': 200, 'info': u'恭喜您!文章发布成功!'}
            return JsonResponse(data)
        else:
            return render(request, 'front_add_article.html',
                          {'error': '表单验证出错!'})
Beispiel #6
0
def AddArticle(request):

    if request.method == 'POST':

        article_tag = request.POST.get('tag', '')
        article_title = request.POST.get('title', '')
        article_content = request.POST.get('content', '')
        add_article_form = AddArticleForm(request.POST)

        print article_tag
        print article_title
        print article_content
        print add_article_form.errors
        if add_article_form.is_valid():
            article = add_article_form.save(commit=False)
            article.author = request.user
            article.save()
            return redirect('/blog/index')

        else:
            return HttpResponseRedirect('/')

    else:
        return redirect('/user/add/')