def refresh_users(): all_players = get_all_players() try: # Fetch user JSON fron "database layer" users_json = database.get_metadata("users.json") if users_json == None: return "no json found" else: users_result = json.loads(users_json) users = users_result["users"] # Overwrite generic player values with user metadata for user in users: all_players[user["userName"]] = user # For every user, if she has no image add gravatar image or generated avatar for player in all_players: user = all_players[player] if not 'imageUrl' in user: user['imageUrl'] = make_image_url(user) # Save users to redis users_data.save_users(all_players.values()) return "done" except requests.HTTPError as e: logging.error(e) return "exception"
def get_similar_beer(beer_id): try: top_beer = database.get_nearest_beers(beer_id, 1)[0] print top_beer recommended_beer_id = top_beer[1] return jsonify(database.get_metadata(recommended_beer_id)) except IndexError: return jsonify({})
def get_next_recommendation(): # we get a POST request from the client in JSON format # whose request body contains beer_id and beer_rating # token comes in "Bearer xvgsfddf" fashion as per the convention token = request.headers['Authorization'].split(' ')[1] data = request.json beer_id = data['beer_id'] beer_rating = data['beer_rating'] user_id = database.get_userid_from_string(token) database.save_to_profile(user_id, beer_id, beer_rating) recommended_beer_id = database.get_next_recommendation(user_id) return jsonify(database.get_metadata(recommended_beer_id))
def get_similar_beer(beer_id): ''' The client sends a get request which represents a request for beers similar to the beer in the url ''' try: beer_id = int(beer_id) except ValueError as e: print 'Bad beer id %s' % beer_id return jsonify({}), 400 recommended_beer_id = database.get_nearest_beer(beer_id) if recommended_beer_id: return jsonify(database.get_metadata(recommended_beer_id)) else: return jsonify({}), 404
def test_get_metadata(self): test_beer_id = 2093 beer_meta_data = database.get_metadata(test_beer_id) eq_(type(beer_meta_data) is dict, True, 'get_meta_data should return a dictionary of metadata about a beer') eq_('beer_name' in beer_meta_data, True, 'get_meta_data should return a dictionary with a beer_name property in it') eq_(isinstance(beer_meta_data['beer_name'], basestring), True, 'the parameter for beer_name in metadata should be a string') eq_(beer_meta_data['beer_name'], '90 Minute IPA', 'the parameter for beer_name in metadata should be "90 Minute IPA"') eq_('beer_id' in beer_meta_data, True, 'get_meta_data should return a dictionary with an beer_id in it') eq_(isinstance(beer_meta_data['beer_id'], (int, long)), True, 'the parameter for beer_id in metadata should be an int') eq_(beer_meta_data['beer_id'], test_beer_id, 'the parameter for name in metadata should be %s' % test_beer_id) eq_('beer_image_url' in beer_meta_data, True, 'get_meta_data should return a dictionary with an beer_image_url property in it') eq_(isinstance(beer_meta_data['beer_image_url'], basestring), True, 'the parameter for beer_image_url in metadata should be a string') eq_(beer_meta_data['beer_image_url'], 'http://cdn.beeradvocate.com/im/beers/2093.jpg', 'the parameter for beer_image_url in metadata should be a http://cdn.beeradvocate.com/im/beers/2093.jpg')
def get_best_recommendation(): ''' The client sends a POST requests that represents a review of a beer we recommended previously. We save this rating and send back a new recommendation The client identifies itself with its unique_string which it passes in the request header ''' try: unique_string = request.headers['Authorization'] except KeyError: print 'client sent review without auth' return jsonify({}), 401 user_id = database.get_userid_from_string(unique_string) if not user_id: print 'bad unique_string %s' % unique_string return jsonify({}), 401 review = request.json if not review: print 'did not get a json review' return jsonify({}), 400 try: beer_id = review['beer_id'] beer_rating = review['beer_rating'] except KeyError as missing_param: print 'Review missing parameter %s' % missing_param return jsonify({}), 400 if not isinstance(beer_id, int) or not isinstance(beer_rating, int): print 'The client shuld be passing review parameters as integers. Possibly SQL injection.' print beer_id, beer_rating return jsonify({}), 400 database.save_to_profile(user_id, beer_id, beer_rating) recommended_beer_id = database.get_next_recommendation(user_id) database.save_recommendation(user_id,recommended_beer_id) # save the recommendation so we dont return it again return jsonify(database.get_metadata(recommended_beer_id))
def dispatch_worker(taskq): conn = connect() fetch_city_ends = { city: get_metadata(conn, '%s_fetch_end' % city) for city in CITIES } fetch_start = min(fetch_city_ends.values()) - timedelta(days=1) try: while True: fetch_start = fetch_start + timedelta(days=1) fetch_end = fetch_start + timedelta(days=1) while fetch_start > datetime.now() - timedelta(days=7): sleep(60 * 60) for city in CITIES: if fetch_city_ends[city] < fetch_end: taskq.put((city, fetch_start)) except KeyboardInterrupt: pass finally: conn.close()
from logging.config import fileConfig # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config # @UndefinedVariable # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name) ### >>> Gong put this here for 'autogenerate' support import imp, os imp.load_source('paths', os.path.dirname(os.path.realpath(__file__)) + '/../paths.py') from database import get_metadata target_metadata = get_metadata() ### <<< Gong # other values from the config, defined by the needs of env.py, # can be acquired: # my_important_option = config.get_main_option("my_important_option") # ... etc. def run_migrations_offline(): """Run migrations in 'offline' mode. This configures the context with just a URL and not an Engine, though an Engine is acceptable here as well. By skipping the Engine creation we don't even need a DBAPI to be available.