def apps_get(api_token): logTraffic(API_URL, endpoint='/developer/<api_token>/apps') current_user = access_manager.check_api(api_token, ACC_SEC, CNT_SEC) if current_user is not None: logAccess(API_URL, 'api', '/developer/<api_token>/apps') print(current_user.group) if current_user.group == "developer" or current_user.group == "user": if fk.request.method == 'GET': apps = ApplicationModel.objects(developer=current_user) apps_json = {'total_apps': len(apps), 'apps': []} for application in apps: apps_json['apps'].append(application.extended()) return api_response(200, 'Developer\'s applications', apps_json) else: return api_response( 405, 'Method not allowed', 'This endpoint supports only a GET method.') elif current_user.group == "admin": # An admin is a meta developer. apps = ApplicationModel.objects() apps_json = {'total_apps': len(apps), 'apps': []} for application in apps: apps_json['apps'].append(application.extended()) return api_response(200, 'Developer\'s applications', apps_json) else: return api_response(401, 'Unauthorized access to the API', 'This is not a developer account.') else: return api_response(401, 'Unauthorized access to the API', 'This API token is not authorized.')
def home_experiment_data(): if fk.request.method == 'POST': if fk.request.files: file_obj = fk.request.files['file'] file_name = file_obj.filename print(file_name) dataObject = BytesIO() tmp_csv = tempfile.NamedTemporaryFile(mode="w+", delete=False) content = file_obj.read().decode("UTF8") tmp_csv.write(str(content)) csv_path = tmp_csv.name data = None data = pd.read_csv(str(csv_path)) # print(data) print("Read in pandas") head = Row(index='0') head.value = [col for col in data.columns if col != 'Unnamed: 0'] print(head.value) head.save() print("Head done.") print("Head section loaded...") header = ','.join(head.value) print('%s\n' % header) dataObject.write(str('%s\n' % header).encode("utf-8")) print("Head written to dataframe...") previous_index = len(Row.objects()) for index, row in data.iterrows(): rw = Row(created_at = datetime.datetime.utcnow(),\ bz_integration = str(row['bz_integration']),\ calculations_type = str(row['calculations_type']),\ code = str(row['code']),\ element = str(row['element']),\ exchange = str(row['exchange']),\ extrapolate = float(row['extrapolate']),\ extrapolate_err = float(row['extrapolate_err']),\ k_point = float(row['k-point']),\ pade_order = float(row['pade_order']),\ perc_precisions = float(row['perc_precisions']),\ precision = float(row['precision']),\ properties = str(row['properties']),\ structure = str(row['structure'])) rw.save() oneline = ','.join( [str(rw._data[k]) for k in list(rw._data.keys())]) print(oneline) dataObject.write(str('%s\n' % oneline).encode("utf-8")) print("New data appended to old data...") return api_response(200, 'Push succeed', 'Your file was pushed.') else: return api_response(204, 'Nothing created', 'You must a set file.') return """
def query_precvalue(): if fk.request.method == 'POST': if fk.request.data: print('comes here with {}'.format(fk.request.data)) data = json.loads(fk.request.data) code = data.get("code", None) exchange = data.get("exchange", None) structure = data.get("structure", None) element = data.get("element", None) kpoint = data.get("k-point", None) data = [] for row in RowPrecValue.objects: include = True if code: include = include & (row.code == code) if exchange: include = include & (row.exchange == exchange) if structure: include = include & (row.structure == structure) if element: include = include & (row.element == element) if include: data.append(row.info()) return api_response(200, 'Filtered rows', data) else: print('does not go there') return api_response(200, 'All rows', [row.info() for row in Row.objects()]) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query(): if fk.request.method == 'POST': if fk.request.data: data = json.loads(fk.request.data) code = data.get("code", None) exchange = data.get("exchange", None) structure = data.get("structure", None) element = data.get("element", None) property = data.get("property", None) data = [] for row in Row.objects: include = True if code: incluce = include & (row.code == code) if exchange: incluce = include & (row.exchange == exchange) if structure: incluce = include & (row.structure == structure) if element: incluce = include & (row.element == element) if property: incluce = include & (row.property == property) if include: data.append(row.info()) return api_response(200, 'Filtered rows', data) else: return api_response(200, 'All rows', [row.info() for row in Row.objects()]) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def home_evk_data(): if fk.request.method == 'POST': if fk.request.files: file_obj = fk.request.files['file'] file_name = file_obj.filename print(file_name) dataObject = BytesIO() #head = Row.objects(index='0').first() tmp_csv = tempfile.NamedTemporaryFile(mode="w+", delete=False) content = file_obj.read().decode("UTF8") tmp_csv.write(str(content)) csv_path = tmp_csv.name data = None data = pd.read_csv(str(csv_path)) # print(data) print("Read in pandas") #if head == None: head = RowEVK(index='0') head.value = [col for col in data.columns if col != 'Unnamed: 0'] print(head.value) head.save() print("Head done.") print("Head section loaded...") header = ','.join(head.value) print('%s\n' % header) dataObject.write(str('%s\n' % header).encode("utf-8")) print("Head written to dataframe...") previous_index = len(RowEVK.objects()) for index, row in data.iterrows(): rw = RowEVK(created_at = datetime.datetime.utcnow(),\ code = str(row['code']),\ element = str(row['element']),\ exchange = str(row['exchange']),\ structure = str(row['structure']),\ energy = float(row['energy']),\ volume = float(row['volume']),\ kpoints = float(row['kpoints'])) rw.save() oneline = ','.join( [str(rw._data[k]) for k in list(rw._data.keys())]) print(oneline) dataObject.write(str('%s\n' % oneline).encode("utf-8")) print("New data appended to old data...") dataObject.seek(0) data_merged = pd.read_csv(dataObject) return api_response(200, 'Push succeed', 'Your file was pushed.') else: return api_response(204, 'Nothing created', 'You must a set file.') return """
def apps_get(api_token): logTraffic(API_URL, endpoint='/developer/<api_token>/apps') current_user = access_manager.check_api(api_token, ACC_SEC, CNT_SEC) if current_user is not None: logAccess(API_URL,'api', '/developer/<api_token>/apps') print(current_user.group) if current_user.group == "developer" or current_user.group == "user": if fk.request.method == 'GET': apps = ApplicationModel.objects(developer=current_user) apps_json = {'total_apps':len(apps), 'apps':[]} for application in apps: apps_json['apps'].append(application.extended()) return api_response(200, 'Developer\'s applications', apps_json) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.') elif current_user.group == "admin": # An admin is a meta developer. apps = ApplicationModel.objects() apps_json = {'total_apps':len(apps), 'apps':[]} for application in apps: apps_json['apps'].append(application.extended()) return api_response(200, 'Developer\'s applications', apps_json) else: return api_response(401, 'Unauthorized access to the API', 'This is not a developer account.') else: return api_response(401, 'Unauthorized access to the API', 'This API token is not authorized.')
def query(): if fk.request.method == 'POST': if fk.request.data: data = json.loads(fk.request.data) code = data.get("code", None) exchange = data.get("exchange", None) structure = data.get("structure", None) element = data.get("element", None) property = data.get("property", None) data = [] for row in Row.objects: include = True if code: incluce = include & (row.code == code) if exchange: incluce = include & (row.exchange == exchange) if structure: incluce = include & (row.structure == structure) if element: incluce = include & (row.element == element) if property: incluce = include & (row.property == property) if include: data.append(row.info()) return api_response(200, 'Filtered rows', data) else: return api_response(200, 'All rows', [row.info() for row in Row.objects()]) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def app_search(api_token, app_name): current_user = check_api(api_token) if current_user is not None: if current_user.group == "developer": if fk.request.method == 'GET': apps = ApplicationModel.objects(name__icontains=app_name) apps_dict = {'total_apps': 0, 'apps': []} for application in apps: if application.developer == current_user: apps_dict['apps'].append(application.info()) else: # Only visible apps from other researchers can be searched for. if application.visibile: apps_dict['apps'].append(application.info()) apps_dict['total_apps'] = len(apps_dict['apps']) return api_response( 200, 'Search results for application with name: %s' % app_name, apps_dict) else: return api_response( 405, 'Method not allowed', 'This endpoint supports only a GET method.') else: return api_response(401, 'Unauthorized access to the API', 'This is not a developer account.') else: return api_response(401, 'Unauthorized access to the API', 'This API token is not authorized.')
def attributes(): if fk.request.method == 'GET': attrs = Attribute.objects() data = {} for attr in attrs: data[attr.name] = attr.values return api_response(200, 'Overall attributes', data) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_row_all(): if fk.request.method == 'GET': _rws = RowModel.objects() rws = [] for rw in _rws: rws.append(rw.info()) return api_response(200, 'Rows values', rws) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_single_single(column): if fk.request.method == 'GET': col = ColModel.objects(column=column).first() if col: return api_response(200, 'Column [%s] content'%column, col.info()) else: return api_response(204, 'Nothing found', "No column with that name.") else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_col_all(): if fk.request.method == 'GET': _cols = ColModel.objects() cols = [] for col in _cols: cols.append(col.info()) return api_response(200, 'Columns values', cols) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_desc_single(column): if fk.request.method == 'GET': description = DescModel.objects(column=column).first() if description: return api_response(200, 'Column [%s] description'%column, description.df) else: return api_response(204, 'Nothing found', "No column with that name.") else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_desc_all(): if fk.request.method == 'GET': _descriptions = DescModel.objects() descriptions = [] for desc in _descriptions: descriptions.append(desc.info()) return api_response(200, 'Columns descriptions', descriptions) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_apps(): if fk.request.method == 'GET': apps = ApplicationModel.objects() apps_json = {'total_apps':len(apps), 'apps':[]} for application in apps: apps_json['apps'].append(application.extended()) return api_response(200, 'Applications', apps_json) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_desc_all(): if fk.request.method == 'GET': _descriptions = DescModel.objects() descriptions = [] for desc in _descriptions: descriptions.append(desc.info()) return api_response(200, 'Columns descriptions', descriptions) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_diffs(): logTraffic(endpoint='/admin/diffs') if fk.request.method == 'GET': diffs = DiffModel.objects() diffs_dict = {'total_diffs':len(diffs), 'diffs':[]} for diff in diffs: diffs_dict['diffs'].append(diff.extended()) return api_response(200, 'Diffs list', diffs_dict) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_col_all(): if fk.request.method == 'GET': _cols = ColModel.objects() cols = [] for col in _cols: cols.append(col.info()) return api_response(200, 'Columns values', cols) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_users(): logTraffic(endpoint='/public/users') if fk.request.method == 'GET': users = UserModel.objects() users_dict = {'total_users':len(users), 'users':[]} for user in users: users_dict['users'].append(user.extended()) return api_response(200, 'Users list', users_dict) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_user_show(user_id): logTraffic(endpoint='/public/user/show/<user_id>') if fk.request.method == 'GET': user = UserModel.objects.with_id(user_id) if user == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this user.') else: return api_response(200, 'User %s account'%user.email, user.extended()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_col_dict_bare(): if fk.request.method == 'GET': _cols = ColModel.objects() cols = {} for col in _cols: info = col.info() cols[col.column] = [] return api_response(200, 'Columns as bare dicts', cols) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_diff_show(diff_id): logTraffic(endpoint='/admin/diff/show/<diff_id>') if fk.request.method == 'GET': diff = DiffModel.objects.with_id(diff_id) if diff == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this diff.') else: return api_response(200, 'Diff info', diff.extended()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_app_show(app_id): logTraffic(endpoint='/public/app/show/<app_id>') if fk.request.method == 'GET': app = ApplicationModel.objects.with_id(app_id) if app == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this application.') else: return api_response(200, 'Application %s'%app.name, app.extended()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_files(): logTraffic(endpoint='/admin/files') if fk.request.method == 'GET': files = FileModel.objects() files_dict = {'total_files':len(files), 'files':[]} for _file in files: files_dict['files'].append(_file.extended()) return api_response(200, 'Files list', files_dict) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_file_show(file_id): logTraffic(endpoint='/admin/file/show/<file_id>') if fk.request.method == 'GET': _file = FileModel.objects.with_id(file_id) if _file == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this file.') else: return api_response(200, 'File %s'%_file.name, _file.extended()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_comment_show(comment_id): logTraffic(endpoint='/public/comment/show/<comment_id>') if fk.request.method == 'GET': comment = CommentModel.objects.with_id(comment_id) if comment == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this comment.') else: return api_response(200, 'Project %s info'%comment_id, comment.info()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_record_show(record_id): logTraffic(endpoint='/admin/record/show/<record_id>') if fk.request.method == 'GET': record = RecordModel.objects.with_id(record_id) if record == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this record.') else: return api_response(200, 'Record info', record.extended()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_messages(): logTraffic(endpoint='/admin/messages') if fk.request.method == 'GET': messages = MessageModel.objects() messages_dict = {'total_messages':len(messages), 'messages':[]} for message in messages: messages_dict['messages'].append(message.extended()) return api_response(200, 'Messages list', messages_dict) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_message_show(message_id): logTraffic(endpoint='/admin/message/show/<message_id>') if fk.request.method == 'GET': message = MessageModel.objects.with_id(message_id) if message == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this message.') else: return api_response(200, 'Message %s'%str(message.id), message.extended()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def attributes(): if fk.request.method == 'GET': attrs = Attribute.objects() data = {} for attr in attrs: data[attr.name] = attr.values return api_response(200, 'Overall attributes', data) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_row_all(): if fk.request.method == 'GET': _rws = RowModel.objects() rws = [] for rw in _rws: rws.append(rw.info()) return api_response(200, 'Rows values', rws) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_project_show(project_id): logTraffic(endpoint='/admin/project/show/<project_id>') if fk.request.method == 'GET': project = ProjectModel.objects.with_id(project_id) if project == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this project.') else: return api_response(200, 'Project %s'%project.name, project.extended()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_records(): logTraffic(endpoint='/admin/records') if fk.request.method == 'GET': records = RecordModel.objects() records_dict = {'total_records':len(records), 'records':[]} for record in records: records_dict['records'].append(record.extended()) return api_response(200, 'Records list', records_dict) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_projects(user_id): logTraffic(endpoint='/public/projects') if fk.request.method == 'GET': projects = ProjectModel.objects() projects_dict = {'total_projects':len(projects), 'projects':[]} for project in projects: if project.access == 'public': projects_dict['projects'].append(project.extended()) return api_response(200, 'Projects list', projects_dict) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_col_dict_bare(): if fk.request.method == 'GET': _cols = ColModel.objects() cols = {} for col in _cols: info = col.info() cols[col.column] = [] return api_response(200, 'Columns as bare dicts', cols) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def app_connectivity(app_token): current_app = check_app(app_token) if current_app is not None: logAccess(current_app,'api', '/<app_token>/connectivity') if fk.request.method == 'GET': name = current_app.name if current_app.name != '' and current_app.name != None else 'unknown' return api_response(200, 'Application %s is accessible'%name, current_app.info()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.') else: return api_response(401, 'Unauthorized access to the API', 'This is not an app token.')
def query_desc_single(column): if fk.request.method == 'GET': description = DescModel.objects(column=column).first() if description: return api_response(200, 'Column [%s] description' % column, description.df) else: return api_response(204, 'Nothing found', "No column with that name.") else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def query_single_single(column): if fk.request.method == 'GET': col = ColModel.objects(column=column).first() if col: return api_response(200, 'Column [%s] content' % column, col.info()) else: return api_response(204, 'Nothing found', "No column with that name.") else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_project_envs_head(project_id): logTraffic(endpoint='/admin/project/envs/head') if fk.request.method == 'GET': project = ProjectModel.objects.with_id(project_id) if project == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this project.') else: head = {} if len(project.history) > 0: head = project.history[-1] return api_response(200, 'Project %s environments head'%project.name, head) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_user_projects(user_id): logTraffic(endpoint='/public/user/projects') if fk.request.method == 'GET': user = UserModel.objects.with_id(user_id) if user == None: return api_response(404, 'No user found', 'We could not fetch the user with id:%s.'%user_id) projects = ProjectModel.objects(owner=user) projects_dict = {'total_projects':len(projects), 'projects':[]} for project in projects: projects_dict['projects'].append(project.extended()) return api_response(200, 'Projects list', projects_dict) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_user_profile_show(user_id): logTraffic(endpoint='/public/user/profile/show/<user_id>') if fk.request.method == 'GET': user = UserModel.objects.with_id(user_id) if user == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this user.') else: profile = ProfileModel.objects(user=user).first() if profile == None: return api_response(404, 'User %s profile is empty'%user.email, 'You have to create a profile for this user.') else: return api_response(200, 'User %s profile'%user.email, profile.extended()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def app_connectivity(app_token): current_app = check_app(app_token) if current_app is not None: logAccess(current_app, 'api', '/<app_token>/connectivity') if fk.request.method == 'GET': name = current_app.name if current_app.name != '' and current_app.name != None else 'unknown' return api_response(200, 'Application %s is accessible' % name, current_app.info()) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.') else: return api_response(401, 'Unauthorized access to the API', 'This is not an app token.')
def public_project_download(project_id): logTraffic(endpoint='/admin/project/download/<project_id>') if fk.request.method == 'GET': project = ProjectModel.objects.with_id(project_id) if project == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this project.') else: prepared = prepare_project(project) if prepared[0] == None: return api_response(404, 'Request suggested an empty response', 'Unable to retrieve an environment to download.') else: return fk.send_file(prepared[0], as_attachment=True, attachment_filename=prepared[1], mimetype='application/zip') else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def public_project_records(project_id): logTraffic(endpoint='/admin/project/records/<project_id>') if fk.request.method == 'GET': project = ProjectModel.objects.with_id(project_id) if project == None: return api_response(404, 'Request suggested an empty response', 'Unable to find this project.') else: records = RecordModel.objects(project=project) records_dict = {'total_records':len(records), 'records':[]} for record in records: records_dict['records'].append(record.extended()) return api_response(200, 'Project [%s] Records list'%project.name, records_dict) else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def clear(): if fk.request.method == 'GET': descriptions = DescModel.objects() for desc in descriptions: desc.delete() cols = ColModel.objects() for col in cols: col.delete() rws = RowModel.objects() for rw in rws: rw.delete() return api_response(204, 'Clear done', "Everything is wiped out.") else: return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def apps_get(api_token): current_user = check_api(api_token) if current_user is not None: if current_user.group == "developer": if fk.request.method == 'GET': apps = ApplicationModel.objects(developer=current_user) apps_json = {'total_apps': len(apps), 'apps': []} for application in apps: apps_json['apps'].append(application.extended()) return api_response(200, 'Developer\'s applications', apps_json) else: return api_response( 405, 'Method not allowed', 'This endpoint supports only a GET method.') else: return api_response(401, 'Unauthorized access to the API', 'This is not a developer account.') else: return api_response(401, 'Unauthorized access to the API', 'This API token is not authorized.')
def app_access(api_token, app_id): logTraffic(API_URL, endpoint='/developer/<api_token>/app/access/<app_id>') current_user = access_manager.check_api(api_token, ACC_SEC, CNT_SEC) if current_user is not None: if current_user.group == "developer" or current_user.group == "user" or current_user.group == "admin": logAccess(API_URL, 'api', '/developer/<api_token>/app/access/<app_id>') if fk.request.method == 'GET': app = ApplicationModel.objects.with_id(app_id) if app != None: if app.developer == current_user or current_user.group == "user" or current_user.group == "admin": app_access = AccessModel.application_access(app) name = app.name if app.name != '' and app.name != None else 'unknown' # print name return api_response( 200, 'Application %s access history' % name, app_access) else: return api_response( 405, 'Application access request denied', 'You are not the developer of this application.') else: return api_response(404, 'Request suggested an empty response', 'Unable to find this application.') else: return api_response( 405, 'Method not allowed', 'This endpoint supports only a GET method.') else: return api_response(401, 'Unauthorized access to the API', 'This is not a developer account.') else: return api_response(401, 'Unauthorized access to the API', 'This API token is not authorized.')
def app_access(api_token, app_id): current_user = check_api(api_token) if current_user is not None: if current_user.group == "developer": if fk.request.method == 'GET': app = ApplicationModel.objects.with_id(app_id) if app != None: if app.developer == current_user: app_access = AccessModel.application_access(app) name = app.name if app.name != '' and app.name != None else 'unknown' # print name return api_response( 200, 'Application %s access history' % name, app_access) else: return api_response( 405, 'Application access request denied', 'You are not the developer of this application.') else: return api_response(404, 'Request suggested an empty response', 'Unable to find this application.') else: return api_response( 405, 'Method not allowed', 'This endpoint supports only a GET method.') else: return api_response(401, 'Unauthorized access to the API', 'This is not a developer account.') else: return api_response(401, 'Unauthorized access to the API', 'This API token is not authorized.')
def home_reference_evaluate_data(): if fk.request.method == 'POST': if fk.request.files: file_obj = fk.request.files['file'] file_name = file_obj.filename dataObject = BytesIO() head = RowModel.objects(index='0').first() tmp_csv = tempfile.NamedTemporaryFile(mode="w+", delete=False) content = file_obj.read().decode("UTF8") tmp_csv.write(str(content)) csv_path = tmp_csv.name data = None # with tempfile.TemporaryDirectory() as tmpdirname: # csv_path = '{0}/{1}'.format(tmpdirname, file_name) # # print(csv_path) # with open(csv_path, 'w') as tmp_csv: # content = file_obj.read().decode("UTF8") # # print(content) # tmp_csv.write(str(content)) # with open(csv_path, 'r') as tmp_csv: # print(tmp_csv.read()) # content = file_obj.read() # tmp_csv.write(str(content)) # print("New file witten to tmp...") # print(tmp_csv.name) data = pd.read_csv(str(csv_path)) # print(data) print("Read in pandas") if head == None: head = RowModel(index='0') head.value = [col for col in data.columns] head.save() print("Head done.") print("Head section loaded...") header = ','.join(head.value) print('%s\n' % header) previous_index = len(RowModel.objects()) for index, row in data.iterrows(): values = [] for c in head.value: values.append(row[c]) rw = RowModel(index=str(previous_index + index), value=values) rw.save() print("New data appended to old data...") dataObject.write(str('%s\n' % header).encode("utf-8")) print("Head written to dataframe...") for row in RowModel.objects(): if int(row.index) > 0: oneline = ','.join([str(v) for v in row.value]) dataObject.write(str('%s\n' % oneline).encode("utf-8")) print("New merged content written to dataframe...") dataObject.seek(0) data_merged = pd.read_csv(dataObject) for desc in DescModel.objects(): desc.delete() for col in ColModel.objects(): col.delete() print("Previous decription and columns deleted...") for c in head.value: _desc = data_merged[c].describe() desc = DescModel(column=c) col = ColModel(column=c) col.values = data_merged[c].tolist() col.save() description = {} description['count'] = int(_desc['count']) description['dtype'] = str(_desc.dtype) if description['dtype'] == 'float64': description['mean'] = _desc['mean'] description['std'] = _desc['std'] description['min'] = _desc['min'] description['max'] = _desc['max'] description['25%'] = _desc['min'] description['50%'] = _desc['min'] description['75%'] = _desc['min'] description['histogram'] = data[c].tolist() elif description['dtype'] == 'object': description['unique'] = int(_desc['unique']) description['top'] = _desc['top'] description['freq'] = int(_desc['freq']) description['options'] = data_merged[c].unique().tolist() # print(_desc['top']) elif description['dtype'] == 'datetime64': description['unique'] = int(_desc['unique']) description['options'] = data_merged[c].unique().tolist() description['first'] = _desc['first'] description['last'] = _desc['last'] desc.df = description desc.save() print("New description and columns added...") return api_response(200, 'Push succeed', 'Your file was pushed.') else: return api_response(204, 'Nothing created', 'You must a set file.') return """
def app_logo(api_token, app_id): current_user = check_api(api_token) if current_user is not None: if current_user.group == "developer": logTraffic(endpoint='/<api_token>/application/logo/<app_id>') if fk.request.method == 'GET': app = ApplicationModel.objects.with_id(app_id) if app != None: name = app.name if app.name != '' and app.name != None else 'unknown' logo = app.logo if logo.location == 'local' and 'http://' not in logo.storage: logo_buffer = s3_get_file('logo', logo.storage) if logo_buffer == None: return api_response( 404, 'No logo found', 'We could not fetch the logo at [%s].' % logo.storage) else: return fk.send_file(logo_buffer, attachment_filename=logo.name, mimetype=logo.mimetype) elif logo.location == 'remote': logo_buffer = web_get_file(logo.storage) if logo_buffer != None: return fk.send_file(logo_buffer, attachment_filename=logo.name, mimetype=logo.mimetype) else: logo_buffer = s3_get_file('logo', 'default-logo.png') if logo_buffer == None: return api_response( 404, 'No logo found', 'We could not fetch the logo at %s.' % logo.storage) else: return fk.send_file( logo_buffer, attachment_filename=logo.name, mimetype=logo.mimetype) else: # solve the file situation and return the appropriate one. if 'http://' in logo.storage: logo.location = 'remote' logo.save() logo_buffer = web_get_file(logo.storage) if logo_buffer != None: return fk.send_file( logo_buffer, attachment_filename=logo.name, mimetype=logo.mimetype) else: logo_buffer = s3_get_file( 'logo', 'default-logo.png') if logo_buffer == None: return api_response( 404, 'No logo found', 'We could not fetch the logo at %s.' % logo.storage) else: return fk.send_file( logo_buffer, attachment_filename=logo.name, mimetype=logo.mimetype) else: logo.location = 'local' logo.save() logo_buffer = s3_get_file('logo', logo.storage) if logo_buffer == None: return api_response( 404, 'No logo found', 'We could not fetch the logo at %s.' % logo.storage) else: return fk.send_file( logo_buffer, attachment_filename=logo.name, mimetype=logo.mimetype) else: return api_response(404, 'Request suggested an empty response', 'Unable to find this application.') else: return api_response( 405, 'Method not allowed', 'This endpoint supports only a GET method.') else: return api_response(401, 'Unauthorized access to the API', 'This is not a developer account.') else: return api_response(401, 'Unauthorized access to the API', 'This API token is not authorized.')