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 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 save_tw_user_oauth(user_id, brand_name, key, secret): twUser_obj = TWUser() twUser_obj.u_id = user_id twUser_obj.brand_name = brand_name twUser_obj.oauth_key = key twUser_obj.oauth_secret = secret def save_obj(twUser): return TWUser.collection().insert(twUser.serialize()) dupe_obj = TWUser.collection().find_one({"u_id": user_id, "brand_name": brand_name}) if dupe_obj is None: twUser_obj._id = save_obj(twUser_obj) else: TWUser.collection().update({"u_id": user_id, "brand_name": brand_name}, {"oauth_key": key, "oauth_secret": secret})
def save_tw_user(user_id, brand_name, key, secret): """ Add twitter account to the unifide user and spawn thread to pull recent tweewts from home timeline """ api, token_key, token_secret = get_api(key, secret) twUser_obj = TWUser() twUser_obj.u_id = user_id twUser_obj.tw_id = api.me().id_str twUser_obj.brand_name = brand_name twUser_obj.username = api.me().screen_name TWUser.collection().update({"u_id": user_id, "brand_name": brand_name}, twUser_obj.serialize()) saved_tw_user_obj = TWUser.unserialize(TWUser.collection().find_one({"u_id": user_id, "brand_name": brand_name})) update_brand_mapping(user_id, brand_name, "twitter", api.me().id_str, {"key": key, "secret": secret}) t = Thread(target=save_tweets, args=(user_id, saved_tw_user_obj.tw_id, brand_name)) t.setDaemon(False) t.start() return saved_tw_user_obj
def del_twitter_user(user_id, brand_name): update_brand_mapping(user_id, brand_name, "twitter") TWUser.collection().remove({"u_id": user_id, "brand_name": brand_name})
def get_tw_user_oauth(user_id, brand_name): dic = TWUser.collection().find_one({"u_id": user_id, "brand_name": brand_name}) return TWUser.unserialize(dic) if dic is not None else None
def save_obj(twUser): return TWUser.collection().insert(twUser.serialize())