add more docstring and rename private function
This commit is contained in:
parent
83e4a23f76
commit
14ffeda5b4
3 changed files with 93 additions and 42 deletions
|
@ -7,6 +7,15 @@ class PDFInfo(object):
|
||||||
PDF Properties
|
PDF Properties
|
||||||
"""
|
"""
|
||||||
def __init__(self, title=None, author=None, subject=None):
|
def __init__(self, title=None, author=None, subject=None):
|
||||||
|
"""
|
||||||
|
PDF Properties
|
||||||
|
:param title: PDF title
|
||||||
|
:type title: str or unicode
|
||||||
|
:param author: PDF author
|
||||||
|
:type author: str or unicode
|
||||||
|
:param subject: PDF subject
|
||||||
|
:type subject: str or unicode
|
||||||
|
"""
|
||||||
self.title = title
|
self.title = title
|
||||||
self.author = author
|
self.author = author
|
||||||
self.subject = subject
|
self.subject = subject
|
||||||
|
@ -17,14 +26,31 @@ class InvoiceInfo(object):
|
||||||
"""
|
"""
|
||||||
Invoice information
|
Invoice information
|
||||||
"""
|
"""
|
||||||
def __init__(self, invoice_id, invoice_datetime, due_datetime):
|
def __init__(self, invoice_id=None, invoice_datetime=None, due_datetime=None):
|
||||||
|
"""
|
||||||
|
Invoice info
|
||||||
|
:param invoice_id: Invoice id
|
||||||
|
:type invoice_id: int or str or unicode or None
|
||||||
|
:param invoice_datetime: Invoice create datetime
|
||||||
|
:type invoice_datetime: str or unicode or datetime or date
|
||||||
|
:param due_datetime: Invoice due datetime
|
||||||
|
:type due_datetime: str or unicode or datetime or date
|
||||||
|
"""
|
||||||
self.invoice_id = invoice_id
|
self.invoice_id = invoice_id
|
||||||
self.invoice_datetime = invoice_datetime
|
self.invoice_datetime = invoice_datetime
|
||||||
self.due_datetime = due_datetime
|
self.due_datetime = due_datetime
|
||||||
|
|
||||||
|
|
||||||
class AddressInfo(object):
|
class AddressInfo(object):
|
||||||
def __init__(self, name, street, city, state, country, post_code):
|
def __init__(self, name=None, street=None, city=None, state=None, country=None, post_code=None):
|
||||||
|
"""
|
||||||
|
:type name: str or unicode or None
|
||||||
|
:type street: str or unicode or None
|
||||||
|
:type city: str or unicode or None
|
||||||
|
:type state: str or unicode or None
|
||||||
|
:type country: str or unicode or None
|
||||||
|
:type post_code: str or unicode or int or None
|
||||||
|
"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.street = street
|
self.street = street
|
||||||
self.city = city
|
self.city = city
|
||||||
|
@ -37,7 +63,17 @@ class ServiceProviderInfo(AddressInfo):
|
||||||
"""
|
"""
|
||||||
Service provider/Merchant information
|
Service provider/Merchant information
|
||||||
"""
|
"""
|
||||||
def __init__(self, name, street, city, state, country, post_code, vat_tax_number=None):
|
def __init__(self, name=None, street=None, city=None, state=None, country=None, post_code=None,
|
||||||
|
vat_tax_number=None):
|
||||||
|
"""
|
||||||
|
:type name: str or unicode or None
|
||||||
|
:type street: str or unicode or None
|
||||||
|
:type city: str or unicode or None
|
||||||
|
:type state: str or unicode or None
|
||||||
|
:type country: str or unicode or None
|
||||||
|
:type post_code: str or unicode or None
|
||||||
|
:type vat_tax_number: str or unicode or None
|
||||||
|
"""
|
||||||
super(ServiceProviderInfo, self).__init__(name, street, city, state, country, post_code)
|
super(ServiceProviderInfo, self).__init__(name, street, city, state, country, post_code)
|
||||||
self.vat_tax_number = vat_tax_number
|
self.vat_tax_number = vat_tax_number
|
||||||
|
|
||||||
|
@ -46,7 +82,18 @@ class ClientInfo(AddressInfo):
|
||||||
"""
|
"""
|
||||||
Client/Custom information
|
Client/Custom information
|
||||||
"""
|
"""
|
||||||
def __init__(self, email, client_id, name, street, city, state, country, post_code):
|
def __init__(self, name=None, street=None, city=None, state=None, country=None, post_code=None,
|
||||||
|
email=None, client_id=None):
|
||||||
|
"""
|
||||||
|
:type name: str or unicode or None
|
||||||
|
:type street: str or unicode or None
|
||||||
|
:type city: str or unicode or None
|
||||||
|
:type state: str or unicode or None
|
||||||
|
:type country: str or unicode or None
|
||||||
|
:type post_code: str or unicode or None
|
||||||
|
:type email: str or unicode or None
|
||||||
|
:type post_code: str or unicode or None
|
||||||
|
"""
|
||||||
super(ClientInfo, self).__init__(name, street, city, state, country, post_code)
|
super(ClientInfo, self).__init__(name, street, city, state, country, post_code)
|
||||||
self.email = email
|
self.email = email
|
||||||
self.client_id = client_id
|
self.client_id = client_id
|
||||||
|
@ -60,8 +107,13 @@ class Item(object):
|
||||||
"""
|
"""
|
||||||
Item modal init
|
Item modal init
|
||||||
:param name: Item name
|
:param name: Item name
|
||||||
|
:type name: str or unicode or int
|
||||||
|
:param description: Item detail
|
||||||
|
:type description: str or unicode or int
|
||||||
:param units: Amount
|
:param units: Amount
|
||||||
|
:type units: int or str or unicode
|
||||||
:param unit_price: Unit price
|
:param unit_price: Unit price
|
||||||
|
:type unit_price: Decimal or str or unicode
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -81,9 +133,13 @@ class Transaction(object):
|
||||||
def __init__(self, gateway, transaction_id, transaction_datetime, amount):
|
def __init__(self, gateway, transaction_id, transaction_datetime, amount):
|
||||||
"""
|
"""
|
||||||
:param gateway: Payment gateway like Paypal, Stripe etc.
|
:param gateway: Payment gateway like Paypal, Stripe etc.
|
||||||
|
:type gateway: str or unicode
|
||||||
:param transaction_id:
|
:param transaction_id:
|
||||||
|
:type transaction_id: int or str or unicode
|
||||||
:param transaction_datetime:
|
:param transaction_datetime:
|
||||||
|
:type transaction_datetime: date or datetime or str or unicode
|
||||||
:param amount: $$
|
:param amount: $$
|
||||||
|
:type amount: int or str or unicode
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.gateway = gateway
|
self.gateway = gateway
|
||||||
|
|
|
@ -74,25 +74,25 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
self._bottom_tip_align = align
|
self._bottom_tip_align = align
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __format_value(value):
|
def _format_value(value):
|
||||||
if isinstance(value, datetime):
|
if isinstance(value, datetime):
|
||||||
value = value.strftime('%Y-%m-%d %H:%M')
|
value = value.strftime('%Y-%m-%d %H:%M')
|
||||||
elif isinstance(value, date):
|
elif isinstance(value, date):
|
||||||
value = value.strftime('%Y-%m-%d')
|
value = value.strftime('%Y-%m-%d')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def __attribute_to_table_data(self, instance, attribute_verbose_name_list):
|
def _attribute_to_table_data(self, instance, attribute_verbose_name_list):
|
||||||
data = []
|
data = []
|
||||||
|
|
||||||
for property_name, verbose_name in attribute_verbose_name_list:
|
for property_name, verbose_name in attribute_verbose_name_list:
|
||||||
attr = getattr(instance, property_name)
|
attr = getattr(instance, property_name)
|
||||||
if attr:
|
if attr:
|
||||||
attr = self.__format_value(attr)
|
attr = self._format_value(attr)
|
||||||
data.append(['{0}:'.format(verbose_name), attr])
|
data.append(['{0}:'.format(verbose_name), attr])
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def __build_invoice_info(self):
|
def _build_invoice_info(self):
|
||||||
if isinstance(self.invoice_info, InvoiceInfo):
|
if isinstance(self.invoice_info, InvoiceInfo):
|
||||||
self._story.append(
|
self._story.append(
|
||||||
Paragraph('Invoice', self._defined_styles.get('RightHeading1'))
|
Paragraph('Invoice', self._defined_styles.get('RightHeading1'))
|
||||||
|
@ -102,16 +102,16 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
('due_datetime', 'Invoice due date')]
|
('due_datetime', 'Invoice due date')]
|
||||||
|
|
||||||
self._story.append(
|
self._story.append(
|
||||||
SimpleTable(self.__attribute_to_table_data(self.invoice_info, props), horizontal_align='RIGHT')
|
SimpleTable(self._attribute_to_table_data(self.invoice_info, props), horizontal_align='RIGHT')
|
||||||
)
|
)
|
||||||
|
|
||||||
def __service_provider_data(self):
|
def _service_provider_data(self):
|
||||||
props = [('name', 'Name'), ('street', 'Street'), ('city', 'City'), ('state', 'State'),
|
props = [('name', 'Name'), ('street', 'Street'), ('city', 'City'), ('state', 'State'),
|
||||||
('country', 'Country'), ('post_code', 'Post code'), ('vat_tax_number', 'Vat/Tax number')]
|
('country', 'Country'), ('post_code', 'Post code'), ('vat_tax_number', 'Vat/Tax number')]
|
||||||
|
|
||||||
return self.__attribute_to_table_data(self.service_provider_info, props)
|
return self._attribute_to_table_data(self.service_provider_info, props)
|
||||||
|
|
||||||
def __build_service_provider_info(self):
|
def _build_service_provider_info(self):
|
||||||
# Merchant
|
# Merchant
|
||||||
if isinstance(self.service_provider_info, ServiceProviderInfo):
|
if isinstance(self.service_provider_info, ServiceProviderInfo):
|
||||||
self._story.append(
|
self._story.append(
|
||||||
|
@ -119,15 +119,15 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
)
|
)
|
||||||
|
|
||||||
self._story.append(
|
self._story.append(
|
||||||
SimpleTable(self.__service_provider_data(), horizontal_align='RIGHT')
|
SimpleTable(self._service_provider_data(), horizontal_align='RIGHT')
|
||||||
)
|
)
|
||||||
|
|
||||||
def __client_info_data(self):
|
def _client_info_data(self):
|
||||||
props = [('name', 'Name'), ('street', 'Street'), ('city', 'City'), ('state', 'State'),
|
props = [('name', 'Name'), ('street', 'Street'), ('city', 'City'), ('state', 'State'),
|
||||||
('country', 'Country'), ('post_code', 'Post code'), ('email', 'Email'), ('client_id', 'Client id')]
|
('country', 'Country'), ('post_code', 'Post code'), ('email', 'Email'), ('client_id', 'Client id')]
|
||||||
return self.__attribute_to_table_data(self.client_info, props)
|
return self._attribute_to_table_data(self.client_info, props)
|
||||||
|
|
||||||
def __build_client_info(self):
|
def _build_client_info(self):
|
||||||
# ClientInfo
|
# ClientInfo
|
||||||
if isinstance(self.client_info, ClientInfo):
|
if isinstance(self.client_info, ClientInfo):
|
||||||
self._story.append(
|
self._story.append(
|
||||||
|
@ -135,10 +135,10 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
)
|
)
|
||||||
|
|
||||||
self._story.append(
|
self._story.append(
|
||||||
SimpleTable(self.__client_info_data(), horizontal_align='LEFT')
|
SimpleTable(self._client_info_data(), horizontal_align='LEFT')
|
||||||
)
|
)
|
||||||
|
|
||||||
def __build_service_provider_and_client_info(self):
|
def _build_service_provider_and_client_info(self):
|
||||||
if isinstance(self.service_provider_info, ServiceProviderInfo) and isinstance(self.client_info, ClientInfo):
|
if isinstance(self.service_provider_info, ServiceProviderInfo) and isinstance(self.client_info, ClientInfo):
|
||||||
# Merge Table
|
# Merge Table
|
||||||
table_data = [
|
table_data = [
|
||||||
|
@ -151,8 +151,8 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
('LINEBELOW', (3, 0), (4, 0), 1, colors.gray),
|
('LINEBELOW', (3, 0), (4, 0), 1, colors.gray),
|
||||||
('LEFTPADDING', (0, 0), (-1, -1), 0),
|
('LEFTPADDING', (0, 0), (-1, -1), 0),
|
||||||
]
|
]
|
||||||
client_info_data = self.__client_info_data()
|
client_info_data = self._client_info_data()
|
||||||
service_provider_data = self.__service_provider_data()
|
service_provider_data = self._service_provider_data()
|
||||||
diff = abs(len(client_info_data) - len(service_provider_data))
|
diff = abs(len(client_info_data) - len(service_provider_data))
|
||||||
if diff > 0:
|
if diff > 0:
|
||||||
if len(client_info_data) < len(service_provider_data):
|
if len(client_info_data) < len(service_provider_data):
|
||||||
|
@ -167,10 +167,10 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
Table(table_data, style=table_style)
|
Table(table_data, style=table_style)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.__build_service_provider_info()
|
self._build_service_provider_info()
|
||||||
self.__build_client_info()
|
self._build_client_info()
|
||||||
|
|
||||||
def __build_items(self):
|
def _build_items(self):
|
||||||
# Items
|
# Items
|
||||||
item_data = []
|
item_data = []
|
||||||
item_subtotal = 0
|
item_subtotal = 0
|
||||||
|
@ -233,13 +233,13 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
|
|
||||||
self._story.append(TableWithHeader(item_data, horizontal_align='LEFT', style=style))
|
self._story.append(TableWithHeader(item_data, horizontal_align='LEFT', style=style))
|
||||||
|
|
||||||
def __build_transactions(self):
|
def _build_transactions(self):
|
||||||
# Transaction
|
# Transaction
|
||||||
transaction_table_data = [
|
transaction_table_data = [
|
||||||
(
|
(
|
||||||
t.transaction_id,
|
t.transaction_id,
|
||||||
Paragraph(t.gateway, self._defined_styles.get('TableParagraph')),
|
Paragraph(t.gateway, self._defined_styles.get('TableParagraph')),
|
||||||
self.__format_value(t.transaction_datetime),
|
self._format_value(t.transaction_datetime),
|
||||||
t.amount,
|
t.amount,
|
||||||
) for t in self._transactions if isinstance(t, Transaction)
|
) for t in self._transactions if isinstance(t, Transaction)
|
||||||
]
|
]
|
||||||
|
@ -251,7 +251,7 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
transaction_table_data.insert(0, ('Transaction id', 'Gateway', 'Transaction date', 'Amount'))
|
transaction_table_data.insert(0, ('Transaction id', 'Gateway', 'Transaction date', 'Amount'))
|
||||||
self._story.append(TableWithHeader(transaction_table_data, horizontal_align='LEFT'))
|
self._story.append(TableWithHeader(transaction_table_data, horizontal_align='LEFT'))
|
||||||
|
|
||||||
def __build_bottom_tip(self):
|
def _build_bottom_tip(self):
|
||||||
if self._bottom_tip:
|
if self._bottom_tip:
|
||||||
self._story.append(Spacer(5, 5))
|
self._story.append(Spacer(5, 5))
|
||||||
self._story.append(
|
self._story.append(
|
||||||
|
@ -268,10 +268,10 @@ class SimpleInvoice(SimpleDocTemplate):
|
||||||
def finish(self):
|
def finish(self):
|
||||||
self._story = []
|
self._story = []
|
||||||
|
|
||||||
self.__build_invoice_info()
|
self._build_invoice_info()
|
||||||
self.__build_service_provider_and_client_info()
|
self._build_service_provider_and_client_info()
|
||||||
self.__build_items()
|
self._build_items()
|
||||||
self.__build_transactions()
|
self._build_transactions()
|
||||||
self.__build_bottom_tip()
|
self._build_bottom_tip()
|
||||||
|
|
||||||
self.build(self._story, onFirstPage=PaidStamp(7 * inch, 5.8 * inch) if self.is_paid else None)
|
self.build(self._story, onFirstPage=PaidStamp(7 * inch, 5.8 * inch) if self.is_paid else None)
|
|
@ -12,11 +12,15 @@ class TestSimpleInvoice(unittest.TestCase):
|
||||||
|
|
||||||
def test_simple(self):
|
def test_simple(self):
|
||||||
invoice_path = os.path.join(self.file_base_dir, 'fixtures/dist/simple.pdf')
|
invoice_path = os.path.join(self.file_base_dir, 'fixtures/dist/simple.pdf')
|
||||||
|
|
||||||
|
if os.path.exists(invoice_path):
|
||||||
|
os.remove(invoice_path)
|
||||||
|
|
||||||
doc = SimpleInvoice(invoice_path)
|
doc = SimpleInvoice(invoice_path)
|
||||||
|
|
||||||
doc.is_paid = True
|
doc.is_paid = True
|
||||||
|
|
||||||
doc.invoice_info = InvoiceInfo('1024', datetime.now(), datetime.now())
|
doc.invoice_info = InvoiceInfo(1023, datetime.now(), datetime.now())
|
||||||
|
|
||||||
doc.service_provider_info = ServiceProviderInfo(
|
doc.service_provider_info = ServiceProviderInfo(
|
||||||
name='PyInvoice',
|
name='PyInvoice',
|
||||||
|
@ -28,16 +32,7 @@ class TestSimpleInvoice(unittest.TestCase):
|
||||||
vat_tax_number='Vat/Tax number'
|
vat_tax_number='Vat/Tax number'
|
||||||
)
|
)
|
||||||
|
|
||||||
doc.client_info = ClientInfo(
|
doc.client_info = ClientInfo(email='client@example.com')
|
||||||
email='My Email',
|
|
||||||
client_id='My Client Id',
|
|
||||||
name='Client Name',
|
|
||||||
street='Client Street',
|
|
||||||
city='Client City',
|
|
||||||
state='Client State',
|
|
||||||
country='Client country',
|
|
||||||
post_code='222222'
|
|
||||||
)
|
|
||||||
|
|
||||||
doc.add_item(Item('Item', 'Item desc', 1, '1.1'))
|
doc.add_item(Item('Item', 'Item desc', 1, '1.1'))
|
||||||
doc.add_item(Item('Item', 'Item desc', 2, '2.2'))
|
doc.add_item(Item('Item', 'Item desc', 2, '2.2'))
|
||||||
|
|
Loading…
Reference in a new issue