diff --git a/.gitignore b/.gitignore index ee67f42..6cf42ea 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ __pycache__/ # Distribution / packaging .Python +venv/ env/ bin/ build/ diff --git a/elvis/__init__.py b/elvis/__init__.py index 83388b4..cc836df 100644 --- a/elvis/__init__.py +++ b/elvis/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.0.9' +__version__ = '0.0.10' __all__ = ['api', 'enums', 'models'] diff --git a/elvis/api.py b/elvis/api.py index 786d4db..a305780 100644 --- a/elvis/api.py +++ b/elvis/api.py @@ -1,8 +1,11 @@ #coding:utf-8 -from datetime import datetime -from decimal import Decimal +import base64 import json import requests +from datetime import datetime +from decimal import Decimal + +from django.utils.encoding import force_text from .enums import (WarehouseType, AssortmentType, TransportOrderRoleContext, WaybillRoleContext) from .models import (FilterItem, SortItem, Address, ElvisModel, TransportOrderListPage, TransportOrder, TransportOrderStatusInfo, @@ -62,7 +65,7 @@ def __init__(self, api_url, person_code, certificate_pass="", session_token=None def load_cert_from_fieldfile(self, fieldfile): try: fieldfile.open(mode='rb') - self.certificate = fieldfile.read().encode("base64") + self.certificate = force_text(base64.b64encode(fieldfile.read()), 'utf-8') fieldfile.close() except IOError: raise Exception("FieldFile %s is invalid" % fieldfile) @@ -70,19 +73,14 @@ def load_cert_from_fieldfile(self, fieldfile): def load_cert_from_file(self, file_path): try: with open(file_path, "rb") as fHandle: - cert_bytes = fHandle.read().encode("base64") + self.certificate = force_text(base64.b64encode(fHandle.read()), 'utf-8') fHandle.close() - self.certificate = cert_bytes except IOError: raise Exception("File %s is invalid" % file_path) def load_cert_from_str(self, certificate_data): # Note: may need validation here? - - try: - self.certificate = certificate_data.encode("base64") - except (UnicodeEncodeError, UnicodeDecodeError): - raise Exception("String is invalid") + self.certificate = force_text(base64.b64encode(certificate_data), 'utf-8') def load_cert_from_base64str(self, certificate_data): # Note: may need validation here? @@ -177,14 +175,14 @@ def search_warehouses(self, filters, sorting, start=0, limit=10, show_count=Fals if isinstance(filters, FilterItem): filters = (filters, ) - assert not filter(lambda x: not isinstance(x, FilterItem), filters), \ + assert not list(filter(lambda x: not isinstance(x, FilterItem), filters)), \ 'All filters must be instances of FilterItem' assert isinstance(sorting, (list, tuple, SortItem)), 'Sorting must be a list or tuple' if isinstance(sorting, SortItem): sorting = (sorting, ) - assert not filter(lambda x: not isinstance(x, SortItem), sorting), \ + assert not list(filter(lambda x: not isinstance(x, SortItem), sorting)), \ 'All sorting rules must be instances of SortItem' assert filters and sorting, 'Need to add filters and sorting!' @@ -225,14 +223,14 @@ def search_transport_orders(self, context, filters, sorting, start=0, limit=10, if isinstance(filters, FilterItem): filters = (filters, ) - assert not filter(lambda x: not isinstance(x, FilterItem), filters), \ + assert not list(filter(lambda x: not isinstance(x, FilterItem), filters)), \ 'All filters must be instances of FilterItem' assert isinstance(sorting, (list, tuple, SortItem)), 'Sorting must be a list or tuple' if isinstance(sorting, SortItem): sorting = (sorting, ) - assert not filter(lambda x: not isinstance(x, SortItem), sorting), \ + assert not list(filter(lambda x: not isinstance(x, SortItem), sorting)), \ 'All sorting rules must be instances of SortItem' result = self.__request("SearchTransportOrders", "POST", { @@ -397,14 +395,14 @@ def search_waybills(self, context, filters, sorting, start=0, limit=10, show_cou if isinstance(filters, FilterItem): filters = (filters, ) - assert not filter(lambda x: not isinstance(x, FilterItem), filters), \ + assert not list(filter(lambda x: not isinstance(x, FilterItem), filters)), \ 'All filters must be instances of FilterItem' assert isinstance(sorting, (list, tuple, SortItem)), 'Sorting must be a list or tuple' if isinstance(sorting, SortItem): sorting = (sorting, ) - assert not filter(lambda x: not isinstance(x, SortItem), sorting), \ + assert not list(filter(lambda x: not isinstance(x, SortItem), sorting)), \ 'All sorting rules must be instances of SortItem' assert filters and sorting, 'Need to add filters and sorting!' diff --git a/elvis/models.py b/elvis/models.py index dbbc4e5..9453ecb 100644 --- a/elvis/models.py +++ b/elvis/models.py @@ -83,7 +83,7 @@ def __init__(self, **kwargs): if isinstance(packs, Pack): packs = [packs, ] - assert not filter(lambda x: not isinstance(x, Pack), packs) + assert not list(filter(lambda x: not isinstance(x, Pack), packs)) self.Packs = packs self.ExtensionData = None @@ -182,7 +182,7 @@ def __init__(self, **kwargs): if isinstance(additional_properties, AdditionalProperty): additional_properties = [additional_properties, ] - assert not filter(lambda x: not isinstance(x, AdditionalProperty), additional_properties) + assert not list(filter(lambda x: not isinstance(x, AdditionalProperty), additional_properties)) self.AdditionalProperties = additional_properties else: if not isinstance(self.Address, Address): @@ -213,7 +213,7 @@ def __init__(self, **kwargs): if isinstance(timber_batches, TimberBatch): timber_batches = [timber_batches, ] - assert not filter(lambda x: not isinstance(x, TimberBatch), timber_batches) + assert not list(filter(lambda x: not isinstance(x, TimberBatch), timber_batches)) self.TimberBatches = timber_batches self.Type = kwargs.get('warehouse_type') # WarehouseType @@ -256,7 +256,7 @@ def __init__(self, **kwargs): if isinstance(timber_batches, TimberBatch): timber_batches = [timber_batches, ] - assert not filter(lambda x: not isinstance(x, TimberBatch), timber_batches) + assert not list(filter(lambda x: not isinstance(x, TimberBatch), timber_batches)) self.TimberBatches = timber_batches self.ExtensionData = None @@ -350,7 +350,7 @@ def __init__(self, **kwargs): if isinstance(additional_properties, AdditionalProperty): additional_properties = [additional_properties, ] - assert not filter(lambda x: not isinstance(x, AdditionalProperty), additional_properties) + assert not list(filter(lambda x: not isinstance(x, AdditionalProperty), additional_properties)) self.AdditionalProperties = additional_properties else: if not isinstance(self.Address, Address): @@ -441,7 +441,7 @@ def __init__(self, **kwargs): if isinstance(additional_properties, AdditionalProperty): additional_properties = [additional_properties, ] - assert not filter(lambda x: not isinstance(x, AdditionalProperty), additional_properties) + assert not list(filter(lambda x: not isinstance(x, AdditionalProperty), additional_properties)) self.AdditionalProperties = additional_properties else: props = self.AdditionalProperties @@ -506,7 +506,7 @@ def __init__(self, **kwargs): if isinstance(fine_measurements, TimberAssortment): fine_measurements = [fine_measurements, ] - assert not filter(lambda x: not isinstance(x, TimberAssortment), fine_measurements) + assert not list(filter(lambda x: not isinstance(x, TimberAssortment), fine_measurements)) self.FineMeasurements = fine_measurements # Load received assortments @@ -515,7 +515,7 @@ def __init__(self, **kwargs): if isinstance(received_assortments, TimberBatch): received_assortments = [received_assortments, ] - assert not filter(lambda x: not isinstance(x, TimberBatch), received_assortments) + assert not list(filter(lambda x: not isinstance(x, TimberBatch), received_assortments)) self.ReceivedAssortments = received_assortments # Load status change logs @@ -524,7 +524,7 @@ def __init__(self, **kwargs): if isinstance(status_change_logs, WaybillStatusChangeLog): status_change_logs = [status_change_logs, ] - assert not filter(lambda x: not isinstance(x, WaybillStatusChangeLog), status_change_logs) + assert not list(filter(lambda x: not isinstance(x, WaybillStatusChangeLog), status_change_logs)) self.StatusChangeLogs = status_change_logs # Load shipments @@ -533,7 +533,7 @@ def __init__(self, **kwargs): if isinstance(shipments, Shipment): shipments = [shipments, ] - assert not filter(lambda x: not isinstance(x, Shipment), shipments) + assert not list(filter(lambda x: not isinstance(x, Shipment), shipments)) self.Shipments = shipments else: # Timber owner @@ -654,7 +654,7 @@ def __init__(self, **kwargs): if isinstance(items, WaybillListItem): items = [items, ] - assert not filter(lambda x: not isinstance(x, WaybillListItem), items) + assert not list(filter(lambda x: not isinstance(x, WaybillListItem), items)) self.Items = items else: @@ -685,7 +685,7 @@ def __init__(self, **kwargs): if isinstance(items, TransportOrderListItem): items = [items, ] - assert not filter(lambda x: not isinstance(x, TransportOrderListItem), items) + assert not list(filter(lambda x: not isinstance(x, TransportOrderListItem), items)) self.Items = items else: @@ -739,7 +739,7 @@ def __init__(self, **kwargs): transports = [transports, ] assert transports - assert not filter(lambda x: not isinstance(x, Transport), transports) + assert not list(filter(lambda x: not isinstance(x, Transport), transports)) self.Transports = transports else: @@ -813,7 +813,7 @@ def __init__(self, **kwargs): if isinstance(status_change_logs, TransportOrderStatusChangeLog): status_change_logs = [status_change_logs, ] - assert not filter(lambda x: not isinstance(x, TransportOrderStatusChangeLog), status_change_logs) + assert not list(filter(lambda x: not isinstance(x, TransportOrderStatusChangeLog), status_change_logs)) self.StatusChangeLogs = status_change_logs # Load shipments @@ -822,7 +822,7 @@ def __init__(self, **kwargs): if isinstance(shipments, Shipment): shipments = [shipments, ] - assert not filter(lambda x: not isinstance(x, Shipment), shipments) + assert not list(filter(lambda x: not isinstance(x, Shipment), shipments)) self.Shipments = shipments else: # Timber owner diff --git a/setup.py b/setup.py index 5f5cce8..fc1c755 100644 --- a/setup.py +++ b/setup.py @@ -18,6 +18,7 @@ packages=find_packages(), install_requires=[ 'requests', + 'django', ], classifiers=[ 'Development Status :: 4 - Beta', diff --git a/test.py b/test.py index 461fb3e..0ad8d64 100644 --- a/test.py +++ b/test.py @@ -23,26 +23,26 @@ def __init__(self): self.waybill = None def test_authorize(self): - print "Ühenduse loomine ..." + print("Ühenduse loomine ...") assert self.client.authorize() - print "Ühenduse loomine õnnestus" + print("Ühenduse loomine õnnestus") def test_assortment_types(self): - print "Sortimendi tüüpide pärimine (elvise omad) ..." + print("Sortimendi tüüpide pärimine (elvise omad) ...") a_types = self.client.get_assortment_types(AssortmentType.ELVIS) - print "Sortimendi tüüpide pärimine õnnestust (%d vastet)!" % len(a_types) + print("Sortimendi tüüpide pärimine õnnestust (%d vastet)!" % len(a_types)) def test_assortment_types_company(self): - print "Sortimendi tüüpide pärimine (firma omad) ..." + print("Sortimendi tüüpide pärimine (firma omad) ...") a_types = self.client.get_assortment_types(AssortmentType.COMPANY) - print "Sortimendi tüüpide pärimine õnnestust (%d vastet)!" % len(a_types) + print("Sortimendi tüüpide pärimine õnnestust (%d vastet)!" % len(a_types)) def test_add_warehouse(self): - print "Lao lisamine" + print("Lao lisamine") the_id = str(uuid.uuid4()).replace('-', '').lower() @@ -101,33 +101,33 @@ def test_add_warehouse(self): ) self.warehouse_id = self.client.insert_warehouse(warehouse) - print "Lao sisestamine õnnestus! ID: %s" % self.warehouse_id + print("Lao sisestamine õnnestus! ID: %s" % self.warehouse_id) def test_get_warehouse(self): - print "Lao lugemine (number = %d) ..." % self.warehouse_id + print("Lao lugemine (number = %d) ..." % self.warehouse_id) ware = self.client.get_warehouse(self.warehouse_id) - print "Lao lugemine õnnestus %d!" % ware.Id + print("Lao lugemine õnnestus %d!" % ware.Id) def test_search_warehouse(self): - print "Ladude otsimine." + print("Ladude otsimine.") warehouse_search_res = self.client.search_warehouses(FilterItem(WarehouseListItemSearchField.IsPublic, True), SortItem(WarehouseListItemSortField.Name, SortDirection.Asc), 0, 10, True) - print "Ladude ostimine õnnestus! Leiti %d vastet." % warehouse_search_res.get('TotalCount', 0) + print("Ladude ostimine õnnestus! Leiti %d vastet." % warehouse_search_res.get('TotalCount', 0)) def test_delete_warehouse(self): - print "Lao %d kustutamine." % self.warehouse_id + print("Lao %d kustutamine." % self.warehouse_id) if self.client.delete_warehouse(self.warehouse_id) is True: - print "Lao kustutamine õnnestus!" + print("Lao kustutamine õnnestus!") def test_insert_waybill(self): - print "Veoselehe sisestamine..." + print("Veoselehe sisestamine...") timber_batch = TimberBatch( appropriation="TestAppropriation", @@ -329,30 +329,30 @@ def test_insert_waybill(self): self.waybill_id = self.client.insert_waybill(waybill) - print u"Veoselehe sisestamine õnnestus %s!" % self.waybill_id + print("Veoselehe sisestamine õnnestus %s!" % self.waybill_id) def test_get_waybill(self): - print "Veoselehe lugemine (number = %s) ..." % self.waybill_id + print("Veoselehe lugemine (number = %s) ..." % self.waybill_id) self.waybill = self.client.get_waybill(self.waybill_id) - print u"Veoselehe lugemine õnnestus %s!" % self.waybill.Number + print("Veoselehe lugemine õnnestus %s!" % self.waybill.Number) def test_set_waybill_status(self): - print u"Veoselehe (number = %s) staatuse muutmine %d -> %d ..." % (self.waybill.Number, - self.waybill.Status, - WaybillStatus.Unloaded) + print("Veoselehe (number = %s) staatuse muutmine %d -> %d ..." % (self.waybill.Number, + self.waybill.Status, + WaybillStatus.Unloaded)) if self.client.set_waybill_status(self.waybill.Number, WaybillStatus.Unloaded, "Maha laetud.", None, 200, None, self.waybill.Version): - print "Veosele staatuse muutmine õnnestus!" + print("Veosele staatuse muutmine õnnestus!") def test_get_waybill_status(self): - print u"Veoselehe (number = %s) staatuse pärimine..." % self.waybill.Number + print("Veoselehe (number = %s) staatuse pärimine..." % self.waybill.Number) status = self.client.get_waybill_status(self.waybill.Number) - print u"Veoselehe staatuse päring õnnestus, staatus on %s." % status.Status + print("Veoselehe staatuse päring õnnestus, staatus on %s." % status.Status) def test_search_waybills(self): - print "Veoselehtede otsimine ..." + print("Veoselehtede otsimine ...") search_result = self.client.search_waybills( WaybillRoleContext.All, @@ -367,7 +367,8 @@ def test_search_waybills(self): 0, 10, True ) - print "Veoselehtede otsimine õnnestus! (%d tulemust)" % search_result.TotalCount + print("Veoselehtede otsimine õnnestus! (%d tulemust)" % search_result.TotalCount) + if __name__ == '__main__': @@ -391,4 +392,4 @@ def test_search_waybills(self): test.test_search_waybills() - print "Testi lõpp!" + print("Testi lõpp!")