Esempio n. 1
0
class EventBriteTests(TestCase):
    def setUp(self):
        env = environ.Env()
        env_file = os.path.join(os.getcwd(), ".env")
        environ.Env.read_env(env_file)

        self.eventbrite = Eventbrite(env('EVENTBRITE_KEY'))

    def test_connection_to_eventbrite(self):
        """
        Test whether connection to EventBrite is successful.
        """
        self.assertEqual(
            self.eventbrite.headers['content-type'], 'application/json'
        )

    def test_eventbrite_user(self):
        """
        Test the correct user is returned when using the EventBrite api.
        """
        user = self.eventbrite.get_user()

        self.assertEqual(user.headers['Content-Type'], 'application/json')
        self.assertEqual(user['name'], 'Ollie Gardner')

    def test_eventbrite_organiser(self):
        """
        Test getting SoCS EventBrite page from organiser ID.
        """
        organiser = self.eventbrite.get_organizers(6830828415)

        self.assertEqual(organiser.headers['Content-Type'], 'application/json')
        self.assertEqual(
            organiser['name'],
            'School of Computing Science, University of Glasgow'
        )

    def test_eventbrite_organiser_events(self):
        """
        Test getting SoCS EventBrite events from organiser ID.
        """
        organiser_events = self.eventbrite.get_organizer_events(6830828415)

        self.assertEqual(
            organiser_events.headers['Content-Type'], 'application/json'
        )
        self.assertGreater(len(organiser_events), 1)
    def eventbrite_feed(self, key):
        print('Retrieving event feed from Eventbrite. Please wait...')

        # Connects to Python Eventbrite API
        eventbrite = Eventbrite(key)

        # Load organiser IDs from config
        config = yaml.safe_load(open('config.yaml'))
        organisers = config['organisers']

        for organiser_id in organisers:
            # Get Eventbrite events from organiser ID
            organiser = eventbrite.get_organizers(organiser_id)
            organiser_events = eventbrite.get_organizer_events(organiser_id,
                                                               expand='venue')

            # Creates seminar group model
            seminar_group, seminar_group_created = SeminarGroup.objects.get_or_create(
                name=organiser['name'])

            if seminar_group_created:
                seminar_group.description = organiser['long_description'][
                    'text']
                seminar_group.url = organiser['url']
                seminar_group.save()

            # For each event in the organiser's events
            for event in organiser_events['events']:
                now = datetime.datetime.now()
                start = datetime.datetime.strptime(event['start']['local'],
                                                   '%Y-%m-%dT%H:%M:%S')

                # If event is in the future
                if start >= now:
                    # Creates seminar model
                    seminar, seminar_created = Seminar.objects.get_or_create(
                        eventbrite_id=event['id'])
                    seminar.title = event['name']['text']
                    seminar.description = event['description']['text']
                    seminar.registration_url = event['url']
                    seminar.start_time = event['start']['local']
                    seminar.end_time = event['end']['local']
                    seminar.online = event['online_event']
                    seminar.seminar_group = seminar_group

                    venue = event['venue']

                    # If this event has a venue
                    if venue:
                        # Create location model
                        location, location_created = Location.objects.get_or_create(
                            location=venue['name'],
                            latitude=venue['latitude'],
                            longitude=venue['longitude'],
                            online=event['online_event'],
                        )

                        seminar.location = location
                        seminar_group.location.add(location)

                    seminar.save()
                    seminar_group.save()

        print('Eventbrite event feed retrieved!')