Skip to content

Commit

Permalink
Fix & character bug in proposal and invoice rows. Closes fgaudin#240
Browse files Browse the repository at this point in the history
  • Loading branch information
fgaudin committed Sep 28, 2011
1 parent c0e1370 commit 4277738
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
49 changes: 49 additions & 0 deletions accounts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,55 @@ def testDownloadPdf(self):
self.assertEquals(hashlib.md5("\n".join(invariant_content)).hexdigest(),
"8d6ca232060a96f8ad6855bdce59dc2c")

def testBug240(self):
"""
& character in proposal/invoice label crashes pdf download
"""
profile = User.objects.get(pk=1).get_profile()
profile.iban_bban = 'FR76 1234 1234 1234 1234 1234 123'
profile.bic = 'CCBPFRABCDE'
profile.save()
customer_address = self.proposal.project.customer.address
customer_address.street = u"128 boulevard des champs élysées\nEspace ZAC du champs de mars\nBP 140"
customer_address.zipcode = '75001'
customer_address.city = 'Paris CEDEX 1'
customer_address.save()

i = Invoice.objects.create(customer_id=self.proposal.project.customer_id,
invoice_id=1,
state=INVOICE_STATE_EDITED,
amount='1000',
edition_date=datetime.date(2010, 8, 31),
payment_date=datetime.date(2010, 9, 30),
paid_date=None,
payment_type=PAYMENT_TYPE_CHECK,
execution_begin_date=datetime.date(2010, 8, 1),
execution_end_date=datetime.date(2010, 8, 7),
penalty_date=datetime.date(2010, 10, 8),
penalty_rate='1.5',
discount_conditions='Nothing',
owner_id=1)

i_row = InvoiceRow.objects.create(proposal_id=self.proposal.id,
invoice_id=i.id,
label='Day & work',
category=ROW_CATEGORY_SERVICE,
quantity=10,
unit_price='100',
balance_payments=False,
detail="detail & comments",
owner_id=1)

response = self.client.get(reverse('invoice_download', kwargs={'id': i.id}))
self.assertEqual(response.status_code, 200)
f = open('/tmp/invoice_bug240.pdf', 'w')
f.write(response.content)
f.close()
content = response.content.split("\n")
invariant_content = content[0:76] + content[77:121] + content[122:-1]
self.assertEquals(hashlib.md5("\n".join(invariant_content)).hexdigest(),
"0e993bfe0f5694eb08370106e59a360a")

def testInvoiceBookDownloadPdf(self):
"""
Tests non-regression on pdf
Expand Down
39 changes: 39 additions & 0 deletions project/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,45 @@ def testDownloadPdf(self):
self.assertEquals(hashlib.md5("\n".join(invariant_content)).hexdigest(),
"489e25b4c6a9c868301776b0883bc0b6")

def testBug240(self):
"""
& character in proposal/invoice label crashes pdf download
"""
customer_address = Contact.objects.get(project=30).address
customer_address.street = u"128 boulevard des champs élysées\nEspace ZAC du champs de mars\nBP 140"
customer_address.zipcode = '75001'
customer_address.city = 'Paris CEDEX 1'
customer_address.save()

p = Proposal.objects.create(project_id=30,
update_date=datetime.date(2011, 2, 5),
state=PROPOSAL_STATE_DRAFT,
begin_date=datetime.date(2010, 8, 1),
end_date=datetime.date(2010, 8, 15),
contract_content='Content of contract',
amount=2005,
reference='XXX',
expiration_date=datetime.date(2010, 8, 2),
owner_id=1)

p_row = ProposalRow.objects.create(proposal_id=p.id,
label='Day & work',
category=ROW_CATEGORY_SERVICE,
quantity=20,
unit_price='200.5',
detail="detail & comments",
owner_id=1)

response = self.client.get(reverse('proposal_download', kwargs={'id': p.id}))
self.assertEqual(response.status_code, 200)
f = open('/tmp/proposal_bug240.pdf', 'w')
f.write(response.content)
f.close()
content = response.content.split("\n")
invariant_content = content[0:76] + content[77:121] + content[122:-1]
self.assertEquals(hashlib.md5("\n".join(invariant_content)).hexdigest(),
"b0ba361910042d9fd727ad17eb80e94a")

def testContractDownloadPdf(self):
"""
Tests non-regression on pdf
Expand Down
21 changes: 16 additions & 5 deletions project/utils/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER
from custom_canvas import NumberedCanvas
from django.template.defaultfilters import force_escape
from reportlab.platypus.paragraph import FragLine, ParaLines

class ProposalTemplate(object):

Expand Down Expand Up @@ -175,11 +177,11 @@ def add_row_detail(self, data, row, label_width):
extra_rows = 0
if row.detail:
for line in row.detail.split("\n"):
para = Paragraph(line, self.styleDetail)
para = Paragraph(force_escape(line), self.styleDetail)
para.width = label_width
splitted_para = para.breakLines(label_width)
for detail_row in splitted_para.lines:
detail = " ".join(detail_row[1])
detail = self.get_splitted_content(detail_row)
data.append((detail,))
extra_rows += 1
return extra_rows
Expand Down Expand Up @@ -227,6 +229,14 @@ def add_row_table(self, data, row_count, extra_style=[]):
def get_label(self, row):
return row.label

def get_splitted_content(self, splitted):
if isinstance(splitted, ParaLines)\
or isinstance(splitted, FragLine):
label = " ".join(["%s" % (frag.text) for frag in splitted.words])
else:
label = " ".join(splitted[1])
return label

def add_rows(self, rows):
row_count = 0
extra_rows = 0
Expand All @@ -241,10 +251,11 @@ def add_rows(self, rows):
for row in rows:
row_count += 1
label = self.get_label(row)
para = Paragraph(label, ProposalTemplate.styleLabel)
#label = label.replace('&', '[et]')
para = Paragraph(force_escape(label), ProposalTemplate.styleLabel)
para.width = label_width
splitted_para = para.breakLines(label_width)
label = " ".join(splitted_para.lines[0][1])
label = self.get_splitted_content(splitted_para.lines[0])
quantity = row.quantity
quantity = quantity.quantize(Decimal(1)) if quantity == quantity.to_integral() else quantity.normalize()
unit_price = row.unit_price
Expand All @@ -260,7 +271,7 @@ def add_rows(self, rows):
data.append(data_row)

for extra_row in splitted_para.lines[1:]:
label = " ".join(extra_row[1])
label = self.get_splitted_content(extra_row)
if self.user.get_profile().vat_number:
data.append([label, '', '', '', ''])
else:
Expand Down

0 comments on commit 4277738

Please sign in to comment.