From 23d2d1bb5720d04f215603ebb743fc332a65cfdc Mon Sep 17 00:00:00 2001 From: "ajay javiya (OpenERP)" Date: Fri, 2 Aug 2013 14:26:07 +0530 Subject: [PATCH] [IMP]: Create email template fot crm meeting instead of calender atttendee , and clean code bzr revid: aja@tinyerp.com-20130802085607-pdnwpbqvycxm91di --- addons/base_calendar/base_calendar.py | 68 +++--------- addons/base_calendar/base_calendar_data.xml | 56 ---------- addons/base_calendar/crm_meeting.py | 69 +++++++++---- addons/base_calendar/crm_meeting_data.xml | 108 ++++++++++++++++++++ 4 files changed, 170 insertions(+), 131 deletions(-) diff --git a/addons/base_calendar/base_calendar.py b/addons/base_calendar/base_calendar.py index 5b1c128db3e..784792ffa59 100644 --- a/addons/base_calendar/base_calendar.py +++ b/addons/base_calendar/base_calendar.py @@ -296,6 +296,7 @@ property or property parameter."), multi='event_end_date'), 'ref': fields.reference('Event Ref', selection=_links_get, size=128), 'availability': fields.selection([('free', 'Free'), ('busy', 'Busy')], 'Free/Busy', readonly="True"), + } _defaults = { 'state': 'needs-action', @@ -403,22 +404,14 @@ property or property parameter."), res = cal.serialize() return res - def meeting_invitation(self, cr, uid, ids, context=None): - meeting_id = self.pool.get('crm.meeting').search(cr, uid, [('attendee_ids','in',ids)]) - action_id = self.pool.get('ir.actions.act_window').search(cr, uid, [('res_model','=','crm.meeting')], context=context) - base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url', default='http://localhost:8069', context=context) - if action_id: - base_url += '/meeting/meeting_invitation?db=%s&token=%s&action=%s&id=%s' % (cr.dbname, ids,action_id[0],meeting_id[0]); - return base_url - def _send_mail(self, cr, uid, ids, mail_to, email_from=tools.config.get('email_from', False), context=None): """ Send mail for event invitation to event attendees. @param email_from: email address for user sending the mail @return: True """ - company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.name mail_id = [] + local_context = context.copy() color = { 'needs-action' : 'grey', 'accepted' :'green', @@ -426,56 +419,23 @@ property or property parameter."), 'declined':'red', 'delegated':'grey' } - image_cricle = "
" for attendee in self.browse(cr, uid, ids, context=context): res_obj = attendee.ref + data_pool = self.pool.get('ir.model.data') if res_obj: - att = {} - att_info = [(image_cricle % color.get('needs-action')) + 'You', (image_cricle % color.get('accepted')) + res_obj.organizer+ '- Organizer
'] - sub = res_obj.name - other_invitation_ids = self.search(cr, uid, [('ref', '=', res_obj._name + ',' + str(res_obj.id))]) - other_invitation_ids.remove(attendee.id) - for att2 in self.browse(cr, uid, other_invitation_ids): - name = att2.user_id and att2.user_id.name or att2.partner_id and att2.partner_id.name or att2.email - att_info.append((image_cricle % color.get(att2.state)) + name) - tz = context.get('tz', pytz.timezone('UTC')) - allday = False - date = fields.datetime.context_timestamp(cr, uid, datetime.strptime(res_obj.date, tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context) - date_deadline = fields.datetime.context_timestamp(cr, uid, datetime.strptime(res_obj.date_deadline, tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context) - if res_obj.allday: - time = "AllDay" + "," + date.strftime('%B-%d-%Y') - tz = '' - elif res_obj.duration < 24: - duration = date + timedelta(hours= res_obj.duration) - time = date.strftime('%B-%d-%Y') + " at (" + date.strftime('%H-%M') + " To " + duration.strftime('%H-%M') + ")" - else : - time = date.strftime('%B-%d-%Y') + " at " + date.strftime('%H-%M') + " To \n" + date_deadline.strftime('%B-%d-%Y') + " at " + date_deadline.strftime('%H-%M') - footer = self.meeting_invitation(cr, uid, attendee.id, context=context) - map_url = '' - if res_obj.location: - map_url = """ (View Map)""" % res_obj.location - body_vals = {'user':attendee.user_id and attendee.user_id.name, - 'name': res_obj.name, - 'responsible': res_obj.user_id and res_obj.user_id.name or 'OpenERP User', - 'company': company, - 'description': res_obj.description or '-', - 'attendees': ',\n' .join(att_info), - 'time': time, - 'tz': tz, - 'location': res_obj.location or '-', - 'map' : map_url or '', - 'organizer' : res_obj.organizer, - 'url' : footer} - body_id = self.pool.get('email.template').search(cr, uid, [('subject', '=', 'Meeting Invitation')], context=context) - body = self.pool.get('email.template').browse(cr, uid, body_id[0], context=context).body_html % body_vals + model,template_id = data_pool.get_object_reference(cr, uid, 'base_calendar', "crm_email_template_meeting_invitation") + model,act_id = data_pool.get_object_reference(cr, uid, 'base_calendar', "view_crm_meeting_calendar") + action_id = self.pool.get('ir.actions.act_window').search(cr, uid, [('view_id','=',act_id)], context=context) + base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url', default='http://localhost:8069', context=context) + body = self.pool.get('email.template').browse(cr, uid, template_id, context=context).body_html if attendee.email and email_from: ics_file = self.get_ics_file(cr, uid, res_obj, context=context) - vals = {'email_from': email_from, - 'email_to': attendee.email, - 'state': 'outgoing', - 'subject': sub, - 'body_html': body, - 'auto_delete': True} + local_context['att_obj'] = attendee + local_context['color'] = color + local_context['action_id'] = action_id[0] + local_context['dbname'] = cr.dbname + local_context['base_url'] = base_url + vals = self.pool.get('email.template').generate_email(cr, uid, template_id, res_obj.id, context=local_context) if ics_file: vals['attachment_ids'] = [(0,0,{'name': 'invitation.ics', 'datas_fname': 'invitation.ics', diff --git a/addons/base_calendar/base_calendar_data.xml b/addons/base_calendar/base_calendar_data.xml index 1d43637a932..64aa7ba59b6 100644 --- a/addons/base_calendar/base_calendar_data.xml +++ b/addons/base_calendar/base_calendar_data.xml @@ -135,60 +135,4 @@ - - - - Meeting Invitation - ${object.user_id.email or ''} - Meeting Invitation - ${object.email} - - - - - -%(name)s - - - - - - - - - - - - - - -
-
%(name)s
Hello %(user)s,
- %(organizer)s invited you for the %(name)s meeting of %(company)s.
- - - - - - - - - - - - - - -
When :
%(time)s (%(tz)s)
-
Where :
%(location)s %(map)s
What :
%(description)s
Attendees:
%(attendees)s
--
Sent by %(responsible)s from %(company)s.View this meeting detail directly in OpenERP
- - - - - - - ]]>
-
-
diff --git a/addons/base_calendar/crm_meeting.py b/addons/base_calendar/crm_meeting.py index 7e66bc296be..1be810b3deb 100644 --- a/addons/base_calendar/crm_meeting.py +++ b/addons/base_calendar/crm_meeting.py @@ -26,6 +26,9 @@ from openerp.tools import DEFAULT_SERVER_DATE_FORMAT from openerp.tools.translate import _ from base_calendar import get_real_ids, base_calendar_id2real_id from openerp.addons.base_status.base_state import base_state +from datetime import datetime, timedelta, date +import pytz +from openerp import tools # # crm.meeting is defined here so that it may be used by modules other than crm, # without forcing the installation of crm. @@ -45,27 +48,48 @@ class crm_meeting(base_state, osv.Model): _order = "id desc" _inherit = ["calendar.event", "mail.thread", "ir.needaction_mixin"] - def _check_status(self, cr, uid, ids, context=None): + def _check_status(self, cr, uid, meeting_id, context=None): attendee_pool = self.pool.get('calendar.attendee') - user = self.pool.get('res.users').read(cr,uid,uid,['partner_id'],context) - for attendee in self.browse(cr,uid,ids,context)[0].attendee_ids: - if user['partner_id'][0] == attendee_pool.read(cr,uid,attendee.id,['partner_id'],context)['partner_id'][0]: + user = self.pool.get('res.users').browse(cr, uid, uid, context=context) + for attendee in self.browse(cr,uid,meeting_id,context).attendee_ids: + if user.partner_id.id == attendee.partner_id.id: return attendee return False def _check_attendee(self, cr, uid, ids, name, arg, context=None): res = {} - res[ids[0]] = False - if self._check_status(cr, uid, ids, context): - res[ids[0]] = True + for meeting_id in ids: + res[meeting_id] = False + if self._check_status(cr, uid, meeting_id, context): + res[meeting_id] = True return res def _compute_status(self, cr, uid, ids, name, arg, context=None): res = {} - res[ids[0]] = 'needs-action' - attendee = self._check_status(cr, uid, ids, context) - if attendee: - res[ids[0]] = attendee.state + for meeting_id in ids: + res[meeting_id] = 'needs-action' + attendee = self._check_status(cr, uid, meeting_id, context) + if attendee: + res[meeting_id] = attendee.state + return res + + def _compute_time(self, cr, uid, ids, name, arg, context=None): + res = {} + tz = context.get('tz', pytz.timezone('UTC')) + for meeting in self.browse(cr, uid, ids, context=context): + date = fields.datetime.context_timestamp(cr, uid, datetime.strptime(meeting.date, tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context) + date_deadline = fields.datetime.context_timestamp(cr, uid, datetime.strptime(meeting.date_deadline, tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context) + event_date = date.strftime('%B-%d-%Y') + event_time = date.strftime('%H-%M') + res[meeting.id] = False + if meeting.allday: + time = _("AllDay , %s") % (date_event) + elif meeting.duration < 24: + duration = date + timedelta(hours= meeting.duration) + time = ("%s at ( %s To %s) (%s)") % (str(event_date), str(event_time), str(duration.strftime('%H-%M')), str(tz)) + else : + time = ("%s at %s To\n %s at %s (%s)") % (event_date, event_time, date_deadline.strftime('%B-%d-%Y'), date_deadline.strftime('%H-%M'), tz) + res[meeting.id] = time return res _columns = { @@ -89,6 +113,7 @@ class crm_meeting(base_state, osv.Model): type="boolean"), 'attendee_status': fields.function(_compute_status, string='Attendee Status', \ type="selection"), + 'event_time': fields.function(_compute_time, string='Event Time', type="char"), } _defaults = { 'state': 'open', @@ -151,20 +176,22 @@ class crm_meeting(base_state, osv.Model): def do_decline(self, cr, uid, ids, context=None, *args): attendee_pool = self.pool.get('calendar.attendee') - attendee = self._check_status(cr, uid, ids, context) - if attendee: - if attendee.state != 'declined': - self.message_post(cr, uid, ids, body=_(("%s has Declined Invitation") % (attendee.cn)), context=context) - attendee_pool.write(cr, uid, attendee.id, {'state': 'declined'}, context) + for meeting_id in ids: + attendee = self._check_status(cr, uid, meeting_id, context) + if attendee: + if attendee.state != 'declined': + self.message_post(cr, uid, meeting_id, body=_(("%s has Declined Invitation") % (attendee.cn)), context=context) + attendee_pool.write(cr, uid, attendee.id, {'state': 'declined'}, context) return True def do_accept(self, cr, uid, ids, context=None, *args): attendee_pool = self.pool.get('calendar.attendee') - attendee = self._check_status(cr, uid, ids, context) - if attendee: - if attendee.state != 'accepted': - self.message_post(cr, uid, ids, body=_(("%s has Accepted Invitation") % (attendee.cn)), context=context) - attendee_pool.write(cr, uid, attendee.id, {'state': 'accepted'}, context) + for meeting_id in ids: + attendee = self._check_status(cr, uid, meeting_id, context) + if attendee: + if attendee.state != 'accepted': + self.message_post(cr, uid, meeting_id, body=_(("%s has Accepted Invitation") % (attendee.cn)), context=context) + attendee_pool.write(cr, uid, attendee.id, {'state': 'accepted'}, context) return True class mail_message(osv.osv): diff --git a/addons/base_calendar/crm_meeting_data.xml b/addons/base_calendar/crm_meeting_data.xml index d1cffddc403..cc9e2190633 100644 --- a/addons/base_calendar/crm_meeting_data.xml +++ b/addons/base_calendar/crm_meeting_data.xml @@ -28,5 +28,113 @@ Meeting crm.meeting + + CRM Meeting Invitation + ${object.user_id.email or ''} + ${object.name} + ${ctx['att_obj'].email} + + + + + + + ${object.name} + + + + + + + + + + + + + + + + + + +
+
${object.name}
+
+
Hello ${ctx['att_obj'].cn},
+ ${object.organizer} invited you for the ${object.name} meeting of ${object.user_id.company_id.name}.
+
+ + + + + + + + + + + + + + + + + +
+
When : +
+
+
+ ${object.event_time} +
+
+
Where : +
+
+
+ ${object.location or ''} + % if object.location : + + (View Map) + + % endif +
+
+
What : +
+
+
+ ${object.description or ''} +
+
+
Attendees:
+
+ % for attendee in object.attendee_ids: +
+
+ + ${attendee.cn} + + % endfor +
+
+
+ + Accept | + Declined + +
+
+
+ --
Sent by ${object.user_id.name} from ${object.user_id.company_id.name}. View this meeting detail + directly in OpenERP. +
+
+ + + ]]]>
+