import logging from flask import request, make_response, jsonify, abort from flask.ext.login import login_required, current_user from functools import wraps from data import model from app import app from util.names import parse_repository_name from auth.permissions import (ReadRepositoryPermission, ModifyRepositoryPermission) logger = logging.getLogger(__name__) @app.route('/api/') def welcome(): return make_response('welcome', 200) @app.route('/api/repository/', methods=['POST']) @login_required def create_repo_api(): pass @app.route('/api/repository/', methods=['GET']) @login_required def list_repos_api(): def repo_view(repo_perm): return { 'namespace': repo_perm.repository.namespace, 'name': repo_perm.repository.name, 'role': repo_perm.role.name, } repos = [repo_view(repo) for repo in model.get_user_repositories(current_user.db_user)] response = { 'repositories': repos } return jsonify(response) @app.route('/api/repository/', methods=['PUT']) @login_required @parse_repository_name def update_repo_api(namespace, repository): permission = ModifyRepositoryPermission(namespace, repository) if permission.can(): repo = model.get_repository(namespace, repository) if repo: values = request.get_json() repo.description = values['description'] repo.save() return jsonify({ 'success': True }) abort(404) @app.route('/api/repository/', methods=['GET']) @login_required @parse_repository_name def get_repo_api(namespace, repository): def image_view(image): return { 'id': image.image_id, 'created': image.created, 'comment': image.comment } def tag_view(tag): image = model.get_tag_image(namespace, repository, tag.name) if not image: return {} return { 'name': tag.name, 'image': image_view(image) } def repo_view(repository, tags = []): tag_dict = {} for tag in tags: tag_dict[tag.name] = tag_view(tag) return { 'namespace': repository.namespace, 'name': repository.name, 'description': repository.description, 'tags': tag_dict, 'can_write': ModifyRepositoryPermission(repository.namespace, repository.name).can() } permission = ReadRepositoryPermission(namespace, repository) if permission.can(): repo = model.get_repository(namespace, repository) if repo: tags = model.list_repository_tags(namespace, repository) return jsonify(repo_view(repo, tags = tags)) abort(404)