示例#1
0
 def clean_data(self):
     events = Event.objects.all()
     self.count = len(events)
     for idx, event in enumerate(events):
         event.title = remove_html(event.title)
         event.description = self.custom_clean(event.description)
         try:
             event.save()
         except Exception, e:
             print e.message
             print "Title: ", event.title
             print "Decsription: ", event.description
         self.update_progress(idx)
示例#2
0
def custom_striptags(value):
    """
    Non-regex-based striptags replacement, using Bleach.
    """
    value = remove_html(value)
    return value
示例#3
0
    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)))