Skip to content

Commit

Permalink
Added original JSON to models, minor bugfix?
Browse files Browse the repository at this point in the history
Updated ``from_json()`` methods to assign the original
JSON object to the model being instantiated

Updated ``from_json`` in ``ticketpy.model.Event`` to
handle missing _name_ key to avoid throwing a KeyError
in a possible edge case
  • Loading branch information
arcward committed Jun 28, 2017
1 parent f407e7f commit 929de7b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ ticketpy.egg-info
*.pyc
*.pickle
ticketpy/__pycache__/
coverage/
27 changes: 18 additions & 9 deletions ticketpy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, number=None, size=None, total_elements=None,
def from_json(json_obj):
"""Instantiate and return a Page(list)"""
pg = Page()
pg.json = json_obj
_assign_links(pg, json_obj, ticketpy.ApiClient.root_url)
pg.number = json_obj['page']['number']
pg.size = json_obj['page']['size']
Expand Down Expand Up @@ -154,6 +155,7 @@ def utc_datetime(self, utc_datetime):
def from_json(json_event):
"""Creates an ``Event`` from API's JSON response"""
e = Event()
e.json = json_event
e.id = json_event['id']
e.name = json_event.get('name')

Expand Down Expand Up @@ -289,8 +291,9 @@ def location(self):
def from_json(json_venue):
"""Returns a ``Venue`` object from JSON"""
v = Venue()
v.id = json_venue['id']
v.name = json_venue['name']
v.json = json_venue
v.id = json_venue.get('id')
v.name = json_venue.get('name')
v.url = json_venue.get('url')
v.postal_code = json_venue.get('postalCode')
v.general_info = json_venue.get('generalInfo')
Expand All @@ -303,16 +306,16 @@ def from_json(json_venue):
v.accessible_seating_detail = json_venue.get('accessibleSeatingDetail')

if 'markets' in json_venue:
v.markets = [m['id'] for m in json_venue['markets']]
v.markets = [m.get('id') for m in json_venue['markets']]
if 'city' in json_venue:
v.city = json_venue['city']['name']
v.city = json_venue['city'].get('name')
if 'address' in json_venue:
v.address = json_venue['address']['line1']
v.address = json_venue['address'].get('line1')
if 'location' in json_venue:
v.latitude = json_venue['location']['latitude']
v.longitude = json_venue['location']['longitude']
v.latitude = json_venue['location'].get('latitude')
v.longitude = json_venue['location'].get('longitude')
if 'state' in json_venue:
v.state_code = json_venue['state']['stateCode']
v.state_code = json_venue['state'].get('stateCode')

_assign_links(v, json_venue)
return v
Expand Down Expand Up @@ -341,6 +344,7 @@ def __init__(self, attraction_id=None, attraction_name=None, url=None,
def from_json(json_obj):
"""Convert JSON object to ``Attraction`` object"""
att = Attraction()
att.json = json_obj
att.id = json_obj.get('id')
att.name = json_obj.get('name')
att.url = json_obj.get('url')
Expand Down Expand Up @@ -377,6 +381,7 @@ def __init__(self, segment=None, classification_type=None, subtype=None,
def from_json(json_obj):
"""Create/return ``Classification`` object from JSON"""
cl = Classification()
cl.json = json_obj
cl.primary = json_obj.get('primary')

if 'segment' in json_obj:
Expand Down Expand Up @@ -413,6 +418,7 @@ def __init__(self, genre=None, subgenre=None, segment=None,
def from_json(json_obj):
"""Create/return ``EventClassification`` object from JSON"""
ec = EventClassification()
ec.json = json_obj
ec.primary = json_obj.get('primary')

segment = json_obj.get('segment')
Expand Down Expand Up @@ -483,8 +489,9 @@ def __init__(self, segment_id=None, segment_name=None, genres=None,
def from_json(json_obj):
"""Create and return a ``Segment`` from JSON"""
seg = Segment()
seg.json = json_obj
seg.id = json_obj['id']
seg.name = json_obj['name']
seg.name = json_obj.get('name')

if '_embedded' in json_obj:
genres = json_obj['_embedded']['genres']
Expand All @@ -511,6 +518,7 @@ def __init__(self, genre_id=None, genre_name=None, subgenres=None,
@staticmethod
def from_json(json_obj):
g = Genre()
g.json = json_obj
g.id = json_obj.get('id')
g.name = json_obj.get('name')
if '_embedded' in json_obj:
Expand All @@ -537,6 +545,7 @@ def __init__(self, subgenre_id=None, subgenre_name=None, links=None):
@staticmethod
def from_json(json_obj):
sg = SubGenre()
sg.json = json_obj
sg.id = json_obj['id']
sg.name = json_obj['name']
_assign_links(sg, json_obj)
Expand Down
7 changes: 0 additions & 7 deletions ticketpy/tests/test_ticketpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,3 @@ def test_all(self):
self.assertListEqual(iter_all, iter_manual)









0 comments on commit 929de7b

Please sign in to comment.