From 9bd0f29fe1a426ee347d42f284a148c92ff6a434 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Tue, 12 Feb 2013 11:13:28 +0100 Subject: [PATCH] [IMP] survey: change in progress bzr revid: chm@openerp.com-20130212101328-j7s5c9jkln4pluvk --- addons/survey/__openerp__.py | 4 +- addons/survey/survey.py | 46 +++- addons/survey/survey_view.xml | 2 +- addons/survey/wizard/__init__.py | 1 - addons/survey/wizard/survey_answer.py | 24 +- addons/survey/wizard/survey_answer.xml | 9 + .../survey/wizard/survey_send_invitation.py | 240 ------------------ .../survey/wizard/survey_send_invitation.xml | 88 ++----- 8 files changed, 94 insertions(+), 320 deletions(-) delete mode 100644 addons/survey/wizard/survey_send_invitation.py diff --git a/addons/survey/__openerp__.py b/addons/survey/__openerp__.py index d5e4c3af10c..2545dc484c1 100644 --- a/addons/survey/__openerp__.py +++ b/addons/survey/__openerp__.py @@ -47,7 +47,7 @@ user name and password for the invitation of the survey. 'wizard/survey_print_answer.xml', 'wizard/survey_browse_answer.xml', 'wizard/survey_print.xml', - 'wizard/survey_send_invitation.xml' + 'survey_send_invitation.xml' ], 'demo': ['survey_demo.xml'], 'test': [ @@ -59,6 +59,6 @@ user name and password for the invitation of the survey. 'installable': True, 'auto_install': False, 'images': ['images/survey_answers.jpeg','images/survey_pages.jpeg','images/surveys.jpeg'], - 'css': ['static/src/css/survey.css','static/css/survey.css'], + 'css': ['static/src/css/survey.css'], } # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/survey/survey.py b/addons/survey/survey.py index be9003c805d..f0f20b54d2a 100644 --- a/addons/survey/survey.py +++ b/addons/survey/survey.py @@ -28,6 +28,7 @@ import os from openerp import netsvc, tools from openerp.osv import fields, osv from openerp.tools.translate import _ +import uuid # token = uuid.uuid4() class survey_type(osv.osv): _name = 'survey.type' @@ -151,12 +152,12 @@ class survey(osv.osv): if not pages: raise osv.except_osv(_('Warning!'), _('This survey has no question defined. Please define the questions and answers first.')) context.update({'active':False,'survey_id': ids[0]}) + return { 'view_type': 'form', 'view_mode': 'form', 'res_model': 'survey.question.wiz', 'type': 'ir.actions.act_window', - 'target': 'new', 'name': name, 'context': context } @@ -168,6 +169,8 @@ class survey(osv.osv): if not pages: raise osv.except_osv(_('Warning!'), _('This survey has no pages defined. Please define pages first.')) context.update({'active':False,'survey_id': ids[0]}) + + context.update({'ir_actions_act_window_target': 'new'}) return { 'view_type': 'form', 'view_mode': 'form', @@ -186,6 +189,8 @@ class survey(osv.osv): if not pages: raise osv.except_osv(_('Warning!'), _('This survey has no question defined. Please define the questions and answers first.')) context.update({'survey_id': ids[0]}) + + context.update({'ir_actions_act_window_target': 'new'}) return { 'view_type': 'form', 'view_mode': 'form', @@ -196,6 +201,41 @@ class survey(osv.osv): 'context': context } + def action_survey_sent(self, cr, uid, ids, context=None): + ''' + This function opens a window to compose an email, with the survey template message loaded by default + ''' + assert len(ids) == 1, 'This option should only be used for a single id at a time.' + ir_model_data = self.pool.get('ir.model.data') + try: + template_id = ir_model_data.get_object_reference(cr, uid, 'survey', 'email_template_survey')[1] + except ValueError: + template_id = False + try: + compose_form_id = ir_model_data.get_object_reference(cr, uid, 'mail', 'email_compose_message_wizard_form')[1] + except ValueError: + compose_form_id = False + ctx = dict(context) + + ctx.update({ + 'default_model': 'survey', + 'default_res_id': ids[0], + 'default_use_template': bool(template_id), + 'default_template_id': template_id, + 'default_composition_mode': 'comment', + 'mark_invoice_as_sent': True, + }) + return { + 'type': 'ir.actions.act_window', + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': 'mail.compose.message', + 'views': [(compose_form_id, 'form')], + 'view_id': compose_form_id, + 'target': 'new', + 'context': ctx, + } + survey() class survey_history(osv.osv): @@ -249,7 +289,7 @@ class survey_page(osv.osv): 'res_model': 'survey.question.wiz', 'type': 'ir.actions.act_window', 'target': 'new', - 'search_view_id': search_id[0], + #'search_view_id': search_id[0], 'context': context } @@ -526,7 +566,7 @@ class survey_question(osv.osv): 'res_model': 'survey.question.wiz', 'type': 'ir.actions.act_window', 'target': 'new', - 'search_view_id': search_id[0], + #'search_view_id': search_id[0], 'context': context } diff --git a/addons/survey/survey_view.xml b/addons/survey/survey_view.xml index 53900b31df5..14d894335e5 100644 --- a/addons/survey/survey_view.xml +++ b/addons/survey/survey_view.xml @@ -321,7 +321,7 @@ diff --git a/addons/survey/wizard/__init__.py b/addons/survey/wizard/__init__.py index 0ea0e5cd424..2da1bfa616f 100644 --- a/addons/survey/wizard/__init__.py +++ b/addons/survey/wizard/__init__.py @@ -19,7 +19,6 @@ # ############################################################################## -import survey_send_invitation import survey_print_statistics import survey_print_answer import survey_browse_answer diff --git a/addons/survey/wizard/survey_answer.py b/addons/survey/wizard/survey_answer.py index 6c3604e22c3..19b151e4999 100644 --- a/addons/survey/wizard/survey_answer.py +++ b/addons/survey/wizard/survey_answer.py @@ -41,10 +41,20 @@ class survey_question_wiz(osv.osv_memory): """ Fields View Get method :- generate the new view and display the survey pages of selected survey. """ + + print "" + print "" + print view_id, view_type, context, toolbar, submenu + print "" + print "" + + if not view_id and not context.get('survey_id', None) and context.get('default_survey_id', None): + context['survey_id'] = context['active_id'] = context.get('default_survey_id') + + if context is None: context = {} - result = super(survey_question_wiz, self).fields_view_get(cr, uid, view_id, \ - view_type, context, toolbar,submenu) + result = super(survey_question_wiz, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar,submenu) surv_name_wiz = self.pool.get('survey.name.wiz') survey_obj = self.pool.get('survey') @@ -213,6 +223,7 @@ class survey_question_wiz(osv.osv_memory): ans_ids = que_rec.answer_choice_ids xml_group = etree.SubElement(xml_form, 'group', {'col': '1', 'colspan': '4'}) + # TODO convert selection field into radio input if que_rec.type == 'multiple_choice_only_one_ans': selection = [] for ans in ans_ids: @@ -373,8 +384,9 @@ class survey_question_wiz(osv.osv_memory): else: etree.SubElement(xml_footer, 'label', {'string': ""}) etree.SubElement(xml_footer, 'button', {'name':"action_next",'string': tools.ustr(but_string) ,'type':"object",'context' : tools.ustr(context), 'class':"oe_highlight"}) - etree.SubElement(xml_footer, 'label', {'string': "or"}) - etree.SubElement(xml_footer, 'button', {'special': "cancel",'string':"Exit",'class':"oe_link"}) + if context.get('ir_actions_act_window_target', None): + etree.SubElement(xml_footer, 'label', {'string': "or"}) + etree.SubElement(xml_footer, 'button', {'special': "cancel",'string':"Exit",'class':"oe_link"}) etree.SubElement(xml_footer, 'label', {'string': tools.ustr(page_number+ 1) + "/" + tools.ustr(total_pages), 'class':"oe_survey_title_page oe_right"}) root = xml_form.getroottree() @@ -1211,7 +1223,7 @@ class survey_question_wiz(osv.osv_memory): "view_mode": 'form', 'res_model': 'survey.question.wiz', 'type': 'ir.actions.act_window', - 'target': 'new', + 'target': context.get('ir_actions_act_window_target', None), 'search_view_id': search_id[0], 'context': context } @@ -1232,7 +1244,7 @@ class survey_question_wiz(osv.osv_memory): "view_mode": 'form', 'res_model': 'survey.question.wiz', 'type': 'ir.actions.act_window', - 'target': 'new', + 'target': context.get('ir_actions_act_window_target', None), 'search_view_id': search_id[0], 'context': context } diff --git a/addons/survey/wizard/survey_answer.xml b/addons/survey/wizard/survey_answer.xml index b02f9fb2be5..27d91d12642 100644 --- a/addons/survey/wizard/survey_answer.xml +++ b/addons/survey/wizard/survey_answer.xml @@ -33,5 +33,14 @@ new + + ir.actions.act_window + survey.question.wiz + form + form + new + {'survey_id': survey_id, 'survey_token': survey_token} + + diff --git a/addons/survey/wizard/survey_send_invitation.py b/addons/survey/wizard/survey_send_invitation.py deleted file mode 100644 index 4913ca09eab..00000000000 --- a/addons/survey/wizard/survey_send_invitation.py +++ /dev/null @@ -1,240 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 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 time -from random import choice -import string -import os -import datetime -import socket - -from openerp import addons, netsvc, tools -from openerp.osv import fields, osv -from openerp.tools.translate import _ - - -class survey_send_invitation(osv.osv_memory): - _name = 'survey.send.invitation' - _columns = { - 'partner_ids': fields.many2many('res.partner','survey_res_partner','partner_id',\ - 'survey_id', "Answer", required=1), - 'send_mail': fields.boolean('Send Mail for New User'), - 'send_mail_existing': fields.boolean('Send Reminder for Existing User'), - 'mail_subject': fields.char('Subject', size=256), - 'mail_subject_existing': fields.char('Subject', size=256), - 'mail_from': fields.char('From', size=256, required=1), - 'mail': fields.text('Body') - } - - _defaults = { - 'send_mail': lambda *a: 1, - 'send_mail_existing': lambda *a: 1, - } - - def genpasswd(self): - chars = string.letters + string.digits - return ''.join([choice(chars) for i in range(6)]) - - def default_get(self, cr, uid, fields_list, context=None): - if context is None: - context = {} - data = super(survey_send_invitation, self).default_get(cr, uid, fields_list, context) - survey_obj = self.pool.get('survey') - msg = "" - name = "" - for sur in survey_obj.browse(cr, uid, context.get('active_ids', []), context=context): - name += "\n --> " + sur.title + "\n" - if sur.state != 'open': - msg += sur.title + "\n" - data['mail_subject'] = _("Invitation for %s") % (sur.title) - data['mail_subject_existing'] = _("Invitation for %s") % (sur.title) - data['mail_from'] = sur.responsible_id.email - if msg: - raise osv.except_osv(_('Warning!'), _('The following surveys are not in open state: %s') % msg) - data['mail'] = _(''' -Hello %%(name)s, \n\n -Would you please spent some of your time to fill-in our survey: \n%s\n -You can access this survey with the following parameters: - URL: %s - Your login ID: %%(login)s\n - Your password: %%(passwd)s\n -\n\n -Thanks,''') % (name, self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url', default='http://localhost:8069', context=context)) - return data - - def create_report(self, cr, uid, res_ids, report_name=False, file_name=False): - if not report_name or not res_ids: - return (False, Exception('Report name and Resources ids are required !!!')) - try: - ret_file_name = addons.get_module_resource('survey', 'report') + file_name + '.pdf' - service = netsvc.LocalService(report_name); - (result, format) = service.create(cr, uid, res_ids, {}, {}) - fp = open(ret_file_name, 'wb+'); - fp.write(result); - fp.close(); - except Exception,e: - print 'Exception in create report:',e - return (False, str(e)) - return (True, ret_file_name) - - - def action_send(self, cr, uid, ids, context=None): - if context is None: - context = {} - record = self.read(cr, uid, ids, [],context=context) - survey_ids = context.get('active_ids', []) - record = record and record[0] - partner_ids = record['partner_ids'] - user_ref= self.pool.get('res.users') - survey_ref= self.pool.get('survey') - mail_message = self.pool.get('mail.message') - - model_data_obj = self.pool.get('ir.model.data') - group_id = model_data_obj._get_id(cr, uid, 'base', 'group_survey_user') - group_id = model_data_obj.browse(cr, uid, group_id, context=context).res_id - - act_id = self.pool.get('ir.actions.act_window') - act_id = act_id.search(cr, uid, [('res_model', '=' , 'survey.name.wiz'), \ - ('view_type', '=', 'form')]) - out = "login,password\n" - skipped = 0 - existing = "" - created = "" - error = "" - new_user = [] - attachments = {} - current_sur = survey_ref.browse(cr, uid, context.get('active_id'), context=context) - exist_user = current_sur.invited_user_ids - if exist_user: - for use in exist_user: - new_user.append(use.id) - for id in survey_ref.browse(cr, uid, survey_ids): - report = self.create_report(cr, uid, [id.id], 'report.survey.form', id.title) - file = open(addons.get_module_resource('survey', 'report') + id.title +".pdf") - file_data = "" - while 1: - line = file.readline() - file_data += line - if not line: - break - file.close() - attachments[id.title +".pdf"] = file_data - os.remove(addons.get_module_resource('survey', 'report') + id.title +".pdf") - - for partner in self.pool.get('res.partner').browse(cr, uid, partner_ids): - if not partner.email: - skipped+= 1 - continue - user = user_ref.search(cr, uid, [('login', "=", partner.email)]) - if user: - if user[0] not in new_user: - new_user.append(user[0]) - user = user_ref.browse(cr, uid, user[0]) - user_ref.write(cr, uid, user.id, {'survey_id':[[6, 0, survey_ids]]}) - mail = record['mail']%{'login':partner.email, 'passwd':user.password, \ - 'name' : partner.name} - if record['send_mail_existing']: - vals = { - 'state': 'outgoing', - 'subject': record['mail_subject_existing'], - 'body_html': '
%s
' % mail, - 'email_to': partner.email, - 'email_from': record['mail_from'], - } - self.pool.get('mail.mail').create(cr, uid, vals, context=context) - existing+= "- %s (Login: %s, Password: %s)\n" % (user.name, partner.email, \ - user.password) - continue - - passwd= self.genpasswd() - out+= partner.email + ',' + passwd + '\n' - mail= record['mail'] % {'login' : partner.email, 'passwd' : passwd, 'name' : partner.name} - if record['send_mail']: - vals = { - 'state': 'outgoing', - 'subject': record['mail_subject'], - 'body_html': '
%s
' % mail, - 'email_to': partner.email, - 'email_from': record['mail_from'], - } - if attachments: - vals['attachment_ids'] = [(0,0,{'name': a_name, - 'datas_fname': a_name, - 'datas': str(a_content).encode('base64')}) - for a_name, a_content in attachments.items()] - ans = self.pool.get('mail.mail').create(cr, uid, vals, context=context) - if ans: - res_data = {'name': partner.name or _('Unknown'), - 'login': partner.email, - 'password': passwd, - 'address_id': partner.id, - 'groups_id': [[6, 0, [group_id]]], - 'action_id': act_id[0], - 'survey_id': [[6, 0, survey_ids]] - } - user = user_ref.create(cr, uid, res_data) - if user not in new_user: - new_user.append(user) - created+= "- %s (Login: %s, Password: %s)\n" % (partner.name or _('Unknown'),\ - partner.email, passwd) - else: - error+= "- %s (Login: %s, Password: %s)\n" % (partner.name or _('Unknown'),\ - partner.email, passwd) - - new_vals = {} - new_vals.update({'invited_user_ids':[[6,0,new_user]]}) - survey_ref.write(cr, uid, context.get('active_id'),new_vals) - note= "" - if created: - note += 'Created users:\n%s\n\n' % (created) - if existing: - note +='Already existing users:\n%s\n\n' % (existing) - if skipped: - note += "%d contacts where ignored (an email address is missing).\n\n" % (skipped) - if error: - note += 'Email not send successfully:\n====================\n%s\n' % (error) - context.update({'note' : note}) - return { - 'view_type': 'form', - "view_mode": 'form', - 'res_model': 'survey.send.invitation.log', - 'type': 'ir.actions.act_window', - 'target': 'new', - 'context': context - } -survey_send_invitation() - -class survey_send_invitation_log(osv.osv_memory): - _name = 'survey.send.invitation.log' - _columns = { - 'note' : fields.text('Log', readonly=1) - } - - def default_get(self, cr, uid, fields_list, context=None): - if context is None: - context = {} - data = super(survey_send_invitation_log, self).default_get(cr, uid, fields_list, context) - data['note'] = context.get('note', '') - return data - -survey_send_invitation_log() - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/survey/wizard/survey_send_invitation.xml b/addons/survey/wizard/survey_send_invitation.xml index a611973d772..f2879990b9c 100644 --- a/addons/survey/wizard/survey_send_invitation.xml +++ b/addons/survey/wizard/survey_send_invitation.xml @@ -1,74 +1,28 @@ - - - - - Send Invitation - survey.send.invitation - -
-
-
- - - - - - - - - - - - - - -
-
+ + + Survey - Send by Email + ${object.responsible_id and object.responsible_id.company_id and object.responsible_id.company_id.email or object.responsible_id.email or ''} + ${object.title} + ${object.lang} + + + +

Hello,

+

+ Would you please spent some of your time to fill-in our survey: + ${object.title} +

+ % if object.responsible_id: +

Thank you for choosing ${object.responsible_id.company_id.name or 'us'}!

+ % endif +
+ + ]]>
- - - - Send Invitation - survey.send.invitation - form - form - new - - - - - - - - survey send invitation log - survey.send.invitation.log - -
- - - -
-
-
- - - - - survey.send.invitation.log - survey.send.invitation.log - form - form - new -