Changeset 326:46fe057f2605

Show
Ignore:
Timestamp:
03/10/10 12:32:00 (5 months ago)
Author:
acracia
Branch:
default
Message:

[api]to_dict method on the model, tailored to use as input of the api

Location:
pdw
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • pdw/model/frbr.py

    r325 r326  
    302302        out_obj.items = items 
    303303        return out_obj 
     304 
    304305    def to_dict(self): 
     306        ''' 
     307        create a dict equivalent to the dict the api is receiving 
     308        ''' 
    305309        out_dict = {} 
    306310        table = orm.class_mapper(self.__class__).mapped_table 
     
    310314            out_dict[col.name] = val 
    311315            # TODO: check type ... 
     316        persons = [] 
    312317        if self.persons: 
    313             persons = [] 
    314318            for person in self.persons: 
     319                #TO DO: change this for a consult to the person table, to get 
     320                #the attrs the person has 
    315321                oneperson = {'name': person.name, 
    316322                             'birth_date': person.birth_date, 
    317323                             'death_date': person.death_date, 
    318                              'type': person.type, 
    319                              'country': person.country,} 
     324                             'type': person.entity_type, 
     325                             } 
    320326                persons.append(oneperson) 
    321327            out_dict['persons'] = persons 
    322  
    323  
    324328        return out_dict 
    325  
    326  
    327329 
    328330 
  • pdw/pd/__init__.py

    r325 r326  
    5858 
    5959now = datetime.datetime.now() 
     60 
    6061def float_date(year, month=0, day=0): 
     62    '''to convert a date in a float type''' 
    6163    return swiss.date.FlexiDate(year, month, day).as_float() 
    6264 
     
    6466    # now dispatch on jurisdiction (+ work type?) 
    6567    # note two letter country codes based on ISO 3166 
    66     #  
     68    # need to add 'when' parameter here and deeper  
    6769    if jurisdiction == 'us': 
    6870        pd_calculator = CalculatorUnitedStates 
     
    8082    To create a Work object and analize its pd status from a  
    8183    python or json dict 
     84    @param  params: json formatted string 
     85    @type   params: string 
    8286    ''' 
    83  
    8487    params = json.loads(json_data) 
    8588    jur = params['jurisdiction']  
     
    108111        return "date_pd=%s pd_prob=%s log=%s" % (self.date_pd, self.pd_prob, self.log) 
    109112 
     113    @classmethod 
     114    def to_dict(cls, self): 
     115        '''Creates a dict result to give back as json in the api 
     116        ''' 
     117        out_dict = { 'confidence': 1-self.uncertainty, 
     118                    'pd probability': self.pd_prob, 
     119                    'date pd': self.date_pd, 
     120                    'log': self.log, 
     121                   } 
     122        return out_dict 
     123#TODO 
     124 
    110125class CalculatorBase(object): 
    111126    """A Public Domain Calculator 
     
    114129    def __init__(self, when): 
    115130        self.author_list = None 
     131        self.death_dates = [] 
     132        self.names = [] 
    116133        if when: 
    117           self._now = when 
     134            self._now = when 
    118135        else: 
    119136            self._now = float_date(datetime.date.today().year) 
  • pdw/tests/functional/test_api.py

    r325 r326  
    1212            'work' : { 
    1313                'title': 'A Christmas Carol'}, 
    14             'jurisdiction': 'netherlands' 
     14            'jurisdiction': 'uk' 
    1515        } 
    1616        params_json = json.dumps(params) 
  • pdw/tests/test_model.py

    r325 r326  
    5858        out = work.to_dict() 
    5959        assert out['title'] == work.title 
    60         assert out['persons'] == work.persons 
     60        assert len(out['persons']) == len(work.persons) 
    6161        assert out['date'] == '1704', out 
    6262        # test it serializes ok 
     
    8181            'title': newtitle, 
    8282            'date': u'1881', 
     83            'type': u'text', 
    8384            'persons': [ 
    8485                { 'name': name1, 'death_date': u'1887' }, 
  • pdw/tests/test_pd.py

    r325 r326  
    418418        calc = pd.determine_status(newwork,'ca') 
    419419        return calc 
     420        assert 'confidence' in calc.log 
    420421 
    421422    def test_uk(self): 
    422423        ''' 
    423         simple parsing of the json to create objects and pass them to the  
    424         calculators / currently uk calculator is working. not Canadian one 
     424        test the calculator api for the UK 
    425425        ''' 
    426426        parsed = json.loads(self.json_data2) 
     
    428428        jurisdiction = parsed['jurisdiction'] 
    429429        newwork = model.Work.from_dict(workdata) 
     430        model.Session.remove() 
    430431        calc = pd.determine_status(newwork,jurisdiction) 
    431432 
    432433        print parsed   
    433434        return calc 
     435        assert calc.log>0 
    434436 
    435437    def test_ca(self): 
    436438        ''' 
    437         simple parsing of the json to create objects and pass them to the  
    438         calculators / currently uk calculator is working. not Canadian one 
     439        test the calculator api for Canada 
    439440 
    440441        ''' 
     
    443444        jurisdiction = parsed['jurisdiction'] 
    444445        newwork = model.Work.from_dict(workdata) 
     446        model.Session.remove() 
    445447        calc = pd.determine_status(newwork,jurisdiction) 
    446448