Revision 61524
Added by Sandro La Bruzzo over 2 years ago
responseModel.py | ||
---|---|---|
62 | 62 |
result:List[ScholixType] = [] |
63 | 63 |
|
64 | 64 |
|
65 |
def get_scholix_resource(item): |
|
65 |
def get_scholix_resource(item, pid=None):
|
|
66 | 66 |
title = '' |
67 | 67 |
if 'title' in item: |
68 | 68 |
title = item.title |
... | ... | |
71 | 71 |
title = title[1:-1] |
72 | 72 |
identifier = [dict(ID=x.identifier, IDScheme=x.schema, IDURL=x.url) for x in |
73 | 73 |
item.identifier] |
74 |
identifier = identifier[0] |
|
74 |
|
|
75 |
if pid!= None: |
|
76 |
items = [x for x in identifier if pid == x["ID"]] |
|
77 |
if len(items) >0: |
|
78 |
identifier = items[0] |
|
79 |
else: |
|
80 |
identifier = identifier[0] |
|
81 |
else: |
|
82 |
identifier = identifier[0] |
|
75 | 83 |
creator = [] |
76 | 84 |
if 'creator' in item and item.creator is not None: |
77 | 85 |
creator = [dict(Name=x.name) for x in item.creator] |
... | ... | |
90 | 98 |
return resource |
91 | 99 |
|
92 | 100 |
|
93 |
def convert_response(response): |
|
101 |
def convert_response(response, sourcePid=None, targetPid=None):
|
|
94 | 102 |
now = datetime.now() |
95 | 103 |
log = logging.getLogger("scholexplorer") |
96 | 104 |
start = time() |
... | ... | |
106 | 114 |
current_item['RelationshipType'] = dict(Name=rel_sub_type, SubType=item.relationship.name, |
107 | 115 |
SubTypeSchema=item.relationship.schema) |
108 | 116 |
|
109 |
current_item['source'] = get_scholix_resource(item.source) |
|
110 |
current_item['target'] = get_scholix_resource(item.target) |
|
117 |
current_item['source'] = get_scholix_resource(item.source, sourcePid)
|
|
118 |
current_item['target'] = get_scholix_resource(item.target, targetPid)
|
|
111 | 119 |
|
112 | 120 |
yield current_item |
113 | 121 |
end = time() |
114 | 122 |
log.debug("response converted in {} ms".format(end-start)) |
115 | 123 |
|
116 |
def create_response(response, current_page): |
|
124 |
def create_response(response, current_page, sourcePid=None,targetPid=None):
|
|
117 | 125 |
log = logging.getLogger("scholexplorer") |
118 | 126 |
if current_page > 9999: |
119 | 127 |
raise HTTPException(status_code=400,detail="MAX NUMBER OF PAGE REACHED") |
... | ... | |
123 | 131 |
|
124 | 132 |
result = {'totalLinks': response.hits.total.value, 'currentPage': current_page /100, |
125 | 133 |
'totalPages': 1 + response.hits.total.value / 100, 'result': []} |
126 |
result['result'] = convert_response(response) |
|
134 |
result['result'] = convert_response(response, sourcePid, targetPid)
|
|
127 | 135 |
end = time() |
128 | 136 |
|
129 | 137 |
log.debug("response created in {} ms".format(end-start)) |
Also available in: Unified diff
fixed problem to extract correct identifier in case of multiple ones