def diff_remove(diff_id): logTraffic(CLOUD_URL, endpoint='/private/diff/remove/<diff_id>') hash_session = basicAuthSession(fk.request) if fk.request.method in ['GET', 'DELETE']: access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/diff/remove/<diff_id>') diff = DiffModel.objects.with_id(diff_id) except: print(str(traceback.print_exc())) return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) if diff is None: return fk.Response('Unable to find this diff.', status.HTTP_404_NOT_FOUND) else: if diff.sender == current_user or diff.targeted == current_user or current_user.group == "admin": diff.delete() logStat(deleted=True, diff=diff) return cloud_response(200, 'Deletion succeeded', 'The diff %s was succesfully deleted.'%diff_id) else: return fk.Response('Unauthorized action on this diff.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Unauthorized action on this diff.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def diff_comment(diff_id): logTraffic(CLOUD_URL, endpoint='/private/diff/comment/<diff_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': caccess_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/diff/comment/<diff_id>') diff = DiffModel.objects.with_id(diff_id) except: print(str(traceback.print_exc())) return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) if diff is None: return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT)) else: if fk.request.data: data = json.loads(fk.request.data) comment = data.get("comment", {}) if len(comment) != 0: diff.comments.append(comment) diff.save() return fk.Response('Diff comment posted', status.HTTP_200_OK) else: return fk.redirect('{0}:{1}/error/?code=400'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=415'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def project_comment(project_id): logTraffic(CLOUD_URL, endpoint='/private/project/comment/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: logAccess(CLOUD_URL, 'cloud', '/private/project/comment/<project_id>') project = ProjectModel.objects.with_id(project_id) if project == None or (project != None and project.access != 'public' and current_user.group != "admin"): return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: if fk.request.data: data = json.loads(fk.request.data) comment = data.get("comment", {}) if len(comment) != 0: project.comments.append(comment) project.save() return fk.Response('Projject comment posted', status.HTTP_200_OK) else: return fk.redirect('{0}:{1}/error/?code=400'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=415'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def file_add(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/file/upload/<record_id>') hash_session = basicAuthSession(fk.request) access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) user_model = access_resp[1] if user_model is None: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: if fk.request.method == 'POST': infos = {} try: logAccess(CLOUD_URL, 'cloud', '/private/record/file/upload/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT)) else: if fk.request.data: file_model = FileModel.objects.get_or_create(created_at=datetime.datetime.utcnow()) infos = json.loads(fk.request.data) relative_path = infos.get("relative_path", "./") group = infos.get("group", "undefined") description = infos.get("description", "") file_model.group = group file_model.description = description if fk.request.files: if fk.request.files['file']: file_obj = fk.request.files['file'] if current_user.quota+file_obj.tell() > 5000000000: return fk.redirect('{0}:{1}/error/?code=403'.format(VIEW_HOST, VIEW_PORT)) else: relative_path = "%s%s"%(relative_path, file_obj.filename) location = str(user_model.id)+"-"+str(record.id)+"_%s"%file_obj.filename try: uploaded = storage_manager.storage_upload_file(file_model, file_obj) if uploaded[0]: file_model.relative_path = relative_path file_model.location = location today = datetime.date.today() (stat, created) = StatModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow()), interval="%s_%s_%s_0_0_0-%s_%s_%s_23_59_59"%(today.year, today.month, today.day, today.year, today.month, today.day), category="storage", periode="daily") if not created: stat.traffic += file_obj.tell() stat.save() file_model.save() return fk.make_response("File uploaded with success.", status.HTTP_200_OK) else: return fk.redirect('{0}:{1}/error/?code=500'.format(VIEW_HOST, VIEW_PORT)) else: file_model.delete() return fk.redirect('{0}:{1}/error/?code=500'.format(VIEW_HOST, VIEW_PORT)) except Exception as e: traceback.print_exc() return fk.redirect('{0}:{1}/error/?code=400'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=400'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def file_remove(file_id): logTraffic(CLOUD_URL, endpoint='/private/record/file/remove/<file_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'DELETE': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) user_model = access_resp[1] if user_model is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/record/file/remove/<file_id>') record_file = FileModel.objects.with_id(file_id) except: print(str(traceback.print_exc())) if record_file is None: return fk.redirect('{0}:{1}/error/?code=204'.format( VIEW_HOST, VIEW_PORT)) else: if record_file.record.project.owner == current_user: storage_manager.delete_record_file(record_file, logStat) return fk.Response('Record file removed', status.HTTP_200_OK) else: return fk.redirect('{0}:{1}/error/?code=401'.format( VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=401'.format( VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format( VIEW_HOST, VIEW_PORT))
def record_comment(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/comment/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/record/comment/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT)) else: # if record.project.owner == current_user or current_user.group == "admin": if fk.request.data: data = json.loads(fk.request.data) comment = data.get("comment", {}) if len(comment) != 0: record.comments.append(comment) record.save() return fk.Response('Projject comment posted', status.HTTP_200_OK) else: return fk.redirect('{0}:{1}/error/?code=400'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=415'.format(VIEW_HOST, VIEW_PORT)) # else: # return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def file_download(file_id): logTraffic(CLOUD_URL, endpoint='/private/record/file/download/<file_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) user_model = access_resp[1] if user_model is None: return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT)) else: try: logAccess(CLOUD_URL, 'cloud', '/private/record/file/download/<file_id>') record_file = FileModel.objects.with_id(file_id) except: print(str(traceback.print_exc())) if record_file is None: return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT)) else: if record_file.record.project.owner == current_user: _file = storage_manager.storage_get_file('file', record_file.storage) return fk.send_file( _file, mimetype=_file.mimetype, as_attachment=True, attachment_filename=_file.name, ) else: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def env_next(project_id): logTraffic(CLOUD_URL, endpoint='/private/env/next/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: logAccess(CLOUD_URL, 'cloud', '/private/env/next/<project_id>') try: project = ProjectModel.objects.with_id(project_id) except: print(str(traceback.print_exc())) if project is None: return fk.Response('Unable to find the referenced project.', status.HTTP_404_NOT_FOUND) else: if fk.request.data: data = json.loads(fk.request.data) try: env = EnvironmentModel(created_at=str(datetime.datetime.utcnow())) application_name = data.get("app", None) if application_name and application_name != '': application = ApplicationModel.objects(name=application_name).first() if application: application.records = application.records + 1 application.save() if str(current_user.id) not in application.users: application.users.append(str(current_user.id)) application.save() env.application = application group = data.get("group", "unknown") system = data.get("system", "undefined") env.group = group env.system = system env.save() version = VersionModel(created_at=str(datetime.datetime.utcnow())) system = data.get("version", "unknown") version.system = system version.save() env.version = version env.save() bundle = BundleModel(created_at=str(datetime.datetime.utcnow())) bundle.scope = 'local' bundle.save() env.bundle = bundle env.save() project.history.append(str(env.id)) project.save() project_content = json.loads(project.summary_json()) project_content["env"] = {"bundle-id":str(bundle.id)} return cloud_response(201, 'Environment successfully created.', project_content) except: print(str(traceback.print_exc())) return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the creation.', status.HTTP_204_NO_CONTENT) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def record_view(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/view/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/record/view/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND) else: if record.project.owner == current_user or record.access == 'public' or current_user.group == "admin": return fk.Response(record.to_json(), mimetype='application/json') else: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def record_comments(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/comments/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/record/comments/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None or (record != None and record.access != 'public'): return fk.redirect('{0}:{1}/error/?code=401'.format( VIEW_HOST, VIEW_PORT)) else: return fk.Response(json.dumps(record.comments, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json') else: return fk.redirect('{0}:{1}/error/?code=401'.format( VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format( VIEW_HOST, VIEW_PORT))
def env_remove(env_id): logTraffic(CLOUD_URL, endpoint='/private/env/remove/<env_id>') hash_session = basicAuthSession(fk.request) if fk.request.method in ['GET', 'DELETE']: access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this environment.', status.HTTP_401_UNAUTHORIZED) else: try: logAccess(CLOUD_URL, 'cloud', '/private/env/remove/<env_id>') env = EnvironmentModel.objects.with_id(env_id) except: print(str(traceback.print_exc())) if env is None: return fk.Response('Unable to find this environment.', status.HTTP_404_NOT_FOUND) else: # result = storage_manager.delete_env_files(env) # if result: # implement project history en removal: project.history.append(str(env.id)) count = 0 for project in ProjectModel.objects(owner=current_user): try: project.history.remove(str(env_id)) project.save() count = count + 1 except: pass if count > 0: env.delete() return cloud_response(200, 'Deletion succeeded', 'The environment %s was succesfully deleted.'%env_id) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def env_view(env_id): logTraffic(CLOUD_URL, endpoint='/private/env/view/<env_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': caccess_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/env/view/<env_id>') env = EnvironmentModel.objects.with_id(env_id) # Make sure user own or used this environment. owned = False for project in ProjectModel.objects(owner=current_user): if str(env.id) in project.history: owned = True break if not owned: env = None except: env = None print(str(traceback.print_exc())) if env is None: return fk.Response('Unable to find this environment.', status.HTTP_404_NOT_FOUND) else: return fk.Response(env.to_json(), mimetype='application/json') else: return fk.Response('Unauthorized action on this environment.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def project_edit(project_id): logTraffic(CLOUD_URL, endpoint='/private/project/edit/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/project/edit/<project_id>') project = ProjectModel.objects.with_id(project_id) if project == None or (project != None and project.owner != current_user and current_user.group != "admin"): return fk.Response('Unauthorized action on this project.', status.HTTP_401_UNAUTHORIZED) else: if fk.request.data: data = json.loads(fk.request.data) try: description = data.get("description", project.description) goals = data.get("goals", project.goals) group = data.get("group", project.group) access = data.get("access", 'unchanged') tags = data.get("tags", ','.join(project.tags)) environment = data.get("environment", {}) project.description = description project.goals = goals project.group = group if access != "unchanged": project.access = access project.tags = tags.split(',') if len(environment) != 0: environment_model = EnvironmentModel.objects.with_id(environment['id']) if environment_model is not None: system = environment.get('system', environment_model.system) version = environment.get('version', environment_model.version) specifics = environment.get('specifics', environment_model.specifics) group = environment.get('group', environment_model.group) remote_bundle = environment.get('bundle', '') environment_model.system = system environment_model.version = version environment_model.specifics = specifics environment_model.group = group if remote_bundle != '' and environment_model.bundle['scope'] != 'local': environment_model.bundle['location'] = remote_bundle environment_model.save() project.save() if access != "unchanged": for record in RecordModel.objects(project=project): record.access = access record.save() return fk.Response('Project updated', status.HTTP_200_OK) except: print(str(traceback.print_exc())) return fk.Response('Failure to process. Contact admin if it persists.', status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the update.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Unauthorized action on this project.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def record_create(project_id): logTraffic(CLOUD_URL, endpoint='/private/record/create/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this endpoint.', status.HTTP_401_UNAUTHORIZED) else: logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/record/create/<project_id>') if current_user.quota >= current_user.max_quota*1024*1024*1024: return fk.Response('You have exceeded your allowed maximum quota.', status.HTTP_401_UNAUTHORIZED) try: project = ProjectModel.objects.with_id(project_id) except: print(str(traceback.print_exc())) if project is None: return fk.Response('Unable to find the referenced project.', status.HTTP_404_NOT_FOUND) else: if project.owner == current_user or current_user.group == "admin": if fk.request.data: data = json.loads(fk.request.data) try: record = RecordModel(created_at=str(datetime.datetime.utcnow()), project=project) tags = data.get("tags", "") rationels = data.get("rationels", "") status = data.get("status", "unknown") content = data.get("content", "no content") access = data.get("access", project.access) record.tags = [tags] record.rationels = [rationels] record.status = status record.access = access record.extend = {"uploaded":content} record.save() if len(project.history) > 0: head = project.history[-1] env = EnvironmentModel.objects.with_id(head) if env: record.environment = env record.save() # project_content = {"project":json.loads(project.summary_json())} # records = [] # for r in RecordModel.objects(project=project): # records.append(r) # project_content["activity"] = {"number":len(records), "records":[{"id":str(record.id), "created":str(record.created_at), "updated":str(record.updated_at), "status":str(record.status)} for record in records]} return cloud_response(201, 'Record successfully created.', json.loads(project.summary_json())) except: print(str(traceback.print_exc())) return fk.Response('Failure to process. Contact admin if it persists.', status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the creation.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def env_create(record_id): logTraffic(CLOUD_URL, endpoint='/private/env/create/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/env/create/<record_id>') try: record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.Response('Unable to find the referenced record.', status.HTTP_404_NOT_FOUND) else: if fk.request.data: data = json.loads(fk.request.data) try: env = EnvironmentModel(created_at=str(datetime.datetime.utcnow())) application_name = data.get("app", None) if application_name and application_name != '': application = ApplicationModel.objects(name=application_name).first() if application: # Maybe not put record increment here. application.records = application.records + 1 application.save() if str(current_user.id) not in application.users: application.users.append(str(current_user.id)) application.save() env.application = application group = data.get("group", "unknown") system = data.get("system", "undefined") env.group = group env.system = system env.save() project = record.project if record.environment: project.history.remove(str(record.environment.id)) record.environment = env record.save() project.history.append(str(env.id)) project.save() return cloud_response(201, 'Environment successfully created.', project.history) except: print(str(traceback.print_exc())) return fk.Response('Failure to process. Contact admin if it persists.', status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the creation.', status.HTTP_204_NO_CONTENT) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def diff_create(): logTraffic(CLOUD_URL, endpoint='/private/diff/create') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this endpoint.', status.HTTP_401_UNAUTHORIZED) else: logAccess(CLOUD_URL, 'cloud', '/private/diff/create') if fk.request.data: data = json.loads(fk.request.data) record_from_id = data.get("record_from", "") record_to_id = data.get("record_to", "") method = data.get("method", "undefined") proposition = data.get("proposition", "undefined") status = data.get("status", "undefined") comments = data.get("comments", []) if record_from_id == "" or record_to_id == "": return cloud_response(400, 'Diff not created.', "Both record from and to be provided.") else: try: record_from = RecordModel.objects.with_id(record_from_id) record_to = RecordModel.objects.with_id(record_to_id) if current_user.group != "admin": if record_from.project.owner == current_user: sender_user = current_user receiver_user = record_to.project.owner else: sender_user = record_to.project.owner receiver_user = record_from.project.owner else: sender_user = record_from.project.owner receiver_user = record_to.project.owner if record_to and record_from: diff = DiffModel(created_at=str(datetime.datetime.utcnow()), sender=sender_user, targeted=receiver_user, record_from=record_from, record_to=record_to) diff.method = method diff.proposition = proposition diff.status = status diff.comments = comments diff.save() return cloud_response(201, 'Diff successfully created.', "The diff was created.") else: return fk.Response('Both record from and to have to exist.', status.HTTP_404_NOT_FOUND) except: return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the creation.', status.HTTP_204_NO_CONTENT) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def project_records(project_name): logTraffic(CLOUD_URL, endpoint='/private/project/record/<project_name>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: logAccess(CLOUD_URL, 'cloud', '/private/project/record/<project_name>') project = ProjectModel.objects(name=project_name).first() if project == None or (project != None and project.owner != current_user and project.access != 'public' and current_user.group != "admin"): return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.Response(project.activity_json(), mimetype='application/json') else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def project_comments(project_id): logTraffic(CLOUD_URL, endpoint='/private/project/comments/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: logAccess(CLOUD_URL, 'cloud', '/private/project/comments/<project_id>') project = ProjectModel.objects.with_id(project_id) if project == None or (project != None and project.access != 'public' and current_user.group != "admin"): return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.Response(json.dumps(project.comments, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json') else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def record_remove(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/remove/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method in ['GET', 'DELETE']: access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: try: logAccess(CLOUD_URL, 'cloud', '/private/record/remove/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND) return fk.redirect('{0}:{1}/error/?code=204'.format( VIEW_HOST, VIEW_PORT)) else: if record.project.owner == current_user or current_user.group == "admin": storage_manager.delete_record_files(record, logStat) logStat(deleted=True, record=record) env_id = None if record.environment: env_id = str(record.environment.id) record.delete() if env_id: try: record.project.history.remove(env_id) record.project.save() except: pass return cloud_response( 200, 'Deletion succeeded', 'The record %s was succesfully deleted.' % record_id) else: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def diff_edit(diff_id): logTraffic(CLOUD_URL, endpoint='/private/diff/edit/<diff_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this diff.', status.HTTP_401_UNAUTHORIZED) else: try: logAccess(CLOUD_URL, 'cloud', '/private/diff/edit/<diff_id>') diff = DiffModel.objects.with_id(diff_id) except: print(str(traceback.print_exc())) if diff is None: return fk.Response('Unable to find this diff.', status.HTTP_404_NOT_FOUND) else: if diff.sender == current_user or diff.targeted == current_user or current_user.group == "admin": if fk.request.data: data = json.loads(fk.request.data) try: d_method = data.get("method", diff.method) proposition = data.get("proposition", diff.proposition) d_status = data.get("status", diff.status) if proposition != diff.proposition or d_method != diff.method: if diff.status == "agreed" or diff.status == "denied": diff.status = "altered" if d_method != "": diff.method = d_method if proposition != "": diff.proposition = proposition if d_status != "": diff.status = d_status diff.save() return fk.Response('Diff edited', status.HTTP_200_OK) except: print(str(traceback.print_exc())) return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the update.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Unauthorized action on this diff.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def record_comments(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/comments/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/record/comments/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None or (record != None and record.access != 'public'): return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.Response(json.dumps(record.comments, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json') else: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def project_remove(project_id): logTraffic(CLOUD_URL, endpoint='/private/project/remove/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method in ['GET', 'DELETE']: access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: logAccess(CLOUD_URL, 'cloud', '/private/project/remove/<project_id>') project = ProjectModel.objects.with_id(project_id) if project == None or (project != None and project.owner != current_user and current_user.group != "admin"): return fk.Response('Unauthorized action on this project.', status.HTTP_401_UNAUTHORIZED) else: storage_manager.delete_project_files(project, logStat) project.delete() logStat(deleted=True, project=project) return cloud_response(200, 'Deletion succeeded', 'The project %s was succesfully deleted.'%project_id) else: return fk.Response('Unauthorized action on this project.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def project_view(project_id): logTraffic(CLOUD_URL, endpoint='/private/project/view/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this project.', status.HTTP_401_UNAUTHORIZED) else: logAccess(CLOUD_URL, 'cloud', '/private/project/view/<project_id>') p = ProjectModel.objects.with_id(project_id) if p == None or (p != None and p.owner != current_user and p.access != 'public' and current_user.group != "admin"): return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: project = {"project":json.loads(p.to_json())} records = RecordModel.objects(project=p) project["activity"] = {"number":len(records), "records":[{"id":str(record.id), "created":str(record.created_at), "updated":str(record.updated_at), "status":str(record.status)} for record in records]} return fk.Response(json.dumps(project, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json') else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def project_sync(project_id): logTraffic(CLOUD_URL, endpoint='/private/project/sync/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/project/sync/<project_id>') p = ProjectModel.objects.with_id(project_id) if p == None or (p != None and p.owner != current_user and p.access != 'public' and current_user.group != "admin"): return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: project = {"project":json.loads(p.summary_json())} records = RecordModel.objects(project=p).order_by('-updated_at') project["activity"] = {"number":len(records), "records":[{"id":str(record.id), "created":str(record.created_at), "updated":str(record.updated_at), "status":str(record.status)} for record in records]} return fk.Response(json.dumps(project, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json') else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def record_comment(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/comment/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/record/comment/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.redirect('{0}:{1}/error/?code=204'.format( VIEW_HOST, VIEW_PORT)) else: # if record.project.owner == current_user or current_user.group == "admin": if fk.request.data: data = json.loads(fk.request.data) comment = data.get("comment", {}) if len(comment) != 0: record.comments.append(comment) record.save() return fk.Response('Projject comment posted', status.HTTP_200_OK) else: return fk.redirect('{0}:{1}/error/?code=400'.format( VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=415'.format( VIEW_HOST, VIEW_PORT)) # else: # return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=401'.format( VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format( VIEW_HOST, VIEW_PORT))
def project_create(): logTraffic(CLOUD_URL, endpoint='/private/project/create') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/project/create') if current_user.quota >= current_user.max_quota*1024*1024*1024: return fk.Response('You have exceeded your allowed maximum quota.', status.HTTP_401_UNAUTHORIZED) if fk.request.data: data = json.loads(fk.request.data) try: name = data.get("name", "") description = data.get("description", "") goals = data.get("goals", "") access = data.get("access", 'public') group = data.get("group", "undefined") tags = data.get("tags", "") environment = data.get("environment", {}) query_project = ProjectModel.objects(owner=current_user, name=name).first() if query_project is None: project = ProjectModel(created_at=str(datetime.datetime.utcnow()), owner=current_user, name=name) project.description = description project.access = access project.goals = goals project.group = group project.tags = [tags] project.save() return cloud_response(201, 'Project successfully created.', project.info()) else: return fk.Response('A project with this name already exists.', status.HTTP_403_FORBIDDEN) except: print(str(traceback.print_exc())) return fk.Response('Failure to process. Contact admin if it persists.', status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the creation.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Unauthorized action on this project.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def diff_view(diff_id): logTraffic(CLOUD_URL, endpoint='/private/diff/view/<diff_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/diff/view/<diff_id>') diff = DiffModel.objects.with_id(diff_id) except: print(str(traceback.print_exc())) return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) if diff is None: return fk.Response('Unable to find this diff.', status.HTTP_404_NOT_FOUND) else: return fk.Response(diff.to_json(), mimetype='application/json') else: return fk.Response('Unauthorized action on this diff.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def env_edit(env_id): logTraffic(CLOUD_URL, endpoint='/private/env/edit/<env_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this environment.', status.HTTP_401_UNAUTHORIZED) else: logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/env/edit/<env_id>') try: env = EnvironmentModel.objects.with_id(env_id) owned = False for project in ProjectModel.objects(owner=current_user): if str(env.id) in project.history: owned = True break if not owned: env = None except: print(str(traceback.print_exc())) if env is None: return fk.Response('Unable to find this environment.', status.HTTP_404_NOT_FOUND) else: if fk.request.data: data = json.loads(fk.request.data) try: group = data.get("group", env.group) system = data.get("system", env.system) env.group = group env.system = system env.save() return fk.Response('Environment edited', status.HTTP_200_OK) except: print(str(traceback.print_exc())) return fk.Response('Failure to process. Contact admin if it persists.', status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the update.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def env_edit(env_id): logTraffic(CLOUD_URL, endpoint='/private/env/edit/<env_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this environment.', status.HTTP_401_UNAUTHORIZED) else: try: env = EnvironmentModel.objects.with_id(env_id) owned = False for project in ProjectModel.objects(owner=current_user): if str(env.id) in project.history: owned = True break if not owned: env = None except: print(str(traceback.print_exc())) if env is None: return fk.Response('Unable to find this environment.', status.HTTP_404_NOT_FOUND) else: if fk.request.data: data = json.loads(fk.request.data) try: group = data.get("group", env.group) system = data.get("system", env.system) env.group = group env.system = system env.save() return fk.Response('Environment edited', status.HTTP_200_OK) except: print(str(traceback.print_exc())) return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the update.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def file_remove(file_id): logTraffic(CLOUD_URL, endpoint='/private/record/file/remove/<file_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'DELETE': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) user_model = access_resp[1] if user_model is not None: try: logAccess(CLOUD_URL, 'cloud', '/private/record/file/remove/<file_id>') record_file = FileModel.objects.with_id(file_id) except: print(str(traceback.print_exc())) if record_file is None: return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT)) else: if record_file.record.project.owner == current_user: storage_manager.delete_record_file(record_file, logStat) return fk.Response('Record file removed', status.HTTP_200_OK) else: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def record_remove(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/remove/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method in ['GET', 'DELETE']: access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: try: logAccess(CLOUD_URL, 'cloud', '/private/record/remove/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND) return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT)) else: if record.project.owner == current_user or current_user.group == "admin": storage_manager.delete_record_files(record, logStat) logStat(deleted=True, record=record) env_id = None if record.environment: env_id = str(record.environment.id) record.delete() if env_id: try: record.project.history.remove(env_id) record.project.save() except: pass return cloud_response(200, 'Deletion succeeded', 'The record %s was succesfully deleted.'%record_id) else: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def file_download(file_id): logTraffic(CLOUD_URL, endpoint='/private/record/file/download/<file_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'GET': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) user_model = access_resp[1] if user_model is None: return fk.redirect('{0}:{1}/error/?code=204'.format( VIEW_HOST, VIEW_PORT)) else: try: logAccess(CLOUD_URL, 'cloud', '/private/record/file/download/<file_id>') record_file = FileModel.objects.with_id(file_id) except: print(str(traceback.print_exc())) if record_file is None: return fk.redirect('{0}:{1}/error/?code=204'.format( VIEW_HOST, VIEW_PORT)) else: if record_file.record.project.owner == current_user: _file = storage_manager.storage_get_file( 'file', record_file.storage) return fk.send_file( _file, mimetype=_file.mimetype, as_attachment=True, attachment_filename=_file.name, ) else: return fk.redirect('{0}:{1}/error/?code=401'.format( VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format( VIEW_HOST, VIEW_PORT))
def file_add(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/file/upload/<record_id>') hash_session = basicAuthSession(fk.request) access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) user_model = access_resp[1] if user_model is None: return fk.redirect('{0}:{1}/error/?code=401'.format( VIEW_HOST, VIEW_PORT)) else: if fk.request.method == 'POST': infos = {} try: logAccess(CLOUD_URL, 'cloud', '/private/record/file/upload/<record_id>') record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.redirect('{0}:{1}/error/?code=204'.format( VIEW_HOST, VIEW_PORT)) else: if fk.request.data: file_model = FileModel.objects.get_or_create( created_at=datetime.datetime.utcnow()) infos = json.loads(fk.request.data) relative_path = infos.get("relative_path", "./") group = infos.get("group", "undefined") description = infos.get("description", "") file_model.group = group file_model.description = description if fk.request.files: if fk.request.files['file']: file_obj = fk.request.files['file'] if current_user.quota + file_obj.tell( ) > 5000000000: return fk.redirect( '{0}:{1}/error/?code=403'.format( VIEW_HOST, VIEW_PORT)) else: relative_path = "%s%s" % (relative_path, file_obj.filename) location = str(user_model.id) + "-" + str( record.id) + "_%s" % file_obj.filename try: uploaded = storage_manager.storage_upload_file( file_model, file_obj) if uploaded[0]: file_model.relative_path = relative_path file_model.location = location today = datetime.date.today() (stat, created ) = StatModel.objects.get_or_create( created_at=str( datetime.datetime.utcnow()), interval= "%s_%s_%s_0_0_0-%s_%s_%s_23_59_59" % (today.year, today.month, today.day, today.year, today.month, today.day), category="storage", periode="daily") if not created: stat.traffic += file_obj.tell() stat.save() file_model.save() return fk.make_response( "File uploaded with success.", status.HTTP_200_OK) else: return fk.redirect( '{0}:{1}/error/?code=500'. format(VIEW_HOST, VIEW_PORT)) else: file_model.delete() return fk.redirect( '{0}:{1}/error/?code=500'.format( VIEW_HOST, VIEW_PORT)) except Exception as e: traceback.print_exc() return fk.redirect( '{0}:{1}/error/?code=400'.format( VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=400'.format( VIEW_HOST, VIEW_PORT)) else: return fk.redirect('{0}:{1}/error/?code=405'.format( VIEW_HOST, VIEW_PORT))
def record_create(project_id): logTraffic(CLOUD_URL, endpoint='/private/record/create/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this endpoint.', status.HTTP_401_UNAUTHORIZED) else: logAccess(CLOUD_URL, 'cloud', '/private/record/create/<project_id>') try: project = ProjectModel.objects.with_id(project_id) except: print(str(traceback.print_exc())) if project is None: return fk.Response('Unable to find the referenced project.', status.HTTP_404_NOT_FOUND) else: if project.owner == current_user or current_user.group == "admin": if fk.request.data: data = json.loads(fk.request.data) try: record = RecordModel(created_at=str( datetime.datetime.utcnow()), project=project) tags = data.get("tags", "") rationels = data.get("rationels", "") status = data.get("status", "unknown") content = data.get("content", "no content") access = data.get("access", "public") record.tags = [tags] record.rationels = [rationels] record.status = status record.access = access record.extend = {"uploaded": content} record.save() if len(project.history) > 0: head = project.history[-1] env = EnvironmentModel.objects.with_id(head) if env: record.environment = env record.save() # project_content = {"project":json.loads(project.summary_json())} # records = [] # for r in RecordModel.objects(project=project): # records.append(r) # project_content["activity"] = {"number":len(records), "records":[{"id":str(record.id), "created":str(record.created_at), "updated":str(record.updated_at), "status":str(record.status)} for record in records]} return cloud_response( 201, 'Record successfully created.', json.loads(project.summary_json())) except: print(str(traceback.print_exc())) return fk.Response( str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response( 'No content provided for the creation.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def env_next(project_id): logTraffic(CLOUD_URL, endpoint='/private/env/next/<project_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT)) else: logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/env/next/<project_id>') if current_user.quota >= current_user.max_quota*1024*1024*1024: return fk.Response('You have exceeded your allowed maximum quota.', status.HTTP_401_UNAUTHORIZED) try: project = ProjectModel.objects.with_id(project_id) except: print(str(traceback.print_exc())) if project is None: return fk.Response('Unable to find the referenced project.', status.HTTP_404_NOT_FOUND) else: if project.owner != current_user: return fk.Response('Unauthorized action on this project.', status.HTTP_401_UNAUTHORIZED) if fk.request.data: data = json.loads(fk.request.data) try: env = EnvironmentModel(created_at=str(datetime.datetime.utcnow())) application_name = data.get("app", None) if application_name and application_name != '': application = ApplicationModel.objects(name=application_name).first() if application: application.records = application.records + 1 application.save() if str(current_user.id) not in application.users: application.users.append(str(current_user.id)) application.save() env.application = application group = data.get("group", "unknown") system = data.get("system", "undefined") env.group = group env.system = system env.save() version = VersionModel(created_at=str(datetime.datetime.utcnow())) system = data.get("version", "unknown") vc_location = data.get("version-location") vc_baselines = vc_location.split("|") if len(vc_baselines) > 0: version.baseline = vc_baselines[0] if len(vc_baselines) > 1: version.marker = vc_baselines[1] version.system = system version.save() env.version = version env.save() bundle = BundleModel(created_at=str(datetime.datetime.utcnow())) scope = data.get("env-location", "unknown") bundle.scope = scope if scope == "remote": bundle.storage = data.get("bundle-location", "unknown") bundle.save() env.bundle = bundle env.save() project.history.append(str(env.id)) project.save() project_content = json.loads(project.summary_json()) project_content["env"] = {"bundle-id":str(bundle.id)} return cloud_response(201, 'Environment successfully created.', project_content) except: print(str(traceback.print_exc())) return fk.Response('Failure to process. Contact admin if it persists.', status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the creation.', status.HTTP_204_NO_CONTENT) else: return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
def record_edit(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/edit/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this endpoint.', status.HTTP_401_UNAUTHORIZED) else: try: record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND) else: if record.project.owner == current_user or current_user.group == "admin": if fk.request.data: data = json.loads(fk.request.data) try: tags = data.get("tags", ','.join(record.tags)) data_pop(data, 'tags') rationels = data.get("rationels", ','.join(record.rationels)) data_pop(data, 'rationels') r_status = data.get("status", record.status) data_pop(data, 'status') r_access = data.get("access", record.access) data_pop(data, 'access') record.tags = tags.split(',') record.rationels = rationels.split(',') record.status = r_status record.access = r_access record.save() body = data.get("body", None) if body: data = body system = data.get("system", record.system) data_pop(data, 'system') execution = data.get("execution", record.execution) data_pop(data, 'execution') inputs = data.get("inputs", record.inputs) data_pop(data, 'inputs') outputs = data.get("outputs", record.outputs) data_pop(data, 'outputs') dependencies = data.get("dependencies", record.dependencies) data_pop(data, 'dependencies') if not isinstance(inputs, list): inputs = [inputs] if not isinstance(outputs, list): outputs = [outputs] if not isinstance(dependencies, list): dependencies = [dependencies] record.system = system record.execution = execution record.inputs = inputs record.outputs = outputs record.dependencies = dependencies record.save() # Allow all the extra keys to go inside body. if len(data) != 0: body, created = RecordBodyModel.objects.get_or_create(head=record) if created: body.data = data else: already = body.data for key, value in data.items(): already[key] = value body.data = already body.save() return fk.Response(record.summary_json(), mimetype='application/json') except: print(str(traceback.print_exc())) return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response('No content provided for the update.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
def record_edit(record_id): logTraffic(CLOUD_URL, endpoint='/private/record/edit/<record_id>') hash_session = basicAuthSession(fk.request) if fk.request.method == 'POST': access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC) current_user = access_resp[1] if current_user is None: return fk.Response('Unauthorized action on this endpoint.', status.HTTP_401_UNAUTHORIZED) else: try: record = RecordModel.objects.with_id(record_id) except: print(str(traceback.print_exc())) if record is None: return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND) else: if record.project.owner == current_user or current_user.group == "admin": if fk.request.data: data = json.loads(fk.request.data) try: tags = data.get("tags", ','.join(record.tags)) data_pop(data, 'tags') rationels = data.get("rationels", ','.join(record.rationels)) data_pop(data, 'rationels') r_status = data.get("status", record.status) data_pop(data, 'status') r_access = data.get("access", record.access) data_pop(data, 'access') record.tags = tags.split(',') record.rationels = rationels.split(',') record.status = r_status record.access = r_access record.save() body = data.get("body", None) if body: data = body system = data.get("system", record.system) data_pop(data, 'system') execution = data.get("execution", record.execution) data_pop(data, 'execution') inputs = data.get("inputs", record.inputs) data_pop(data, 'inputs') outputs = data.get("outputs", record.outputs) data_pop(data, 'outputs') dependencies = data.get("dependencies", record.dependencies) data_pop(data, 'dependencies') if not isinstance(inputs, list): inputs = [inputs] if not isinstance(outputs, list): outputs = [outputs] if not isinstance(dependencies, list): dependencies = [dependencies] record.system = system record.execution = execution record.inputs = inputs record.outputs = outputs record.dependencies = dependencies record.save() # Allow all the extra keys to go inside body. if len(data) != 0: body, created = RecordBodyModel.objects.get_or_create( head=record) if created: body.data = data else: already = body.data for key, value in data.items(): already[key] = value body.data = already body.save() return fk.Response(record.summary_json(), mimetype='application/json') except: print(str(traceback.print_exc())) return fk.Response( str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR) else: return fk.Response( 'No content provided for the update.', status.HTTP_204_NO_CONTENT) else: return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED) else: return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)