[IMP] survey: change in progress

bzr revid: chm@openerp.com-20130212101328-j7s5c9jkln4pluvk
This commit is contained in:
Christophe Matthieu
2013-02-12 11:13:28 +01:00
parent 1723d0524f
commit 9bd0f29fe1
8 changed files with 94 additions and 320 deletions
+2 -2
View File
@@ -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:
+43 -3
View File
@@ -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
}
+1 -1
View File
@@ -321,7 +321,7 @@
<ul>
<li t-if="widget.view.is_action_enabled('edit')"><a type="edit">Configuration</a></li>
<li t-if="widget.view.is_action_enabled('edit')"><a name="edit_survey" string="Edit Survey" type="object" context="{'active':True,'edit' : True}">Edit Survey</a></li>
<li t-if="widget.view.is_action_enabled('edit')"><a name="%(action_view_survey_send_invitation)d" type="action">Collect informations</a></li>
<li t-if="widget.view.is_action_enabled('edit')"><a name="action_survey_sent" type="object">Collect informations</a></li>
<li><a name="fill_survey" type="object">Answer Survey</a></li>
<li t-if="widget.view.is_action_enabled('edit')"><a>Analysis</a></li>
</ul>
-1
View File
@@ -19,7 +19,6 @@
#
##############################################################################
import survey_send_invitation
import survey_print_statistics
import survey_print_answer
import survey_browse_answer
+18 -6
View File
@@ -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
}
+9
View File
@@ -33,5 +33,14 @@
<field name="target">new</field>
</record>
<record id="action_answer_survey" model="ir.actions.act_window">
<field name="type">ir.actions.act_window</field>
<field name="res_model">survey.question.wiz</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="context">{'survey_id': survey_id, 'survey_token': survey_token}</field>
</record>
</data>
</openerp>
@@ -1,240 +0,0 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>)
#
# 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 <http://www.gnu.org/licenses/>
#
##############################################################################
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': '<pre>%s</pre>' % 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': '<pre>%s</pre>' % 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:
+21 -67
View File
@@ -1,74 +1,28 @@
<?xml version="1.0" ?>
<openerp>
<data>
<!-- Survey send invitation Form View -->
<record id="view_survey_send_invitation" model="ir.ui.view">
<field name="name">Send Invitation</field>
<field name="model">survey.send.invitation</field>
<field name="arch" type="xml">
<form string="Send Invitation" version="7.0">
<header>
<button name="action_send" string="_Send" type="object" class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" special="cancel" />
</header>
<group col="4">
<separator string="Select Partner" colspan="4"/>
<field name="partner_ids" nolabel="1" colspan="4" widget="many2many_tags"/>
<separator colspan="4" string="Send Mail for New User"/>
<field name="send_mail" nolabel="1"/>
<field name="mail_subject" colspan="3"/>
<separator colspan="4" string="Send Reminder for Existing User"/>
<field name="send_mail_existing" nolabel="1"/>
<field name="mail_subject_existing" colspan="3"/>
<separator colspan="4"/>
<field name="mail_from" colspan="4"/>
<separator string="Message" colspan="4"/>
<field name="mail" nolabel="1" colspan="4"/>
</group>
</form>
</field>
<!--Survey send invitation Email template -->
<record id="email_template_survey" model="email.template">
<field name="name">Survey - Send by Email</field>
<field name="email_from">${object.responsible_id and object.responsible_id.company_id and object.responsible_id.company_id.email or object.responsible_id.email or ''}</field>
<field name="subject">${object.title}</field>
<field name="lang">${object.lang}</field>
<field name="model_id" ref="model_survey"/>
<field name="auto_delete" eval="True"/>
<field name="body_html"><![CDATA[
<div style="font-family: 'Lucica Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">
<p>Hello,</p>
<p>
Would you please spent some of your time to fill-in our survey:
<a href="#action=survey.action_answer_survey&survey_id=${object.id}&survey_token=__TOKEN__">${object.title}</a>
</p>
% if object.responsible_id:
<p>Thank you for choosing ${object.responsible_id.company_id.name or 'us'}!</p>
% endif
<br/>
</div>
]]></field>
</record>
<!-- Survey send invitation action -->
<record id="action_view_survey_send_invitation" model="ir.actions.act_window">
<field name="name">Send Invitation</field>
<field name="res_model">survey.send.invitation</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<act_window id="action_act_view_survey_send_invitation"
key2="client_action_multi" name="Send Invitations"
res_model="survey.send.invitation" src_model="survey"
view_mode="form" target="new" view_type="form" />
<!-- Survey send invitation Display Log Form View -->
<record id="view_survey_send_invitation_log" model="ir.ui.view">
<field name="name">survey send invitation log</field>
<field name="model">survey.send.invitation.log</field>
<field name="arch" type="xml">
<form string="User creation" version="7.0">
<group string="Results :">
<field name="note"/>
</group>
</form>
</field>
</record>
<!-- Survey send invitation action -->
<record id="action_view_survey_send_invitation_log" model="ir.actions.act_window">
<field name="name">survey.send.invitation.log</field>
<field name="res_model">survey.send.invitation.log</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</data>
</openerp>