multiple_domains

pull/17/head
kthouky 2023-08-08 11:54:49 +02:00
parent 459c8afd6c
commit f7ae4cc7b0
2 changed files with 22 additions and 13 deletions

View File

@ -11,22 +11,25 @@ class Approvals(Resource):
@cache.cached(timeout=10, query_string=True) @cache.cached(timeout=10, query_string=True)
@api.marshal_with(models.response_model_model_Whitelist_get, code=200, description='Instances', skip_none=True) @api.marshal_with(models.response_model_model_Whitelist_get, code=200, description='Instances', skip_none=True)
@api.response(404, 'Instance not registered', models.response_model_error) @api.response(404, 'Instance not registered', models.response_model_error)
def get(self, domain): def get(self, domains):
'''Display all endorsements given by a specific domain '''Display all endorsements given out by one or more domains
You can pass a comma-separated list of domain names and the results will be a set of all their
endorsements together.
''' '''
domains_list = domains.split(',')
self.args = self.get_parser.parse_args() self.args = self.get_parser.parse_args()
instance = database.find_instance_by_domain(domain) instances = database.find_multiple_instance_by_domains(domains_list)
if not instance: if not instances:
raise e.NotFound(f"No Instance found matching provided domain. Have you remembered to register it?") raise e.NotFound(f"No Instances found matching any of the provided domains. Have you remembered to register them?")
instance_details = [] instance_details = []
for instance in database.get_all_endorsed_instances_by_approving_id(instance.id): for instance in database.get_all_endorsed_instances_by_approving_id([instance.id for instance in instances]):
instance_details.append(instance.get_details()) instance_details.append(instance.get_details())
if self.args.csv: if self.args.csv:
return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 return {"csv": ",".join([instance["domain"] for instance in instance_details])},200
if self.args.domains: if self.args.domains:
return {"domains": [instance["domain"] for instance in instance_details]},200 return {"domains": [instance["domain"] for instance in instance_details]},200
return {"instances": instance_details},200 return {"instances": instance_details},200
class Endorsements(Resource): class Endorsements(Resource):
get_parser = reqparse.RequestParser() get_parser = reqparse.RequestParser()
get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers")
@ -140,4 +143,4 @@ class Endorsements(Resource):
instance=target_instance, instance=target_instance,
) )
logger.info(f"{instance.domain} Withdrew endorsement from {domain}") logger.info(f"{instance.domain} Withdrew endorsement from {domain}")
return {"message":'Changed'}, 200 return {"message":'Changed'}, 200

View File

@ -35,7 +35,7 @@ def get_all_instances(min_endorsements = 0, min_guarantors = 1):
return query.all() return query.all()
def get_all_endorsed_instances_by_approving_id(approving_id): def get_all_endorsed_instances_by_approving_id(approving_ids):
query = db.session.query( query = db.session.query(
Instance Instance
).outerjoin( ).outerjoin(
@ -43,13 +43,13 @@ def get_all_endorsed_instances_by_approving_id(approving_id):
).options( ).options(
joinedload(Instance.endorsements), joinedload(Instance.endorsements),
).filter( ).filter(
Endorsement.approving_id == approving_id Endorsement.approving_id.in_(approving_ids)
).group_by( ).group_by(
Instance.id Instance.id
) )
return query.all() return query.all()
def get_all_approving_instances_by_endorsed_id(endorsed_id): def get_all_approving_instances_by_endorsed_id(endorsed_ids):
query = db.session.query( query = db.session.query(
Instance Instance
).outerjoin( ).outerjoin(
@ -57,7 +57,7 @@ def get_all_approving_instances_by_endorsed_id(endorsed_id):
).options( ).options(
joinedload(Instance.approvals), joinedload(Instance.approvals),
).filter( ).filter(
Endorsement.endorsed_id == endorsed_id Endorsement.endorsed_id.in_(endorsed_ids)
).group_by( ).group_by(
Instance.id Instance.id
) )
@ -156,6 +156,12 @@ def find_instance_by_domain(domain):
instance = Instance.query.filter_by(domain=domain).first() instance = Instance.query.filter_by(domain=domain).first()
return instance return instance
def find_multiple_instance_by_domains(domains):
instance = Instance.query.filter(
Instance.domain.in_(domains)
).all()
return instance
def find_authenticated_instance(domain,api_key): def find_authenticated_instance(domain,api_key):
instance = Instance.query.join( instance = Instance.query.join(
Claim Claim
@ -247,4 +253,4 @@ def get_instances_by_ids(instance_ids):
query = Instance.query.filter( query = Instance.query.filter(
Instance.id.in_(instance_ids) Instance.id.in_(instance_ids)
) )
return query return query