def comment(): parent = blogDB.Post.query.filter_by(id=request.args.get('post_id')).first() form = formread.CommentForm(request.form) if request.method == 'POST': user_management.verify_user_log_in() user = user_management.session_user() new_comment = blogDB.Post(user, form.comment_text.data, parent=parent, title='comment') db.session.add(new_comment) db.session.commit() commented_post = blogDB.Post.query.filter_by(id=parent.id).first() commented_post.content = Markup(commented_post.content) comments = blogDB.Post.query.filter_by(parent_id=parent.id).all() return render_template('comment.html', post=parent, comments=comments, form=form, bootstrap=True)
def create_project(): # make sure user is logged in UM.verify_user_log_in() # if the user is trying to post, make sure they have the credentials. UM.verify_user_admin() # set up the page properly form = formread.ProjectForm(request.form) base = app.jinja_env.get_template('makeproject.html') # if we're submitting the form as opposed to requesting it do these things. if request.method == 'POST': # get header image if 'headIMG' in request.files: headIMG = request.files['headIMG'] # if the file is legit replace the filename with it if headIMG and allowed_file(headIMG.filename): file_name = werkzeug.secure_filename(headIMG.filename) headIMG.save(os.path.join(app.root_path, app.config['PROJECT_IMG_FOLDER'], file_name)) flash('file submitted. ') else: flash('If a file was submitted, it was of a non-allowed format.') return render_template(base, title="creating project", form=form, bootstrap=True) # add the entry to the database entry = blogDB.Project(title=form.title.data, pict=file_name, abstract=form.abstract.data) try: db.session.add(entry) db.session.commit() return redirect('/projects') except sql_exception.OperationalError: flash('Post was not able to be submitted due to a server error. Maybe try again?') return render_template(base, title="creating project", form=form, bootstrap=True)
def edit_post(): # make sure user is logged in and is an administrator verify_user_log_in() verify_user_admin() # pull the entry from the database try: entry = blogDB.Post.query.filter_by(id=request.args.get('entry')).first() except sql_exception.OperationalError: entry = blogDB.Post("General Error", "Whoops", "The server had a senior moment, or something like that.\ Give it a minute and try again, and then contact the administrator", "") # if the entry doesn't exist, tell the user. if entry is None: flash("There are no posts with ID # " + request.args.get('entry') + " to be edited.") return redirect('/home') form = formread.BlogForm(request.form) # update the relevant parts of the post if request.method == 'POST': if form.delete.data is True: db.session.delete(entry) else: if form.textHTML.data != "": entry.content = form.textHTML.data flash("content updated") if form.title.data != "": entry.title = form.title.data flash("title updated") # get header image if 'headIMG' in request.files: headIMG = request.files['headIMG'] # if the file is legit replace the filename with it if headIMG and allowed_file(headIMG.filename): filename = werkzeug.secure_filename(headIMG.filename) headIMG.save(os.path.join(app.root_path, app.config['POST_IMG_FOLDER'], filename)) # update the entry entry.head_img = filename flash("head image updated") # locate associated projects form_projects = form.projects.data entry.set_projects(form_projects) db.session.commit() return redirect('/home') else: # pre-populate the form: tags = ', '.join([tag.value for tag in entry.tags]) form = formread.BlogForm(title=entry.title, textHTML=entry.content, tags=tags) projects = blogDB.Project.query.all() form.projects.choices = [(project.id, project.title) for project in projects] selected = [str(p.id) for p in entry.projects] form.projects.data = selected base = app.jinja_env.get_template('edit-post.html') return render_template(base, title="editing post", form=form, id=request.args.get('entry'))
def post(): # make sure user is logged in verify_user_log_in() # if the user is trying to post, make sure they have the credentials. verify_user_admin() form = formread.BlogForm(request.form) projects = blogDB.Project.query.all() form.projects.choices = [(project.id, project.title) for project in projects] # if we're submitting the form as opposed to requesting it do these things. if request.method == 'POST': # import the form content if 'bodyHTML' not in request.files or form.textHTML.data != "": content = form.textHTML.data # if the form is empty, upload the file, the effect is identical. else: bodyHTML = request.files['bodyHTML'] content = bodyHTML.read() # get header image if 'headIMG' in request.files: headIMG = request.files['headIMG'] # if the file is legit replace the filename with it if headIMG and allowed_file(headIMG.filename): file_name = werkzeug.secure_filename(headIMG.filename) headIMG.save(os.path.join(app.root_path, app.config['POST_IMG_FOLDER'], file_name)) flash('file submitted. ') else: flash('If a file was submitted it was of a non-allowed format.') user = blogDB.User.query.filter_by(username=session['user']['username']).first() entry = blogDB.Post(user=user, title=form.title.data, content=content) for tags in csv.reader(form.tags.data.split('\n'), delimiter=','): for tag in tags: new_tag = blogDB.Tag(tag.lstrip().rstrip().lower()) old_tag = blogDB.Tag.query.filter_by(value=new_tag.value).first() if old_tag is None: entry.tags.append(new_tag) else: entry.tags.append(old_tag) # attach the projects form_projects = form.projects.data projects = blogDB.Project.query.filter(blogDB.Project.id.in_(form_projects)).all() # add the entry to the database try: db.session.add(entry) db.session.commit() for project in projects: project.rel_posts.append(entry) db.session.commit() return redirect('/home') except sql_exception.OperationalError: flash('Post was not able to be submitted due to a server error. Maybe try again?') base = app.jinja_env.get_template('makepost.html') return render_template(base, title="entering post", form=form, bootstrap=True)