diff --git a/addons/base_module_quality/unit_test/unit_test.py b/addons/base_module_quality/unit_test/unit_test.py index 1a9857b4966..13253486cd3 100644 --- a/addons/base_module_quality/unit_test/unit_test.py +++ b/addons/base_module_quality/unit_test/unit_test.py @@ -94,21 +94,20 @@ This test checks the Unit Test(PyUnit) Cases of the module. Note that 'unit_test res = detail + html + '' return res else: - detail_lst = [] - cnt = 0 + detail_dict = {} detail += '''Details''' data = data_list[1].split("======================================================================") test = data[0].split('\n') for err in (data_list[0].failures,data_list[0].errors): for value in err: - detail_lst.append(value[1]) - + detail_dict[value[0]._testMethodName] = value[1] for case in map(lambda x:x.split('...'), test): if len(case[0]) < 2: continue test_name = case[0].split(' (')[0] - html += '%s%s%s'%(test_name,case[1],detail_lst[cnt]) - cnt += 1 + if not detail_dict.has_key(test_name): + detail_dict[test_name] = '' + html += '%s%s%s'%(test_name, case[1], detail_dict[test_name]) return detail + html +'' return '' diff --git a/addons/crm/__init__.py b/addons/crm/__init__.py index edaf878fed0..e2403aa29ab 100644 --- a/addons/crm/__init__.py +++ b/addons/crm/__init__.py @@ -25,5 +25,8 @@ import crm_segmentation import report import wizard + + + # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/crm/crm.py b/addons/crm/crm.py index 71a6a243fd5..e80bfc3892e 100644 --- a/addons/crm/crm.py +++ b/addons/crm/crm.py @@ -32,6 +32,11 @@ import tools from osv import fields,osv,orm from osv.orm import except_orm +from scripts.openerp_mailgate import openerp_mailgate +import email +import netsvc +from poplib import POP3, POP3_SSL +from imaplib import IMAP4, IMAP4_SSL MAX_LEVEL = 15 AVAILABLE_STATES = [ @@ -203,101 +208,128 @@ class crm_case_section(osv.osv): return res crm_case_section() +class crm_email_gateway_server(osv.osv): + _name = "crm.email.gateway.server" + _description = "Email Gateway Server" + _columns = { + 'name': fields.char('Server Address',size=64,required=True ,help="IMAP/POP Address Of Email gateway Server"), + 'login': fields.char('User',size=64,required=True,help="User Login Id of Email gateway"), + 'password': fields.char('Password',size=64,required=True,help="User Password Of Email gateway"), + 'server_type': fields.selection([("pop","POP"),("imap","Imap")],"Type of Server", required=True, help="Type of Email gateway Server"), + 'port': fields.integer("Port" , help="Port Of Email gateway Server. If port is omitted, the standard POP3 port (110) is used for POP EMail Server and the standard IMAP4 port (143) is used for IMAP Sever."), + 'ssl': fields.boolean('SSL',help ="Use Secure Authentication"), + 'active': fields.boolean('Active'), + } + _defaults = { + 'server_type':lambda * a:'pop', + 'active':lambda * a:True, + } +crm_email_gateway_server() + + + class crm_email_gateway(osv.osv): _name = "crm.email.gateway" _description = "Email Gateway" - _rec_name="login" - _columns = { - 'pop': fields.char('POP Server Name',size=64,required=True ,help="POP Server Name Of Email gateway"), - 'login': fields.char('User',size=64,required=True,help="User Login Id of Email gateway"), - 'password': fields.char('Password',size=64,required=True,help="User Password Of Email gateway"), - 'email': fields.char('Email Id',size=64,help="Default eMail in case of any trouble."), - 'mailgateway':fields.selection([("fetchmail","Using Fetchmail"),("postfix","Using Postfix")],"EMail gateway", readonly=True), - 'section_id':fields.many2one('crm.case.section',"Section"), - 'path':fields.char("Path ",size=255,required= True,help ="Path of script file of Email gateway"), - 'port':fields.integer("Port" , help="Port Number "), - 'ssl':fields.boolean('Use Secure Authentication',help ="Use Secure Authentication"), - } - _defaults = { - 'path': lambda *a: tools.config['addons_path']+"/crm/scripts/openerp-mailgate/openerp-mailgate.py", - 'port':lambda * a:110, - 'mailgateway':lambda * a:'fetchmail', - } - - def _get_fetchmail_path(self, cr): - return os.path.join(tools.config['root_path'], '.fetchmail', cr.dbname) - - def fetch_mail(self, cr, uid, section_ids=[], context={}): - path = self._get_fetchmail_path(cr) - sect_obj = self.pool.get("crm.case.section") - if not len(section_ids): - section_ids = sect_obj.search(cr, uid, []) - for section in sect_obj.browse(cr, uid, section_ids): - fetch_file = path + "/" +section.name - if os.path.isfile(fetch_file): - try : - os.system("fetchmail -f %s" %(fetch_file)) - except Exception, e: - import netsvc - netsvc.Logger().notifyChannel('fetchmail', netsvc.LOG_ERROR, "%s" % e) - return True - - def make_fetchmail(self, cr, uid, section_ids=[], context={}): - sect_obj = self.pool.get("crm.case.section") - user_obj = self.pool.get("res.users") - for section in sect_obj.browse(cr, uid, section_ids, context): - user = user_obj.browse(cr,uid,uid) - path = self._get_fetchmail_path(cr) - if not os.path.isdir(path): - try: - os.makedirs(path) - except: - raise except_orm(_('Permission Denied !'), _('You do not permissions to write on the server side.')) - - fmail_path = path + "/" +section.name - fmail = open(fmail_path , 'w') - os.chmod(fmail_path,0710) - for mailgateway in section.gateway_ids: - mdatext = '%s -u %d -p %s -s %d -d %s '%(mailgateway.path,uid,user.password,mailgateway.section_id.id,cr.dbname) - if mailgateway.email: - mdatext += ' -m %s'%(mailgateway.email) - if section.reply_to: - mdatext += ' -e %s'%(section.reply_to) - text = "\npoll %s user '%s' password '%s'"%(mailgateway.pop,mailgateway.login,mailgateway.password) - if mailgateway.port: - text += ' port %d' % (mailgateway.port) - if mailgateway.ssl: - text += ' ssl' - text += " mda '%s'"%(mdatext) - fmail.write(text) - fmail.close() - - def create(self, cr, uid, vals, context=None): - res = super(crm_email_gateway, self).create(cr, uid, vals, context=context) - self.make_fetchmail(cr, uid, [vals['section_id']]) - return res - - def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True): - res = super(crm_email_gateway, self).write(cr, uid, ids, vals, context) - section_ids = [] - for gateway in self.browse(cr, uid, ids, context): - if gateway.section_id.id not in section_ids: - section_ids.append(gateway.section_id.id) - self.make_fetchmail(cr, uid, section_ids) - return res - - def unlink(self, cr, uid, ids, context={}, check=True): - section_ids = [] - for gateway in self.browse(cr, uid, ids, context): - if gateway.section_id.id not in section_ids: - section_ids.append(gateway.section_id.id) - res = super(crm_email_gateway, self).unlink(cr, uid,ids, context=context) - self.make_fetchmail(cr, uid, section_ids) - return res + _columns = { + 'name': fields.char('Name',size=64,help="Name of Mail Gateway."), + 'server_id': fields.many2one('crm.email.gateway.server',"Gateway Server", required=True), + 'to_email_id': fields.char('TO', size=64, help="Email address used in the From field of outgoing messages"), + 'cc_email_id': fields.char('CC',size=64,help="Default eMail in case of any trouble."), + 'section_id': fields.many2one('crm.case.section',"Section",required=True), + 'mail_history': fields.one2many("crm.email.history","gateway_id","History", readonly=True) + } + + def _fetch_mails(self, cr, uid, ids=False, context={}): + ''' + Function called by the scheduler to fetch mails + ''' + cr.execute('select * from crm_email_gateway gateway \ + inner join crm_email_gateway_server server \ + on server.id = gateway.server_id where server.active = True') + ids2 = map(lambda x: x[0], cr.fetchall() or []) + return self.fetch_mails(cr, uid, ids=ids2, context=context) + + def fetch_mails(self, cr, uid, ids=[], section_ids=[], context={}): + if len(section_ids): + casesection_obj = self.pool.get('crm.case.section') + for section in casesection_obj.read(cr, uid, section_ids, ['gateway_ids']): + ids += section['gateway_ids'] + log_messages = [] + user_obj = self.pool.get('res.users') + mail_history_obj = self.pool.get('crm.email.history') + for mailgateway in self.browse(cr, uid, ids): + try : + mailgate_server = mailgateway.server_id + if not mailgate_server.active: + continue + mailgate_name = mailgateway.name or "%s (%s)" % (mailgate_server.login, mailgate_server.name) + log_messages.append("Mail Server : %s" % mailgate_name) + log_messages.append("="*40) + email_messages = [] + read_messages = mailgateway.mail_history and map(lambda x:x.name, mailgateway.mail_history) or [] + new_messages = [] + if mailgate_server.server_type == 'pop': + if mailgate_server.ssl: + pop_server = POP3_SSL(mailgate_server.name or 'localhost', mailgate_server.port or 110) + else: + pop_server = POP3(mailgate_server.name or 'localhost', mailgate_server.port or 110) + pop_server.user(mailgate_server.login) + pop_server.pass_(mailgate_server.password) + pop_server.list() + (numMsgs, totalSize) = pop_server.stat() + for i in range(1, numMsgs + 1): + (header, msges, octets) = pop_server.retr(i) + email_messages.append((i,'\n'.join(msges))) + new_messages.append(i) + pop_server.quit() + + elif mailgate_server.server_type == 'imap': + if mailgate_server.ssl: + imap_server = IMAP4_SSL(mailgate_server.name or 'localhost', mailgate_server.port or 143) + else: + imap_server = IMAP4(mailgate_server.name or 'localhost', mailgate_server.port or 143) + imap_server.login(mailgate_server.login, mailgate_server.password) + imap_server.select() + typ, data = imap_server.search(None, '(UNSEEN)') + for num in data[0].split(): + typ, data = imap_server.fetch(num, '(RFC822)') + email_messages.append((num, data[0][1])) + new_messages.append(num) + imap_server.close() + imap_server.logout() + + except Exception, e: + log_messages.append("Error in Fetching Mail: %s " %(str(e))) + netsvc.Logger().notifyChannel('Emailgate:Fetching mail:[%d]%s' % (mailgate_server.id, mailgate_server.name), netsvc.LOG_ERROR, str(e)) + + if len(email_messages): + users = user_obj.read(cr, uid, uid, ['password']) + parser = openerp_mailgate.email_parser(uid, users['password'], mailgateway.section_id.id, + mailgateway.to_email_id or '', mailgateway.cc_email_id or '', dbname=cr.dbname, + host=tools.config['interface'] or 'localhost', port=tools.config['port'] or '8069') + for msg, message in email_messages: + msg_id = case_id = note = False + try : + msg_txt = email.message_from_string(message) + msg_id = msg_txt['Message-ID'] + case_id = parser.parse(msg_txt)[0] + log_messages.append('Case Successfull created : %d' % case_id) + + except Exception, e: + note = "Error in Parsing Mail: %s " %(str(e)) + log_messages.append(note) + netsvc.Logger().notifyChannel('Emailgate:Parsing mail:[%d]%s' % (mailgate_server.id, mailgate_server.name), netsvc.LOG_ERROR, str(e)) + mail_history_obj.create(cr, uid, {'name':msg_id, 'case_id': case_id, 'gateway_id':mailgateway.id, 'note':note}) + log_messages.append("-"*25) + log_messages.append("Total Read Mail: %d\n\n" %(len(new_messages))) + return log_messages crm_email_gateway() + + class crm_case_categ(osv.osv): _name = "crm.case.categ" _description = "Category of case" @@ -980,6 +1012,17 @@ class crm_case_history(osv.osv): } crm_case_history() +class crm_email_history(osv.osv): + _name = "crm.email.history" + _description = "Email History" + _columns = { + 'name': fields.char('Message Id', size=64, help="Message Id in Email Server."), + 'case_id': fields.many2one('crm.case',"Case"), + 'gateway_id': fields.many2one('crm.email.gateway',"Email Gateway", required=True), + 'note': fields.text('Notes'), + } + _order = 'id desc' +crm_email_history() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/crm/crm_data.xml b/addons/crm/crm_data.xml index abde7ed051d..2fe771ce2c7 100644 --- a/addons/crm/crm_data.xml +++ b/addons/crm/crm_data.xml @@ -74,7 +74,7 @@ -1 - + diff --git a/addons/crm/crm_view.xml b/addons/crm/crm_view.xml index 06798626476..f0c4bd21c9c 100644 --- a/addons/crm/crm_view.xml +++ b/addons/crm/crm_view.xml @@ -40,29 +40,27 @@ - - - - - + + + -
- - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -288,8 +286,9 @@ crm.case tree - + + @@ -626,56 +625,52 @@ - - crm.email.gateway.form - crm.email.gateway + + + crm.email.gateway.server.form + crm.email.gateway.server form -
- - + + - - - + + + - - - - - - - - + + +
- - crm.email.gateway.tree - crm.email.gateway + + crm.email.gateway.server.tree + crm.email.gateway.server tree - - - - - - - + + + + + - - Email Gateway - crm.email.gateway + + + + Email Gateway Server + crm.email.gateway.server form tree,form - + - - + + + diff --git a/addons/crm/scripts/__init__.py b/addons/crm/scripts/__init__.py new file mode 100644 index 00000000000..7ccd749f0a5 --- /dev/null +++ b/addons/crm/scripts/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +import openerp_mailgate + + diff --git a/addons/crm/scripts/openerp_mailgate/__init__.py b/addons/crm/scripts/openerp_mailgate/__init__.py new file mode 100644 index 00000000000..6f9eb256735 --- /dev/null +++ b/addons/crm/scripts/openerp_mailgate/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +import openerp_mailgate diff --git a/addons/crm/scripts/openerp-mailgate/openerp-mailgate.py b/addons/crm/scripts/openerp_mailgate/openerp_mailgate.py similarity index 78% rename from addons/crm/scripts/openerp-mailgate/openerp-mailgate.py rename to addons/crm/scripts/openerp_mailgate/openerp_mailgate.py index 73934869708..a51ebcd3568 100755 --- a/addons/crm/scripts/openerp-mailgate/openerp-mailgate.py +++ b/addons/crm/scripts/openerp_mailgate/openerp_mailgate.py @@ -49,8 +49,83 @@ priorities = { '5': '5 (Lowest)', } +def html2plaintext(html, body_id=None, encoding='utf-8'): + ## (c) Fry-IT, www.fry-it.com, 2007 + ## + ## download here: http://www.peterbe.com/plog/html2plaintext + + + """ from an HTML text, convert the HTML to plain text. + If @body_id is provided then this is the tag where the + body (not necessarily ) starts. + """ + try: + from BeautifulSoup import BeautifulSoup, SoupStrainer, Comment + except: + return html + + urls = [] + if body_id is not None: + strainer = SoupStrainer(id=body_id) + else: + strainer = SoupStrainer('body') + + soup = BeautifulSoup(html, parseOnlyThese=strainer, fromEncoding=encoding) + for link in soup.findAll('a'): + title = link.renderContents() + for url in [x[1] for x in link.attrs if x[0]=='href']: + urls.append(dict(url=url, tag=str(link), title=title)) + + html = soup.__str__() + + url_index = [] + i = 0 + for d in urls: + if d['title'] == d['url'] or 'http://'+d['title'] == d['url']: + html = html.replace(d['tag'], d['url']) + else: + i += 1 + html = html.replace(d['tag'], '%s [%s]' % (d['title'], i)) + url_index.append(d['url']) + + html = html.replace('','*').replace('','*') + html = html.replace('','*').replace('','*') + html = html.replace('

','*').replace('

','*') + html = html.replace('

','**').replace('

','**') + html = html.replace('

','**').replace('

','**') + html = html.replace('','/').replace('','/') + + + # the only line breaks we respect is those of ending tags and + # breaks + + html = html.replace('\n',' ') + html = html.replace('
', '\n') + html = html.replace('', '\n') + html = html.replace('

', '\n\n') + html = re.sub('', '\n', html) + html = html.replace(' ' * 2, ' ') + + + # for all other tags we failed to clean up, just remove then and + # complain about them on the stderr + def desperate_fixer(g): + #print >>sys.stderr, "failed to clean up %s" % str(g.group()) + return ' ' + + html = re.sub('<.*?>', desperate_fixer, html) + + # lstrip all lines + html = '\n'.join([x.lstrip() for x in html.splitlines()]) + + for i, url in enumerate(url_index): + if i == 0: + html += '\n\n' + html += '[%s] %s\n' % (i+1, url) + return html + class rpc_proxy(object): - def __init__(self, uid, passwd, host='localhost', port=8069, path='object', dbname='terp'): + def __init__(self, uid, passwd, host='localhost', port=8069, path='object', dbname='terp'): self.rpc = xmlrpclib.ServerProxy('http://%s:%s/xmlrpc/%s' % (host, port, path)) self.user_id = uid self.passwd = passwd @@ -60,8 +135,8 @@ class rpc_proxy(object): return self.rpc.execute(self.dbname, self.user_id, self.passwd, *request) class email_parser(object): - def __init__(self, uid, password, section, email, email_default, dbname, host): - self.rpc = rpc_proxy(uid, password, host=host, dbname=dbname) + def __init__(self, uid, password, section, email, email_default, dbname, host, port): + self.rpc = rpc_proxy(uid, password, host=host, port=port, dbname=dbname) try: self.section_id = int(section) except: @@ -82,7 +157,7 @@ class email_parser(object): adr = self.rpc('res.partner.address', 'read', adr_ids, ['partner_id']) return { 'partner_address_id': adr[0]['id'], - 'partner_id': adr[0]['partner_id'][0] + 'partner_id': adr[0].get('partner_id',False) and adr[0]['partner_id'][0] or False } def _decode_header(self, s): @@ -142,7 +217,7 @@ class email_parser(object): # # def msg_body_get(self, msg): message = {}; - message['body'] = u''; + message['body'] = ''; message['attachment'] = {}; attachment = message['attachment']; counter = 1; @@ -153,13 +228,17 @@ class email_parser(object): if part.get_content_maintype() == 'multipart': continue - if part.get_content_maintype()=='text' and part.get_content_subtype() in ('plain','html'): + if part.get_content_maintype()=='text': buf = part.get_payload(decode=True) if buf: txt = buf.decode(part.get_charsets()[0] or 'ascii', 'replace') txt = re.sub("<(\w)>", replace, txt) txt = re.sub("<\/(\w)>", replace, txt) - message['body'] += txt + if txt and part.get_content_subtype() == 'plain': + message['body'] += txt + elif txt and part.get_content_subtype() == 'html': + message['body'] += html2plaintext(txt) + elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text': filename = part.get_filename(); if filename : @@ -171,7 +250,7 @@ class email_parser(object): #end if #end if message['attachment'] = attachment - #end for + #end for return message #end def @@ -297,7 +376,7 @@ class email_parser(object): del msg['Subject'] msg['Subject'] = '[OpenERP-CaseError] ' + a self.msg_send(msg, self.email_default.split(',')) - return emails + return case_id, emails if __name__ == '__main__': import sys, optparse @@ -317,10 +396,11 @@ if __name__ == '__main__': parser.add_option("-m", "--default", dest="default", help="Default eMail in case of any trouble.", default=None) parser.add_option("-d", "--dbname", dest="dbname", help="Database name (default: terp)", default='terp') parser.add_option("--host", dest="host", help="Hostname of the Open ERP Server", default="localhost") + parser.add_option("--port", dest="port", help="Port of the Open ERP Server", default="8069") (options, args) = parser.parse_args() - parser = email_parser(options.userid, options.password, options.section, options.email, options.default, dbname=options.dbname, host=options.host) + parser = email_parser(options.userid, options.password, options.section, options.email, options.default, dbname=options.dbname, host=options.host, port=options.port) msg_txt = email.message_from_file(sys.stdin) diff --git a/addons/crm/wizard/wizard_fetch_mail.py b/addons/crm/wizard/wizard_fetch_mail.py index 273462dc8a6..2e41dcbd8d1 100644 --- a/addons/crm/wizard/wizard_fetch_mail.py +++ b/addons/crm/wizard/wizard_fetch_mail.py @@ -26,28 +26,59 @@ import tools import os _email_form = '''
-
+ + + step.vat.listing + vat.listing.clients + form + +
+ + + + + + + + + +
+ + + step.vat.listing + vat.listing.clients + tree + + + + + + + + + + + + + diff --git a/addons/l10n_be/wizard/partner_vat_listing.py b/addons/l10n_be/wizard/partner_vat_listing.py index c4b8758ae0e..4e9fed80300 100644 --- a/addons/l10n_be/wizard/partner_vat_listing.py +++ b/addons/l10n_be/wizard/partner_vat_listing.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- ############################################################################## -# +# # OpenERP, Open Source Management Solution # Copyright (C) 2004-2009 Tiny SPRL (). # @@ -15,16 +15,30 @@ # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . +# along with this program. If not, see . # ############################################################################## -import wizard import time import datetime -import pooler import base64 + +import wizard +import pooler from tools.translate import _ import tools +from osv import fields, osv + +class vat_listing_clients(osv.osv_memory): + _name = 'vat.listing.clients' + _columns = { + 'name': fields.char('Cleint Name', size=64), + 'vat': fields.char('VAT', size=64), + 'country': fields.char('Country', size=64), + 'amount': fields.float('Amount'), + 'turnover': fields.float('Turnover'), + } + +vat_listing_clients() form = """
@@ -45,14 +59,29 @@ fields = { 'limit_amount':{'string':'Limit Amount','type':'integer','required': True, }, 'test_xml': {'string':'Test XML file', 'type':'boolean', }, } + +client_form = """ + +