示例#1
0
def new_beer():
	form = AddBeerForm(request.form)
	if request.method == 'POST' and form.validate():
		beer = model.Beer()
		beer.name = form.name.data
		beer.brewer = form.brewer.data
		beer.origin = form.origin.data
		beer.style = form.style.data
		beer.abv = form.abv.data
		beer.link = form.link.data
		beer.image = form.image.data
		model.session.add(beer)
		model.session.commit()

		new_beer = model.session.query(model.Beer).filter(model.Beer.name == form.name.data).first()
		add4 = model.Rating(user_id = 4, beer_id = new_beer.id, rating = 1)
		add5 = model.Rating(user_id = 5, beer_id = new_beer.id, rating = 2)
		add6 = model.Rating(user_id = 6, beer_id = new_beer.id, rating = 3)
		add7 = model.Rating(user_id = 7, beer_id = new_beer.id, rating = 4)
		add8 = model.Rating(user_id = 8, beer_id = new_beer.id, rating = 5)
		model.session.add(add4)
		model.session.add(add5)
		model.session.add(add6)
		model.session.add(add7)
		model.session.add(add8)
		model.session.commit()
		return redirect(url_for("all_beers"))
	return render_template('new_beer.html', form=form)
示例#2
0
文件: views.py 项目: nickpegg/home
def beer_new(request):
    if not request.user.is_superuser:
        return redirect('brewhouse-display')

    if request.method == 'POST':
        form = AddBeerForm(request.POST)
        if form.is_valid():
            b = Beer()
            b.name = form.cleaned_data['name']
            b.style = form.cleaned_data['style']
            b.recipe_url = form.cleaned_data['recipe_url']
            b.save()

            # Create some basic events based on the brewed-on date
            # tuple is of (event_type, days_since_brew_date)
            brew_date = form.cleaned_data['brew_date']

            events = (
                (7, 0),     # brewing
                (1, 0),     # brewed
                (2, 0),     # primary fermentation
                (3, 7),     # secondary fermentation
                (4, 21),    # kegged
                (0, 28),    # ready!
            )

            for etype, num_days in events:
                event = Event()
                event.beer = b
                event.event_type = etype
                event.date = brew_date + datetime.timedelta(days=num_days)
                event.save()

            return redirect('beer-show', b.id)
        else:
            pass    # TODO set an error
    else:
        form = AddBeerForm()

    return render(request, 'brewhouse/new_beer.html', locals())