Mache die Invoice-Library fit für Python 3
Bernd Wurst

Bernd Wurst commited on 2017-06-22 10:11:48
Zeige 5 geänderte Dateien mit 22 Einfügungen und 20 Löschungen.

... ...
@@ -27,11 +27,10 @@ class Table(object):
27 27
     '''bekommt einen Eintrag und liefert einen Eintrag wenn ok, wirft ansonsten ValueError.
28 28
     wird benutzt um z.B. die Summe auszurechnen oder ähnliches
29 29
     '''
30
-    k = entry.keys()
31 30
     e = entry
32
-    if not ('count' in k and 'subject' in k and 'price' in k and 'vat' in k):
31
+    if not ('count' in e and 'subject' in e and 'price' in e and 'vat' in e):
33 32
       raise ValueError('Some data is missing!')
34
-    if not 'unit' in e.keys():
33
+    if not 'unit' in e:
35 34
       e['unit'] = None
36 35
     ret = {'count': e['count'],
37 36
            'unit': e['unit'],
... ...
@@ -41,9 +40,9 @@ class Table(object):
41 40
           'vat': e['vat'],
42 41
           'tender': False,
43 42
           }
44
-    if 'tender' in e.keys():
43
+    if 'tender' in e:
45 44
       ret['tender'] = e['tender']
46
-    if 'desc' in k:
45
+    if 'desc' in e:
47 46
       ret['desc'] = e['desc']
48 47
     return ret
49 48
 
... ...
@@ -52,7 +51,7 @@ class Table(object):
52 51
     Typen sein'''
53 52
     d = self.validEntry(data)
54 53
     if d['vat'] != 0:
55
-      if not d['vat'] in self.vat.keys():
54
+      if not d['vat'] in self.vat:
56 55
         self.vat[d['vat']] = [0, chr(65+len(self.vat))]
57 56
       if 'tender' not in data or not data['tender']:
58 57
         self.vat[d['vat']][0] += d['total']
... ...
@@ -1,5 +1,5 @@
1 1
 # -* coding: utf8 *-
2
-from metrics import *
2
+from .metrics import *
3 3
 
4 4
 
5 5
 address_header = 'schokokeks.org · Köchersberg 32 · 71540 Murrhardt'
... ...
@@ -5,7 +5,7 @@ def setup():
5 5
   from reportlab.pdfbase.ttfonts import TTFont
6 6
   from reportlab.pdfbase import pdfmetrics
7 7
   
8
-  if 'DejaVuSans' not in pdfmetrics._fonts.keys():
8
+  if 'DejaVuSans' not in pdfmetrics._fonts:
9 9
     pdfmetrics.registerFont(TTFont("DejaVuSans", "DejaVuSans.ttf"))
10 10
     pdfmetrics.registerFont(TTFont("DejaVuSans-Bold", "DejaVuSans-Bold.ttf"))
11 11
     pdfmetrics.registerFont(TTFont("DejaVuSans-Oblique", "DejaVuSans-Oblique.ttf"))
... ...
@@ -4,9 +4,9 @@ import Invoice
4 4
 import re
5 5
 
6 6
 # our page size and margins
7
-from metrics import *
7
+from .metrics import *
8 8
 # our custom page style
9
-from custom_elements import basicPage, firstPage, address_header
9
+from .custom_elements import basicPage, firstPage, address_header
10 10
 
11 11
 # reportlab imports
12 12
 from reportlab.lib.units import cm
... ...
@@ -120,7 +120,10 @@ def address(canvas, lines):
120 120
 
121 121
 
122 122
 def InvoiceToPDF(iv, bankdata=True):
123
+  try:
123 124
     from StringIO import StringIO
125
+  except ImportError:
126
+    from io import StringIO
124 127
   fd = StringIO()
125 128
   canvas = Canvas.Canvas(fd, pagesize=A4)
126 129
   
... ...
@@ -381,7 +384,7 @@ def InvoiceToPDF(iv, bankdata=True):
381 384
           y -= line_height + line_padding
382 385
           summaries = []
383 386
           if len(part.vat) == 1:
384
-            vat = part.vat.keys()[0]
387
+            vat = list(part.vat.keys())[0]
385 388
             (integer, decimals) = _niceCount( (vat * 100) )
386 389
             vatstr = '%s' % integer
387 390
             if decimals:
... ...
@@ -392,7 +395,7 @@ def InvoiceToPDF(iv, bankdata=True):
392 395
               summaries.append(('Im Rechnungsbetrag sind %s%% MwSt enthalten:' % vatstr, _formatPrice((part.sum/(vat+1))*vat)))
393 396
             summaries.append(('Nettobetrag:', _formatPrice(part.sum - (part.sum/(vat+1))*vat)))
394 397
           else:
395
-            for vat, vatdata in part.vat.iteritems():
398
+            for vat, vatdata in part.vat.items():
396 399
               (integer, decimals) = _niceCount( (vat * 100) )
397 400
               vatstr = '%s' % integer
398 401
               if decimals:
... ...
@@ -409,14 +412,14 @@ def InvoiceToPDF(iv, bankdata=True):
409 412
           y -= line_height
410 413
           summaries = []
411 414
           if len(part.vat) == 1:
412
-            vat = part.vat.keys()[0]
415
+            vat = list(part.vat.keys())[0]
413 416
             (integer, decimals) = _niceCount( (vat * 100) )
414 417
             vatstr = '%s' % integer
415 418
             if decimals:
416 419
               vatstr += ',%s' % decimals
417 420
             summaries.append(('zzgl. %s%% MwSt:' % vatstr, _formatPrice(vat*part.sum)))
418 421
           elif len(part.vat) > 1:
419
-            for vat, vatdata in part.vat.iteritems():
422
+            for vat, vatdata in part.vat.items():
420 423
               (integer, decimals) = _niceCount( (vat * 100) )
421 424
               vatstr = '%s' % integer
422 425
               if decimals:
... ...
@@ -428,7 +431,7 @@ def InvoiceToPDF(iv, bankdata=True):
428 431
             canvas.drawRightString(left + 16.8*cm, y-font_height, line[1])
429 432
             y -= line_height
430 433
           sum = part.sum
431
-          for vat, vatdata in part.vat.iteritems():
434
+          for vat, vatdata in part.vat.items():
432 435
             sum += vat*vatdata[0]
433 436
           canvas.setFont(font+'-Bold', font_size)
434 437
           if iv.tender:
... ...
@@ -82,10 +82,10 @@ def InvoiceTableToText(invoiceTable):
82 82
     ret.append('')
83 83
     summaries = []
84 84
     if len(invoiceTable.vat) == 1:
85
-      vat = invoiceTable.vat.keys()[0]
85
+      vat = list(invoiceTable.vat.keys())[0]
86 86
       summaries.append(u'      Im Rechnungsbetrag sind %.1f%% MwSt enthalten (%s)' % (vat*100, format_price((invoiceTable.sum/(vat+1))*vat)))
87 87
     else:
88
-      for vat, vatdata in invoiceTable.vat.iteritems():
88
+      for vat, vatdata in invoiceTable.vat.items():
89 89
         summaries.append(u'    %s: Im Teilbetrag von %s sind %.1f%% MwSt enthalten (%s)' % (vatdata[1], format_price(vatdata[0]), vat*100, format_price((vatdata[0]/(vat+1))*vat)))
90 90
     summaries.sort()
91 91
     for line in summaries:
... ...
@@ -94,16 +94,16 @@ def InvoiceTableToText(invoiceTable):
94 94
     ret.append((u'Nettobetrag: %11s' % format_price(invoiceTable.sum)).rjust(72))
95 95
     summaries = []
96 96
     if len(invoiceTable.vat) == 1:
97
-      vat = invoiceTable.vat.keys()[0]
97
+      vat = list(invoiceTable.vat.keys())[0]
98 98
       summaries.append((u'zzgl. %.1f%% MwSt: %11s' % (vat*100, format_price(vat*invoiceTable.sum))).rjust(72))
99 99
     elif len(invoiceTable.vat) > 1:
100
-      for vat, vatdata in invoiceTable.vat.iteritems():
100
+      for vat, vatdata in invoiceTable.vat.items():
101 101
         summaries.append((u'zzgl. %4.1f%% MwSt (%s): %11s' % (vat*100, vatdata[1], format_price(vat*vatdata[0]))).rjust(72))
102 102
     summaries.sort()
103 103
     for line in summaries:
104 104
       ret.append(line)
105 105
     sum = invoiceTable.sum
106
-    for vat, vatdata in invoiceTable.vat.iteritems():
106
+    for vat, vatdata in invoiceTable.vat.items():
107 107
       sum += vat*vatdata[0]
108 108
     ret.append((u'Rechnungsbetrag: %11s' % format_price(sum)).rjust(72))
109 109
   ret.append('')
110 110