Skip to content

Commit

Permalink
Code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
ej2 committed Jan 19, 2017
1 parent c46a51d commit f4ddecd
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 42 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
========

* 0.5.6 (January 18th, 2017)
* Fixed issue downloading PDFs in Python 3

* 0.5.5 (January 4th, 2017)
* Imported QuickBooks objects into __init__.py for easier imports
* Removed duplicate class AttachableRef from deposit.py
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ python-quickbooks

|Build Status| |Coverage Status|


A Python library for accessing the Quickbooks API. Complete rework of
`quickbooks-python`_.

Expand Down
23 changes: 14 additions & 9 deletions quickbooks/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def api_url(self):
return self.api_url_v3

def create_session(self):
if self.consumer_secret and self.consumer_key and self.access_token_secret and self.access_token:
if self.consumer_secret and self.consumer_key \
and self.access_token_secret and self.access_token:
session = OAuth1Session(
self.consumer_key,
self.consumer_secret,
Expand All @@ -138,7 +139,8 @@ def create_session(self):
)
self.session = session
else:
raise QuickbooksException("Quickbooks authenication fields not set. Cannot create session.")
raise QuickbooksException(
"Quickbooks authenication fields not set. Cannot create session.")

return self.session

Expand All @@ -162,14 +164,14 @@ def get_authorize_url(self):
return self.qbService.get_authorize_url(self.request_token)

def get_current_user(self):
'''Get data from the current user endpoint'''
"""Get data from the current user endpoint"""
url = self.current_user_url
result = self.make_request("GET", url)
return result

def get_report(self, report_type, qs=None):
'''Get data from the report endpoint'''
if qs == None:
"""Get data from the report endpoint"""
if qs is None:
qs = {}

url = self.api_url + "/company/{0}/reports/{1}".format(self.company_id, report_type)
Expand Down Expand Up @@ -273,7 +275,8 @@ def make_request(self, request_type, url, request_body=None, content_type='appli
) % (boundary, request_body, boundary, binary_data, boundary)

req = self.session.request(
request_type, url, True, self.company_id, headers=headers, params=params, data=request_body)
request_type, url, True, self.company_id,
headers=headers, params=params, data=request_body)

if req.status_code == httplib.UNAUTHORIZED:
raise AuthorizationException("Application authentication failed", detail=req.text)
Expand Down Expand Up @@ -349,7 +352,8 @@ def batch_operation(self, request_body):
return results

def download_pdf(self, qbbo, item_id):
url = self.api_url + "/company/{0}/{1}/{2}/pdf".format(self.company_id, qbbo.lower(), item_id)
url = self.api_url + "/company/{0}/{1}/{2}/pdf".format(
self.company_id, qbbo.lower(), item_id)

if self.session is None:
self.create_session()
Expand All @@ -363,9 +367,10 @@ def download_pdf(self, qbbo, item_id):

if response.status_code != httplib.OK:
try:
json = response.json()
result = response.json()
except:
raise QuickbooksException("Error reading json response: {0}".format(response.text), 10000)
self.handle_exceptions(json["Fault"])

self.handle_exceptions(result["Fault"])
else:
return response.content
28 changes: 22 additions & 6 deletions quickbooks/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def to_json(self):

def json_filter(self):
"""
filter out properties that have names starting with _ or properties that have a value of None
filter out properties that have names starting with _
or properties that have a value of None
"""
return lambda obj: dict((k, v) for k, v in obj.__dict__.items()
if not k.startswith('_') and getattr(obj, k) is not None)
Expand Down Expand Up @@ -85,23 +86,31 @@ class ListMixin(object):
@classmethod
def all(cls, start_position="", max_results=100, qb=None):
"""
:param max_results: The maximum number of entities that can be returned in a response is 1000.
:param start_position:
:param max_results: The max number of entities that can be returned in a response is 1000.
:param qb:
:return: Returns list
"""
return cls.where("", start_position=start_position, max_results=max_results, qb=qb)

@classmethod
def filter(cls, start_position="", max_results="", qb=None, **kwargs):
"""
:param start_position:
:param max_results:
:param qb:
:param kwargs: field names and values to filter the query
:return: Filtered list
"""
return cls.where(build_where_clause(**kwargs), start_position=start_position, max_results=max_results, qb=qb)
return cls.where(build_where_clause(**kwargs),
start_position=start_position, max_results=max_results, qb=qb)

@classmethod
def choose(cls, choices, field="Id", qb=None):
"""
:param kwargs: field names and values to filter the query
:param choices:
:param field:
:param qb:
:return: Filtered list
"""
return cls.where(build_choose_clause(choices, field), qb=qb)
Expand All @@ -110,6 +119,9 @@ def choose(cls, choices, field="Id", qb=None):
def where(cls, where_clause="", start_position="", max_results="", qb=None):
"""
:param where_clause: QBO SQL where clause (DO NOT include 'WHERE')
:param start_position:
:param max_results:
:param qb:
:return: Returns list filtered by input where_clause
"""
if where_clause:
Expand All @@ -121,14 +133,16 @@ def where(cls, where_clause="", start_position="", max_results="", qb=None):
if max_results:
max_results = " MAXRESULTS " + str(max_results)

select = "SELECT * FROM {0} {1}{2}{3}".format(cls.qbo_object_name, where_clause, start_position, max_results)
select = "SELECT * FROM {0} {1}{2}{3}".format(
cls.qbo_object_name, where_clause, start_position, max_results)

return cls.query(select, qb=qb)

@classmethod
def query(cls, select, qb=None):
"""
:param select: QBO SQL query select statement
:param qb:
:return: Returns list
"""
if not qb:
Expand All @@ -152,4 +166,6 @@ def download_pdf(self, qb=None):
if self.Id and self.Id > 0 and qb is not None:
return qb.download_pdf(self.qbo_object_name, self.Id)
else:
raise QuickbooksException("Cannot download {0} when no Id is assigned or if no quickbooks client is passed in".format(self.qbo_object_name))
raise QuickbooksException(
"Cannot download {0} when no Id is assigned or if no quickbooks client is passed in".format(
self.qbo_object_name))
15 changes: 7 additions & 8 deletions quickbooks/objects/billpayment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def __str__(self):
@python_2_unicode_compatible
class BillPayment(QuickbooksManagedObject, QuickbooksTransactionEntity, LinkedTxnMixin):
"""
QBO definition: A BillPayment entity represents the financial transaction of payment of bills that the
business owner receives from a vendor for goods or services purchased from the vendor. QuickBooks Online
supports bill payments through a credit card or a checking account.
BillPayment.TotalAmt is the total amount associated with this payment. This includes the total of all the
payments from the payment line details. If TotalAmt is greater than the total on the lines being paid,
the overpayment is treated as a credit and exposed as such on the QuickBooks UI. The total amount
cannot be negative.
QBO definition: A BillPayment entity represents the financial transaction of payment
of bills that the business owner receives from a vendor for goods or services purchased
from the vendor. QuickBooks Online supports bill payments through a credit card or a
checking account. BillPayment.TotalAmt is the total amount associated with this payment.
This includes the total of all the payments from the payment line details. If TotalAmt is
greater than the total on the lines being paid, the overpayment is treated as a credit and
exposed as such on the QuickBooks UI. The total amount cannot be negative.
"""

class_dict = {
Expand All @@ -82,7 +82,6 @@ def __init__(self):
self.TotalAmt = 0
self.PrivateNote = ""
self.DocNumber = ""
#self.ProcessBillPayment = False

self.VendorRef = None
self.CheckPayment = None
Expand Down
20 changes: 11 additions & 9 deletions quickbooks/objects/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
@python_2_unicode_compatible
class Item(QuickbooksManagedObject, QuickbooksTransactionEntity):
"""
QBO definition: An item is a thing that your company buys, sells, or re-sells, such as products and services.
An item is shown as a line on an invoice or other sales form. The Item.Type attribute, which specifies how
the item is used, has one of the following values:
QBO definition: An item is a thing that your company buys, sells, or re-sells,
such as products and services. An item is shown as a line on an invoice or other sales
form. The Item.Type attribute, which specifies how the item is used, has one of
the following values:
Inventory - This type tracks merchandise that your business purchases, stocks, and re-sells as inventory.
QuickBooks tracks the current number of inventory items in stock, cost of goods sold, and the asset value of
the inventory after the purchase and sale of every item.
Inventory - This type tracks merchandise that your business purchases, stocks,
and re-sells as inventory. QuickBooks tracks the current number of inventory items in stock,
cost of goods sold, and the asset value of the inventory after the purchase and sale
of every item.
Service - This type tracks services that you charge on the purchase and tracks merchandise you sell and buy that
is not tracked as inventory. For example, specialized labor, consulting hours, and professional fees.
Service - This type tracks services that you charge on the purchase and tracks
merchandise you sell and buy that is not tracked as inventory. For example, specialized
labor, consulting hours, and professional fees.
"""

class_dict = {
Expand Down Expand Up @@ -74,4 +77,3 @@ def to_ref(self):
ref.value = self.Id

return ref

14 changes: 8 additions & 6 deletions quickbooks/objects/salesreceipt.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from six import python_2_unicode_compatible
from .base import QuickbooksBaseObject, Ref, CustomField, QuickbooksManagedObject, LinkedTxnMixin, Address, \
from .base import Ref, CustomField, QuickbooksManagedObject, LinkedTxnMixin, Address, \
EmailAddress, QuickbooksTransactionEntity, LinkedTxn
from .tax import TxnTaxDetail
from .detailline import DetailLine
from ..mixins import QuickbooksPdfDownloadable


@python_2_unicode_compatible
class SalesReceipt(QuickbooksPdfDownloadable, QuickbooksManagedObject, QuickbooksTransactionEntity, LinkedTxnMixin):
class SalesReceipt(QuickbooksPdfDownloadable, QuickbooksManagedObject,
QuickbooksTransactionEntity, LinkedTxnMixin):
"""
QBO definition: SalesReceipt represents the sales receipt that is given to a customer. A sales receipt is
similar to an invoice. However, for a sales receipt, payment is received as part of the sale of goods and
services. The sales receipt specifies a deposit account where the customer deposits the payment. If the
deposit account is not specified, the payment type is classified as Undeposited Account.
QBO definition: SalesReceipt represents the sales receipt that is given to a customer.
A sales receipt is similar to an invoice. However, for a sales receipt, payment is received
as part of the sale of goods and services. The sales receipt specifies a deposit account
where the customer deposits the payment. If the deposit account is not specified, the
payment type is classified as Undeposited Account.
"""
class_dict = {
"DepartmentRef": Ref,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def read(*parts):
return fp.read()


VERSION = (0, 5, 5)
VERSION = (0, 5, 6)
version = '.'.join(map(str, VERSION))

setup(
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ def test_invalid_operation(self):

@patch('quickbooks.batch.BatchManager.process_batch')
def test_batch_create(self, process_batch):
results = batch.batch_create(self.obj_list)
batch.batch_create(self.obj_list)
self.assertTrue(process_batch.called)

@patch('quickbooks.batch.BatchManager.process_batch')
def test_batch_update(self, process_batch):
results = batch.batch_update(self.obj_list)
batch.batch_update(self.obj_list)
self.assertTrue(process_batch.called)

@patch('quickbooks.batch.BatchManager.process_batch')
def test_batch_delete(self, process_batch):
results = batch.batch_delete(self.obj_list)
batch.batch_delete(self.obj_list)
self.assertTrue(process_batch.called)

def test_list_to_batch_request(self):
Expand Down

0 comments on commit f4ddecd

Please sign in to comment.