def blog(): # connect to the db and get the users conn = sqlite3.connect('survivor.db') conn.row_factory = dict_factory c = conn.cursor() c.execute('SELECT user_nm FROM CompUser') results = c.fetchall() users = [(results.index(item), item) for item in results] form = BlogForm() form.username.choices = users if form.validate_on_submit(): # get the users choice choices = form.username.choices user = (choices[form.username.data][1]) time_ = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S') content = form.content.data query = "insert into Blog VALUES ('" + time_ + \ "'," + "'" + user + "'," + "'" + content + "')" c.execute(query) conn.commit() flash(f'Blog created for {user}!', 'success') return redirect(url_for('home')) return render_template('blog.html', form=form)
def blog(): form = BlogForm() if form.validate_on_submit(): # get the users choice time_ = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S') content = form.content.data # insert the blog message into the database c, conn = get_db() query = 'insert into Blog (time_, user_nm, comp_nm, post) VALUES ("{}","{}", "{}", "{}")'.format(time_, USERNM.name, COMPNAME, content) c.execute(query) conn.commit() return redirect(url_for('blog')) conn = sqlite3.connect('survivor.db') #Display all blogs from the 'blogs' table conn.row_factory = dict_factory c = conn.cursor() c.execute("SELECT * FROM Blog \ ORDER BY time_ DESC") blogs = c.fetchall() return render_template('blog.html', posts=blogs, form=form, user_nm=USERNM.name)
def editBlog(id): blog_id = id org_blog = session.query(BlogModel).get(blog_id) form = BlogForm() if form.validate_on_submit(): name = form['title'].data content = form['content'].data if org_blog.author != current_user.id: return "Permission Denied" session.query(BlogModel).filter(BlogModel.id == blog_id).update({ "name": name, "content": content, "edited_on": datetime.datetime.now() }) session.commit() flash("Blog edited successfully") return redirect("/blog/%d" % id) else: blog = session.query(BlogModel).get(blog_id) if blog.author == current_user.id: form.title.data = blog.name form.content.data = blog.content return render_template('edit_blog.html', title=blog.name, content=blog.content, blog_id=blog_id, form=form) else: return "Sign in with correct user"
def edit_blog(user_id): blog = session.query(Blog).filter_by(user_id=user_id).one() old_img = blog.profile_img form = BlogForm(obj=blog) if form.validate_on_submit(): blog.last_modified = time() form.populate_obj(blog) if form.image.data and allowed_file(form.image.data.filename): if old_img: # first delete the previous file in the upload_folder deleting_file = os.path.join(app.config['UPLOAD_FOLDER'], old_img) os.remove(deleting_file) # then upload the new file file = form.image.data # upload the file with prefix(blog_id), suffix(last_modified) blog.profile_img = upload(file, str(login_session['blog_id']), str(int(blog.last_modified))) session.add(blog) session.commit() return redirect(url_for('view_blog', user_id=user_id)) return render_template('blog_profile.html', form=form, user_id=login_session.get('user_id'), username=login_session.get('username'))
def update(blog_id): blog_form = BlogForm(user_id=current_user.id) if blog_form.validate_on_submit(): Blog.update(blog_id, blog_form.data) return redirect(url_for('.detail', blog_id=blog_id)) else: flash('修改博客失败') return redirect(url_for('.edit', blog_id=blog_id))
def add(): blog_form = BlogForm(user_id=current_user.id) if blog_form.validate_on_submit(): Blog.new(blog_form.data) return redirect(url_for('.index')) else: flash('添加博客失败') return redirect(url_for('.new'))
def post_blog(): form = BlogForm() if form.validate_on_submit(): blog = form.get_blog() mongo.db.blogs.insert(blog) flash('发布成功') return redirect(request.args.get('next') or url_for('main.index')) return render_template('post.html', form=form)
def edit_blog(blog_id): blog = mongo.db.blogs.find_one_or_404({'_id': ObjectId(blog_id)}) form = BlogForm(data=blog) if form.validate_on_submit(): formdata = form.get_blog() formdata.pop("time") formdata.pop("view_count") mongo.db.blogs.update({"_id": ObjectId(blog_id)}, {"$set": formdata}, True, False) flash('更新成功') return redirect(request.args.get('next') or url_for('main.index')) return render_template('post.html', form=form)
def edit_blog(id): blog = Blog.query.get_or_404(id) if current_user != blog.author and not current_user.is_administrator: abort(403) form = BlogForm(obj=blog) if form.validate_on_submit(): form.populate_obj(blog) db.session.add(blog) db.session.commit() flash('博客已经更新', 'success') return redirect(url_for('front.blog', id=blog.id)) return render_template('user/edit_blog.html', form=form)
def post_blog(): # Check if user logged in to give access to page, otherwise direct to login form2 = BlogForm() if 'username' in session: if form2.validate_on_submit(): with open('data/blog_posts.csv', 'a') as f: writer = csv.writer(f) writer.writerow([form2.blog_title.data, form2.content.data, form2.topic_area.data]) return redirect(url_for('successful_post')) return render_template('Post_blog.html', form2=form2) else: flash('Log in first!') return redirect(url_for('login'))
def newBlog(): form = BlogForm() if form.validate_on_submit(): print(form) title = form['title'].data content = form['content'].data blog = BlogModel(name=title, content=content, author=current_user.id, created_on=datetime.datetime.now()) session.add(blog) session.commit() return redirect('/blog/{}'.format(blog.id)) else: return render_template("post_blog.html", form=form)
def new_blog(): """Blog List.""" error = None form = BlogForm(request.form) if form.validate_on_submit(): print 'hi' new_message = BlogPost(form.title.data, form.description.data, current_user.id) db.session.add(new_message) db.session.commit() flash('New entry was successfully posted. Thanks.') return redirect(url_for('blog.blogs')) else: posts = db.session.query(BlogPost).all() return render_template('new_blog.html', posts=posts, form=form, error=error)
def addBlog(): title = {'title': 'Add Blog', 'year': forDate()} form = BlogForm() if form.validate_on_submit(): '''---------------Add blog to database--------------''' blogTitle = request.form.get('blogTitle') auther = request.form.get('auther') blogContent = request.form.get('blogContent') tags = request.form.get('tags') now = datetime.now() now = now.strftime('%Y-%m-%d %H:%M:%S') dbconn = pymysql.connect(host="localhost", user="******", password="******", db="mooz") curr = dbconn.cursor() curr.execute("INSERT INTO posts (userId,title,Auther,post,tags,created) values ('" + '1' + "','" + blogTitle + "','" + auther + "','" + blogContent + "','" + tags + "','" + now + "')") dbconn.commit() flash("User Adding his blog of {} Title ".format(form.blogTitle.data)) return redirect(url_for('home')) return render_template('addBlog.html',title=title,form=form)
def adminBlogEdit(post_id=1): # logout_user() blog = BlogPost.query.get(post_id) form = BlogForm() form.content.data = blog.content form.preview.data = blog.preview form.published.data = blog.published # print blog.content # print(request.args.get('value')) # print("what!") if form.validate_on_submit(): # continue editing hereself. # b = BlogPost(blogTitle = form.title.data, date= datetime.now(), content=form.content.data, preview=form.preview.data, published=form.published.data, views=0, previewImage="../static/images/seattle.jpg" ) # db.session.add(b) pdb.set_trace() blog.blogTitle = form.title.data blog.content = form.content.raw_data[0] blog.preview = form.preview.raw_data[0] blog.published = form.published.label.text print form.published.data print form.published.data print form.published.data print blog.content print blog.preview print blog.published db.session.add(blog) db.session.commit() # print "I just updated something!" else: # print "Form failed validation" print blog.comments published_blogs = BlogPost.query.filter_by(published=True) unpublished_blogs = BlogPost.query.filter_by(published=False) return render_template( "admin.html", published_blogs=published_blogs, unpublished_blogs=unpublished_blogs, current_blog=blog, form=form )
def index(): '''网站首页''' form = BlogForm() # 凡是登录成功的用户,就有写博客的权限 if current_user.is_authenticated: if form.validate_on_submit(): blog = Blog() form.populate_obj(blog) blog.author = current_user db.session.add(blog) db.session.commit() flash('成功发布博客', 'success') return redirect(url_for('.index')) page = request.args.get('page', 1, type=int) pagination = Blog.query.order_by(Blog.time_stamp.desc()).paginate( page, per_page=current_app.config['BLOGS_PER_PAGE'], error_out=False) blogs = pagination.items return render_template('index.html', form=form, blogs=blogs, pagination=pagination)