Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
def list_cities():
    """Renders the main cities page, with all the cities"""
    cities = session.query(City).all()
    if 'user_id' not in login_session:
        return render_template('list_cities_public.html', cities=cities)
    else:
        return render_template('list_cities.html', cities=cities)
Esempio n. 4
0
def activities_json():
    """
    Provides:
        Functionality to get all activities and transform it into JSON

    Returns:
        JSON formatted list of activities for a city
    """
    activities = session.query(Activity).all()
    return jsonify(activities=[i.serialize for i in activities])
Esempio n. 5
0
def activities_json():
    """
    Provides:
        Functionality to get all activities and transform it into JSON

    Returns:
        JSON formatted list of activities for a city
    """
    activities = session.query(Activity).all()
    return jsonify(activities=[i.serialize for i in activities])
Esempio n. 6
0
def activities_xml():
    """
    Provides:
        Functionality to get all activities and transform it into XML

    Returns:
        XML formatted list of activities for a city
    """
    activities = session.query(Activity).all()
    activities_in_xml = dicttoxml(activity.serialize for activity in activities)
    activities_in_pretty_xml = parseString(activities_in_xml).toprettyxml()
    return Response(activities_in_pretty_xml, mimetype='text/xml')
Esempio n. 7
0
def get_user_info(user_id):
    """
    Provides:
        Functionality retrieve the user's details from the database

    Args:
        user_id: the unique identifier for the user

    Returns:
        user: an object containing the user's information
    """
    user = session.query(User).filter(User.id == user_id).one()
    return user
Esempio n. 8
0
def activities_xml():
    """
    Provides:
        Functionality to get all activities and transform it into XML

    Returns:
        XML formatted list of activities for a city
    """
    activities = session.query(Activity).all()
    activities_in_xml = dicttoxml(activity.serialize
                                  for activity in activities)
    activities_in_pretty_xml = parseString(activities_in_xml).toprettyxml()
    return Response(activities_in_pretty_xml, mimetype='text/xml')
Esempio n. 9
0
def get_user_info(user_id):
    """
    Provides:
        Functionality retrieve the user's details from the database

    Args:
        user_id: the unique identifier for the user

    Returns:
        user: an object containing the user's information
    """
    user = session.query(User).filter(User.id == user_id).one()
    return user
Esempio n. 10
0
def city_activities_json(city_id):
    """
    Provides:
        Functionality to get a activities for a city and transform it into JSON

    Args:
        city_id: the unique identifier for the city.

    Returns:
        JSON formatted list of activities for a city
    """
    activities = \
        session.query(Activity).filter(Activity.city_id == city_id).all()
    return jsonify(activities=[i.serialize for i in activities])
Esempio n. 11
0
def city_activities_json(city_id):
    """
    Provides:
        Functionality to get a activities for a city and transform it into JSON

    Args:
        city_id: the unique identifier for the city.

    Returns:
        JSON formatted list of activities for a city
    """
    activities = \
        session.query(Activity).filter(Activity.city_id == city_id).all()
    return jsonify(activities=[i.serialize for i in activities])
Esempio n. 12
0
def get_city(city_id):
    """
    Provides:
        functionality to get the city from the DB, if it exists
        If the city doesn't exist, we abort with a 404

    Args:
        city_id: the identifier for the city we're trying to find

    Returns:
        an object representing the city
    """
    try:
        city = session.query(City).filter(City.id == city_id).one()
    except NoResultFound, error:
        abort(404, error)
Esempio n. 13
0
def get_city(city_id):
    """
    Provides:
        functionality to get the city from the DB, if it exists
        If the city doesn't exist, we abort with a 404

    Args:
        city_id: the identifier for the city we're trying to find

    Returns:
        an object representing the city
    """
    try:
        city = session.query(City).filter(City.id == city_id).one()
    except NoResultFound, error:
        abort(404, error)
Esempio n. 14
0
def get_creator(creator_id):
    """
    Provides:
        Functionality to get the creator from the DB, if he/she exists
        If the creator doesn't exist, we abort with a 404

    Args:
        creator_id: the identifier for the user who created the item

    Returns:
        an object representing the creator of the item
    """
    try:
        creator = session.query(User).filter(User.id == creator_id).one()
    except NoResultFound, error:
        abort(404, error)
Esempio n. 15
0
def city_activities_xml(city_id):
    """
    Provides:
        Functionality to get a activities for a city and transform it into XML

    Args:
        city_id: the unique identifier for the city.

    Returns:
        XML formatted list of activities for a city
    """
    activities = \
        session.query(Activity).filter(Activity.city_id == city_id).all()
    activities_in_xml = dicttoxml(activity.serialize for activity in activities)
    activities_in_pretty_xml = parseString(activities_in_xml).toprettyxml()
    return Response(activities_in_pretty_xml, mimetype='text/xml')
Esempio n. 16
0
def get_creator(creator_id):
    """
    Provides:
        Functionality to get the creator from the DB, if he/she exists
        If the creator doesn't exist, we abort with a 404

    Args:
        creator_id: the identifier for the user who created the item

    Returns:
        an object representing the creator of the item
    """
    try:
        creator = session.query(User).filter(User.id == creator_id).one()
    except NoResultFound, error:
        abort(404, error)
Esempio n. 17
0
def create_user(login_session):
    """
    Provides:
        Functionality to create a new user in the database

    Args:
        login_session: the dictionary storing login details
    """
    new_user = User(name=login_session['username'],
                    email=login_session['email'],
                    picture_url=login_session['picture'])
    session.add(new_user)
    session.commit()
    user = session.query(User).filter(
        User.email == login_session['email']).one()
    return user.id
Esempio n. 18
0
def city_activities_xml(city_id):
    """
    Provides:
        Functionality to get a activities for a city and transform it into XML

    Args:
        city_id: the unique identifier for the city.

    Returns:
        XML formatted list of activities for a city
    """
    activities = \
        session.query(Activity).filter(Activity.city_id == city_id).all()
    activities_in_xml = dicttoxml(activity.serialize
                                  for activity in activities)
    activities_in_pretty_xml = parseString(activities_in_xml).toprettyxml()
    return Response(activities_in_pretty_xml, mimetype='text/xml')
Esempio n. 19
0
def get_user_id(email):
    """
    Provides:
        Functionality to see if a user has already been registered

    Args:
        email: the email address of the user, from their Facebook
            or Google account

    Returns:
        user.id or none: the unique identifier for the user,
            if the user has registered
    """
    try:
        user = session.query(User).filter(User.email == email).one()
        return user.id
    except NoResultFound:
        return None
Esempio n. 20
0
def get_user_id(email):
    """
    Provides:
        Functionality to see if a user has already been registered

    Args:
        email: the email address of the user, from their Facebook
            or Google account

    Returns:
        user.id or none: the unique identifier for the user,
            if the user has registered
    """
    try:
        user = session.query(User).filter(User.email == email).one()
        return user.id
    except NoResultFound:
        return None
Esempio n. 21
0
def create_user(login_session):
    """
    Provides:
        Functionality to create a new user in the database

    Args:
        login_session: the dictionary storing login details
    """
    new_user = User(
        name=login_session['username'],
        email=login_session['email'],
        picture_url=login_session['picture']
    )
    session.add(new_user)
    session.commit()
    user = session.query(User).filter(
        User.email == login_session['email']
    ).one()
    return user.id
Esempio n. 22
0
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)
Esempio n. 23
0
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)
Esempio n. 24
0
def cities_json():
    """Returns list of cities in JSON format."""
    cities = session.query(City).all()
    return jsonify(cities=[city.serialize for city in cities])
Esempio n. 25
0
def cities_json():
    """Returns list of cities in JSON format."""
    cities = session.query(City).all()
    return jsonify(cities=[city.serialize for city in cities])
Esempio n. 26
0
def cities_xml():
    """Returns list of cities in XML format."""
    cities = session.query(City).all()
    cities_in_xml = dicttoxml(city.serialize for city in cities)
    cities_in_pretty_xml = parseString(cities_in_xml).toprettyxml()
    return Response(cities_in_pretty_xml, mimetype='text/xml')
Esempio n. 27
0
def cities_xml():
    """Returns list of cities in XML format."""
    cities = session.query(City).all()
    cities_in_xml = dicttoxml(city.serialize for city in cities)
    cities_in_pretty_xml = parseString(cities_in_xml).toprettyxml()
    return Response(cities_in_pretty_xml, mimetype='text/xml')