def store_view(): error_info = {} status_code = 200 try: categories_results = api.get_categories() except ApiError as api_error: categories_results = [] status_code, error_info = _handle_errors(api_error) categories = logic.get_categories(categories_results) try: featured_snaps_results = api.get_searched_snaps( snap_searched="", category="featured", size=24, page=1 ) except ApiError: featured_snaps_results = [] featured_snaps = logic.get_searched_snaps(featured_snaps_results) return ( flask.render_template( "store/store.html", categories=categories, featured_snaps=featured_snaps, error_info=error_info, ), status_code, )
def store_view(): error_info = {} status_code = 200 try: categories_results = api.get_categories() except ApiError as api_error: categories_results = [] status_code, error_info = _handle_errors(api_error) categories = logic.get_categories(categories_results) try: featured_snaps_results = api.get_searched_snaps( snap_searched="", category="featured", size=24, page=1) except ApiError: featured_snaps_results = [] featured_snaps = logic.get_searched_snaps(featured_snaps_results) return ( flask.render_template( "store/store.html", categories=categories, featured_snaps=featured_snaps, error_info=error_info, ), status_code, )
def index(): query = request.args.get("q", default=None, type=str) sort = request.args.get("sort", default="sort-asc", type=str) fields = [ "categories", "summary", "media", "name", "publisher", "revision", "channel", ] if query: results = app.store_api.find(query=query, fields=fields).get("results") else: results = app.store_api.find(fields=fields).get("results", []) charms = [] categories = [] for i, item in enumerate(results): results[i]["store_front"] = {} results[i]["store_front"]["icons"] = logic.get_icons(results[i]) results[i]["store_front"]["last_release"] = logic.convert_date( results[i]["default-release"]["channel"]["released-at"]) if results[i]["result"]["categories"]: results[i]["store_front"]["categories"] = logic.get_categories( results[i]["result"]["categories"]) else: results[i]["store_front"]["categories"] = [{ "name": "Other", "slug": "other" }] if (results[i]["type"] == "charm" and results[i]["result"]["publisher"]["display-name"]): for category in results[i]["store_front"]["categories"]: if category not in categories: categories.append(category) charms.append(results[i]) sorted_categories = sorted(categories, key=lambda k: k["name"]) sort_order = True if sort == "name-desc" else False charms = sorted(charms, key=lambda c: c["name"], reverse=sort_order) context = { "categories": sorted_categories, "sort": sort, "q": query, "results": charms, } return render_template("store.html", **context)
def test_get_categories(self): categories = { "_embedded": { "clickindex:sections": [ {"name": "featured"}, {"name": "test"}, {"name": "development"}, ] } } category_list = logic.get_categories(categories) self.assertTrue( {"name": "Development", "slug": "development"} in category_list ) self.assertTrue({"name": "Games", "slug": "games"} in category_list) self.assertTrue({"name": "Test", "slug": "test"} in category_list)
def store_view(): error_info = {} status_code = 200 try: categories_results = api.get_categories() except ApiError as api_error: categories_results = [] status_code, error_info = _handle_errors(api_error) categories = logic.get_categories(categories_results) try: featured_snaps_results = api.get_searched_snaps( snap_searched="", category="featured", size=10, page=1) except ApiError: featured_snaps_results = [] featured_snaps = logic.get_searched_snaps(featured_snaps_results) # if the first snap (banner snap) doesn't have an icon, remove the last # snap from the list to avoid a hanging snap (grid of 9) if len(featured_snaps) == 10 and featured_snaps[0]["icon_url"] == "": featured_snaps = featured_snaps[:-1] featured_banners_enabled = flask.current_app.config.get( "FEATURED_BANNERS_ENABLED") if featured_banners_enabled: for index in range(len(featured_snaps)): featured_snaps[index] = logic.get_snap_banner_url( featured_snaps[index]) livestream = snapcraft_logic.get_livestreams() return ( flask.render_template( "store/store.html", categories=categories, has_featured=True, featured_snaps=featured_snaps, error_info=error_info, livestream=livestream, ), status_code, )
def store_view(): error_info = {} status_code = 200 try: categories_results = api.get_categories() except StoreApiError: categories_results = [] categories = logic.get_categories(categories_results) try: featured_snaps_results = api.get_featured_items() except (StoreApiError, ApiError) as api_error: status_code, error_info = _handle_error(api_error) return flask.abort(status_code) featured_snaps = logic.get_searched_snaps(featured_snaps_results) if not featured_snaps: return flask.abort(503) # if the first snap (banner snap) doesn't have an icon, remove the last # snap from the list to avoid a hanging snap (grid of 9) if len(featured_snaps) == 10 and featured_snaps[0]["icon_url"] == "": featured_snaps = featured_snaps[:-1] for index in range(len(featured_snaps)): featured_snaps[index] = logic.get_snap_banner_url( featured_snaps[index] ) livestream = snapcraft_logic.get_livestreams() return ( flask.render_template( "store/store.html", categories=categories, has_featured=True, featured_snaps=featured_snaps, error_info=error_info, livestream=livestream, ), status_code, )
def store_view(): error_info = {} status_code = 200 try: categories_results = api.get_categories() except ApiError as api_error: categories_results = [] status_code, error_info = _handle_errors(api_error) categories = logic.get_categories(categories_results) return ( flask.render_template( "store/store.html", categories=categories, error_info=error_info, ), status_code, )
def test_get_categories(self): categories = { "_embedded": { "clickindex:sections": [ { "name": "featured" }, { "name": "test" }, { "name": "developers" }, ] } } category_list = logic.get_categories(categories) self.assertEqual( category_list, [ { "name": "Developers", "slug": "developers" }, { "name": "Games", "slug": "games" }, { "name": "Social networking", "slug": "social-networking" }, { "name": "Test", "slug": "test" }, ], )
def store_view(): error_info = {} status_code = 200 try: categories_results = api.get_categories() except ApiError as api_error: categories_results = [] status_code, error_info = _handle_errors(api_error) categories = logic.get_categories(categories_results) try: featured_snaps_results = api.get_featured_snaps() except ApiError as api_error: featured_snaps_results = [] status_code, error_info = _handle_errors(api_error) featured_snaps = logic.get_searched_snaps(featured_snaps_results) return flask.render_template('store/store.html', featured_snaps=featured_snaps, categories=categories, error_info=error_info), status_code
def search_snap(): status_code = 200 snap_searched = flask.request.args.get("q", default="", type=str) snap_category = flask.request.args.get("category", default="", type=str) if snap_category: snap_category_display = snap_category.capitalize().replace( "-", " ") else: snap_category_display = None if not snap_searched and not snap_category: return flask.redirect(flask.url_for(".homepage")) size = flask.request.args.get("limit", default=24, type=int) offset = flask.request.args.get("offset", default=0, type=int) try: page = floor(offset / size) + 1 except ZeroDivisionError: size = 10 page = floor(offset / size) + 1 error_info = {} categories_results = [] searched_results = [] try: categories_results = api.get_categories() except ApiError as api_error: status_code, error_info = _handle_errors(api_error) categories = logic.get_categories(categories_results) try: searched_results = api.get_searched_snaps( quote_plus(snap_searched), category=snap_category, size=size, page=page, ) except ApiError as api_error: status_code, error_info = _handle_errors(api_error) if "total" in searched_results: total_results_count = searched_results["total"] else: total_results_count = None snaps_results = logic.get_searched_snaps(searched_results) links = logic.get_pages_details( flask.request.base_url, (searched_results["_links"] if "_links" in searched_results else []), ) context = { "query": snap_searched, "category": snap_category, "category_display": snap_category_display, "categories": categories, "snaps": snaps_results, "total": total_results_count, "links": links, "error_info": error_info, } return ( flask.render_template("store/search.html", **context), status_code, )
def search_snap(): status_code = 200 snap_searched = flask.request.args.get("q", default="", type=str) snap_category = flask.request.args.get( "category", default="", type=str ) if snap_category: snap_category_display = snap_category.capitalize().replace( "-", " " ) else: snap_category_display = None if not snap_searched and not snap_category: return flask.redirect(flask.url_for(".homepage")) size = flask.request.args.get("limit", default=24, type=int) offset = flask.request.args.get("offset", default=0, type=int) try: page = floor(offset / size) + 1 except ZeroDivisionError: size = 10 page = floor(offset / size) + 1 error_info = {} categories_results = [] searched_results = [] try: categories_results = api.get_categories() except ApiError as api_error: status_code, error_info = _handle_errors(api_error) categories = logic.get_categories(categories_results) try: searched_results = api.get_searched_snaps( quote_plus(snap_searched), category=snap_category, size=size, page=page, ) except ApiError as api_error: status_code, error_info = _handle_errors(api_error) total_results_count = searched_results["total"] snaps_results = logic.get_searched_snaps(searched_results) links = logic.get_pages_details( flask.request.base_url, ( searched_results["_links"] if "_links" in searched_results else [] ), ) context = { "query": snap_searched, "category": snap_category, "category_display": snap_category_display, "categories": categories, "snaps": snaps_results, "total": total_results_count, "links": links, "error_info": error_info, } return ( flask.render_template("store/search.html", **context), status_code, )
def post_listing_snap(snap_name): changes = None changed_fields = flask.request.form.get("changes") if changed_fields: changes = loads(changed_fields) if changes: snap_id = flask.request.form.get("snap_id") error_list = [] if "images" in changes: # Add existing screenshots try: current_screenshots = api.snap_screenshots( snap_id, flask.session ) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort( 404, "No snap named {}".format(snap_name) ) else: return _handle_error_list(api_response_error_list.errors) except ApiError as api_error: return _handle_errors(api_error) images_json, images_files = logic.build_changed_images( changes["images"], current_screenshots, flask.request.files.get("icon"), flask.request.files.getlist("screenshots"), ) try: api.snap_screenshots( snap_id, flask.session, images_json, images_files ) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort( 404, "No snap named {}".format(snap_name) ) else: error_list = error_list + api_response_error_list.errors except ApiError as api_error: return _handle_errors(api_error) body_json = logic.filter_changes_data(changes) if body_json: if "description" in body_json: body_json["description"] = logic.remove_invalid_characters( body_json["description"] ) try: api.snap_metadata(snap_id, flask.session, body_json) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort( 404, "No snap named {}".format(snap_name) ) else: error_list = error_list + api_response_error_list.errors except ApiError as api_error: return _handle_errors(api_error) if error_list: try: snap_details = api.get_snap_info(snap_name, flask.session) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort( 404, "No snap named {}".format(snap_name) ) else: error_list = error_list + api_response_error_list.errors except ApiError as api_error: return _handle_errors(api_error) field_errors, other_errors = logic.invalid_field_errors(error_list) details_metrics_enabled = snap_details["public_metrics_enabled"] details_blacklist = snap_details["public_metrics_blacklist"] is_on_stable = logic.is_snap_on_stable( snap_details["channel_maps_list"] ) # Filter icon & screenshot urls from the media set. icon_urls = [ m["url"] for m in snap_details["media"] if m["type"] == "icon" ] screenshot_urls = [ m["url"] for m in snap_details["media"] if m["type"] == "screenshot" ] licenses = [] for license in get_licenses(): licenses.append( {"key": license["licenseId"], "name": license["name"]} ) license = snap_details["license"] license_type = "custom" if ( " AND " not in license.upper() and " WITH " not in license.upper() ): license_type = "simple" try: categories_results = store_api.get_categories() except ApiError: categories_results = [] categories = get_categories(categories_results) snap_categories = logic.replace_reserved_categories_key( snap_details["categories"] ) snap_categories = logic.filter_categories(snap_categories) context = { # read-only values from details API "snap_id": snap_details["snap_id"], "snap_name": snap_details["snap_name"], "snap_categories": snap_categories, "icon_url": icon_urls[0] if icon_urls else None, "publisher_name": snap_details["publisher"]["display-name"], "username": snap_details["publisher"]["username"], "screenshot_urls": screenshot_urls, "display_title": snap_details["title"], # values posted by user "snap_title": ( changes["title"] if "title" in changes else snap_details["title"] or "" ), "summary": ( changes["summary"] if "summary" in changes else snap_details["summary"] or "" ), "description": ( changes["description"] if "description" in changes else snap_details["description"] or "" ), "contact": ( changes["contact"] if "contact" in changes else snap_details["contact"] or "" ), "private": snap_details["private"], "website": ( changes["website"] if "website" in changes else snap_details["website"] or "" ), "public_metrics_enabled": details_metrics_enabled, "video_urls": ( [changes["video_urls"]] if "video_urls" in changes else snap_details["video_urls"] ), "public_metrics_blacklist": details_blacklist, "license": license, "license_type": license_type, "licenses": licenses, "is_on_stable": is_on_stable, "categories": categories, # errors "error_list": error_list, "field_errors": field_errors, "other_errors": other_errors, } return flask.render_template("publisher/listing.html", **context) flask.flash("Changes applied successfully.", "positive") else: flask.flash("No changes to save.", "information") return flask.redirect( flask.url_for(".get_listing_snap", snap_name=snap_name) )
def get_listing_snap(snap_name): try: snap_details = api.get_snap_info(snap_name, flask.session) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort(404, "No snap named {}".format(snap_name)) else: return _handle_error_list(api_response_error_list.errors) except ApiError as api_error: return _handle_errors(api_error) details_metrics_enabled = snap_details["public_metrics_enabled"] details_blacklist = snap_details["public_metrics_blacklist"] is_on_stable = logic.is_snap_on_stable(snap_details["channel_maps_list"]) # Filter icon & screenshot urls from the media set. icon_urls = [ m["url"] for m in snap_details["media"] if m["type"] == "icon" ] screenshot_urls = [ m["url"] for m in snap_details["media"] if m["type"] == "screenshot" ] licenses = [] for license in get_licenses(): licenses.append({"key": license["licenseId"], "name": license["name"]}) license = snap_details["license"] license_type = "custom" if " AND " not in license.upper() and " WITH " not in license.upper(): license_type = "simple" referrer = None if flask.request.args.get("from"): referrer = flask.request.args.get("from") try: categories_results = store_api.get_categories() except ApiError: categories_results = [] categories = sorted( get_categories(categories_results), key=lambda category: category["slug"], ) snap_categories = logic.replace_reserved_categories_key( snap_details["categories"] ) snap_categories = logic.filter_categories(snap_categories) context = { "snap_id": snap_details["snap_id"], "snap_name": snap_details["snap_name"], "snap_title": snap_details["title"], "snap_categories": snap_categories, "summary": snap_details["summary"], "description": snap_details["description"], "icon_url": icon_urls[0] if icon_urls else None, "publisher_name": snap_details["publisher"]["display-name"], "username": snap_details["publisher"]["username"], "screenshot_urls": screenshot_urls, "contact": snap_details["contact"], "private": snap_details["private"], "website": snap_details["website"] or "", "public_metrics_enabled": details_metrics_enabled, "public_metrics_blacklist": details_blacklist, "license": license, "license_type": license_type, "licenses": licenses, "video_urls": snap_details["video_urls"], "is_on_stable": is_on_stable, "from": referrer, "categories": categories, } return flask.render_template("publisher/listing.html", **context)
def get_listing_snap(snap_name): try: snap_details = api.get_snap_info(snap_name, flask.session) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort(404, "No snap named {}".format(snap_name)) else: return _handle_error_list(api_response_error_list.errors) except ApiError as api_error: return _handle_error(api_error) details_metrics_enabled = snap_details["public_metrics_enabled"] details_blacklist = snap_details["public_metrics_blacklist"] is_on_stable = logic.is_snap_on_stable(snap_details["channel_maps_list"]) # Filter icon & screenshot urls from the media set. icon_urls, screenshot_urls, banner_urls = logic.categorise_media( snap_details["media"]) licenses = [] for license in get_licenses(): licenses.append({"key": license["licenseId"], "name": license["name"]}) license = snap_details["license"] license_type = "custom" if " AND " not in license.upper() and " WITH " not in license.upper(): license_type = "simple" referrer = None if flask.request.args.get("from"): referrer = flask.request.args.get("from") try: categories_results = store_api.get_categories() except StoreApiError: categories_results = [] categories = sorted( get_categories(categories_results), key=lambda category: category["slug"], ) snap_categories = logic.replace_reserved_categories_key( snap_details["categories"]) snap_categories = logic.filter_categories(snap_categories) snap_categories["categories"] = [ category["name"] for category in snap_categories["categories"] ] filename = f"publisher/content/listing_tour.yaml" tour_steps = helpers.get_yaml(filename, typ="rt") context = { "snap_id": snap_details["snap_id"], "snap_name": snap_details["snap_name"], "snap_title": snap_details["title"], "snap_categories": snap_categories, "summary": snap_details["summary"], "description": snap_details["description"], "icon_url": icon_urls[0] if icon_urls else None, "publisher_name": snap_details["publisher"]["display-name"], "username": snap_details["publisher"]["username"], "screenshot_urls": screenshot_urls, "banner_urls": banner_urls, "contact": snap_details["contact"], "private": snap_details["private"], "website": snap_details["website"] or "", "public_metrics_enabled": details_metrics_enabled, "public_metrics_blacklist": details_blacklist, "license": license, "license_type": license_type, "licenses": licenses, "video_urls": snap_details["video_urls"], "is_on_stable": is_on_stable, "from": referrer, "categories": categories, "tour_steps": tour_steps, } return flask.render_template("publisher/listing.html", **context)
def post_listing_snap(snap_name): changes = None changed_fields = flask.request.form.get("changes") if changed_fields: changes = loads(changed_fields) if changes: snap_id = flask.request.form.get("snap_id") error_list = [] if "images" in changes: # Add existing screenshots try: current_screenshots = api.snap_screenshots( snap_id, flask.session) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort(404, "No snap named {}".format(snap_name)) else: return _handle_error_list(api_response_error_list.errors) except ApiError as api_error: return _handle_error(api_error) images_json, images_files = logic.build_changed_images( changes["images"], current_screenshots, flask.request.files.get("icon"), flask.request.files.getlist("screenshots"), flask.request.files.get("banner-image"), ) try: api.snap_screenshots(snap_id, flask.session, images_json, images_files) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort(404, "No snap named {}".format(snap_name)) else: error_list = error_list + api_response_error_list.errors except ApiError as api_error: return _handle_error(api_error) body_json = logic.filter_changes_data(changes) if body_json: if "description" in body_json: body_json["description"] = logic.remove_invalid_characters( body_json["description"]) try: api.snap_metadata(snap_id, flask.session, body_json) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort(404, "No snap named {}".format(snap_name)) else: error_list = error_list + api_response_error_list.errors except ApiError as api_error: return _handle_error(api_error) if error_list: try: snap_details = api.get_snap_info(snap_name, flask.session) except ApiResponseErrorList as api_response_error_list: if api_response_error_list.status_code == 404: return flask.abort(404, "No snap named {}".format(snap_name)) else: error_list = error_list + api_response_error_list.errors except ApiError as api_error: return _handle_error(api_error) field_errors, other_errors = logic.invalid_field_errors(error_list) details_metrics_enabled = snap_details["public_metrics_enabled"] details_blacklist = snap_details["public_metrics_blacklist"] is_on_stable = logic.is_snap_on_stable( snap_details["channel_maps_list"]) # Filter icon & screenshot urls from the media set. icon_urls, screenshot_urls, banner_urls = logic.categorise_media( snap_details["media"]) licenses = [] for license in get_licenses(): licenses.append({ "key": license["licenseId"], "name": license["name"] }) license = snap_details["license"] license_type = "custom" if (" AND " not in license.upper() and " WITH " not in license.upper()): license_type = "simple" try: categories_results = store_api.get_categories() except StoreApiError: categories_results = [] categories = get_categories(categories_results) snap_categories = logic.replace_reserved_categories_key( snap_details["categories"]) snap_categories = logic.filter_categories(snap_categories) filename = f"publisher/content/listing_tour.yaml" tour_steps = helpers.get_yaml(filename, typ="rt") context = { # read-only values from details API "snap_id": snap_details["snap_id"], "snap_name": snap_details["snap_name"], "snap_categories": snap_categories, "icon_url": icon_urls[0] if icon_urls else None, "publisher_name": snap_details["publisher"]["display-name"], "username": snap_details["publisher"]["username"], "screenshot_urls": screenshot_urls, "banner_urls": banner_urls, "display_title": snap_details["title"], # values posted by user "snap_title": (changes["title"] if "title" in changes else snap_details["title"] or ""), "summary": (changes["summary"] if "summary" in changes else snap_details["summary"] or ""), "description": (changes["description"] if "description" in changes else snap_details["description"] or ""), "contact": (changes["contact"] if "contact" in changes else snap_details["contact"] or ""), "private": snap_details["private"], "website": (changes["website"] if "website" in changes else snap_details["website"] or ""), "public_metrics_enabled": details_metrics_enabled, "video_urls": ([changes["video_urls"]] if "video_urls" in changes else snap_details["video_urls"]), "public_metrics_blacklist": details_blacklist, "license": license, "license_type": license_type, "licenses": licenses, "is_on_stable": is_on_stable, "categories": categories, # errors "error_list": error_list, "field_errors": field_errors, "other_errors": other_errors, "tour_steps": tour_steps, } return flask.render_template("publisher/listing.html", **context) flask.flash("Changes applied successfully.", "positive") else: flask.flash("No changes to save.", "information") return flask.redirect( flask.url_for(".get_listing_snap", snap_name=snap_name))
def search_snap(): status_code = 200 snap_searched = flask.request.args.get('q', default='', type=str) snap_category = flask.request.args.get('category', default='', type=str) if snap_category: snap_category_display = snap_category.capitalize().replace( '-', ' ') else: snap_category_display = None if not snap_searched and not snap_category: return flask.redirect(flask.url_for('.homepage')) size = flask.request.args.get('limit', default=25, type=int) offset = flask.request.args.get('offset', default=0, type=int) try: page = floor(offset / size) + 1 except ZeroDivisionError: size = 10 page = floor(offset / size) + 1 error_info = {} featured_snaps = [] categories_results = [] searched_results = [] try: categories_results = api.get_categories() except ApiError as api_error: status_code, error_info = _handle_errors(api_error) categories = logic.get_categories(categories_results) try: searched_results = api.get_searched_snaps( quote_plus(snap_searched), category=snap_category, size=size, page=page) except ApiError as api_error: status_code, error_info = _handle_errors(api_error) snaps_results = logic.get_searched_snaps(searched_results) links = logic.get_pages_details(flask.request.base_url, (searched_results['_links'] if '_links' in searched_results else [])) if not snaps_results: featured_snaps_results = [] try: featured_snaps_results = api.get_featured_snaps() except ApiError as api_error: status_code, error_info = _handle_errors(api_error) featured_snaps = logic.get_searched_snaps(featured_snaps_results) context = { "query": snap_searched, "category": snap_category, "category_display": snap_category_display, "categories": categories, "snaps": snaps_results, "links": links, "featured_snaps": featured_snaps, "error_info": error_info } return flask.render_template('store/search.html', **context), status_code