diff options
author | Markus Zapke-Grùˆndemann <info@keimlink.de> | 2012-01-05 13:47:50 +0100 |
---|---|---|
committer | Markus Zapke-Grùˆndemann <info@keimlink.de> | 2012-01-05 13:47:50 +0100 |
commit | 1a558547538f9f910784125193c3d61c7255d8f2 (patch) | |
tree | 72b4f86ff49e24f01b9feb26be3942908fda56d1 | |
parent | 7710c5924134eccd1ee2ad32e96b6ba2a0be6dce (diff) |
Improved logging and added result.
-rw-r--r-- | sublab_project/calendarium/tasks.py | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/sublab_project/calendarium/tasks.py b/sublab_project/calendarium/tasks.py index 9174105..ee35e45 100644 --- a/sublab_project/calendarium/tasks.py +++ b/sublab_project/calendarium/tasks.py @@ -18,17 +18,17 @@ class CalendariumImport(PeriodicTask): run_every = timedelta(hours=1) ignore_result = True - def flush(self): + def flush(self, logger): """Flushes all previously imported events. """ Event.objects.filter(source=Event.SOURCE_IMPORT).delete() - self.logger.info('Flushed all previously imported events.') + logger.debug('Flushed all previously imported events.') - def fetch_calendar(self): + def fetch_calendar(self, logger): """Fetches the calendar events and returns a icalendar.Calendar instance. """ response = urllib2.urlopen(settings.CALENDARIUM_IMPORT_URL) - self.logger.info('Fetched calendar from %s' % settings.CALENDARIUM_IMPORT_URL) + logger.debug('Fetched calendar from %s' % settings.CALENDARIUM_IMPORT_URL) return icalendar.Calendar.from_string(response.read()) def get_events(self, calendar, after, before): @@ -67,16 +67,19 @@ class CalendariumImport(PeriodicTask): def run(self, **kwargs): """Imports all events. """ - self.logger = self.get_logger(**kwargs) + logger = self.get_logger(**kwargs) + event_count = 0 now = datetime.now() after = now - relativedelta(days=1) before = now + relativedelta(months=+2) - self.logger.info('Importing events from %s to %s' % (after, before)) + logger.info('Importing events from %s to %s' % (after, before)) try: - calendar = self.fetch_calendar() - self.flush() + calendar = self.fetch_calendar(logger) + self.flush(logger) for event_info in self.get_events(calendar, after, before): event = Event.objects.create(**event_info) - self.logger.info('Added event "%s".' % event) + event_count += 1 + logger.debug('Added event "%s".' % event) except urllib2.URLError, error: - self.logger.error(error) + logger.error(error) + return 'Imported %d events.' % event_count |