def generate_pdf_content(events, filename, event_count): path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/pdf_template.html') events = format_events_for_html(events) html_output = template.render(path, {'events': events, 'filename': filename, 'event_count': event_count}) pdf_output = StringIO.StringIO() # convert HTML to PDF pisa_status = pisa.CreatePDF(html_output, dest=pdf_output) if pisa_status.err: logging.error('Failed to covert to pdf') raise pdf_output.seek(0) return pdf_output.getvalue()
def get(self): config = Configuration.get_instance() matches = re.match( r"/(?P<hash>[0-9a-z]+)", self.request.path) if matches: file_hash = matches.group("hash") current_conversion = get_conversion_from_hash(file_hash) if current_conversion: path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/main.html') first_ten_events = current_conversion.get_first_ten_events() first_ten_events = format_events_for_html(first_ten_events) items_view_title = generate_items_title(current_conversion.event_count) self.response.out.write(template.render(path, {'show_file': True, 'stripe_key': config.public_stripe_key, 'paid': current_conversion.paid_date is not None, 'event_count': current_conversion.event_count, 'event_count_minus_ten': current_conversion.event_count - 10, 'key': current_conversion.hash, 'filename': current_conversion.filename, 'full_filename': current_conversion.full_filename, 'events': first_ten_events, 'items_view_title': items_view_title, 'web_debug': config.web_debug})) return path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/error.html') self.response.out.write(template.render(path, {'status': '404', 'message': "We don't have what you're looking for."})) self.response.status = 404
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)
def generate_html_content(events, filename, event_count): path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/html_template.html') events = format_events_for_html(events) return template.render(path, {'events': events, 'filename': filename, 'event_count': event_count})