We have an odoo server running (http://www.odoo.com/documentation/9.0/api_integration.html), which allows us to programmatically query and download invoice reports as PDF's
We're running this on python 2.7.3. I'm looking for tips on DRYness, future maintainability and help on blind spots in the code where something might go wrong, and the ability to output helpful data to the person running the script.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import xmlrpclib
import os
class UniReportPrinter():
def __init__(self, mode):
self.mode = mode
from apiv9connector import s51OpenERPConnector_apiv9
# during dev only get vals from prod that are needed
self.odooconnection1 = s51OpenERPConnector_apiv9()
self.odooconnection1.login()
print "logged in"
odooconnection1 = ''
self.partners = []
@property
def partners(self):
return self.partners
@partners.setter
def partners(self, value):
self.partners = value
def get_reports(self):
nextmonth = self.__get_next_month()
print('searching for invoices, filtered by:')
print('state: open')
print('date: ' + nextmonth.strftime("%Y-%m-%d"))
print('reference: like SB%')
invoice_ids = self.odooconnection1.models.execute_kw(
self.odooconnection1.db, self.odooconnection1.uid, self.odooconnection1.password, 'account.invoice', 'search',
[[('state', '=', 'open'), ('date_invoice', '=', nextmonth.strftime("%Y-%m-%d")),('reference', 'like', 'SB%')]])
self.__get_partner_details(invoice_ids)
self.__download_report(invoice_ids)
def __get_next_month(self):
from datetime import date
from dateutil.relativedelta import *
today = date.today()
nextmonth = today+relativedelta(months=+1)
nextmonth = nextmonth+relativedelta(day=1)
return nextmonth
def __get_partner_details(self, invoice_ids):
partners = self.odooconnection1.models.execute_kw(
self.odooconnection1.db, self.odooconnection1.uid, self.odooconnection1.password, 'account.invoice', 'search_read',
[[['id', 'in', invoice_ids]]],
{'fields': ['reference', 'id', 'partner_id']})
self.partners = partners
def __download_report(self, invoice_ids):
for invoice_id in invoice_ids:
report = xmlrpclib.ServerProxy('{}/xmlrpc/2/report'.format('https://odoo.example.com'))
result = report.render_report(
self.odooconnection1.db, self.odooconnection1.uid, self.odooconnection1.password, 'account.report_invoice', [invoice_id])
filename = self.__get_file_name(invoice_id)
sys.stdout.write("\r[%s]" % filename )
sys.stdout.flush()
self.__save_report(result, filename)
def __get_file_name(self, invoice_id):
partner = ([p for p in self.partners if p["id"] == invoice_id])[0]
return str(partner['partner_id'][0])+'-'+partner['reference'] + '.pdf'
def __save_report(self, report_data, filename):
nextmonth = self.__get_next_month()
year = nextmonth.year
month = nextmonth.month
path = str(year) + '/' + str(month) + '/'
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
fileNamePath = os.path.join(path, filename)
my_file = open(fileNamePath, 'w')
my_file.write(report_data['result'].decode('base64'))
my_file.close()
###############main#############
if __name__ == "__main__":
unireportprinter = UniReportPrinter(mode='live')
unireportprinter.get_reports()