This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/endpoints/api/robot_models_pre_oci.py
Joseph Schorr 854155fe82 Fix missing to_dict and import in robots model
Also adds a test to catch this issue
2017-08-09 20:33:14 -04:00

87 lines
3.5 KiB
Python

from app import avatar
from data import model
from data.database import User, FederatedLogin, Team as TeamTable, Repository
from endpoints.api.robot_models_interface import (RobotInterface, Robot, RobotWithPermissions, Team,
Permission)
class RobotPreOCIModel(RobotInterface):
def list_robot_permissions(self, username):
permissions = model.permission.list_robot_permissions(username)
return [Permission(permission.repository.name, permission.repository.visibility.name, permission.role.name) for
permission in permissions]
def list_entity_robot_permission_teams(self, prefix, include_permissions=False):
tuples = model.user.list_entity_robot_permission_teams(prefix,
include_permissions=include_permissions)
robots = {}
robot_teams = set()
for robot_tuple in tuples:
robot_name = robot_tuple.get(User.username)
if robot_name not in robots:
robot_dict = {
'name': robot_name,
'token': robot_tuple.get(FederatedLogin.service_ident),
}
if include_permissions:
robot_dict.update({
'teams': [],
'repositories': []
})
robots[robot_name] = Robot(robot_dict['name'], robot_dict['token'])
if include_permissions:
team_name = robot_tuple.get(TeamTable.name)
repository_name = robot_tuple.get(Repository.name)
if team_name is not None:
check_key = robot_name + ':' + team_name
if check_key not in robot_teams:
robot_teams.add(check_key)
robot_dict['teams'].append(Team(
team_name,
avatar.get_data(team_name, team_name, 'team')
))
if repository_name is not None:
if repository_name not in robot_dict['repositories']:
robot_dict['repositories'].append(repository_name)
robots[robot_name] = RobotWithPermissions(robot_dict['name'], robot_dict['token'], robot_dict['teams'],
robot_dict['repositories'])
return robots.values()
def regenerate_user_robot_token(self, robot_shortname, owning_user):
robot, password = model.user.regenerate_robot_token(robot_shortname, owning_user)
return Robot(robot.username, password)
def regenerate_org_robot_token(self, robot_shortname, orgname):
parent = model.organization.get_organization(orgname)
robot, password = model.user.regenerate_robot_token(robot_shortname, parent)
return Robot(robot.username, password)
def delete_robot(self, robot_username):
model.user.delete_robot(robot_username)
def create_user_robot(self, robot_shortname, owning_user):
robot, password = model.user.create_robot(robot_shortname, owning_user)
return Robot(robot.username, password)
def create_org_robot(self, robot_shortname, orgname):
parent = model.organization.get_organization(orgname)
robot, password = model.user.create_robot(robot_shortname, parent)
return Robot(robot.username, password)
def get_org_robot(self, robot_shortname, orgname):
parent = model.organization.get_organization(orgname)
robot, password = model.user.get_robot(robot_shortname, parent)
return Robot(robot.username, password)
def get_user_robot(self, robot_shortname, owning_user):
robot, password = model.user.get_robot(robot_shortname, owning_user)
return Robot(robot.username, password)
pre_oci_model = RobotPreOCIModel()