def edit_actors(payload, actor_id): body = request.get_json() actor = Actor.query.get(actor_id) if actor is None: # if there is no actor I want to raise 404 error (not found) abort(404) new_name = body.get('name') new_age = body.get( 'age') # get the age and the name as will as the gender new_gender = body.get('gender') if (new_name is None) or (new_age is None) or (new_gender is None): abort(422) try: if new_name is not None: actor.name = new_name if new_age is not None: actor.age = new_age # if the data i exist => I will add it to thhe database if new_gender is not None: actor.gender = new_gender actor.update() except: rollback( ) #d if there is something wrong I want to rollback I set this func in the models.py abort(422) return jsonify({"success": True, "patched_actor": actor.format()}), 200
def edit_actors(jwt, actor_id): body = request.get_json() actor = Actor.query.get(actor_id) if actor is None: abort(404) new_name = body.get('name') new_age = body.get('age') new_gender = body.get('gender') if (new_name is None) or (new_age is None) or (new_gender is None): abort(422) try: if new_name is not None: actor.name = new_name if new_age is not None: actor.age = new_age if new_gender is not None: actor.gender = new_gender actor.update() except: rollback() abort(422) return jsonify({"success": True, "patched_actor": actor.format()}), 200
def initialiseDB(): try: loc = ("Mywords.xlsx") wb = xlrd.open_workbook(loc) sheet = wb.sheet_by_index(0) meanings = [] # As we are generating options using meanings of other words, we # first get all the meanings and store them for i in range(sheet.nrows): meanings.append(sheet.cell_value(i, 1)) # We insert the words one by one into the database for i in range(1, sheet.nrows): word = sheet.cell_value(i, 0).lower() meaning = sheet.cell_value(i, 1).lower() hint = sheet.cell_value(i, 2).lower() completed = sheet.cell_value(i, 3) options = [] while len(options) <= 3: newChoice = random.choice(meanings) if newChoice not in options: options.append(newChoice) newWord = Word(word=word, meaning=meaning, hint=hint, options=options, completed=completed) newWord.insert() return jsonify({"success": True}) except BaseException: rollback() print("Word formatting error") abort(400)
def edit_artist_submission(artist_id): # TODO: take values from the form submitted, and update existing # artist record with ID <artist_id> using the new attributes try: seeking = False if 'seeking_venue' in request.form: seeking = True description = '' if 'seeking_description' in request.form: description = request.form['seeking_description'] artist = Artist.query.get(artist_id) artist.name = request.form['name'] artist.city = request.form['city'] artist.state = request.form['state'] artist.genres = request.form.getlist('genres') artist.phone = request.form['phone'] artist.image_link = request.form['image_link'] artist.facebook_link = request.form['facebook_link'] artist.website = request.form['site_link'] artist.seeking_description = description artist.seeking_venue = seeking artist.update() except: rollback() finally: close() return redirect(url_for('show_artist', artist_id=artist_id))
def add_actor(): error = False new_name = request.form['name'] new_age = request.form['age'] new_gender = request.form['gender'] new_image_link = request.form['image_link'] new_description = request.form['description'] try: actor = Actors(name=new_name, age=new_age, gender=new_gender, image_link=new_image_link, description=new_description) actor.insert() flash('Artist ' + request.form['name'] + ' successfully created 🚀') except Exception: rollback() error = True abort(400) flash('An error occurred. ' + actor.name + ' could not be listed.') return redirect(url_for('get_actors'))
def getWord(): try: # Select a random word from the list of words which the user has # not completed yet word = random.choice(words) # print(word) if word is None: return jsonify( {"word": "You have learnt all the words in our database!"}) gotWord = Word.query.get(word.id) gotWord.completed = "yes" gotWord.insert() words.remove(word) return jsonify({ "id": word.id, "word": word.word, "meaning": word.meaning, "hint": word.hint, "options": word.options }) except BaseException: rollback() abort(400)
def create_or_search_question(): body = request.get_json() if not body: abort(400) question = body.get('question', None) answer = body.get('answer', None) category = body.get('category', None) difficulty = body.get('difficulty', None) search_term = body.get('searchTerm', None) # if not questions search case, any missing args in the body will be considered bad request if search_term is None: for arg in ['question', 'answer', 'category', 'difficulty']: if arg not in body: abort(400) try: if search_term is not None: questions = Question.query.filter( Question.question.ilike('%{}%'.format(search_term))).all() selection = [question.format() for question in questions] current_questions = paginate_questions(request, selection) return jsonify({ 'success': True, 'questions': current_questions, 'total_questions': len(selection), 'current_category': None }) else: for arg in [question, answer, category, difficulty]: if not arg: abort(422) if not Category.query.get(category): abort(422) question = Question(question=question, answer=answer, category=category, difficulty=difficulty) question.insert() question_id = question.id selection = [ question.format() for question in Question.query.all() ] current_questions = paginate_questions(request, selection) return jsonify({ 'success': True, 'created': question_id, 'questions': current_questions, 'total_questions': len(selection), 'current_category': None }) except: rollback() print(sys.exc_info()) abort(422) finally: close_connection()
def create_artist_submission(): # called upon submitting the new artist listing form # TODO: insert form data as a new Venue record in the db, instead # TODO: modify data to be the data object returned from db insertion try: description = '' seeking = False if 'seeking_venue' in request.form: seeking = True if 'seeking_description' in request.form: description = request.form['seeking_description'] artist = Artist( name=request.form['name'], city=request.form['city'], state=request.form['state'], phone=request.form['phone'], genres=request.form.getlist('genres'), image_link=request.form['image_link'], facebook_link=request.form['facebook_link'], website=request.form['site_link'], seeking_description=description, seeking_venue=seeking, ) artist.insert() flash('Artist ' + request.form['name'] + ' was successfully listed!') except: rollback() flash('An error occurred. Artist ' + request.form['name'] + ' could not be listed.') finally: close() return render_template('pages/home.html')
def edit_venue_submission(venue_id): # TODO: take values from the form submitted, and update existing # venue record with ID <venue_id> using the new attributes try: seeking = False if 'seeking_talent' in request.form: seeking = True description = '' if 'seeking_description' in request.form: description = request.form['seeking_description'] venue = Venue.query.get(venue_id) venue.name = request.form['name'] venue.city = request.form['city'] venue.state = request.form['state'] venue.address = request.form['address'] venue.genres = request.form.getlist('genres') venue.phone = request.form['phone'] venue.image_link = request.form['image_link'] venue.facebook_link = request.form['facebook_link'] venue.website = request.form['site_link'] venue.seeking_description = description venue.seeking_talent = seeking venue.update() except: rollback() finally: close() return redirect(url_for('show_venue', venue_id=venue_id))
def delete_movies(jwk, movie_id): movie = Movie.query.get(movie_id) if movie is None: abort(404) try: movie.delete() except Exception: rollback() abort(422) return jsonify({"success": True, "deleted_movie": movie.format()}), 200
def delete_actors(payload, actor_id): actor = Actor.query.get(actor_id) # get the actor by id if actor is None: abort(404) try: actor.delete() # if there is nothing wrong I will delete it except Exception: rollback( ) # but if there is an error I want to rollback and raise 500 error abort(500) return jsonify({"success": True, "deleted_actor": actor.format()}), 200
def delete_venue(venue_id): try: venue = Venue.query.get(venue_id) venue.remove() except: rollback() finally: close() # TODO: Complete this endpoint for taking a venue_id, and using # SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail. # BONUS CHALLENGE: Implement a button to delete a Venue on a Venue Page, have it so that # clicking that button delete it from the db then redirect the user to the homepage return render_template('pages/home.html')
def delete_movies(jwk, movie_id): movie = Movie.query.get(movie_id) if movie is None: abort(404) try: movie.delete() except Exception: rollback() abort(500) return (jsonify({ 'success': True, 'deleted_movie': movie.format() }), 200)
def delete_actors(jwt, actor_id): actor = Actor.query.get(actor_id) if actor is None: abort(404) try: actor.delete() return jsonify({ "success": True, "deleted_actor": actor.format() }), 200 except Exception: rollback() abort(500)
def update_actor(actor_id): actor = Actors.query.get(actor_id) try: actor.name = request.form['name'] actor.age = request.form['age'] actor.gender = request.form['gender'] actor.image_link = request.form['image_link'] actor.update() flash(request.form['name'] + ' successfully updated 🚀 ') except BaseException: rollback() flash(request.form['name'] + ' cannot be updated! 😞') return redirect(url_for('get_actors', actor_id=actor_id))
def delete_actors(jwk, actor_id): actor = Actor.query.get(actor_id) if actor is None: abort(404) try: actor.delete() except Exception: rollback() abort(500) return (jsonify({ 'success': True, 'deleted_actor': actor.format() }), 200)
def delete_movie(payload, movie_id): try: movie = Movie.query.filter(Movie.id == movie_id).one_or_none() if movie is None: abort(404) movie.delete() return jsonify({ 'success': True, 'deleted_movie': movie.format() }), 200 except: rollback() abort(422)
def create_show_submission(): # 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 try: show = Show( artist_id=request.form['artist_id'], venue_id=request.form['venue_id'], start_time=request.form['start_time'], ) show.insert() flash('Show was successfully listed!') except: rollback() flash('An error occurred. Show could not be listed.') finally: close() return render_template('pages/home.html')
def add_question(): try: #insures the request had json with the appropriate keys/values fields = { 'question': request.json['question'], 'answer': request.json['answer'], 'category': request.json['category'], 'difficulty': request.json['difficulty'] } except: abort(400) try: question = Question(**fields) question.insert() except: #should be due to inappropriate values for the database rollback() print(sys.exc_info()) abort(422) return jsonify({'success': True})
def delete_question(question_id): question = Question.query.get(question_id) if question is None: abort(422) try: question.delete() selection = [ question.format() for question in Question.query.all() ] current_questions = paginate_questions(request, selection) return jsonify({ 'success': True, 'deleted': question_id, 'questions': current_questions, 'total_questions': len(selection) }) except: rollback() print(sys.exc_info()) abort(422) finally: close_connection()
def edit_actors(jwk, actor_id): body = request.get_json() actor = Actor.query.get(actor_id) name = (body.get('name') if body.get('name') else actor.name) age = (body.get('age') if body.get('age') else actor.age) gender = (body.get('gender') if body.get('gender') else actor.gender) if actor is None: abort(404) try: actor.name = name actor.age = age actor.gender = gender actor.update() except Exception: rollback() abort(500) return (jsonify({ 'success': True, 'patched_actor': actor.format() }), 200)