def update_fb_event(event_id, page, state, title, description, start_time, end_time): datetime_now = datetime.datetime.utcnow().isoformat("T") event = FBEvent.collection().find_one({"event_id": event_id}) event["fields"]["name"] = title event["fields"]["description"] = description event["updated_time"] = datetime_now if start_time is not None: event["fields"]["start_time"] = isoformat(start_time) if end_time is not None: event["fields"]["end_time"] = isoformat(end_time) if state == CampaignState.PUBLISHED: api = GraphAPI(page["access_token"]) url = event_id dict = { "name": title, "start_time": isoformat(start_time) + "Z", "description": strip_tags(replace_newline(description)), } if end_time is not None: dict["end_time"] = isoformat(end_time) + "Z" api.request(url, post_args=dict) event_data = api.request(event_id) event = save_fb_event(event_data, page["id"]) else: FBEvent.collection().update({"event_id": event_id}, {"$set": {event}}) event = FBEvent.unserialize(FBEvent.collection().find_one({"event_id": event_id})) return event
def del_campaign(): """ (DELETE: /campaign) """ from campaigns.campaign.model import Campaign from unifide_backend.action.social.facebook.model import FBPost, FBEvent from unifide_backend.action.social.twitter.model import TWTweet from unifide_backend.action.social.foursquare.model import FSQPageUpdate from unifide_backend.action.mapping.action import del_mapping, get_brand_mapping, get_mapping from unifide_backend.action.social.facebook.action import del_fb_post, del_fb_event from unifide_backend.action.social.twitter.action import del_tweet from unifide_backend.action.social.foursquare.action import del_fsq_update user_id = request.args.get("user_id") brand_name = request.args.get("brand_name") campaigns = request.args.get("campaign_list") brand_obj = get_brand_mapping(user_id, brand_name) campaign_list = campaigns.split(',') for c in campaign_list: mapping_obj = get_mapping(c) # delete facebook post / event if mapping_obj.facebook is not None: id = mapping_obj.facebook post = FBPost.collection().find_one({"_id": coerce_bson_id(id)}) try: if post is not None: del_fb_post(post["post_id"], id, brand_obj.facebook["access_token"]) event = FBEvent.collection().find_one({"_id": coerce_bson_id(id)}) if event is not None: del_fb_event(event["event_id"], id, brand_obj.facebook["access_token"]) except Exception, err: print traceback.format_exc() print id # delete twitter status if mapping_obj.twitter is not None: id = mapping_obj.twitter tweet = TWTweet.collection().find_one({"_id": coerce_bson_id(id)}) try: del_tweet(tweet["fields"]["id_str"] if tweet["fields"] is not None else None, id, brand_obj.twitter["access_token"]["key"], brand_obj.twitter["access_token"]["secret"]) except Exception, err: print traceback.format_exc() print id
def update_campaign_data(): """ (PUT: /campaign/date/update) """ from campaigns.campaign.model import Campaign from unifide_backend.action.mapping.action import get_brand_mapping, get_mapping from unifide_backend.action.social.facebook.model import FBEvent from unifide_backend.action.social.facebook.action import update_fb_event user_id = request.form.get("user_id") brand_name = request.form.get("brand_name") campaign_id = request.form.get('campaign_id') title = request.form.get('title') description = request.form.get('description') event_datetime_start = float(request.form.get("datetime_start")) if request.form.get("datetime_start") is not None else None event_datetime_end = float(request.form.get("datetime_end")) if request.form.get("datetime_end") is not None else None place = request.form.get("place") item_list = request.form.get("item_list") print title, description print campaign_id brand_obj = get_brand_mapping(user_id, brand_name) mapping_obj = get_mapping(campaign_id) print brand_obj print mapping_obj set_dic = { "title": title, "description": description, "happening_datetime_start": event_datetime_start, "happening_datetime_end": event_datetime_end, } if item_list is not None and len(item_list) > 0: set_dic["item_id_lis"] = item_list if mapping_obj.campaign is not None: Campaign.collection().update({"_id": mapping_obj.campaign}, {"$set" : set_dic}) if mapping_obj.blog is not None: Campaign.collection().update({"_id": mapping_obj.blog}, {"$set" : set_dic}) if mapping_obj.facebook is not None and mapping_obj.type == "event": event = FBEvent.collection().find_one({"_id": coerce_bson_id(mapping_obj.facebook)}) update_fb_event(event["event_id"], brand_obj.facebook, mapping_obj.state, title, description, event_datetime_start, event_datetime_end) return jsonify({"status": "ok"})
def del_fb_event(event_id, obj_id, access_token): if event_id is not None: data = GraphAPI(access_token).delete_object(event_id) print data FBEvent.collection().update({"_id": coerce_bson_id(obj_id)}, {"$set": {"is_deleted": 1}})
def put_fb_event(page, fb_id, state, title, description, start_time, end_time, media_file=None): datetime_now = datetime.datetime.utcnow().isoformat("T") event = FBEvent() event.page_id = page["id"] event.owner = {"id": fb_id} event.fields = {"name": title, "description": description} event.created_time = datetime_now event.updated_time = datetime_now if start_time is not None: event.fields["start_time"] = isoformat(start_time) if end_time is not None: event.fields["end_time"] = isoformat(end_time) if state == CampaignState.PUBLISHED: api = GraphAPI(page["access_token"]) url = "%s/%s" % (page["id"], "events") # todo : note the start and end UTC time formats, currently hard-coded +0000 dict = { "name": title, "start_time": isoformat(start_time) + "Z", "description": strip_tags(replace_newline(description)), } if end_time is not None: dict["end_time"] = isoformat(end_time) + "Z" if media_file: dict["source"] = media_file dict["access_token"] = page["access_token"] event.event_id = api.put_event(dict)["id"] else: event.event_id = api.request(url, post_args=dict)["id"] event_data = api.request(event.event_id) print "event data" print event_data event = save_fb_event(event_data, page["id"]) else: event._id = FBEvent.collection().insert(event.serialize()) return event
def save_fb_event(event, page_id): event_obj = FBEvent() event_obj.event_id = event["id"] event_obj.page_id = page_id event_obj.owner = event["owner"] event_obj.fields = event dupe_obj = FBEvent.collection().find_one({"event_id": event["id"]}) if dupe_obj is None: event_obj._id = FBEvent.collection().save(event_obj.serialize()) else: FBEvent.collection().update({"event_id": event["id"]}, event_obj.serialize()) event_obj = FBEvent.unserialize(FBEvent.collection().find_one({"event_id": event["id"]})) return event_obj