def parenting_cat(cat, depth=1): """Return the name of category id `cat` (or name), stopping at level `depth`.""" if not cat: return None _, path = fsc.search_categories(cat) cat_is_name = fsc.choose_type(cat) answer = path[depth] if len(path) > depth else path[-1] if cat_is_name: return answer return fsc.CAT_TO_ID[:answer]
def question_categories(session): """Return a list of all categories id for the current question.""" cats = actual_question(session).cat res = [] for cat in cats: res.extend(fsc.get_subcategories(cat, fsc.Field.id)) return res
def venues_list(db, question, city): """Export list of venue for a given (city, question) to a JSON file.""" import FSCategories as fsc import cities as c space = c.GBOXES[city] cats = [] for cat in QUESTIONS[question].cat: cats.extend(fsc.get_subcategories(cat, fsc.Field.id)) venues = db.find({'loc': space, 'cat': {'$in': cats}}, {'name': 1, 'loc': 1, 'likes': 1, 'where': 1}) res = {} def insert_venue(new_venue): """insert `new_venue` by giving it a unique name.""" name = new_venue['name'] i = 0 while name in res: name += ' ' i += 1 # if i > 250: # print(u'at least {} {} in {}'.format(i, name.strip(), city)) res[name] = [venue['likes'], venue.get('where')] + loc + id_ for venue in venues: loc = [round(_, 6) for _ in reversed(venue['loc']['coordinates'])] id_ = [str(venue['_id'])] insert_venue(venue) import ujson import codecs import gzip writer = codecs.getwriter('utf8') filename = 'static/q/{}_{}.js.gz'.format(city, question) k = Key(bucket) k.key = filename k.set_metadata('Content-Encoding', 'gzip') with gzip.open(filename, 'wb') as result: writer(result).write('var CATS='+ujson.dumps(cats)+';\n') writer(result).write('var VENUES='+ujson.dumps(res)+';') with open(filename) as result: k.set_contents_from_file(result) print('sent ' + filename) k.make_public()
def is_event(cat_id): """Does `cat_id` represent an event.""" return cat_id in fsc.get_subcategories('Event', fsc.Field.id)
def parenting_cat(place, depth): """Return the category of `place`, without going beyond `depth`""" _, path = fsc.search_categories(place['_id']) if len(path) > depth: return fsc.CAT_TO_ID[:path[depth]] return fsc.CAT_TO_ID[:path[-1]]