Project

General

Profile

1
from pydantic import BaseModel, Schema
2
from typing import Dict, List
3
from datetime import datetime
4
from fastapi import HTTPException
5
import logging
6
from time import time
7

    
8

    
9

    
10

    
11
class LinkProvider(BaseModel):
12
    name:str = Schema(None, title= "The name of the Provider that provides the links", max_length= 300)
13
    totalRelationships:int=Schema(None, title= "The number of links that It provides")
14

    
15
class LinkPublisher(BaseModel):
16
    name:str = Schema(None, title= "The name of the Publisher that provides the links", max_length= 300)
17
    totalRelationships:int=Schema(None, title= "The number of links that It provides")
18

    
19

    
20
class IdentifierType(BaseModel):
21
    identifier: str = None
22
    schema_str:str = Schema(None, alias="schema")
23
    
24

    
25
class ScholixProviderType(BaseModel):
26
    name:str
27
    identifiers:List[IdentifierType]  =None
28

    
29
class RelationshipType(BaseModel):
30
    name:str
31
    schema_str:str = Schema(None, alias="schema")
32
    inverseRelationship:str = None
33

    
34
class CreatorType(BaseModel):
35
    name: str
36
    identifiers:List[IdentifierType] = None
37
    
38
class ScholixObjectType(BaseModel):
39
    subtype:str = None
40
    type:str = None
41

    
42
class ScholixItemType(BaseModel):
43
    identifiers:List[IdentifierType] =None
44
    title:str = None
45
    objectType:str 
46
    creators:List[CreatorType] =None    
47
    publisher:List[ScholixProviderType] =None 
48
    objectProvider: List[ScholixProviderType] =None  
49

    
50
class ScholixType(BaseModel):
51
    linkProvider:List[ScholixProviderType] =None    
52
    publicationDate:str =None
53
    relationship:RelationshipType=None
54
    source:ScholixItemType=None
55
    target:ScholixItemType=None
56
    
57
def convert_response(response):
58
    log = logging.getLogger("scholexplorer")    
59
    for item in response.hits:
60
        result = item.__dict__['_d_']        
61
        result['linkProvider'] = result.pop('linkprovider')
62
        if 'creator' in result['source']:
63
            result['source']['creators']=result['source'].pop('creator')        
64
        result['source'].pop('objectSubType')
65
        result['source']['identifiers']=result['source'].pop('identifier')
66
        result['source']['objectProvider'] = [s['provider'] for s in result['source'].get('collectedFrom',[])]
67
        if result['source']['publisher'] is not None:
68
            result['source']['publisher'] = [x for x in result['source']['publisher'] if x.get('name') is not None]
69

    
70
        
71
        
72
        if 'creator' in result['target']:
73
            result['target']['creators']=result['target'].pop('creator')
74
        result['target'].pop('objectSubType')
75
        result['target']['identifiers']=result['target'].pop('identifier')
76
        if result['target'].get('publisher') is not None:
77
            result['target']['publisher'] = [x for x in result['target']['publisher'] if x.get('name') is not None]
78
        if result['target'].get('collectedFrom') is not None:
79
            result['target']['objectProvider'] = [s['provider'] for s in result['target'].get('collectedFrom',[])]
80
        else:
81
            result['target']['objectProvider'] = []
82
        yield  result
(3-3/3)