Ejemplo n.º 1
0
def edit_categories():

    add_cat = AddCategoryForm()
    categories = BlogCategory.query.all()

    # add new category
    if add_cat.validate_on_submit() and add_cat.submit.data:

        # check duplicates
        for cat in categories:
            if add_cat.category.data.lower() == cat.category.lower():
                flash('Category already exists.')
                return redirect(url_for('edit_categories'))

        new_cat = BlogCategory(category=add_cat.category.data.lower())

        db.session.add(new_cat)
        db.session.commit()

        flash('Category added.')
        return redirect(url_for('edit_categories'))

    return render_template('edit_categories.html',
                           add_cat=add_cat,
                           categories=categories)
Ejemplo n.º 2
0
def admin_blog_category():
    blogCategoryForm = BlogCategoryForm()
    t_blogcategories = BlogCategory.query.all()
    if request.method == 'POST':
        blog_category = BlogCategory(name=blogCategoryForm.name.data)
        db.session.add(blog_category)
        db.session.commit()
        return redirect('/admin/blog/category/')
    return render_template('admin/blog_category.html',
                           form=blogCategoryForm,
                           t_blogcategories=t_blogcategories)
Ejemplo n.º 3
0
def generateData(numUser,numCategory,numBlog):
    User.fake(numUser)
    
    Follow.fake(numUser)
        
    Category.fake(numCategory)
    
    Blog.fake(numBlog,numUser)
    
    Comment.fake(numBlog,numUser)
    
    BlogCategory.fake(numBlog,numCategory*5)
    
    BlogLabel.fake(numBlog)
    
    BlogLike.fake(numBlog*20,numUser,numBlog)
    
    CommentLike.fake(numBlog*30,numUser,numBlog*8)
    
    Collection.fake(numBlog*10,numUser,numBlog)
Ejemplo n.º 4
0
def blog_category_create():
    form = BlogCategoryForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            cat = BlogCategory(name=form.name.data,
                               is_featured=form.is_featured.data,
                               order=form.order.data)
            db.session.add(cat)
            db.session.commit()
            flash('Category {} successfully created'.format(cat.name),
                  'success')
            return redirect(url_for('admin.blog_categories'))
    return render_template('admin/blog/categories/add-edit.html', form=form)
Ejemplo n.º 5
0
def publish(blog_id):
    form = PublishBlogForm(request.form)
    if blog_id == 0:
        if form.validate_on_submit():
            blog_title = request.form.get("title")
            blog_content = request.form.get("content")
            category = request.form.getlist("category")
            label = request.form.get("label")
            if not len(blog_content.strip()):
                flash("The content is necessray!")
                return redirect(url_for("publish"))
            if not len(blog_title.strip()):
                flash("The title is necessray!")
                return redirect(url_for("publish"))
            blog = Blog(BlogTitle=blog_title,
                        BlogContent=blog_content,
                        PublisherID=current_user.UserID)
            if db.session.add(blog):
                db.session.commit()
                blog = Blog.query.filter_by(BlogTitle=blog_title,
                                            PublisherID=current_user.UserID)[0]
                for c in category:
                    blog_category = BlogCategory(BlogID=blog.BlogID,
                                                 CategoryID=int(c))
                    db.session.add(blog_category)
                db.session.commit()
                for l in label.split(';'):
                    if l.strip() != "":
                        blog_label = BlogLabel(BlogID=blog.BlogID,
                                               LabelName=l.strip())
                        db.session.add(blog_label)
                db.session.commit()

            else:
                flash("Database error!")
                return redirect(url_for("publish"))

            flash("Publish Successful!")
            return redirect(
                url_for("users", user_id=current_user.UserID, page=0))
        categories = Category.query.all()
        return render_template("publish.html",
                               form=form,
                               categories=[(category.CategoryID,
                                            category.CategoryName)
                                           for category in categories])
    else:
        blog = Blog.query.get(blog_id)
        if form.validate_on_submit():
            blog_title = request.form.get("title")
            blog_content = request.form.get("content")
            category = request.form.getlist("category")
            label = request.form.get("label")
            if not len(blog_content.strip()):
                flash("The content is necessray!")
                return redirect(url_for("publish", blog_id=blog_id))
            if not len(blog_title.strip()):
                flash("The title is necessray!")
                return redirect(url_for("publish", blog_id=blog_id))
            if blog.BlogTitle != blog_title:
                blog.BlogTitle = blog_title
            if blog.BlogContent != blog_content:
                blog.BlogContent = blog_content
            blog.LastEditTime = datetime.datetime.now()
            BlogLabel.query.execute("DELETE FROM BlogLabel WHERE BlogID=" +
                                    str(blog_id))
            BlogCategory.query.execute(
                "DELETE FROM BlogCategory WHERE BlogID=" + str(blog_id))
            db.session.commit()
            for c in category:
                blog_category = BlogCategory(BlogID=blog.BlogID,
                                             CategoryID=int(c))
                db.session.add(blog_category)
            db.session.commit()
            for l in label.split(';'):
                if l.strip() != "":
                    blog_label = BlogLabel(BlogID=blog.BlogID,
                                           LabelName=l.strip())
                    db.session.add(blog_label)
            db.session.commit()

            flash("Edit Successful!")
            return redirect(
                url_for("users", user_id=current_user.UserID, page=0))
        if not blog:
            return redirect("/index")
        if blog.PublisherID != current_user.UserID:
            flash("you can only edit your blog")
            return redirect("/index")
        labels = BlogLabel.query.filter_by(BlogID=blog.BlogID)
        if len(labels) > 0:
            label = labels[0].LabelName
            for l in labels[1:]:
                label += ";" + l.LabelName
        else:
            label = ""
        category = [
            c.CategoryID
            for c in BlogCategory.query.filter_by(BlogID=blog.BlogID)
        ]
        categories = Category.query.all()
        return render_template("publish.html",
                               form=form,
                               blog=blog,
                               label=label,
                               category=category,
                               categories=[(category.CategoryID,
                                            category.CategoryName)
                                           for category in categories])