Exemplo n.º 1
0
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
Exemplo n.º 2
0
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