def delete_activity(city_id, activity_id): """ Provides: Functionality to delete an activity in a city On GET: renders the form to delete an activity On POST: takes the input from the form and deletes the activity from the DB Args: city_id: the unique identifier for the city. activity_id: the unique identifier for the activity in that city """ city = get_city(city_id) activity = get_activity(city_id, activity_id) if request.method == 'POST': session.delete(activity) session.commit() flash("The activity has been successfully deleted from {0}". format(city.name)) return redirect(url_for('.show_city', city_id=city_id)) else: if 'user_id' not in login_session or \ activity.user_id != login_session['user_id']: return redirect(url_for('.show_activity', city_id=city_id, activity_id=activity_id)) else: return render_template('delete_activity.html', city=city, activity=activity)
def edit_city(city_id): """ Provides: Functionality for editing a city On GET: render the edit city page On POST: updates the city in the DB Args: city_id: the unique identifier for the city. """ city = get_city(city_id) if request.method == 'POST': city.name = request.form['name'] city.state_provence = request.form['state'] city.country = request.form['country'] city.description = request.form['description'] city.image = request.form['image'] session.query(City).filter(City.id == city_id).update( { City.name: city.name, City.state_provence: city.state_provence, City.country: city.country, City.description: city.description, City.image: city.image }, synchronize_session=False) session.commit() flash("{0} has been successfully edited".format(city.name)) return redirect(url_for('.show_city', city_id=city.id)) else: if 'user_id' not in login_session or \ city.user_id != login_session['user_id']: return redirect(url_for('.show_city', city_id=city.id)) else: return render_template('edit_city.html', city=city)
def new_activity(city_id): """ Provides: Functionality for adding a new activity for a city On GET: renders the form to add a new activity On POST: takes the input from the form and adds a new activity to the DB Args: city_id: the unique identifier for the city. """ city = get_city(city_id) if request.method == 'POST': activity_to_add = Activity(name=request.form['name'], city_id=city_id, address=request.form['address'], category=request.form['category'], description=request.form['description'], website=request.form['website'], image=request.form['image'], user_id=login_session['user_id']) session.add(activity_to_add) session.commit() flash("{0} has been successfully added to {1}".format( activity_to_add.name, city.name)) return redirect(url_for('.show_city', city_id=city_id)) else: if 'user_id' not in login_session or \ city.user_id != login_session['user_id']: return redirect(url_for('.show_city', city_id=city_id)) return render_template('new_activity.html', city=city)
def city_json(city_id): """ Provides: Functionality to get a city and transform it into JSON Args: city_id: the unique identifier for the city Returns: JSON formatted city information """ city = get_city(city_id) return jsonify(city=city.serialize)
def edit_activity(city_id, activity_id): """ Provides functionality for editing an activity for a city On GET: renders the form to edit an activity On POST: takes the input from the form and updates the activity in the DB Args: city_id: the unique identifier for the city. activity_id: the unique identifier for the activity in that city """ city = get_city(city_id) activity = get_activity(city_id, activity_id) if request.method == 'POST': activity.name = request.form['name'] activity.address = request.form['address'] activity.category = request.form['category'] activity.description = request.form['description'] activity.website = request.form['website'] activity.image = request.form['image'] session.query(Activity).filter( Activity.id == activity_id, Activity.city_id == city_id ).update( { Activity.name: activity.name, Activity.address: activity.address, Activity.description: activity.description, Activity.category: activity.category, Activity.website: activity.website, Activity.image: activity.image } ) session.commit() flash("This item has been successfully edited!") return redirect(url_for('.show_activity', city_id=city_id, activity_id=activity_id)) else: if 'user_id' not in login_session or \ activity.user_id != login_session['user_id']: return redirect(url_for('.show_activity', city_id=city_id, activity_id=activity_id)) else: return render_template('edit_activity.html', city=city, activity=activity)
def city_xml(city_id): """ Provides: Functionality to get a city and transform it into XML Args: city_id: the unique identifier for the city Returns: XML formatted city information """ city = get_city(city_id) city_in_xml = dicttoxml(city.serialize) city_in_pretty_xml = parseString(city_in_xml).toprettyxml() return Response(city_in_pretty_xml, mimetype='text/xml')
def show_activity(city_id, activity_id): """ Provides functionality for displaying an activity for a city Args: city_id: the unique identifier for the city. activity_id: the unique identifier for the activity in that city """ city = get_city(city_id) activity = get_activity(city_id, activity_id) creator = get_creator(activity.user_id) if 'user_id' not in login_session or \ creator.id != login_session['user_id']: return render_template('show_activity_public.html', city=city, activity=activity, creator=creator) else: return render_template('show_activity.html', city=city, activity=activity, creator=creator)
def show_city(city_id): """ Provides: Functionality for displaying a city and associated activities Args: city_id: the unique identifier for the city. """ city = get_city(city_id) activities = session.query(Activity).filter( Activity.city_id == city_id).all() creator = get_creator(city.user_id) if 'user_id' not in login_session or creator.id != login_session['user_id']: return render_template('show_city_public.html', city=city, activities=activities, creator=creator) else: return render_template('show_city.html', city=city, activities=activities, creator=creator)
def delete_city(city_id): """ Provides functionality for deleting a city and associated activities On GET: render the delete city page On POST: deletes the city and related activities from the DB Args: city_id: the unique identifier for the city. """ city = get_city(city_id) activities = session.query(Activity).filter(Activity.city_id == city_id).all() if request.method == 'POST': session.delete(city) session.commit() flash("The location has been successfully deleted.") return redirect(url_for('.list_cities')) else: if 'user_id' not in login_session or \ city.user_id != login_session['user_id']: return redirect(url_for('.show_city', city_id=city.id)) else: return render_template('delete_city.html', city=city)