import time
from datetime import datetime
from dateutil.relativedelta import relativedelta
+ from pytz import timezone
+ import pytz
from openerp.osv import fields, osv
- from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
+ from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
from openerp.tools.translate import _
-from openerp import netsvc
class hr_timesheet_sheet(osv.osv):
_name = "hr_timesheet_sheet.sheet"
'timesheet_max_difference': lambda *args: 0.0
}
-res_company()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
-
import base64
-import openerp.addons.web.http as oeweb
+import psycopg2
+
+import openerp
+from openerp import SUPERUSER_ID
+from openerp import http
+from openerp.http import request
from openerp.addons.web.controllers.main import content_disposition
+ import mimetypes
-#----------------------------------------------------------
-# Controller
-#----------------------------------------------------------
-class MailController(oeweb.Controller):
+
+class MailController(http.Controller):
_cp_path = '/mail'
- @oeweb.httprequest
- def download_attachment(self, req, model, id, method, attachment_id, **kw):
- Model = req.session.model(model)
- res = getattr(Model, method)(int(id), int(attachment_id))
+ @http.route('/mail/download_attachment', type='http', auth='user')
+ def download_attachment(self, model, id, method, attachment_id, **kw):
+ # FIXME use /web/binary/saveas directly
+ Model = request.registry.get(model)
+ res = getattr(Model, method)(request.cr, request.uid, int(id), int(attachment_id))
if res:
filecontent = base64.b64decode(res.get('base64'))
filename = res.get('filename')
+ content_type = mimetypes.guess_type(filename)
if filecontent and filename:
- return req.make_response(filecontent,
+ return request.make_response(
+ filecontent,
- headers=[('Content-Type', 'application/octet-stream'),
+ headers=[('Content-Type', content_type[0] or 'application/octet-stream'),
- ('Content-Disposition', content_disposition(filename, req))])
- return req.not_found()
+ ('Content-Disposition', content_disposition(filename))])
+ return request.not_found()
+
+ @http.route('/mail/receive', type='json', auth='none')
+ def receive(self, req):
+ """ End-point to receive mail from an external SMTP server. """
+ dbs = req.jsonrequest.get('databases')
+ for db in dbs:
+ message = dbs[db].decode('base64')
+ try:
+ registry = openerp.registry(db)
+ with registry.cursor() as cr:
+ mail_thread = registry['mail.thread']
+ mail_thread.message_process(cr, SUPERUSER_ID, None, message)
+ except psycopg2.Error:
+ pass
+ return True
res = None
for email in email_list:
msg = ir_mail_server.build_email(
- email_from = mail.email_from,
- email_to = email.get('email_to'),
- subject = email.get('subject'),
- body = email.get('body'),
- body_alternative = email.get('body_alternative'),
- email_cc = tools.email_split(mail.email_cc),
- reply_to = email.get('reply_to'),
- attachments = attachments,
- message_id = mail.message_id,
- references = mail.references,
- object_id = mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
- subtype = 'html',
- subtype_alternative = 'plain')
+ email_from=mail.email_from,
+ email_to=email.get('email_to'),
+ subject=email.get('subject'),
+ body=email.get('body'),
+ body_alternative=email.get('body_alternative'),
+ email_cc=tools.email_split(mail.email_cc),
+ reply_to=mail.reply_to,
+ attachments=attachments,
+ message_id=mail.message_id,
+ references=mail.references,
+ object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
+ subtype='html',
+ subtype_alternative='plain',
+ headers=headers)
res = ir_mail_server.send_email(cr, uid, msg,
- mail_server_id=mail.mail_server_id.id, context=context)
+ mail_server_id=mail.mail_server_id.id,
+ context=context)
-
++
if res:
mail.write({'state': 'sent', 'message_id': res})
mail_sent = True
# see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1
if mail_sent:
self._postprocess_sent_message(cr, uid, mail, context=context)
+ except MemoryError:
+ # prevent catching transient MemoryErrors, bubble up to notify user or abort cron job
+ # instead of marking the mail as failed
+ raise
- except Exception:
+ except Exception as e:
_logger.exception('failed sending mail.mail %s', mail.id)
mail.write({'state': 'exception'})
+ if raise_exception:
+ if isinstance(e, AssertionError):
+ # get the args of the original error, wrap into a value and throw a MailDeliveryException
+ # that is an except_orm, with name and value as arguments
+ value = '. '.join(e.args)
+ raise MailDeliveryException(_("Mail Delivery Failed"), value)
+ raise
if auto_commit == True:
cr.commit()