Example #1
0
def rsvp():
    form = RsvpForm()
    if 'guestid' not in session:
        return redirect(url_for('login'))
    guests = Guest.query.filter_by(group=session['group']).all()

    if request.method == 'POST' and form.validate():
        #if form.validate_on_submit():
            #testg = Rsvp(2, 1, 'friday','afternoon',0)
            #return testg
            #db.session.add(testg)
            msg = Message("Thank you for your RSVP", sender=("Name", "*****@*****.**"))
            msg.bcc = ("*****@*****.**","*****@*****.**")
            for i in guests:
                msg.add_recipient(i.email_address)
                #return i.id
                #gid = i.id
                #logging.debug("this is i.id: ", i.id)
                #rsvpi = Rsvp(gid, form.i['id'].data, form.arrival_date.data, form.arrival_time.data, form.child_care.data, 1)
                radioname = "guest" + str(i.id)
                radval = form[radioname].data
                radval = str(radval)
 		if radval not in ('0','1'): radval = -1
                #if i.id == session['guestid']: session['response'] = radval
                ccval = form.child_care.data
                if ccval not in ('0','1'): ccval = -1
                rsvpi = Rsvp(i.id, radval, form.notebox.data, form.arrival_date.data, form.arrival_time.data, ccval, 1 )
                db.session.add(rsvpi)
            db.session.commit()
            rsvp_response0 = GuestRsvp.query.filter_by(id=session['guestid']).first()
            rsvp_response1 = GuestRsvp.query.filter_by(group=session['group']).all()
            #msg.body = "Insert receipt email here" # text body
            msg.body = render_template('rsvp-thank-you-email.txt', rsvp0=rsvp_response0, rsvpr=rsvp_response1)
            msg.html = render_template('rsvp-thank-you-email.html', rsvp0=rsvp_response0, rsvpr=rsvp_response1)
            mail.send(msg)
            return redirect(url_for('rsvp'))
        #else:
            #return "Didn't work"

    glist = []
    for j in guests:
        glist.append(j.id)

    rsvp_response0 = GuestRsvp.query.filter_by(id=session['guestid']).first()
    rsvp_response = GuestRsvp.query.filter_by(group=session['group']).all()
    for r in rsvp_response:
        if r.id == session['guestid']:
            session['response'] = r.response

    if rsvp_response:
        return render_template('rsvp-thank-you.html', rsvpr=rsvp_response, guests=guests, glist=glist, rsvp0=rsvp_response0)

    return render_template('rsvp.html', form=form, guests=guests)