summaryrefslogtreecommitdiffstats
path: root/python/fatcat_web/entity_helpers.py
blob: 8a28deb345ddc5116ac7b62bc05aca471a5dd898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

from flask import abort
from fatcat_client.rest import ApiException
from fatcat_tools.transforms import *
from fatcat_web import api


def generic_get_entity(entity_type, ident):
    try:
        if entity_type == 'container':
            entity = api.get_container(ident)
            if entity.state == "active":
                entity.es = container_to_elasticsearch(entity, force_bool=False)
            return entity
        else:
            raise NotImplementedError
    except ApiException as ae:
        abort(ae.status)

def generic_get_entity_revision(entity_type, revision_id):
    try:
        if entity_type == 'container':
            entity = api.get_container_revision(revision_id)
            return entity
        else:
            raise NotImplementedError
    except ApiException as ae:
        abort(ae.status)

def generic_get_editgroup_entity(editgroup, entity_type, ident):
    if entity_type == 'container':
        edits = editgroup.edits.containers
    else:
        raise NotImplementedError
    revision_id = None
    for e in edits:
        if e.ident == ident:
            revision_id = e.revision
            edit = e
            break
    if not revision_id:
        # couldn't find relevent edit in this editgroup
        abort(404)
    try:
        if entity_type == 'container':
            entity = api.get_container_revision(revision_id)
        else:
            raise NotImplementedError
    except ApiException as ae:
        abort(ae.status)
    entity.ident = ident
    return entity, edit