def user_products_modify(id): action = request.args.get("action", "refresh") user = get_user_for_response(id, request) # logger.debug(u"got user {user}".format( # user=user)) source = request.args.get("source", "webapp") if request.method == "POST" and action == "deduplicate": deleted_tiids = remove_duplicates_from_profile(user.id) resp = {"deleted_tiids": deleted_tiids} local_sleep(30) elif request.method == "POST" and (action == "refresh"): tiids_being_refreshed = user.refresh_products(source) resp = {"products": tiids_being_refreshed} else: # Actions that require authentication abort_if_user_not_logged_in(user) if request.method == "PATCH": local_sleep(2) added_products = user.add_products(request.json) resp = {"products": added_products} else: abort(405) # method not supported. We shouldn't get here. return json_resp_from_thing(resp)
def product_biblio_modify(tiid): # This should actually be like /profile/:id/product/:tiid/biblio # and it should return the newly-modified product, instead of the # part-product it gets from core now. try: if not current_user_owns_tiid(tiid): abort_json(401, "You have to own this product to modify it.") except AttributeError: abort_json(405, "You musts be logged in to modify products.") query = u"{core_api_root}/v1/product/{tiid}/biblio?api_admin_key={api_admin_key}".format( core_api_root=os.getenv("API_ROOT"), tiid=tiid, api_admin_key=os.getenv("API_ADMIN_KEY") ) data_dict = json.loads(request.data) r = requests.patch( query, data=json.dumps(data_dict), headers={'Content-type': 'application/json', 'Accept': 'application/json'} ) local_sleep(1) return json_resp_from_thing(r.json())
def get_current_user(): local_sleep(1) try: user_info = g.user.dict_about() except AttributeError: # anon user has no as_dict() user_info = None return json_resp_from_thing({"user": user_info})
def products_biblio_modify_multi(comma_separated_tiids): resp = [] for tiid in comma_separated_tiids.split(","): current_user_must_own_tiid(tiid) resp.append(patch_biblio(tiid, request.json)) local_sleep(1) return json_resp_from_thing({"msg": resp}) # angular needs obj not array.
def product_from_tiid(url_slug, tiid): local_sleep(1) product = get_product(tiid) if not product: abort_json(404, "This product does not exist.") product_dict = product.to_dict() return json_resp_from_thing(product_dict)
def refresh_status(profile_id): local_sleep(0.5) # client to webapp plus one trip to database id_type = request.args.get("id_type", "url_slug") # url_slug is default profile_bare_products = get_profile_from_id(profile_id, id_type, include_product_relationships=False) if profile_bare_products: status = profile_bare_products.get_refresh_status() else: abort_json(404, "This profile does not exist.") return json_resp_from_thing(status)
def user_about(profile_id): user = get_user_for_response(profile_id, request) dict_about = user.dict_about() # logger.debug(u"got the user dict out: {user}".format( # user=dict_about)) local_sleep(1) return json_resp_from_thing({"about": dict_about})
def product_biblio_modify(tiid): # This should actually be like /profile/:id/product/:tiid/biblio # and it should return the newly-modified product, instead of the # part-product it gets from core now. current_user_must_own_tiid(tiid) resp = patch_biblio(tiid, request.json) local_sleep(1) return json_resp_from_thing({"msg": resp})
def profile_products_modify(id): action = request.args.get("action", "refresh") source = request.args.get("source", "webapp") profile = get_user_for_response(id, request) if request.method == "POST" and action == "after-refresh-cleanup": logger.info(u"deduplicating for {url_slug}".format( url_slug=profile.url_slug)) deleted_tiids = profile.remove_duplicates() logger.info(u"parse_and_save_tweets for {url_slug}".format( url_slug=profile.url_slug)) profile.parse_and_save_tweets() resp = {"deleted_tiids": deleted_tiids} # local_sleep(30) if request.method == "POST" and action == "dedup": logger.info(u"deduplicating for {url_slug}".format( url_slug=profile.url_slug)) deleted_tiids = profile.remove_duplicates() resp = {"deleted_tiids": deleted_tiids} # local_sleep(30) elif request.method == "POST" and action == "refresh": tiids_being_refreshed = profile.refresh_products(source) resp = {"products": tiids_being_refreshed} else: # Actions that require authentication abort_if_user_not_logged_in(profile) local_sleep(2) if request.method == "PATCH": added_products = profile.add_products(request.json) resp = {"products": added_products} elif request.method == "DELETE": tiids_to_delete = request.args.get("tiids", []).split(",") resp = delete_products_from_profile(profile, tiids_to_delete) else: abort(405) # method not supported. We shouldn't get here. return json_resp_from_thing(resp)
def import_products(importer_name): query = u"{core_api_root}/v1/importer/{importer_name}?api_admin_key={api_admin_key}".format( core_api_root=g.api_root, importer_name=importer_name, api_admin_key=os.getenv("API_ADMIN_KEY") ) analytics_credentials = current_user.get_analytics_credentials() data_dict = json.loads(request.data) data_dict["analytics_credentials"] = analytics_credentials r = requests.post( query, data=json.dumps(data_dict), headers={'Content-type': 'application/json', 'Accept': 'application/json'} ) local_sleep(1) return json_resp_from_thing(r.json())
def product_biblio_modify(tiid): # TODO: check this user has permission to make this change query = u"{core_api_root}/v1/product/{tiid}/biblio?api_admin_key={api_admin_key}".format( core_api_root=g.api_root, tiid=tiid, api_admin_key=os.getenv("API_ADMIN_KEY") ) data_dict = json.loads(request.data) r = requests.patch( query, data=json.dumps(data_dict), headers={'Content-type': 'application/json', 'Accept': 'application/json'} ) local_sleep(1) return json_resp_from_thing(r.json())
def get_user_for_response(id, request, include_products=True): id_type = unicode(request.args.get("id_type", "url_slug")) try: logged_in = unicode(getattr(current_user, id_type)) == id except AttributeError: logged_in = False retrieved_user = get_user_from_id(id, id_type, logged_in, include_products) if include_products: local_sleep(1) if retrieved_user is None: logger.debug(u"in get_user_for_response, user {id} doesn't exist".format( id=id)) abort(404, "That user doesn't exist.") g.profile_slug = retrieved_user.url_slug return retrieved_user
def product_without_needing_profile(tiid): """ I think this is unused, replaced by product_from_tiid. If it is used, it's broken because all the markups it gives just point to /jason profile. """ local_sleep(1) product = get_product(tiid) if not product: return abort_json(404, "product not found") markup = Markup("jason", embed=False) product_dict = product.to_markup_dict( markup=markup ) product_dict["metrics"] = product.metrics product_dict["countries"] = product.countries product_dict["metrics"] = product.metrics product_dict["countries"] = product.countries return json_resp_from_thing(product_dict)
def product_biblio_modify(tiid): try: if tiid not in current_user.tiids: abort_json(401, "You have to own this product to modify it.") except AttributeError: abort_json(405, "You musts be logged in to modify products.") query = u"{core_api_root}/v1/product/{tiid}/biblio?api_admin_key={api_admin_key}".format( core_api_root=os.getenv("API_ROOT"), tiid=tiid, api_admin_key=os.getenv("API_ADMIN_KEY")) data_dict = json.loads(request.data) r = requests.patch(query, data=json.dumps(data_dict), headers={ 'Content-type': 'application/json', 'Accept': 'application/json' }) local_sleep(1) return json_resp_from_thing(r.json())
def product_biblio_modify(tiid): try: if tiid not in current_user.tiids: abort_json(401, "You have to own this product to modify it.") except AttributeError: abort_json(405, "You musts be logged in to modify products.") query = u"{core_api_root}/v1/product/{tiid}/biblio?api_admin_key={api_admin_key}".format( core_api_root=os.getenv("API_ROOT"), tiid=tiid, api_admin_key=os.getenv("API_ADMIN_KEY") ) data_dict = json.loads(request.data) r = requests.patch( query, data=json.dumps(data_dict), headers={'Content-type': 'application/json', 'Accept': 'application/json'} ) local_sleep(1) return json_resp_from_thing(r.json())
def refresh_status(profile_id): local_sleep(0.5) # client to webapp plus one trip to database id_type = request.args.get("id_type", "url_slug") # url_slug is default profile_bare_products = get_profile_from_id(profile_id, id_type, include_product_relationships=False) print profile_bare_products return json_resp_from_thing(profile_bare_products.get_refresh_status())