def show_ads(template_name, user_id=None): """ get ads_data data structure to be displayed in the list of ads. Filtering parameters are extracted from request. :param user_id: if None - ignored. Otherwise, user id will be used to filter in only user-specific ads :param template_name: template to use to render an ad :return: rendered list of ads """ ads_html = list() search_filtering_parameters = get_search_filtering_parameters_from_request( request) if user_id: search_filtering_parameters["user_id"] = user_id ads, total_number_of_ads, min_ad_idx_displayed, max_ad_idx_displayed = \ database.get_ads_to_display(**search_filtering_parameters) if total_number_of_ads > 0: for ad in ads: ads_html.append( render_template(template_name, ad=database.ad_to_dict(ad))) ads_data = dict() ads_data["ads_html"] = ads_html ads_data["total_number_of_ads"] = str(total_number_of_ads) ads_data["min_ad_idx_displayed"] = str(min_ad_idx_displayed) ads_data["max_ad_idx_displayed"] = str(max_ad_idx_displayed) return jsonify(ads_data)
def test_jsonify_ads(): city_id = 2 ads = database.get_ads_by_city(city_id) print ads list_of_ads = [database.ad_to_dict(ad) for ad in ads] print list_of_ads print jsonify(list_of_ads)
def edit_ad(ad_id): """ Updates specific ad with user edited information. :param ad_id: id of the ad :return: either current ad info (on GET) or updated ad (on POST) """ selected_ad = database.get_ad_by_id(int(ad_id)) if not user_is_authorized_to_update_item(selected_ad.user_id): flash("You are not authorized to update this page") return redirect(url_for("index")) edit_ad_form = FlaskForm() if request.method == "POST" and edit_ad_form.validate_on_submit(): # using FlaskForm only for csrf protection in this case, rest is custom-built update_ad_from_form_info(selected_ad, request.form) flash("Ad was updated") categories_with_sub_categories = database.get_categories_with_subcategories( ) cities = database.get_cities() categories_json = json.dumps(categories_with_sub_categories) ad_dict = database.ad_to_dict(selected_ad) selected_sub_categories = categories_with_sub_categories[ ad_dict["category"]]["value"] return render_template("edit_my_ad.html", ad=ad_dict, categories_json=categories_json, categories=categories_with_sub_categories, selected_sub_categories=selected_sub_categories, cities=cities, edit_ad_form=edit_ad_form, page_info=get_page_info())
def delete_ad(ad_id): """ :param ad_id: ad to be deleted :return: on get add page; on post deletes ad and redirects to my ads page """ selected_ad = database.get_ad_by_id(int(ad_id)) if not user_is_authorized_to_update_item(selected_ad.user_id): flash("You are not authorized to update this page") return redirect(url_for("login")) # Use FlaskForm for csrf protection delete_ad_form = FlaskForm() if request.method == "POST" and delete_ad_form.validate_on_submit(): ad_deleted_msg = "Your ad #" + str(selected_ad.id) + " was deleted" database.delete_ad(selected_ad) flash(ad_deleted_msg) return redirect(url_for("my_ads")) ad_dict = database.ad_to_dict(selected_ad) return render_template("delete_ad.html", ad=ad_dict, page_info=get_page_info(), delete_ad_form=delete_ad_form)
def get_ad_json(ad_id): """ Returns JSON for a single ad :param ad_id: itn id of the ad :return: JSON for the requested ad """ ad = database.get_ad_by_id(ad_id) if ad: return jsonify(database.ad_to_dict(ad, serialize=True)) return jsonify({})
def ad_page(ad_id): """ :param ad_id: id of the ad :return: rendered view for an ad """ selected_ad = database.ad_to_dict(database.get_ad_by_id(ad_id)) return render_template("ad.html", ad=selected_ad, page_info=get_page_info())
def get_city_ads_json(city_id): """ Returns JSON for all ads in a city (as per city_id) :param city_id: int id of the city :return: JSON for all ads in a city """ ads = database.get_ads_by_city(city_id) list_of_ads_dictionaries = [ database.ad_to_dict(ad, serialize=True) for ad in ads ] return jsonify(list_of_ads_dictionaries)