Changeset 326:46fe057f2605
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r325
|
r326
|
|
| 302 | 302 | out_obj.items = items |
| 303 | 303 | return out_obj |
| | 304 | |
| 304 | 305 | def to_dict(self): |
| | 306 | ''' |
| | 307 | create a dict equivalent to the dict the api is receiving |
| | 308 | ''' |
| 305 | 309 | out_dict = {} |
| 306 | 310 | table = orm.class_mapper(self.__class__).mapped_table |
| … |
… |
|
| 310 | 314 | out_dict[col.name] = val |
| 311 | 315 | # TODO: check type ... |
| | 316 | persons = [] |
| 312 | 317 | if self.persons: |
| 313 | | persons = [] |
| 314 | 318 | 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 |
| 315 | 321 | oneperson = {'name': person.name, |
| 316 | 322 | 'birth_date': person.birth_date, |
| 317 | 323 | 'death_date': person.death_date, |
| 318 | | 'type': person.type, |
| 319 | | 'country': person.country,} |
| | 324 | 'type': person.entity_type, |
| | 325 | } |
| 320 | 326 | persons.append(oneperson) |
| 321 | 327 | out_dict['persons'] = persons |
| 322 | | |
| 323 | | |
| 324 | 328 | return out_dict |
| 325 | | |
| 326 | | |
| 327 | 329 | |
| 328 | 330 | |
-
|
r325
|
r326
|
|
| 58 | 58 | |
| 59 | 59 | now = datetime.datetime.now() |
| | 60 | |
| 60 | 61 | def float_date(year, month=0, day=0): |
| | 62 | '''to convert a date in a float type''' |
| 61 | 63 | return swiss.date.FlexiDate(year, month, day).as_float() |
| 62 | 64 | |
| … |
… |
|
| 64 | 66 | # now dispatch on jurisdiction (+ work type?) |
| 65 | 67 | # note two letter country codes based on ISO 3166 |
| 66 | | # |
| | 68 | # need to add 'when' parameter here and deeper |
| 67 | 69 | if jurisdiction == 'us': |
| 68 | 70 | pd_calculator = CalculatorUnitedStates |
| … |
… |
|
| 80 | 82 | To create a Work object and analize its pd status from a |
| 81 | 83 | python or json dict |
| | 84 | @param params: json formatted string |
| | 85 | @type params: string |
| 82 | 86 | ''' |
| 83 | | |
| 84 | 87 | params = json.loads(json_data) |
| 85 | 88 | jur = params['jurisdiction'] |
| … |
… |
|
| 108 | 111 | return "date_pd=%s pd_prob=%s log=%s" % (self.date_pd, self.pd_prob, self.log) |
| 109 | 112 | |
| | 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 | |
| 110 | 125 | class CalculatorBase(object): |
| 111 | 126 | """A Public Domain Calculator |
| … |
… |
|
| 114 | 129 | def __init__(self, when): |
| 115 | 130 | self.author_list = None |
| | 131 | self.death_dates = [] |
| | 132 | self.names = [] |
| 116 | 133 | if when: |
| 117 | | self._now = when |
| | 134 | self._now = when |
| 118 | 135 | else: |
| 119 | 136 | self._now = float_date(datetime.date.today().year) |
-
|
r325
|
r326
|
|
| 12 | 12 | 'work' : { |
| 13 | 13 | 'title': 'A Christmas Carol'}, |
| 14 | | 'jurisdiction': 'netherlands' |
| | 14 | 'jurisdiction': 'uk' |
| 15 | 15 | } |
| 16 | 16 | params_json = json.dumps(params) |
-
|
r325
|
r326
|
|
| 58 | 58 | out = work.to_dict() |
| 59 | 59 | assert out['title'] == work.title |
| 60 | | assert out['persons'] == work.persons |
| | 60 | assert len(out['persons']) == len(work.persons) |
| 61 | 61 | assert out['date'] == '1704', out |
| 62 | 62 | # test it serializes ok |
| … |
… |
|
| 81 | 81 | 'title': newtitle, |
| 82 | 82 | 'date': u'1881', |
| | 83 | 'type': u'text', |
| 83 | 84 | 'persons': [ |
| 84 | 85 | { 'name': name1, 'death_date': u'1887' }, |
-
|
r325
|
r326
|
|
| 418 | 418 | calc = pd.determine_status(newwork,'ca') |
| 419 | 419 | return calc |
| | 420 | assert 'confidence' in calc.log |
| 420 | 421 | |
| 421 | 422 | def test_uk(self): |
| 422 | 423 | ''' |
| 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 |
| 425 | 425 | ''' |
| 426 | 426 | parsed = json.loads(self.json_data2) |
| … |
… |
|
| 428 | 428 | jurisdiction = parsed['jurisdiction'] |
| 429 | 429 | newwork = model.Work.from_dict(workdata) |
| | 430 | model.Session.remove() |
| 430 | 431 | calc = pd.determine_status(newwork,jurisdiction) |
| 431 | 432 | |
| 432 | 433 | print parsed |
| 433 | 434 | return calc |
| | 435 | assert calc.log>0 |
| 434 | 436 | |
| 435 | 437 | def test_ca(self): |
| 436 | 438 | ''' |
| 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 |
| 439 | 440 | |
| 440 | 441 | ''' |
| … |
… |
|
| 443 | 444 | jurisdiction = parsed['jurisdiction'] |
| 444 | 445 | newwork = model.Work.from_dict(workdata) |
| | 446 | model.Session.remove() |
| 445 | 447 | calc = pd.determine_status(newwork,jurisdiction) |
| 446 | 448 | |