def get_events(self): blob_reader = blobstore.BlobReader(self.blob_key) file_content = blob_reader.read() converted_calendar = icalendar.Calendar.from_ical(file_content) return process_calendar(converted_calendar)
def post(self): if len(self.request.params.multi.dicts) > 1 and 'file' in self.request.params.multi.dicts[1]: file_info = self.request.POST['file'] head, tail = ntpath.split(file_info.filename) full_filename = tail or ntpath.basename(head) file_content = file_info.file.read() file_size = len(file_content) file_hash = hashlib.md5(file_content).hexdigest() try: current_conversion = self.get_conversion_from_hash(file_hash) start_time = time.time() if not current_conversion: # noinspection PyBroadException try: cal = icalendar.Calendar.from_ical(file_content) except Exception, e: cal = None logging.warn('Could not convert "' + full_filename + '".') logging.warn('Storing failed conversion "' + full_filename + '" with hash ' + file_hash + '.') logging.warn(e.message) self.save_file(file_hash, file_content) if cal: current_conversion = conversion.Conversion() events = process_calendar(cal) current_conversion.hash = file_hash current_conversion.full_filename = full_filename current_conversion.filename = drop_extension_from_filename(full_filename) current_conversion.file_size = file_size current_conversion.event_count = len(events) current_conversion.first_ten_events = simplejson.dumps(events[:10]) current_conversion.blob_key = self.save_file(file_hash, file_content) current_conversion.put() first_ten_events = current_conversion.get_first_ten_events() first_ten_events = format_events_for_html(first_ten_events) response = {'message': "Calendar created.", 'paid': not current_conversion.paid_date is None, 'filename': current_conversion.filename, 'full_filename': current_conversion.full_filename, 'event_count': current_conversion.event_count, 'todo_count': current_conversion.todo_count, 'key': current_conversion.hash, 'events': first_ten_events} logging.info('Uploaded "' + current_conversion.full_filename + '" with ' + str(current_conversion.event_count) + ' events.') log_upload(current_conversion, time.time() - start_time) else: # Not a valid iCalendar response = {'message': "That's not a valid iCalendar file.", 'filename': None, 'paid': False, 'key': None} self.response.status = 500 else: filename = drop_extension_from_filename(full_filename) if current_conversion.full_filename != full_filename or current_conversion.filename != filename: current_conversion.full_filename = full_filename current_conversion.filename = filename current_conversion.put() first_ten_events = format_events_for_html(current_conversion.get_first_ten_events()) response = {'message': "Exisiting calendar.", 'paid': not current_conversion.paid_date is None, 'filename': current_conversion.filename, 'full_filename': current_conversion.full_filename, 'event_count': current_conversion.event_count, 'todo_count': current_conversion.todo_count, 'key': current_conversion.hash, 'events': first_ten_events} log_upload(current_conversion, time.time() - start_time)