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_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 activity_json(city_id, activity_id): """ Provides: Functionality to get an activity and transform it into JSON Args: city_id: the unique identifier for the city. activity_id: the unique identifier for the activity in that city Returns: JSON formatted activity information """ activity = get_activity(city_id, activity_id) return jsonify(activity=activity.serialize)
def activity_xml(city_id, activity_id): """ Provides: Functionality to get an activity and transform it into XML Args: city_id: the unique identifier for the city. activity_id: the unique identifier for the activity in that city Returns: XML formatted activity information """ activity = get_activity(city_id, activity_id) activity_in_xml = dicttoxml(activity.serialize) activity_in_pretty_xml = parseString(activity_in_xml).toprettyxml() return Response(activity_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)