def _process_time(component): ''' Returns a UTC-based timezone-naive datetime for the time present in the given component. The component must have a dt element and an optional TZID parameter. Helper assumes the existance of a 'notices' list, a 'uid' string and a 'default_tz_str' string (which may be empty or None). ''' dt = component.dt try: # pull the datetime first -- if its UTC, we'll know immediately if dt.tzinfo: return component.dt.replace(tzinfo=None) except AttributeError: # if no timezone, it must be a regular date object. give it a time of midnight return datetime.datetime.combine(dt,datetime.time()) # otherwise, we need to find a timezone tz_str = component.params.get('TZID',default_tz_str) # if couldn't find one, return the bare dt with a notice if not tz_str: notices.append(EventImportReport.UnavailableTimezone()) return component.dt # we have a timezone string, try converting to UTC now. if the string is invalid, return a notice try: return localtoutc(component.dt,tz_str,return_naive=True) except pytz.exceptions.UnknownTimeZoneError: notices.append(EventImportReport.UnknownTimezone(tz_str)) return component.dt
def store_fbevent(event_info,event_image=None, create_owners=True, resolve_cache=None): ''' Takes a dict of properties retreived from a Facebook Graph API call for a page and stores a Place from the information. The following fields in the dict are used: - id (required) - type (required with value 'event' or a TypeError will be thrown) - name (required) - start_time (required. assumed to be in EST at the moment) - end_time (required. assumed to be in EST at the moment) - description - venue (dict with location values) - location (simple string place name) - owner (dict with stub info about organizer) No new Event will be returned if either an identical one already exists in the db, or a FacebookEventEntry already exists for the given Facebook id. An INFO message is logged to note the attempt to store an existing page as a Place. event_image is a URL to an image to use for this event. The Facebook event object doesn't store it's picture directly, instead it stores it as a connection to the event. If this argument is not provided, the live service will be queried to retreive it. If create_owners is True, a Facebook-linked model instance will be created for the owner. The resolve_cache is an optional instance of a VenueResolveCache objects. See docs for it for details. ''' fbid = event_info.get('id') if fbid is None: raise TypeError("Cannot store object without 'event' type.") # look to see if event already exists. return with it if so. try: event = FacebookEventRecord.objects.get(fb_id=fbid).event outsourcing_log.info('Existing fb event found for fbid %s'%unicode(fbid)) return event except FacebookEventRecord.DoesNotExist: pass # ensure this is actually an event if event_info.get('type') != 'event': raise TypeError("Cannot store object without 'event' type.") ename = event_info.get('name').strip() # need to log events that don't have names if not ename: outsourcing_log.warning('No name for event with fbid %s' % fbid) event = Event(name=event_info['name'], description=unicode(event_info.get('description','')), url='http://www.facebook.com/%s' % fbid) # process times try: dtstart_est = EST.localize(dtparser.parse(event_info.get('start_time'))) dtend_est = EST.localize(dtparser.parse(event_info.get('end_time'))) except ValueError as e: raise ValueError('Bad start/end time for event fbid %s: %s' % (unicode(fbid),unicode(e))) event.dtstart = localtoutc(dtstart_est,return_naive=True) event.dtend = localtoutc(dtend_est,return_naive=True) # process image if event_image is None: try: event.image_url = facebook_client.graph_api_picture_request(fbid) except IOError as e: outsourcing_log.error('Error retreiving picture for event %s: %s' % (unicode(eid),unicode(e))) else: event.image_url = event_image # process place if resolve_cache: event.place = _process_place_cache_support(event_info, resolve_cache=resolve_cache) else: event.place = _process_place(event_info) if event.place and event.place.pk is None: if event.place.location and event.place.location.pk is None: event.place.location.save() event.place.save() event.save() # get the update time from the fbevent dtupdate_str = event_info.get('updated_time') if dtupdate_str: dtupdated = dtparser.parse(event_info.get('updated_time')) if dtupdated.tzinfo: # if a tz was part of the time string, convert to UTC (otherwise just assume UTC) dtupdated = localtoutc(dtupdated,return_naive=True) event.dtmodified = dtupdated else: dtupdated = event.dtmodified # create a FB record FacebookEventRecord.objects.create(fb_id=fbid,event=event,last_updated=dtupdated) # add event categories as EventMeta objects categorize.add_event_oldtypes(event) # now set the event owner fbowner_id = event_info.get('owner',{}).get('id') if fbowner_id: owner = _get_owner(fbowner_id,create_new=create_owners) if owner: role = Role.objects.create(role_type='host', organization=owner, event=event) return event
def store_fbevent(event_info, event_image=None, create_owners=True, resolve_cache=None): ''' Takes a dict of properties retreived from a Facebook Graph API call for a page and stores a Place from the information. The following fields in the dict are used: - id (required) - type (required with value 'event' or a TypeError will be thrown) - name (required) - start_time (required. assumed to be in EST at the moment) - end_time (required. assumed to be in EST at the moment) - description - venue (dict with location values) - location (simple string place name) - owner (dict with stub info about organizer) No new Event will be returned if either an identical one already exists in the db, or a FacebookEventEntry already exists for the given Facebook id. An INFO message is logged to note the attempt to store an existing page as a Place. event_image is a URL to an image to use for this event. The Facebook event object doesn't store it's picture directly, instead it stores it as a connection to the event. If this argument is not provided, the live service will be queried to retreive it. If create_owners is True, a Facebook-linked model instance will be created for the owner. The resolve_cache is an optional instance of a VenueResolveCache objects. See docs for it for details. ''' fbid = event_info.get('id') if fbid is None: raise TypeError("Cannot store object without 'event' type.") # look to see if event already exists. return with it if so. try: event = FacebookEventRecord.objects.get(fb_id=fbid).event outsourcing_log.info('Existing fb event found for fbid %s' % unicode(fbid)) return event except FacebookEventRecord.DoesNotExist: pass # ensure this is actually an event if event_info.get('type') != 'event': raise TypeError("Cannot store object without 'event' type.") ename = event_info.get('name').strip() # need to log events that don't have names if not ename: outsourcing_log.warning('No name for event with fbid %s' % fbid) event = Event(name=event_info['name'], description=unicode(event_info.get('description', '')), url='http://www.facebook.com/%s' % fbid) # process times try: dtstart_est = EST.localize(dtparser.parse( event_info.get('start_time'))) dtend_est = EST.localize(dtparser.parse(event_info.get('end_time'))) except ValueError as e: raise ValueError('Bad start/end time for event fbid %s: %s' % (unicode(fbid), unicode(e))) event.dtstart = localtoutc(dtstart_est, return_naive=True) event.dtend = localtoutc(dtend_est, return_naive=True) # process image if event_image is None: try: event.image_url = facebook_client.graph_api_picture_request(fbid) except IOError as e: outsourcing_log.error('Error retreiving picture for event %s: %s' % (unicode(eid), unicode(e))) else: event.image_url = event_image # process place if resolve_cache: event.place = _process_place_cache_support(event_info, resolve_cache=resolve_cache) else: event.place = _process_place(event_info) if event.place and event.place.pk is None: if event.place.location and event.place.location.pk is None: event.place.location.save() event.place.save() event.save() # get the update time from the fbevent dtupdate_str = event_info.get('updated_time') if dtupdate_str: dtupdated = dtparser.parse(event_info.get('updated_time')) if dtupdated.tzinfo: # if a tz was part of the time string, convert to UTC (otherwise just assume UTC) dtupdated = localtoutc(dtupdated, return_naive=True) event.dtmodified = dtupdated else: dtupdated = event.dtmodified # create a FB record FacebookEventRecord.objects.create(fb_id=fbid, event=event, last_updated=dtupdated) # add event categories as EventMeta objects categorize.add_event_oldtypes(event) # now set the event owner fbowner_id = event_info.get('owner', {}).get('id') if fbowner_id: owner = _get_owner(fbowner_id, create_new=create_owners) if owner: role = Role.objects.create(role_type='host', organization=owner, event=event) return event