diff --git a/CHANGELOG b/CHANGELOG index 9a958f31..c5ce222d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ - Added support for Fulfillment.update_tracking ([#432](https://github.com/Shopify/shopify_python_api/pull/432)) +- Add FulfillmentEvent resource ([#454](https://github.com/Shopify/shopify_python_api/pull/454)) == Version 8.2.0 - [Feature] Add support for Dynamic API Versioning. When the library is initialized, it will now make a request to diff --git a/shopify/resources/__init__.py b/shopify/resources/__init__.py index 8f978535..803c5351 100644 --- a/shopify/resources/__init__.py +++ b/shopify/resources/__init__.py @@ -38,6 +38,7 @@ from .country import Country from .refund import Refund from .fulfillment import Fulfillment, FulfillmentOrders +from .fulfillment_event import FulfillmentEvent from .fulfillment_service import FulfillmentService from .carrier_service import CarrierService from .transaction import Transaction diff --git a/shopify/resources/fulfillment_event.py b/shopify/resources/fulfillment_event.py new file mode 100644 index 00000000..886e4598 --- /dev/null +++ b/shopify/resources/fulfillment_event.py @@ -0,0 +1,21 @@ +from ..base import ShopifyResource + +class FulfillmentEvent(ShopifyResource): + _prefix_source = "/orders/$order_id/fulfillments/$fulfillment_id/" + _singular = 'event' + _plural = 'events' + + @classmethod + def _prefix(cls, options={}): + order_id = options.get("order_id") + fulfillment_id = options.get('fulfillment_id') + event_id = options.get("event_id") + + return "%s/orders/%s/fulfillments/%s" % ( + cls.site, order_id, fulfillment_id) + + def save(self): + status = self.attributes['status'] + if status not in ['label_printed', 'label_purchased', 'attempted_delivery', 'ready_for_pickup', 'picked_up', 'confirmed', 'in_transit', 'out_for_delivery', 'delivered', 'failure']: + raise AttributeError("Invalid status") + return super(ShopifyResource, self).save() diff --git a/test/fixtures/fulfillment_event.json b/test/fixtures/fulfillment_event.json new file mode 100644 index 00000000..7115a158 --- /dev/null +++ b/test/fixtures/fulfillment_event.json @@ -0,0 +1,22 @@ +{ + "fulfillment_event": { + "id": 12584341209251, + "fulfillment_id": 2608403447971, + "status": "label_printed", + "message": null, + "happened_at": "2021-01-25T16:32:23-05:00", + "city": null, + "province": null, + "country": null, + "zip": null, + "address1": null, + "latitude": null, + "longitude": null, + "shop_id": 49144037539, + "created_at": "2021-01-25T16:32:23-05:00", + "updated_at": "2021-01-25T16:32:23-05:00", + "estimated_delivery_at": null, + "order_id": 2776493818019, + "admin_graphql_api_id": "gid://shopify/FulfillmentEvent/12584341209251" + } +} diff --git a/test/fulfillment_event_test.py b/test/fulfillment_event_test.py new file mode 100644 index 00000000..1ba9a73e --- /dev/null +++ b/test/fulfillment_event_test.py @@ -0,0 +1,24 @@ +import shopify +from test.test_helper import TestCase +from pyactiveresource.activeresource import ActiveResource + +class FulFillmentEventTest(TestCase): + def test_get_fulfillment_event(self): + self.fake("orders/2776493818019/fulfillments/2608403447971/events", method='GET', body=self.load_fixture('fulfillment_event')) + fulfillment_event = shopify.FulfillmentEvent.find(order_id=2776493818019, fulfillment_id=2608403447971) + self.assertEqual(1, len(fulfillment_event)) + + def test_create_fulfillment_event(self): + self.fake("orders/2776493818019/fulfillments/2608403447971/events", method='POST', body=self.load_fixture('fulfillment_event'), headers={'Content-type': 'application/json'}) + new_fulfillment_event = shopify.FulfillmentEvent({'order_id': '2776493818019', 'fulfillment_id': '2608403447971'}) + new_fulfillment_event.status = 'ready_for_pickup' + new_fulfillment_event.save() + + def test_error_on_incorrect_status(self): + with self.assertRaises(AttributeError): + self.fake("orders/2776493818019/fulfillments/2608403447971/events/12584341209251", method='GET', body=self.load_fixture('fulfillment_event')) + incorrect_status = 'asdf' + fulfillment_event = shopify.FulfillmentEvent.find(12584341209251, order_id='2776493818019', fulfillment_id='2608403447971') + fulfillment_event.status = incorrect_status + fulfillment_event.save() +