Exemple #1
0
    def create_event(self):
        """
        Create an event
        """
        groups = MemberGroup.objects.all()
        if len(groups) == 0:
            self.stdout.write("Your database does not contain any "
                              "member groups.")
            self.stdout.write("Creating a committee.")
            self.create_member_group(Committee)
            groups = MemberGroup.objects.all()
        event = Event()

        event.title_nl = _generate_title()
        event.title_en = event.title_nl
        event.description_nl = _faker.paragraph()
        event.description_en = _faker.paragraph()
        event.start = _faker.date_time_between("-30d", "+120d", _current_tz)
        duration = math.ceil(random.expovariate(0.2))
        event.end = event.start + timedelta(hours=duration)
        event.organiser = random.choice(groups)
        event.category = random.choice(Event.EVENT_CATEGORIES)[0]

        if random.random() < 0.5:
            week = timedelta(days=7)
            event.registration_start = _faker.date_time_between_dates(
                datetime_start=event.start - 4 * week,
                datetime_end=event.start - week,
                tzinfo=_current_tz,
            )
            event.registration_end = _faker.date_time_between_dates(
                datetime_start=event.registration_start,
                datetime_end=event.start,
                tzinfo=_current_tz,
            )
            event.cancel_deadline = _faker.date_time_between_dates(
                datetime_start=event.registration_end,
                datetime_end=event.start,
                tzinfo=_current_tz,
            )

        event.location_nl = _faker.street_address()
        event.location_en = event.location_nl
        event.map_location = event.location_nl
        event.send_cancel_email = False

        if random.random() < 0.5:
            event.price = random.randint(100, 2500) / 100
            event.fine = max(
                5.0,
                random.randint(round(100 * event.price),
                               round(500 * event.price)) / 100,
            )

        if random.random() < 0.5:
            event.max_participants = random.randint(20, 200)

        event.published = random.random() < 0.9

        event.save()
Exemple #2
0
    def handle(self, *args, **options):

        with open('E:/moviews_scrapy/events.json') as f:
            events = json.load(f)

            for event in events:
                event_object = Event()
                event_object.title = event['title']
                event_object.date = event['date']
                event_object.description = event['description']
                event_object.category = event['category']
                event_object.runtime = event['runtime']
                event_object.image_url = event['images'][0]['path']

                event_object.save()

        self.stdout.write(self.style.SUCCESS('Sucessfully Imported Events'))
    def handle(self, *args, **options):

        self.create_categories()
        self.create_locations()

        old_calendars = UNLCalendar.objects.all()
        for old_calendar in old_calendars:

            # Check if the old calendar creator exists in our DB
            calendar_creator = self.get_create_user(str(old_calendar.uidcreated))
            if calendar_creator is not None:
                new_calendar = Calendar(title=old_calendar.name, owner=calendar_creator)
                new_calendar.pk = old_calendar.id
                try:
                    new_calendar.save()
                except Exception, e:
                    logging.error('Unable to save calendar `%s`: %s' % (old_calendar.name, str(e)))
                else:

                    # Editors
                    # Assume if they had any permissions at all, they are an editor
                    # (unless they are the calendar owner or a superuser)
                    for uid in UNLUserHasPermission.objects.filter(calendar_id=old_calendar.id).values_list('user_uid').distinct():
                        uid = uid[0]
                        editor = self.get_create_user(str(uid))
                        if editor is not None and editor != calendar_creator and not editor.is_superuser:
                            new_calendar.editors.add(editor)

                    # Events
                    for old_calendar_event in UNLCalendarHasEvent.objects.filter(calendar_id = old_calendar.id):
                        try:
                            old_event = UNLEvent.objects.get(id=old_calendar_event.event_id)
                        except UNLEvent.DoesNotExist:
                            logging.error('Has event missing %d' % old_calendar_event.event_id)

                        old_title = old_event.title
                        if len(old_title) > 255:
                            old_title = old_title[0:254]
                        elif old_event.subtitle is not None and len(old_event.title + ' - ' + old_event.subtitle) < 255:
                            old_title += ' - ' + old_event.subtitle

                        old_contact_name = old_event.listingcontactname
                        if not old_contact_name:
                            old_contact_name = calendar_creator.first_name
                        old_contact_name = remove_html(old_contact_name) # Clean before checking length

                        # check to see if the contact name is too long
                        if len(old_contact_name) > 64:
                            old_contact_name = old_contact_name[0:63]

                        old_contact_email = remove_html(old_event.listingcontactemail)
                        old_contact_phone = remove_html(old_event.listingcontactphone)

                        if not old_event.description:
                            old_event.description = settings.FALLBACK_EVENT_DESCRIPTION

                        new_event = Event(title=old_title,
                                          description=old_event.description,
                                          calendar=new_calendar,
                                          contact_name=old_contact_name,
                                          contact_email=old_contact_email,
                                          contact_phone=old_contact_phone)

                        # Statuses: pending, posted, archived
                        state = None
                        if old_calendar_event.status == 'pending':
                            new_event.state = State.pending
                        elif old_calendar_event.status == 'posted' or old_calendar_event.status == 'archived':
                            new_event.state = State.posted
                        else:
                            logging.error('Unknown old event status `%s`' % old_calendar_event.status)
                            continue

                        event_creator = self.get_create_user(str(old_event.uidcreated))
                        if event_creator is not None:
                            new_event.creator = event_creator

                            # Event Type -> Category
                            category = self.get_event_category(old_event)
                            if category is not None:
                                new_event.category = category

                            try:
                                new_event.save()
                            except Exception, e:
                                logging.error('Unable to save new event `%s`: %s' % (new_event.title,str(e)))
                                continue
                            else:
                                # Instances
                                for old_instance in UNLEventdatetime.objects.filter(event_id=old_event.id):
                                    new_instance = EventInstance(event=new_event,
                                                                 unl_eventdatetime_id=old_instance.id)

                                    old_start_time = old_instance.starttime
                                    old_end_time = old_instance.endtime

                                    # Validate start/end datetimes (UNL event times can be invalid and/or empty)
                                    if not old_end_time:
                                        new_instance.start = old_start_time
                                        new_instance.end = old_start_time + timedelta(hours=1)
                                        logging.error('Invalid event instance datetimes: No end datetime available. Setting new end datetime to 1 hour after the given start datetime value `%s`. (Event: `%s`)' % (old_start_time, old_title))
                                    elif old_start_time > old_end_time:
                                        new_instance.start = old_start_time
                                        new_instance.end = old_start_time + timedelta(hours=1)
                                        logging.error('Invalid event instance datetimes: End datetime `%s` occurs before start datetime `%s`. Setting new end datetime value. (Event: `%s`)' % (old_end_time, old_start_time, old_title))
                                    else:
                                        new_instance.start = old_start_time
                                        new_instance.end = old_end_time

                                    # Location
                                    old_location_id = old_instance.location_id
                                    if not old_location_id:
                                        old_location_id = 1

                                    try:
                                        old_location = UNLLocation.objects.get(id=old_location_id)
                                    except UNLLocation.DoesNotExist:
                                        logging.info('UNL event instance location not in UNL Location table: %d' % old_location_id)
                                    else:
                                        if old_location.name:
                                            # check to see if the location name is too long
                                            old_location_name = old_location.name
                                            if len(old_location_name) > 256:
                                                old_location_name = old_location_name[0:256]

                                            try:
                                                new_instance.location = Location.objects.get(title__iexact=old_location_name)
                                            except Location.DoesNotExist:
                                                logging.error('No Location for UNL Location %s' % old_location_name)

                                    try:
                                        new_instance.save()
                                    except Exception, e:
                                        logging.error('Unable to save event instance for event `%s`: %s' % (new_event.title,str(e)))