Skip to content

Commit

Permalink
Python3 compability
Browse files Browse the repository at this point in the history
Added Django to requirements
  • Loading branch information
Taavi Teska authored and Taavi Teska committed Sep 3, 2014
1 parent 8f4fe61 commit 9f260dd
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ __pycache__/

# Distribution / packaging
.Python
venv/
env/
bin/
build/
Expand Down
2 changes: 1 addition & 1 deletion elvis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '0.0.9'
__version__ = '0.0.10'
__all__ = ['api', 'enums', 'models']
30 changes: 14 additions & 16 deletions elvis/api.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -62,27 +65,22 @@ 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)

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?
Expand Down Expand Up @@ -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!'
Expand Down Expand Up @@ -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", {
Expand Down Expand Up @@ -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!'
Expand Down
30 changes: 15 additions & 15 deletions elvis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
packages=find_packages(),
install_requires=[
'requests',
'django',
],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
55 changes: 28 additions & 27 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand 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__':

Expand All @@ -391,4 +392,4 @@ def test_search_waybills(self):

test.test_search_waybills()

print "Testi lõpp!"
print("Testi lõpp!")

0 comments on commit 9f260dd

Please sign in to comment.