diff --git a/addons/survey/__terp__.py b/addons/survey/__terp__.py index a06216b362d..367e6af5b8d 100644 --- a/addons/survey/__terp__.py +++ b/addons/survey/__terp__.py @@ -34,7 +34,6 @@ 'depends': ['base'], 'update_xml': ['survey_report.xml', 'survey_data.xml', - 'survey_wizard.xml', 'wizard/survey_selection.xml', 'wizard/survey_answer.xml', 'survey_view.xml', @@ -42,7 +41,8 @@ 'wizard/survey_print_statistics.xml', 'wizard/survey_print_answer.xml', 'wizard/survey_browse_answer.xml', - 'wizard/survey_print.xml'], + 'wizard/survey_print.xml', + 'wizard/survey_send_invitation.xml'], 'demo_xml': ['survey_demo.xml'], 'installable': True, 'active': False, diff --git a/addons/survey/survey_wizard.xml b/addons/survey/survey_wizard.xml deleted file mode 100644 index 1654a426b46..00000000000 --- a/addons/survey/survey_wizard.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - diff --git a/addons/survey/wizard/__init__.py b/addons/survey/wizard/__init__.py index 5221f26db0e..33ba7445f9e 100755 --- a/addons/survey/wizard/__init__.py +++ b/addons/survey/wizard/__init__.py @@ -20,7 +20,7 @@ # ############################################################################## -import wizard_survey +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 5a1ce7b9b33..1b837ecd394 100644 --- a/addons/survey/wizard/survey_answer.py +++ b/addons/survey/wizard/survey_answer.py @@ -1,5 +1,3 @@ - - # -*- encoding: utf-8 -*- ############################################################################## # @@ -73,11 +71,10 @@ class survey_question_wiz(osv.osv_memory): 'response':0 } wiz_id = surv_name_wiz.create(cr, uid, res_data) - #TODO: change to the browse, use read if only need to read few columns values - sur_name_rec = surv_name_wiz.read(cr, uid, wiz_id, []) + sur_name_rec = surv_name_wiz.browse(cr, uid, wiz_id) context.update({'sur_name_id' :wiz_id}) else: - sur_name_rec = surv_name_wiz.read(cr, uid, context.get('sur_name_id', False)) + sur_name_rec = surv_name_wiz.browse(cr, uid, context.get('sur_name_id', False)) if context.has_key('active_id'): context.pop('active_id') @@ -85,7 +82,7 @@ class survey_question_wiz(osv.osv_memory): survey_id = context.get('survey_id', False) #TODO: change to the browse, use read if only need to read few columns values # this will fire select * from quary - sur_rec = survey_obj.read(cr, uid, survey_id, []) + sur_rec = survey_obj.read(cr, uid, survey_id, ['page_ids', 'max_response_limit', 'state', 'tot_start_survey', 'tot_comp_survey', 'send_response']) p_id = sur_rec['page_ids'] total_pages = len(p_id) pre_button = False @@ -94,44 +91,45 @@ class survey_question_wiz(osv.osv_memory): if context.has_key('response_id') and context.get('response_id', False) and int(context['response_id'][0]) > 0: readonly = 1 - if not sur_name_rec['page_no'] + 1 : + if not sur_name_rec.page_no + 1 : surv_name_wiz.write(cr, uid, [context.get('sur_name_id',False)], {'store_ans':{}}) - sur_name_read = surv_name_wiz.read(cr, uid, context.get('sur_name_id',False)) - page_number = int(sur_name_rec['page_no']) - if sur_name_read['transfer'] or not sur_name_rec['page_no'] + 1: + sur_name_read = surv_name_wiz.browse(cr, uid, context.get('sur_name_id',False)) + page_number = int(sur_name_rec.page_no) + if sur_name_read.transfer or not sur_name_rec.page_no + 1: surv_name_wiz.write(cr, uid, [context.get('sur_name_id', False)], {'transfer':False}) flag = False fields = {} - if sur_name_read['page'] == "next" or sur_name_rec['page_no'] == -1: - if total_pages > sur_name_rec['page_no'] + 1: - if ((context.has_key('active') and not context.get('active', False)) or not context.has_key('active')) and not sur_name_rec['page_no'] + 1: + if sur_name_read.page == "next" or sur_name_rec.page_no == -1: + if total_pages > sur_name_rec.page_no + 1: + if ((context.has_key('active') and not context.get('active', False)) or not context.has_key('active')) and not sur_name_rec.page_no + 1: if sur_rec['state'] != "open" : raise osv.except_osv(_('Warning !'),_("You can not give answer because of survey is not open for answer")) cr.execute('select count(id) from survey_history where user_id=%s\ and survey_id=%s' % (uid,survey_id)) res = cr.fetchone()[0] - user_limit = survey_obj.read(cr, uid, survey_id, ['response_user'])['response_user'] + user_limit = survey_obj.browse(cr, uid, survey_id) + user_limit = user_limit.response_user if user_limit and res >= user_limit: raise osv.except_osv(_('Warning !'),_("You can not give response for this survey more than %s times") % (user_limit)) - if sur_rec['max_response_limit'] and sur_rec['max_response_limit'] <= sur_rec['tot_start_survey'] and not sur_name_rec['page_no'] + 1: + if sur_rec['max_response_limit'] and sur_rec['max_response_limit'] <= sur_rec['tot_start_survey'] and not sur_name_rec.page_no + 1: survey_obj.write(cr, uid, survey_id, {'state':'close', 'date_close':strftime("%Y-%m-%d %H:%M:%S")}) - p_id = p_id[sur_name_rec['page_no'] + 1] - surv_name_wiz.write(cr, uid, [context.get('sur_name_id', False)], {'page_no' : sur_name_rec['page_no'] + 1}) + p_id = p_id[sur_name_rec.page_no + 1] + surv_name_wiz.write(cr, uid, [context.get('sur_name_id', False)], {'page_no' : sur_name_rec.page_no + 1}) flag = True page_number += 1 - if sur_name_rec['page_no'] > - 1: + if sur_name_rec.page_no > - 1: pre_button = True else: - if sur_name_rec['page_no'] != 0: - p_id = p_id[sur_name_rec['page_no'] - 1] - surv_name_wiz.write(cr, uid, [context.get('sur_name_id', False)], {'page_no' : sur_name_rec['page_no'] - 1}) + if sur_name_rec.page_no != 0: + p_id = p_id[sur_name_rec.page_no - 1] + surv_name_wiz.write(cr, uid, [context.get('sur_name_id', False)], {'page_no' : sur_name_rec.page_no - 1}) flag = True page_number -= 1 - if sur_name_rec['page_no'] > 1: + if sur_name_rec.page_no > 1: pre_button = True if flag: pag_rec = page_obj.read(cr, uid, p_id) @@ -173,7 +171,7 @@ class survey_question_wiz(osv.osv_memory): xml_group = etree.SubElement(xml_form, 'group', {'col': '1', 'colspan': '2'}) etree.SubElement(xml_group, 'separator', {'string': star+to_xml(separator_string), 'colspan': '3'}) xml_group1 = etree.SubElement(xml_form, 'group', {'col': '2', 'colspan': '2'}) - context.update({'question_id' : tools.ustr(que),'page_number' : sur_name_rec['page_no'] , 'transfer' : sur_name_read['transfer'], 'page_id' : p_id}) + context.update({'question_id' : tools.ustr(que),'page_number' : sur_name_rec.page_no , 'transfer' : sur_name_read.transfer, 'page_id' : p_id}) etree.SubElement(xml_group1, 'button', {'string' :'','icon': "gtk-edit", 'type' :'object', 'name':"action_edit_question", 'context' : tools.ustr(context)}) etree.SubElement(xml_group1, 'button', {'string' :'','icon': "gtk-delete", 'type' :'object','name':"action_delete_question", 'context' : tools.ustr(context)}) else: @@ -361,7 +359,7 @@ class survey_question_wiz(osv.osv_memory): if context.has_key('active') and context.get('active',False) and context.has_key('edit'): etree.SubElement(xml_form, 'separator', {'string' : '','colspan': '4'}) - context.update({'page_id' : tools.ustr(p_id),'page_number' : sur_name_rec['page_no'] , 'transfer' : sur_name_read['transfer']}) + context.update({'page_id' : tools.ustr(p_id),'page_number' : sur_name_rec.page_no , 'transfer' : sur_name_read.transfer}) xml_group3 = etree.SubElement(xml_form, 'group', {'col': '4', 'colspan': '4'}) etree.SubElement(xml_group3, 'button', {'string' :'Add Page','icon': "gtk-new", 'type' :'object','name':"action_new_page", 'context' : tools.ustr(context)}) etree.SubElement(xml_group3, 'button', {'string' :'Edit Page','icon': "gtk-edit", 'type' :'object','name':"action_edit_page", 'context' : tools.ustr(context)}) @@ -374,7 +372,7 @@ class survey_question_wiz(osv.osv_memory): result['context'] = context else: survey_obj.write(cr, uid, survey_id, {'tot_comp_survey' : sur_rec['tot_comp_survey'] + 1}) - sur_response_obj.write(cr, uid, [sur_name_read['response']], {'state' : 'done'}) + sur_response_obj.write(cr, uid, [sur_name_read.response], {'state' : 'done'}) if sur_rec['send_response']: survey_data = survey_obj.browse(cr, uid, int(survey_id)) @@ -467,7 +465,6 @@ class survey_question_wiz(osv.osv_memory): tot_page_id = self.pool.get('survey').browse(cr, uid, context.get('survey_id',False)) tot_per = (float(100) * (int(field.split('_')[2]) + 1) / len(tot_page_id.page_ids)) value[field] = tot_per - r esponse_obj = self.pool.get('survey.response') surv_name_wiz = self.pool.get('survey.name.wiz') @@ -773,7 +770,7 @@ class survey_question_wiz(osv.osv_memory): if not select_count: resp_obj.write(cr, uid, resp_id, {'state':'skip'}) - if que_rec[also'numeric_required_sum'] and numeric_sum > que_rec['numeric_required_sum']: + if que_rec['numeric_required_sum'] and numeric_sum > que_rec['numeric_required_sum']: for res in resp_id_list: sur_name_read['store_ans'].pop(res) raise osv.except_osv(_('Error re !'), _("'" + que_rec['question'] + "' " + tools.ustr(que_rec['numeric_required_sum_err_msg']))) diff --git a/addons/survey/wizard/survey_send_invitation.py b/addons/survey/wizard/survey_send_invitation.py new file mode 100644 index 00000000000..7286a017560 --- /dev/null +++ b/addons/survey/wizard/survey_send_invitation.py @@ -0,0 +1,213 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (). All Rights Reserved +# $Id$ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 General Public License for more details. +# +# You should have received a copy of the GNU 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 osv import fields +from osv import osv +import tools +from tools.translate import _ +import netsvc + + +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', "Response", 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, required=1), + 'mail_subject_existing' : fields.char('Subject', size=256, required=1), + '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, + 'mail_subject' : lambda *a: "New user account.", + 'mail_subject_existing' : lambda *a: "User account info.", + 'mail_from' : lambda *a: tools.config['email_from'] + } + + 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): + 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', [])): + name += "\t --> " + sur.title + "\n" + if sur.state != 'open': + msg += sur.title + "\n" + if msg: + raise osv.except_osv(_('Error !'), _('%sSurvey is not in open state') % msg) + data['mail'] = '''Hello %(name)s, \n\n We are inviting you for following survey. \ + \n ''' + name + '''\n Your login ID: %(login)s, Your password: %(passwd)s + \n link :- http://'''+ str(socket.gethostname()) + ''':8080 \n\n Thanks,''' + 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 = tools.config['addons_path'] + '/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): + record = self.read(cr, uid, ids, []) + 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') + + model_data_obj = self.pool.get('ir.model.data') + group_id = model_data_obj._get_id(cr, uid, 'survey', '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= "" + res_user = "" + user_exists = False + attachments = [] + 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(tools.config['addons_path'] + '/survey/report/' + id.title +".pdf") + file_data = "" + while 1: + line = file.readline() + file_data += line + if not line: + break + attachments.append((id.title +".pdf",file_data)) + file.close() + os.remove(tools.config['addons_path'] + '/survey/report/' + id.title + ".pdf") + + for partner in self.pool.get('res.partner').browse(cr, uid, partner_ids): + for addr in partner.address: + if not addr.email: + skipped+= 1 + continue + user = user_ref.search(cr, uid, [('login', "=", addr.email)]) + if user: + 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':addr.email, 'passwd':user.password, \ + 'name' : addr.name} + if record['send_mail_existing']: + tools.email_send(record['mail_from'], [addr.email] , \ + record['mail_subject_existing'] , mail) + existing+= "- %s (Login: %s, Password: %s)\n" % (user.name, addr.email, \ + user.password) + continue + user_id =user_ref.search(cr, uid, [('address_id', '=', addr.id)]) + if user_id: + for user_email in user_ref.browse(cr, uid, user_id): + mail= record['mail']%{'login':user_email.login, \ + 'passwd':user_email.password, 'name' : addr.name} + if record['send_mail_existing']: + tools.email_send(record['mail_from'], [addr.email],\ + record['mail_subject_existing'], mail) + res_user+= "- %s (Login: %s, Password: %s)\n" % \ + (user_email.name, user_email.login, user_email.password) + continue + passwd= self.genpasswd() + out+= addr.email + ',' + passwd + '\n' + mail= record['mail'] % {'login' : addr.email, 'passwd' : passwd, 'name' : addr.name} + if record['send_mail']: + ans = tools.email_send(record['mail_from'], [addr.email], \ + record['mail_subject'], mail,attach = attachments) + + if ans: + res_data = {'name' : addr.name or 'Unknown', + 'login' : addr.email, + 'password' : passwd, + 'address_id' : addr.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) + created+= "- %s (Login: %s, Password: %s)\n" % (addr.name or 'Unknown',\ + addr.email, passwd) + else: + error+= "- %s (Login: %s, Password: %s)\n" % (addr.name or 'Unknown',\ + addr.email, passwd) + 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 += 'E-Mail not send successfully:\n====================\n%s\n' % (error) + if res_user: + note += 'E-mail ID used the following user:\n====================\n%s\n' % (res_user) + 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): + 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: \ No newline at end of file diff --git a/addons/survey/wizard/survey_send_invitation.xml b/addons/survey/wizard/survey_send_invitation.xml new file mode 100644 index 00000000000..3762475e420 --- /dev/null +++ b/addons/survey/wizard/survey_send_invitation.xml @@ -0,0 +1,77 @@ + + + + + + + + Send Invitation + survey.send.invitation + form + +
+ + + + + + + + + + + + + +