Our users are now able to create accounts and log in. You may be curious, and ask yourself, “How can I make sure that they manipulate only their data and not someone else’s?”
We solve this association problem by making every dinner party an instance of a DinnerParty
model, where each party is created by an instance of the User
model. We can then use the unique identifying attributes of each object to ensure functionality like creating new dinner parties hosted by a specific user and letting users RSVP to a specific dinner party.
We can update our user endpoint with functionality to check for existing dinner parties and create a new dinner party using a form:
def user(username): user = User.query.filter_by(username=username).first_or_404() dinner_parties = DinnerParty.query.filter_by(party_host_id=user.id) if dinner_parties is None: dinner_parties = [] form = DinnerPartyForm(csrf_enabled=False)
- query the
DinnerParty
model for all dinner parties where the party host is our logged-in user, and store the parties indinner_parties
- if there is no dinner party hosted by the logged-in user, set
dinner_parties
to an empty list - create a
DinnerPartyForm
namedform
Once the user submits the form for a new dinner party, we can use the form data to create a new DinnerParty
instance:
# user route continued if form.validate_on_submit(): new_dinner_party = DinnerParty( date=form.date.data, venue=form.venue.data, main_dish=form.main_dish.data, number_seats=int(form.number_seats.data), party_host_id=user.id, attendees=username) db.session.add(new_dinner_party) db.session.commit() return render_template('user.html', user=user, dinner_parties=dinner_parties, form=form)
- if
form
validates, create a newDinnerParty
objectnew_dinner_party
- the
DinnerParty
attributes (date
,venue
, …,attendees
) are assigned values from their accompanying form field data - the
attendees
attribute is initialized with the logged-in user’susername
new_dinner_party
is added to the session and committed
We can create a new route that will allow users to see all the dinner parties that are happening and provide a form for RSVPing to a specific party:
def rsvp(username): user = User.query.filter_by(username=username).first_or_404() dinner_parties = DinnerParty.query.all() if dinner_parties is None: dinner_parties = [] form = RsvpForm(csrf_enabled=False) if form.validate_on_submit(): dinner_party = DinnerParty.query.filter_by(id=int(form.party_id.data)).first() dinner_party.attendees += f", {username}" db.session.commit() return render_template('rsvp.html', user=user, dinner_parties=dinner_parties, form=form)
- set
user
to the logged-in user - query all dinner parties in the
DinnerParty
model and save them todinner_parties
for display on the page - create an RSVP form named
form
- if
form
validates, query theDinnerParty
model for the dinner party with anid
that matches theid
entered in the form - update the attendee list with the logged-in user’s
username
and commit the change
Instructions
Let’s start by adding dinner party creation functionality to our application! In order to make the code easier to navigate in the workspace, we’ve split the code into multiple files: routes.py, models.py, forms.py, and app.py.
Review the code in models.py, where a DinnerParty
class has been implemented with the following attributes: id
, date
, venue
, main_dish
, number_seats
, party_host_id
, and attendees
.
Then navigate to forms.py, where a DinnerPartyForm
form has been implemented with the following fields: date
, venue
, main_dish
and number_seats
.
When you have reviewed the class and the form, run the code to proceed to the next checkpoint. View the hint for more detailed information about the class and form!
Navigate back to routes.py. The user route has been updated to include much of the code necessary for creating a new dinner party, but the code for creating a new DinnerParty
instance is incomplete.
Update the values of each attribute in the definition of the DinnerParty
instance new_dinner_party
.
Set the values as follows:
date
as the date set inform
venue
as the venue set inform
main_dish
as the main dish set inform
number_seats
as an integer of the number of seats set in the formparty_host_id
asuser
‘sid
attributeattendees
as theusername
of the current user
In routes.py we’ve added a new route, rsvp()
, that allows logged-in users to RSVP to a dinner party. In order for a user to RSVP, you need a simple RSVP form. Navigate to forms.py and review the provided RsvpForm
, with its one field, party_id
.
Now you can query the DinnerParty
model to find the specific dinner party the user wants to RSVP for. Query DinnerParty
and filter_by()
the party_id
as entered on the form. Select the first()
value from the query, and save the result to a variable dinner_party
.
Now that you have the dinner party the user wants to RSVP for, you can add them to the attendee list! Update dinner_party
‘s attendees
attribute as follows:
dinner_party.attendees += f", {username}"