def post_event(request): ''' Get data for a specific event by ID. This returns the event object, populated by all date range objects ''' try: filtered_request = __user_filter.set_user_id(request) response = __event_service.create_event(filtered_request) except BaseAppException as e: response = HTTPBadRequest() response.text = e.get_payload() response.content_type = 'application/json' return response
def put_event(request): ''' Update an existing date rang in this eventDetails If this eventDetails has existing date ranges, this will update the one with the correspodning composite id. ''' try: json_body = __user_filter.set_user_id(request.json_body) response = __event_service.update_event(json_body) except BaseAppException as e: response = HTTPBadRequest() response.text = e.get_payload() response.content_type = 'application/json' return response
def get_event_attendees(request): ''' Returns all attendees in this event's attendee list /events/{id}/attendees Returns a JSON list of names and email addresses ''' try: filtered_request = __user_filter.set_user_id(request) response = __event_service.add_event_attendee(filtered_request) except BaseAppException as e: response = HTTPBadRequest() response.text = e.get_payload() response.content_type = 'application/json' return response
def delete_event_attendee(request): ''' Remove this attendee from the event. /events/{id}/attendees/{id} Returns 200 if ok. Removes this attendee from the event's attendee list. ''' try: filtered_request = __user_filter.set_user_id(request) response = __attendee_service.remove_attendee_from_event( filtered_request) except BaseAppException as e: response = HTTPBadRequest() response.text = e.get_payload() response.content_type = 'application/json' return response
def post_event_attendees(request): ''' create an attendee and add it to this event /events/{id}/attendees The user making this request is added to the event attendees list. If no ID is found in cookies, a new attendee instance is created and the user id cookie is generated. If a valid user ID cookie is found, it uses the provided user id ''' try: filtered_request = __user_filter.set_user_id(request) response = __event_service.add_event_attendee(filtered_request) except BaseAppException as e: response = HTTPBadRequest() response.text = e.get_payload() response.content_type = 'application/json' return response
def getDataPath(request): try: fileOptions = { 'txt': [('all files','.*'),('txt files', '.txt')], 'csv': [('all files','.*'),('csv files', '.csv')], 'json': [('all files','.*'),('json files', '.json')], 'xlsx': [('all files','.*'),('excel files', '.xlsx')], 'zip': [('all files','.*'),('zip files', '.zip')], 'npz': [('all files','.*'),('numpy files', '.npz')] } fileType = request.json['fileType'] options = { 'filetypes': fileOptions[fileType], # 'initialdir': 'c:\\', 'title': 'Select ' + fileType + ' file' } Tk().withdraw() dataPath = askopenfilename(**options) except IOError as err: response = HTTPBadRequest() response.text = 'getDataPath Error: ' + err.args[0] return response else: if fileType == 'xlsx': wb = openpyxl.load_workbook(dataPath, read_only=True) sheets = {} sheet_names = wb.get_sheet_names() for sheet_name in sheet_names: sheets[sheet_name] = [] ws = wb.get_sheet_by_name(sheet_name) for icol in range(1, ws.max_column + 1): cell_1 = ws.cell(row=1, column=icol) cell_2 = ws.cell(row=2, column=icol) dtype = type(cell_2.value).__name__ if cell_2.value is None: dtype = 'str' sheets[sheet_name].append({'name': cell_1.value.replace('.','_'), 'dtype': dtype}) return {'dataPath': dataPath,'sheets': sheets} else: return {'dataPath': dataPath}
def getDataFields(request): try: fields = [] dtypes = [] fileType = request.json['fileType'] dataPath = request.json['dataPath'] if fileType == 'json': with open(dataPath, 'rb') as datafile: for item in ijson.items(datafile, 'item'): flatdoc = {} ProcessJSON.flattenDoc(item, None, flatdoc) #fields = sorted(list(flatdoc.keys())) fields = list(flatdoc.keys()) dtypes = [] for field in fields: if (type(flatdoc[field]).__name__ == 'Decimal'): dtypes.append('float') else: dtypes.append(type(flatdoc[field]).__name__) break datafile.close() return {'fields': fields, 'dtypes': dtypes} elif fileType == 'csv': delimiter = request.json['delimiter'] headersIdx = request.json['headersIdx'] with open(dataPath) as datafile: csvreader = csv.reader(datafile, delimiter=delimiter, ) if headersIdx != None: for i in range(int(headersIdx)): fields = next(csvreader) else: fields = next(csvreader) datafile.close() return {'fields': fields} except IOError as err: response = HTTPBadRequest() response.text = 'getDataFields Error: ' + err.args[0] return response
def createResponse(self, output, status_code): res = HTTPBadRequest() res.text = json.dumps(output) res.content_type = 'application/json' res.status = status_code return res
def response_from_error(error): response = HTTPBadRequest() msg = 'Evil client is unable to send a proper request. Error is: ' response.text = to_unicode(msg + error.error, 'utf-8') return response