def put_fsq_venue(user_id, brand_name, venue_id): url = "%s/%s" % ("venues", venue_id) brand_obj = get_brand_mapping(user_id, brand_name) api = FoursquareAPI(brand_obj.foursquare["access_token"]) data = api.request(url) v = data["response"] venue = FSQVenue() venue.venue_id = v["venue"]["id"] venue.name = v["venue"]["name"] venue.fields = v dupe_obj = FSQVenue.collection().find_one({"venue_id": v["venue"]["id"]}) if dupe_obj is None: FSQVenue.collection().insert(venue.serialize()) else: FSQVenue.collection().update({"venue_id": v["venue"]["id"]}, venue.serialize()) venue = FSQVenue.unserialize(FSQVenue.collection().find_one({"venue_id": v["venue"]["id"]})) BrandMapping.collection().update({"uid": user_id, "brand_name": brand_name}, {"$push": {"foursquare.venues": v["venue"]["id"]}}) t = Thread(target=load_fsq_tips_to_db, args=(v["venue"]["id"], brand_obj.foursquare["access_token"])) t.setDaemon(False) t.start() return venue
def update_info(user_id, old_brand, new_brand, email): if new_brand is not None: Mapping.collection().update({"uid": user_id, "brand_name": old_brand}, {"$set": {"brand_name": new_brand}}) BrandMapping.collection().update({"uid": user_id, "brand_name": old_brand}, {"$set": {"brand_name": new_brand}}) FBUser.collection().update({"u_id": user_id, "brand_name": old_brand}, {"$set": {"brand_name": new_brand}}) TWUser.collection().update({"u_id": user_id, "brand_name": old_brand}, {"$set": {"brand_name": new_brand}}) FSQUser.collection().update({"u_id": user_id, "brand_name": old_brand}, {"$set": {"brand_name": new_brand}}) if email is not None: User.collection().update({"_id": user_id}, {"$set": {"emails.0.address": email}})
def put_new_user_mapping(user_id): brand_mapping_obj = BrandMapping() brand_mapping_obj.uid = user_id brand_mapping_obj.brand_name = "default" brand_mapping_obj.is_selected = 1 brand_mapping_obj._id = BrandMapping.collection().insert(brand_mapping_obj.serialize()) return brand_mapping_obj
def get_post_from_fb(post_id, page_id): url = "%s_%s" % (page_id, post_id) access_token = (BrandMapping.unserialize(BrandMapping.collection().find_one({"facebook.id": page_id}))).facebook[ "access_token" ] print url return GraphAPI(access_token).request(url)
def get_tw_user(user_id, brand_name): dic = TWUser.collection().find_one({"u_id": str(user_id), "brand_name": brand_name}) tw_user = TWUser.unserialize(dic) if dic is not None else None if tw_user is not None: brand_dic = BrandMapping.collection().find_one({"uid": user_id, "brand_name": brand_name}) brand = BrandMapping.unserialize(brand_dic) if brand_dic is not None else None return tw_user, brand.twitter["access_token"]
def get_brand_available_slots(user_id, platform): avail_max_brands = get_max_brands(user_id) dic_list = BrandMapping.collection().find({"uid": user_id}) for d in dic_list: brand = BrandMapping.unserialize(d) avail_max_brands = (avail_max_brands-1) if getattr(brand, platform) is not None else avail_max_brands return avail_max_brands
def update_brand_mapping(user_id, brand_name, platform, platform_id=None, access_token=None): if platform_id is None and access_token is None: BrandMapping.collection().update({"uid": user_id, "brand_name": brand_name}, {"$set": {platform: None}}) else: BrandMapping.collection().update({"uid": user_id, "brand_name": brand_name}, {"$set": {platform: {"id": platform_id, "access_token": access_token}}})
def get_brand_platform_id(user_id, brand_id, platform): dic = BrandMapping.collection().find_one({"uid": user_id, "_id": coerce_bson_id(brand_id)}) brand = BrandMapping.unserialize(dic) if dic is not None else None return getattr(brand, platform)
def get_brand_mapping(user_id, brand_name): collection = BrandMapping.collection() if user_id is None or brand_name is None: return None dic = collection.find_one({"uid": user_id, "brand_name": brand_name}) return BrandMapping.unserialize(dic) if dic is not None else None
def del_fsq_venue(user_id, brand_name, venue_id): BrandMapping.collection().update({"uid": user_id, "brand_name": brand_name}, {"$pop": {"foursquare.venues": venue_id}})