Exemple #1
0
	def new_event(data):
		event_data = {}

		if data['event_source'] == 'facebook':
			event_data = FacebookBackend().get_event_data(data['event_url'])
			# Checks if an event with the same name or URL already exists
			if Event.objects.filter(url=event_data.get('url', '')):
				raise EventAlreadyExistsException('Event with name "' + event_data['name'] + '" has already been submitted.')
		elif data['event_source'] == 'manual':
			event_data = data
			# Checks if an event with the same name or URL already exists
			if Event.objects.filter(name=event_data['name']):
				raise EventAlreadyExistsException('Event with name "' + event_data['name'] + '" already exists.')
			elif Event.objects.exclude(url='').filter(url=event_data.get('url', '')):
				raise EventAlreadyExistsException('Event with external URL "' + event_data['url'] + '" already exists.')

			event_data['start'] = datetime.strptime(event_data['start'], '%Y-%m-%dT%H:%M')
			if 'end' in event_data and event_data['end'] != '':
				event_data['end'] = datetime.strptime(event_data['end'], '%Y-%m-%dT%H:%M')
			del event_data['event_source']
		else:  # If corrupted data or erroneous POST request, do nothing
			return False

		# Updates an existing event or adds a new one to the database
		# get_or_create returns an object and a boolean value specifying whether a new object was created or not
		event, is_new = Event.objects.get_or_create(name=event_data['name'], defaults={'start': datetime.today(), 'status': 'pending'})

		for key, value in event_data.items():
			setattr(event, key, value)
		event.save()

		return event
Exemple #2
0
	def new_facebook_event_page(data):
		page_data = FacebookBackend().get_page_data(data['page_url'])

		# Updates an existing event page or adds a new one to the database
		# get_or_create returns an object and a boolean value specifying whether a new object was created or not
		event_page, is_new = FacebookEventPage.objects.get_or_create(name=page_data['name'])
		for key, value in page_data.items():
			setattr(event_page, key, value)
		event_page.save()

		# TODO:
		# Don't scrape on creation of a new page, because the user doesn't want to wait around for that
		# Instead, schedule a task to scrape from the new page in the background
		# FacebookEventPageController().scrape_page_events(event_page)

		return event_page
Exemple #3
0
    def new_facebook_event_page(data):
        page_data = FacebookBackend().get_page_data(data['page_url'])

        # Updates an existing event page or adds a new one to the database
        # get_or_create returns an object and a boolean value specifying whether a new object was created or not
        event_page, is_new = FacebookEventPage.objects.get_or_create(
            name=page_data['name'])
        for key, value in page_data.items():
            setattr(event_page, key, value)
        event_page.save()

        # TODO:
        # Don't scrape on creation of a new page, because the user doesn't want to wait around for that
        # Instead, schedule a task to scrape from the new page in the background
        # FacebookEventPageController().scrape_page_events(event_page)

        return event_page
Exemple #4
0
    def new_event(data):
        event_data = {}

        if data['event_source'] == 'facebook':
            event_data = FacebookBackend().get_event_data(data['event_url'])
            # Checks if an event with the same name or URL already exists
            if Event.objects.filter(url=event_data.get('url', '')):
                raise EventAlreadyExistsException(
                    'Event with name "' + event_data['name'] +
                    '" has already been submitted.')
        elif data['event_source'] == 'manual':
            event_data = data
            # Checks if an event with the same name or URL already exists
            if Event.objects.filter(name=event_data['name']):
                raise EventAlreadyExistsException('Event with name "' +
                                                  event_data['name'] +
                                                  '" already exists.')
            elif Event.objects.exclude(url='').filter(
                    url=event_data.get('url', '')):
                raise EventAlreadyExistsException('Event with external URL "' +
                                                  event_data['url'] +
                                                  '" already exists.')

            event_data['start'] = datetime.strptime(event_data['start'],
                                                    '%Y-%m-%dT%H:%M')
            if 'end' in event_data and event_data['end'] != '':
                event_data['end'] = datetime.strptime(event_data['end'],
                                                      '%Y-%m-%dT%H:%M')
            del event_data['event_source']
        else:  # If corrupted data or erroneous POST request, do nothing
            return False

        # Updates an existing event or adds a new one to the database
        # get_or_create returns an object and a boolean value specifying whether a new object was created or not
        event, is_new = Event.objects.get_or_create(name=event_data['name'],
                                                    defaults={
                                                        'start':
                                                        datetime.today(),
                                                        'status': 'pending'
                                                    })

        for key, value in event_data.items():
            setattr(event, key, value)
        event.save()

        return event
Exemple #5
0
    def scrape_page_events(event_page):
        event_page_event_ids = FacebookBackend().get_page_event_ids(
            event_page.page_id)
        for event_id in event_page_event_ids:
            # Mimics raw data being passed via GET so we can reuse the new_event method
            normalized_event_data = {
                'event_source': 'facebook',
                'event_url': 'http://facebook.com/events/' + event_id
            }

            try:
                EventController().new_event(normalized_event_data)
            except (InvalidEventException, EventAlreadyExistsException):
                pass  # No need to be concerned if a page has malformed or past events... not our problem, we just won't import them