def storynew(): if 'username' in session: user = session['username'] form = StoryForm() if form.validate_on_submit(): uid = dbsession.query(User.id).filter_by(name=user).first() newstory = Story(form.title.data) newstory.text = markdown.markdown(form.body.data) newstory.uid = uid[0] newstory.adult = form.adult.data tagslist = form.tags.data tagslist = tagslist.split(',') for tagitem in tagslist: tagitem = tagitem.strip() tagitem = tagitem.lower() tag = dbsession.query(Tag).filter_by(tagname=tagitem).first() if tag is None: tag = Tag(tagitem) newstory.tags.append(tag) dbsession.add(newstory) dbsession.commit() return redirect("~"+user) return render_template("storynew.html", form=form) else: return render_template("storynew.html")
def add_story(): check_admin() add_story = True form = StoryForm() if form.validate_on_submit(): filename = secure_filename(form.upload.data.filename) src = UPLOADPATH + filename form.upload.data.save(src) filemd5 = hashlib.md5() with open(UPLOADPATH + filename, 'rb') as f: for chunk in iter(lambda: f.read(4096), b""): filemd5.update(chunk) if form.available.data: available = True else: available = False dst = S_IMAGEPATH + filemd5.hexdigest() + '.' + filename.split('.')[1] if Story.query.filter_by(imgurl=filemd5.hexdigest() + '.' + filename.split('.')[1]).first() == None: copyfile(src, dst) os.remove(src) story = Story(title=form.title.data, author=form.author.data, imgurl=filemd5.hexdigest() + '.' + filename.split('.')[1], location=form.location.data, description=form.description.data, hitnumber=0, available=available) db.session.add(story) db.session.commit() flash('Add story successfull.') else: os.remove(src) flash('Upload image file was in used.') # redirect to the departments page return redirect(url_for('admin.stories', page=1)) # form.common_name.data = product.common_name # form.price.data = product.price catalogs = Catalog.get_all() stories = Story.get_all() return render_template('admin/story.html', action="Add", add_story=add_story, form=form, stories=stories, title="Add Story", catalogs=catalogs)
def add_story(): form = StoryForm(request.form) if request.method == 'POST' and form.validate_on_submit(): story = Story(form.title.data, form.content.data) try: StoryService.add_story(_story=story, _nick_name=current_user.username, _category=form.category.data, _author=current_user.username) flash("add success") return redirect(url_for('index')) except Exception, e: return jsonify({"errorMessage": e.message})
def render_form(): """Render questions for madlibs form""" form = StoryForm() if form.validate_on_submit(): place = form.place.data noun = form.noun.data verb = form.verb.data adjective = form.adjective.data plural_noun = form.plural_noun.data return f"These are the things in the form: {place}, {noun}, {verb}, {adjective}, {plural_noun}" else: return render_template("questions_form.html", form=form)