def post(self): if self.logged_in: try: if len(self.request.params.multi.dicts) > 1 and 'file' in self.request.params.multi.dicts[1]: file_info = self.request.POST['file'] file_content = file_info.file.read() if file_info.filename.endswith('.json'): events = json_to_events(file_content) if file_info.filename.endswith('.xlsx'): events = excel_to_events(file_content) if file_info.filename.endswith('.xls'): events = excel_to_events(file_content) if file_info.filename.endswith('.csv'): events = csv_to_events(file_content) if file_info.filename.endswith('.ics'): events = ics_to_events(file_content) new_attacks = [] for event in events: new_attack = attack.Attack(parent=self.current_user.key) new_attack.start_time = event['Start'].replace(tzinfo=None) new_attack.duration = event['Duration'] new_attack.comment = event['Comment'] new_attack.start_text = create_start_text(event['Start']) new_attack.duration_text = create_duration_text(event['Duration']) new_attacks.append(new_attack) ndb.put_multi(new_attacks) self.response.out.write(simplejson.dumps({'message': str(len(new_attacks)) + ' attacks uploaded.'})) support_email("List uploaded", str(len(new_attacks)) + " attacks uploaded.") except Exception as e: trace = traceback.format_exc() logging.error(e.message) logging.error(trace) self.response.set_status(500) self.response.out.write(simplejson.dumps({'message': "Upload failed.<br/>Sorry! We're looking at it."})) support_email("Upload failed", "Couldn't upload: " + file_info.filename) else: self.response.out.write(simplejson.dumps({'message': 'Please login before uploading attacks.'}))
def get_example_attacks(): f = open('migraines.json') file_content = f.read() events = json_to_events(file_content) i = -1 last_event = events[-1:][0] most_recent_start = datetime.datetime.now() - datetime.timedelta(45) last_event['Start'] = datetime.datetime(most_recent_start.year, most_recent_start.month, most_recent_start.day, 20, 0, 0) for event in events: event['DurationText'] = create_duration_text(event['Duration']) event['Id'] = i i -= 1 return events
def post(self): if self.logged_in: try: if len(self.request.params.multi.dicts ) > 1 and 'file' in self.request.params.multi.dicts[1]: file_info = self.request.POST['file'] file_content = file_info.file.read() if file_info.filename.endswith('.json'): events = json_to_events(file_content) if file_info.filename.endswith('.xlsx'): events = excel_to_events(file_content) if file_info.filename.endswith('.xls'): events = excel_to_events(file_content) if file_info.filename.endswith('.csv'): events = csv_to_events(file_content) if file_info.filename.endswith('.ics'): events = ics_to_events(file_content) new_attacks = [] for event in events: new_attack = attack.Attack( parent=self.current_user.key) new_attack.start_time = event['Start'].replace( tzinfo=None) new_attack.duration = event['Duration'] new_attack.comment = event['Comment'] new_attack.start_text = create_start_text( event['Start']) new_attack.duration_text = create_duration_text( event['Duration']) new_attacks.append(new_attack) ndb.put_multi(new_attacks) self.response.out.write( simplejson.dumps({ 'message': str(len(new_attacks)) + ' attacks uploaded.' })) support_email("List uploaded", str(len(new_attacks)) + " attacks uploaded.") except Exception as e: trace = traceback.format_exc() logging.error(e.message) logging.error(trace) self.response.set_status(500) self.response.out.write( simplejson.dumps({ 'message': "Upload failed.<br/>Sorry! We're looking at it." })) support_email("Upload failed", "Couldn't upload: " + file_info.filename) else: self.response.out.write( simplejson.dumps( {'message': 'Please login before uploading attacks.'}))