Esempio n. 1
0
    def testGoogleCalendarFieldSizes(self):
        """
        Test some large field sizes (e.g. summary, description).

        If this test passes, Google Calendar supports those fields up to the
        size we're testing here, and the tests documents them.
        If it fails, it just means we have to reduce that maximum size.
        """
        calId = settings.TEST_CALENDAR_ID
        tzName = 'Europe/Berlin'

        startDt = datetime.datetime.today() - datetime.timedelta(days=28)
        startDt = datetime.datetime(startDt.year, startDt.month, startDt.day,
                9, 45)
        endDt = startDt + datetime.timedelta(minutes=30)
        attendingEmails = [testutil.Alice_email, testutil.Brad_email]

        longSummary = 'ab c' * 256
        longDescription = 'ef gh' * 1000
        longLocation = 'ij k' * 256

        tasksched.sleepForRateLimit()
        ev = calendar.createEvent(calId, False, longSummary, longDescription,
                longLocation, startDt, endDt, tzName, attendingEmails)

        self.assertEqual(ev['summary'], longSummary)
        self.assertEqual(ev['description'], longDescription)
        self.assertEqual(ev['location'], longLocation)

        tasksched.sleepForRateLimit()
        calendar.deleteEvent(calId, ev['id'])
Esempio n. 2
0
def processDeleteUsersRequest(sr_id, userIdsToDelete):
    """
    DeleteUsersTask constain users to delete and the scheduling request in question. There users are removent from events and state of the scheduling request is returned to 'SUCCESS'
    """

    schedReq = SchedulingRequest.objects.get(id=sr_id)
    usersToDelete = map(lambda user_id: get_user_model().objects.get(id=user_id), userIdsToDelete)

    #choose the first of the schedules in the schReq to be the base for new schedules
    schedule = Schedule.objects.filter(schedulingRequest=schedReq)[0]
    if not schedule.template:
        failAction(schedReq.id, "Schedule template has been removed. This scheduling request cannot be updated.")
        return
    #all event templates have to exist
    for event in Event.objects.filter(schedules=schedule):
        if not event.template:
            failAction(schedReq.id, "This scheduling request cannot be updated because some of the event templates are missing")
            return

    #remove users from collective events
    for event in Event.objects.filter(schedules=schedule):
        if event.template.isCollective:
            eventData = json.loads(event.json)
            schedules = Schedule.objects.filter(schedulingRequest=schedReq)
            users = map(lambda s: s.forUser, schedules)
            users = filter(lambda u: u not in usersToDelete, users)
            summary = createSummary(event.template.summary, users)
            updated_event = calendar.deleteUsersFromEvent(schedule.template.calendar.email, eventData['id'], list(usersToDelete), summary, sendNotifications=False)
            event.json = json.dumps(updated_event)
            event.save()

    #Delete non-collective events
    for user in usersToDelete:
        for schedule in Schedule.objects.filter(schedulingRequest=schedReq, forUser=user):
            for event in Event.objects.filter(schedules=schedule):
                if not event.template.isCollective:
                    eventData = json.loads(event.json)
                    calendar.deleteEvent(eventData['organizer']['email'], eventData['id'])
                    event.delete()

    Schedule.objects.filter(schedulingRequest=schedReq).filter(forUser__in=usersToDelete).delete()

    #Remove users from scheduling request
    schedReqJson = json.loads(schedReq.json)
    schedReqJson['users'] = filter(lambda user: user not in userIdsToDelete, schedReqJson['users'])
    schedReq.json = json.dumps(schedReqJson)
    schedReq.save()

    markSchedReqSuccess.delay(schedReq.id)
Esempio n. 3
0
    def testCreateAndDeleteEvent(self):
        calId = settings.TEST_CALENDAR_ID
        tzName = 'Europe/Berlin'

        startDt = datetime.datetime.today() - datetime.timedelta(days=28)
        startDt = datetime.datetime(startDt.year, startDt.month, startDt.day,
                9, 45)
        endDt = startDt + datetime.timedelta(minutes=30)
        attendingEmails = [testutil.Alice_email, testutil.Brad_email]

        tasksched.sleepForRateLimit()
        ev = calendar.createEvent(calId, False, 'TEST: sample event',
                'Test: create & delete a simple event', 'Reception',
                startDt, endDt, tzName, attendingEmails)

        def parseToTimezone(string, tz):
            """Parse a datetime string and convert it to timezone tz."""
            return dateutil.parser.parse(string).astimezone(tz)

        def sameLocalFields(dt1, dt2):
            """Compare year, month, day, hour, minute and second in args."""
            return (dt1.year == dt2.year and dt1.month == dt2.month
                    and dt1.day == dt2.day and dt1.hour == dt2.hour
                    and dt1.minute == dt2.minute and dt1.second == dt2.second)

        # This is a handy way to quickly see what the returned JSON looks like
        #print(json.dumps(ev, indent=2))

        # Google returns an ISO dateTime string which includes a UTC offset
        # (apparently the local offset for you). Convert to local time for the
        # timezone we requested above, and check the date&time.
        tzObj = pytz.timezone(tzName)
        self.assertTrue(sameLocalFields(startDt,
            parseToTimezone(ev['start']['dateTime'], tzObj)))
        self.assertTrue(sameLocalFields(endDt,
            parseToTimezone(ev['end']['dateTime'], tzObj)))

        self.assertTrue({x for x in attendingEmails}
                == {x['email'] for x in ev['attendees']})

        tasksched.sleepForRateLimit()
        calendar.deleteEvent(calId, ev['id'])
Esempio n. 4
0
    def testEmptyEventSummaryDescriptionLocation(self):
        calId = settings.TEST_CALENDAR_ID
        tzName = 'Europe/Berlin'

        startDt = datetime.datetime.today() - datetime.timedelta(days=28)
        startDt = datetime.datetime(startDt.year, startDt.month, startDt.day,
                7, 30)
        endDt = startDt + datetime.timedelta(minutes=30)
        attendingEmails = [testutil.Alice_email, testutil.Brad_email]

        tasksched.sleepForRateLimit()
        ev = calendar.createEvent(calId, False, '', '', '',
                startDt, endDt, tzName, attendingEmails)

        for fName in ('summary', 'description', 'location'):
            # The empty fields are missing from the response
            self.assertFalse(fName in ev)

        tasksched.sleepForRateLimit()
        calendar.deleteEvent(calId, ev['id'])
Esempio n. 5
0
def processCleanupSchedulingRequest(modelId):
    """
    Delete what got created in Google Calendar, our Events and Schedules.

    Either something went wrong or the user decided to delete an entire
    scheduling request. In either case, this task is set to roll-back and
    delete objects.
    """
    schReq = SchedulingRequest.objects.get(id=modelId)
    for schedule in schReq.schedule_set.all():
        for event in schedule.event_set.all():
            sleepForRateLimit()
            try:
                evData = json.loads(event.json)
                # Getting the calendar ID like this might be fragile. If so,
                # in the future we can add a foreign key from the Event to a
                # Calendar. But for the way we're making calendar events now,
                # jsonData.organizer.email is the calendar ID.
                calendar.deleteEvent(evData['organizer']['email'], evData['id'])
            except:
                logging.error(traceback.format_exc())
            event.delete()
        schedule.delete()
Esempio n. 6
0
def processCleanupSchedulingRequest(modelId):
    """
    Delete what got created in Google Calendar, our Events and Schedules.

    Either something went wrong or the user decided to delete an entire
    scheduling request. In either case, this task is set to roll-back and
    delete objects.
    """
    schReq = SchedulingRequest.objects.get(id=modelId)
    for schedule in schReq.schedule_set.all():
        for event in schedule.event_set.all():
            sleepForRateLimit()
            try:
                evData = json.loads(event.json)
                # Getting the calendar ID like this might be fragile. If so,
                # in the future we can add a foreign key from the Event to a
                # Calendar. But for the way we're making calendar events now,
                # jsonData.organizer.email is the calendar ID.
                calendar.deleteEvent(evData['organizer']['email'],
                                     evData['id'])
            except:
                logging.error(traceback.format_exc())
            event.delete()
        schedule.delete()