def create_show_submission(): """ function for creating shows, that will create new 'show' object in the DB and add it then commit """ # called to create new shows in the db, upon submitting new show listing form # TODO: insert form data as a new Show record in the db, instead (DONE) err = False try: date_format = '%Y-%m-%d %H:%M:%S' show = Shows() show.artist_id = request.form['artist_id'] show.venue_id = request.form['venue_id'] show.start_time = datetime.strptime(request.form['start_time'], date_format) db.session.add(show) db.session.commit() # TODO: on unsuccessful db insert, flash an error instead. # e.g., flash('An error occurred. Show could not be listed.') # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ except: err = True db.session.rollback() finally: db.session.close() if err: flash('ERROR. The entered show has not been listed.') else: flash('Show was successfully listed!') # on successful db insert, flash success return render_template("pages/home.html")
def create_show_submission(): # called to create new shows in the db, upon submitting new show listing form data_recd_imm = request.form # print(data_recd_imm) just_fetch = Shows.query.first( ) # if we skip this then id is None & error occurs show_ins = Shows() # Instance of class for i in data_recd_imm: setattr(show_ins, i, data_recd_imm[i]) show_ins.start_time = datetime.strptime(show_ins.start_time, '%Y-%m-%d %H:%M:%S') show_date = show_ins.start_time.date() show_time = show_ins.start_time.time() reqArtist = Artist.query.filter_by(id=show_ins.artist_id).first() timingNotSuitable = False # Default state if ((reqArtist.artist_time_begin) and (show_time < reqArtist.artist_time_begin)): # Check begin time timingNotSuitable = True if ((reqArtist.artist_time_end) and (show_time > reqArtist.artist_time_end)): # Check end time timingNotSuitable = True if ((reqArtist.artist_date_begin) and (show_date < reqArtist.artist_date_begin)): # Check begin time timingNotSuitable = True if ((reqArtist.artist_date_end) and (show_date > reqArtist.artist_date_end)): # Check end time timingNotSuitable = True if (timingNotSuitable): flash('Available date for artist is ' + reqArtist.artist_date_begin.strftime('%Y-%m-%d') + ' to ' + reqArtist.artist_date_end.strftime('%Y-%m-%d') + ' between ' + reqArtist.artist_time_begin.strftime('%H:%M:%S') + ' to ' + reqArtist.artist_time_end.strftime('%H:%M:%S')) return redirect(url_for('create_shows')) error = False try: db.session.add(show_ins) # Insert in DB db.session.commit() except: db.session.rollback() error = True finally: db.session.close() if (error): flash('An error occurred. Show could not be listed.') else: flash('Show was successfully listed!') return render_template('pages/home.html')