Skip to content

Commit

Permalink
Merge pull request #16 from seatsio/matti/social-distancing-calls
Browse files Browse the repository at this point in the history
Added social distancing calls
  • Loading branch information
mroloux authored Jun 23, 2020
2 parents 432b6fb + 1574e52 commit de6228f
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 12 deletions.
10 changes: 8 additions & 2 deletions seatsio/charts/chartsClient.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import json

from munch import munchify

from seatsio.charts.chartReports import ChartReports
from seatsio.charts.chartsRequest import ChartRequest
from seatsio.charts.socialDistancingRulesetsRequest import SocialDistancingRulesetsRequest
from seatsio.domain import Chart, ChartValidation
from seatsio.pagination.listableObjectsClient import ListableObjectsClient
from seatsio.pagination.lister import Lister
from seatsio.pagination.pageFetcher import PageFetcher
import json


class ChartsClient(ListableObjectsClient):
Expand Down Expand Up @@ -106,5 +108,9 @@ def validate_published_version(self, key):
return ChartValidation(json.loads(response.text))

def validate_draft_version(self, key):
response = self.http_client.url("/charts/{key}/version/draft/actions/validate", key=key).post()
response = self.http_client.url("/charts/{key}/version/draft/actions/validate", key=key).post()
return ChartValidation(json.loads(response.text))

def save_social_distancing_rulesets(self, key, rulesets):
request = SocialDistancingRulesetsRequest(rulesets)
self.http_client.url("/charts/{key}/social-distancing-rulesets", key=key).post(request)
16 changes: 16 additions & 0 deletions seatsio/charts/socialDistancingRulesetsRequest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class SocialDistancingRulesetsRequest:

def __init__(self, rulesets):
self.socialDistancingRulesets = {k: self.to_json(v) for k, v in rulesets.items()}

def to_json(self, ruleset):
return {
"name": ruleset.name,
"numberOfDisabledSeatsToTheSides": ruleset.number_of_disabled_seats_to_the_sides,
"disableSeatsInFrontAndBehind": ruleset.disable_seats_in_front_and_behind,
"numberOfDisabledAisleSeats": ruleset.number_of_disabled_aisle_seats,
"maxGroupSize": ruleset.max_group_size,
"disabledSeats": ruleset.disabled_seats,
"enabledSeats": ruleset.enabled_seats,
"index": ruleset.index,
}
49 changes: 48 additions & 1 deletion seatsio/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def __init__(self, data):
self.tags = data.get("tags")
self.archived = data.get("archived")
self.validation = data.get("validation")
self.social_distancing_rulesets = {k: SocialDistancingRuleset.create(v) for k, v in
data.get("socialDistancingRulesets").items()}


class ChartValidation:
Expand All @@ -37,6 +39,7 @@ def __init__(self, data):
self.created_on = parse_date(data.get("createdOn"))
self.updated_on = parse_date(data.get("updatedOn"))
self.channels = Channel.createList(data.get("channels"))
self.social_distancing_ruleset_key = data.get("socialDistancingRulesetKey")

@classmethod
def create_list(cls, lst):
Expand All @@ -60,6 +63,7 @@ def create(cls, param):
if param is not None:
return ForSaleConfig(param)


class Channel:
def __init__(self, name, color, index, key=None, objects=None):
self.key = key
Expand All @@ -81,14 +85,57 @@ def __hash__(self):
@classmethod
def create(cls, param):
if param is not None:
return Channel(param.get('name'), param.get('color'), param.get('index'), param.get('key'), param.get('objects'))
return Channel(param.get('name'), param.get('color'), param.get('index'), param.get('key'),
param.get('objects'))

@classmethod
def createList(cls, param):
if param is not None:
return list(map(Channel.create, param))


class SocialDistancingRuleset:
def __init__(self, name, number_of_disabled_seats_to_the_sides=0, disable_seats_in_front_and_behind=False,
number_of_disabled_aisle_seats=0, max_group_size=0, disabled_seats=[], enabled_seats=[], index=0):
self.name = name
self.number_of_disabled_seats_to_the_sides = number_of_disabled_seats_to_the_sides
self.disable_seats_in_front_and_behind = disable_seats_in_front_and_behind
self.number_of_disabled_aisle_seats = number_of_disabled_aisle_seats
self.max_group_size = max_group_size
self.disabled_seats = disabled_seats
self.enabled_seats = enabled_seats
self.index = index

def __eq__(self, other):
return self.name == other.name and \
self.number_of_disabled_seats_to_the_sides == other.number_of_disabled_seats_to_the_sides and \
self.disable_seats_in_front_and_behind == other.disable_seats_in_front_and_behind and \
self.number_of_disabled_aisle_seats == other.number_of_disabled_aisle_seats and \
self.max_group_size == other.max_group_size and \
self.disabled_seats == other.disabled_seats and \
self.enabled_seats == other.enabled_seats and \
self.index == other.index

def __hash__(self):
return hash((self.name, self.number_of_disabled_seats_to_the_sides, self.disable_seats_in_front_and_behind,
self.number_of_disabled_aisle_seats,
self.max_group_size, self.disabled_seats, self.enabled_seats, self.index))

@classmethod
def create(cls, param):
if param is not None:
return SocialDistancingRuleset(
param.get('name'),
param.get('numberOfDisabledSeatsToTheSides'),
param.get('disableSeatsInFrontAndBehind'),
param.get('numberOfDisabledAisleSeats'),
param.get('maxGroupSize'),
param.get('disabledSeats'),
param.get('enabledSeats'),
param.get('index')
)


class ChartReport:
def __init__(self, response_body):
self.items = {}
Expand Down
6 changes: 4 additions & 2 deletions seatsio/events/createSingleEventRequest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class CreateSingleEventRequest:
def __init__(self, chart_key, event_key=None, book_whole_tables=None, table_booking_modes=None):
def __init__(self, chart_key, event_key=None, book_whole_tables=None, table_booking_modes=None, social_distancing_ruleset_key=None):
if chart_key:
self.chartKey = chart_key
if event_key:
self.eventKey = event_key
if book_whole_tables is not None:
self.bookWholeTables = book_whole_tables
if table_booking_modes is not None:
self.tableBookingModes = table_booking_modes
self.tableBookingModes = table_booking_modes
if social_distancing_ruleset_key is not None:
self.socialDistancingRulesetKey = social_distancing_ruleset_key
4 changes: 3 additions & 1 deletion seatsio/events/eventProperties.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class EventProperties:
def __init__(self, event_key=None, book_whole_tables=None, table_booking_modes=None):
def __init__(self, event_key=None, book_whole_tables=None, table_booking_modes=None, social_distancing_ruleset_key=None):
if event_key:
self.eventKey = event_key
if book_whole_tables is not None:
self.bookWholeTables = book_whole_tables
if table_booking_modes is not None:
self.tableBookingModes = table_booking_modes
if social_distancing_ruleset_key is not None:
self.socialDistancingRulesetKey = social_distancing_ruleset_key
8 changes: 4 additions & 4 deletions seatsio/events/eventsClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ def __init__(self, http_client):
ListableObjectsClient.__init__(self, http_client, Event, "/events")
self.reports = EventReports(self.http_client)

def create(self, chart_key, event_key=None, book_whole_tables=None, table_booking_modes=None):
def create(self, chart_key, event_key=None, book_whole_tables=None, table_booking_modes=None, social_distancing_ruleset_key=None):
response = self.http_client.url("/events").post(
CreateSingleEventRequest(chart_key, event_key, book_whole_tables, table_booking_modes))
CreateSingleEventRequest(chart_key, event_key, book_whole_tables, table_booking_modes, social_distancing_ruleset_key))
return Event(response.json())

def create_multiple(self, chart_key, events_properties):
response = self.http_client.url("/events/actions/create-multiple").post(
CreateMultipleEventsRequest(chart_key, events_properties))
return Event.create_list(response.json().get("events"))

def update(self, key, chart_key=None, event_key=None, book_whole_tables=None, table_booking_modes=None):
def update(self, key, chart_key=None, event_key=None, book_whole_tables=None, table_booking_modes=None, social_distancing_ruleset_key=None):
self.http_client.url("/events/{key}", key=key).post(
CreateSingleEventRequest(chart_key, event_key, book_whole_tables, table_booking_modes))
CreateSingleEventRequest(chart_key, event_key, book_whole_tables, table_booking_modes, social_distancing_ruleset_key))

def delete(self, key):
self.http_client.url("/events/{key}", key=key).delete()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='seatsio',
version='v51.2.0',
version='v51.3.0',
description='The official Seats.io Python client library',
author='The seats.io dev team',
author_email='[email protected]',
Expand Down
26 changes: 26 additions & 0 deletions tests/charts/saveSocialDistancingRulesets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from seatsio import SocialDistancingRuleset
from tests.seatsioClientTest import SeatsioClientTest
from tests.util.asserts import assert_that


class SaveSocialDistancingRulesetsTest(SeatsioClientTest):

def test(self):
chart = self.client.charts.create()
rulesets = {
'ruleset1': SocialDistancingRuleset(
name='My first ruleset',
number_of_disabled_seats_to_the_sides=1,
disable_seats_in_front_and_behind=True,
number_of_disabled_aisle_seats=2,
max_group_size=1,
disabled_seats=["A-1"],
enabled_seats=["A-2"],
index=4
),
'ruleset2': SocialDistancingRuleset(name='My second ruleset'),
}
self.client.charts.save_social_distancing_rulesets(chart.key, rulesets)

retrieved_chart = self.client.charts.retrieve(chart.key)
assert_that(retrieved_chart.social_distancing_rulesets).is_equal_to(rulesets)
13 changes: 12 additions & 1 deletion tests/events/createEventTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from seatsio import SocialDistancingRuleset
from tests.seatsioClientTest import SeatsioClientTest
from tests.util.asserts import assert_that

Expand Down Expand Up @@ -43,4 +44,14 @@ def test_table_booking_modes_is_optional(self):

assert_that(event.key).is_not_blank()
assert_that(event.book_whole_tables).is_false()
assert_that(event.table_booking_modes).is_equal_to(table_booking_modes)
assert_that(event.table_booking_modes).is_equal_to(table_booking_modes)

def test_social_distancing_ruleset_key_is_optional(self):
chart_key = self.create_test_chart()
self.client.charts.save_social_distancing_rulesets(chart_key, {
'ruleset1': SocialDistancingRuleset(name='My first ruleset'),
})

event = self.client.events.create(chart_key, social_distancing_ruleset_key='ruleset1')

assert_that(event.social_distancing_ruleset_key).is_equal_to('ruleset1')
16 changes: 16 additions & 0 deletions tests/events/createEventsTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from seatsio import SocialDistancingRuleset
from seatsio.events.eventProperties import EventProperties
from seatsio.exceptions import SeatsioException
from tests.seatsioClientTest import SeatsioClientTest
Expand Down Expand Up @@ -55,6 +56,21 @@ def test_table_booking_modes_can_be_passed_in(self):
{"T1": "BY_SEAT", "T2": "BY_TABLE"}
)

def test_social_distancing_ruleset_key_can_be_passed_in(self):
chart_key = self.create_test_chart()
self.client.charts.save_social_distancing_rulesets(chart_key, {
'ruleset1': SocialDistancingRuleset(name='My first ruleset'),
})

events = self.client.events.create_multiple(chart_key, [
EventProperties(social_distancing_ruleset_key='ruleset1'),
EventProperties(social_distancing_ruleset_key='ruleset1')
])

assert_that(events).extracting("social_distancing_ruleset_key").contains_exactly(
'ruleset1', 'ruleset1'
)

def test_error_on_duplicate_key(self):
try:
chart_key = self.create_test_chart()
Expand Down
26 changes: 26 additions & 0 deletions tests/events/updateEventTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from seatsio import SocialDistancingRuleset
from tests.seatsioClientTest import SeatsioClientTest
from tests.util.asserts import assert_that

Expand Down Expand Up @@ -54,3 +55,28 @@ def test_updateTableBookingModes(self):
retrieved_event = self.client.events.retrieve(event.key)
assert_that(retrieved_event.table_booking_modes).is_equal_to({"T1": "BY_SEAT"})
assert_that(retrieved_event.updated_on).is_between_now_minus_and_plus_minutes(datetime.utcnow(), 1)

def test_updateSocialDistancingRulesetKey(self):
chart_key = self.create_test_chart()
self.client.charts.save_social_distancing_rulesets(chart_key, {
'ruleset1': SocialDistancingRuleset(name='My first ruleset'),
'ruleset2': SocialDistancingRuleset(name='My second ruleset'),
})
event = self.client.events.create(chart_key, social_distancing_ruleset_key='ruleset1')

self.client.events.update(event.key, social_distancing_ruleset_key='ruleset2')

retrieved_event = self.client.events.retrieve(event.key)
assert_that(retrieved_event.social_distancing_ruleset_key).is_equal_to('ruleset2')

def test_removeSocialDistancingRulesetKey(self):
chart_key = self.create_test_chart()
self.client.charts.save_social_distancing_rulesets(chart_key, {
'ruleset1': SocialDistancingRuleset(name='My first ruleset')
})
event = self.client.events.create(chart_key, social_distancing_ruleset_key='ruleset1')

self.client.events.update(event.key, social_distancing_ruleset_key='')

retrieved_event = self.client.events.retrieve(event.key)
assert_that(retrieved_event.social_distancing_ruleset_key).is_none()

0 comments on commit de6228f

Please sign in to comment.