def static(rel_path): log_info = log.init_log_info(request) logger.debug('Getting static', extra=log_info) try: return static_file(rel_path, root=str(STATIC_PATH)) except Exception as e: logging.exception('Error fetching %s', rel_path, extra=log_info)
def docs(rel_path): log_info = log.init_log_info(request) logger.debug('Getting doc file', extra=log_info) try: return static_file(rel_path, root=str(config.documents_path)) except Exception as e: logging.exception('Error fetching %s', rel_path, extra=log_info)
def api_update_all(): """ Update all projects {'updated_projects': [], 'error': str} """ log_info = log.init_log_info(request) update_info, error = update_all(log_info) if error: response.status = 500 return {'projects': update_info, 'error': None}
def get_projects(): """ {'projects': projects, 'error': error} """ log_info = log.init_log_info(request) search = request.params.get('search') try: projects = [p.json_dict for p in query_projects(search)] error = None except Exception as exc: response.status = 500 logger.exception('Cannot retrieve project list', extra=log_info) projects = [] error = repr(exc) log_info['projects'] = projects return {'projects': projects, 'error': error}
def api_update_project(project_id): """ {'updated': bool, 'error': str} """ log_info = log.init_log_info(request) try: project = model_project.Project.get(project_id) except KeyError: logger.warning('Error updating %s: Project not found', project_id) return {'updated': False, 'error': 'Project {} not found'.format(project_id)} try: updated = update_project(project, log_info) return {'updated': updated, 'error': None} except Exception as e: logger.exception('Error updating %s', project_id) return {'updated': False, 'error': str(e)}
def get_project(project_id: str): """ {'project': project_data, 'error': error} """ log_info = log.init_log_info(request) try: project = model_project.Project.get(project_id) project_data = project.json_dict() logger.info('Successfully Retrieved project', extra=log_info) return {'project': project_data, 'error': None} except KeyError: response.status = 404 logger.warning('Project not found', extra=log_info) error = "'{}' not found".format(project_id) return {'project': None, 'error': error} except Exception as exc: response.status = 500 logger.exception('Cannot retrieve project.', extra=log_info) return {'project': None, 'error': repr(exc)}
def add_project(project_id: str) -> dict: """ {'project_uid': uid, 'error': None} """ log_info = log.init_log_info(request) logger.info('ADDING %s', project_id, extra=log_info) try: project_data = request.json except Exception as exc: logger.exception('Cannot parse json', extra=log_info) return {'project_id': project_id, 'error': repr(exc)} log_info['project_data'] = project_data if project_id != project_data.get('project_id'): response.status = 400 return {'project_id': project_id, 'error': "Project ID doesn't match"} try: project = model_project.Project(**project_data) except TypeError as exc: missing_values = str(exc).split(':')[-1] logger.error('Cannot add project due to missing values:%s', extra=log_info) response.status = 400 return {'project_id': project_id, 'error': "Missing values: {}".format(missing_values)} except Exception as exc: response.status = 500 logger.exception('Cannot add project.') return {'project_id': project_id, 'error': repr(exc)} logger.debug('Adding to Redis') project.save() logger.debug('Added project', extra=log_info) old_docs = config.documents_path / project_id if old_docs.is_dir(): logger.debug('Removing old files') shutil.rmtree(str(old_docs)) return {'project_id': project_id, 'error': None}
def delete_project(project_id: str) -> dict: """ {'project_uid': uid, 'error': error} """ log_info = log.init_log_info(request) logger.info('DELETING %s', project_id, extra=log_info) try: project = model_project.Project.get(project_id) project.delete() error = None except KeyError: error = "'{}' doesn't exist".format(project_id) logger.warning('Cannot delete project because it doesn\'t exist', extra=log_info) response.status = 404 except Exception as e: response.status = 500 logger.exception('Cannot delete project', extra=log_info) error = repr(e) old_docs = config.documents_path / project_id if old_docs.is_dir(): logger.debug('Removing old files') shutil.rmtree(str(old_docs)) return {'project_uid': project_id, 'error': error}
def add_project(): log_info = log.init_log_info(request) logger.info('Add Project Page', extra=log_info) return template('add_project.tpl', is_logged_in=auth.is_logged_in(request))
def do_logout(): log_info = log.init_log_info(request) logger.info('Generating 401', extra=log_info) response.status = 401 response.set_header('Www-Authenticate', 'Basic realm="private"') return 'Ok'
def test_login(): log_info = log.init_log_info(request) logger.info('Login OK', extra=log_info) return 'Ok'
def login(): log_info = log.init_log_info(request) logger.info('Login Page', extra=log_info) return template('login.tpl', is_logged_in=auth.is_logged_in(request))
def index(): log_info = log.init_log_info(request) search = request.params.get('search') projects = sorted(controller_projects.query_projects(search), key=lambda p: p.name) logger.info('Index Page', extra=log_info) return template('index.tpl', projects=projects, search=search, is_logged_in=auth.is_logged_in(request))