2013-02-14 08:20:38 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
2013-02-26 11:34:03 +00:00
# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
2013-02-14 08:20:38 +00:00
#
# This program is free software: you can redistribute it and/or modify
2013-02-26 11:34:03 +00:00
# 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.
2013-02-14 08:20:38 +00:00
#
# 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
2013-02-26 11:34:03 +00:00
# GNU General Public License for more details.
2013-02-14 08:20:38 +00:00
#
2013-02-26 11:34:03 +00:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
2013-02-14 08:20:38 +00:00
#
##############################################################################
from openerp . osv import fields , osv
2013-02-20 12:33:28 +00:00
from openerp . tools . safe_eval import safe_eval
2013-02-15 13:32:34 +00:00
2013-02-28 13:53:29 +00:00
from templates import TemplateHelper
2013-02-26 14:54:59 +00:00
2013-02-28 13:53:29 +00:00
from datetime import date , datetime , timedelta
2013-02-26 14:54:59 +00:00
2013-04-15 14:57:43 +00:00
import logging
_logger = logging . getLogger ( __name__ )
2013-03-11 09:39:30 +00:00
2013-02-14 17:07:36 +00:00
class gamification_goal_type ( osv . Model ) :
""" Goal type definition
A goal type defining a way to set an objective and evaluate it
Each module wanting to be able to set goals to the users needs to create
a new gamification_goal_type
"""
_name = ' gamification.goal.type '
_description = ' Gamification goal type '
2013-04-10 08:49:40 +00:00
def _get_suffix ( self , cr , uid , ids , field_name , arg , context = None ) :
res = dict . fromkeys ( ids , 0 )
for goal in self . browse ( cr , uid , ids , context ) :
if goal . unit and not goal . monetary :
res [ goal . id ] = goal . unit
elif goal . monetary :
# use the current user's company currency
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context )
if goal . unit :
res [ goal . id ] = " %s %s " % ( user . company_id . currency_id . symbol , goal . unit )
else :
res [ goal . id ] = user . company_id . currency_id . symbol
else :
res [ goal . id ] = " "
return res
2013-02-14 17:07:36 +00:00
_columns = {
2013-04-08 13:17:29 +00:00
' name ' : fields . char ( ' Goal Type Name ' , required = True , translate = True ) ,
' description ' : fields . text ( ' Goal Description ' ) ,
2013-04-10 08:49:40 +00:00
2013-04-08 13:17:29 +00:00
' monetary ' : fields . boolean ( ' Monetary Value ' , help = " The target and current value are defined in the company currency. " ) ,
' unit ' : fields . char ( ' Suffix ' ,
2013-03-11 11:04:23 +00:00
help = " The unit of the target and current values " , translate = True ) ,
2013-04-10 08:49:40 +00:00
' full_suffix ' : fields . function ( _get_suffix , type = " char " ,
string = " Full Suffix " , help = " The currency and suffix field " ) ,
2013-02-20 12:33:28 +00:00
' computation_mode ' : fields . selection ( [
2013-04-08 13:17:29 +00:00
( ' manually ' , ' Recorded manually ' ) ,
( ' count ' , ' Automatic: number of records ' ) ,
( ' sum ' , ' Automatic: sum on a field ' ) ,
2013-04-15 14:57:43 +00:00
( ' python ' , ' Automatic: execute a specific Python code ' ) ,
2013-02-20 12:33:28 +00:00
] ,
2013-04-08 13:17:29 +00:00
string = " Computation Mode " ,
2013-02-14 17:07:36 +00:00
required = True ) ,
2013-04-16 10:27:24 +00:00
' display_mode ' : fields . selection ( [
( ' progress ' , ' Progressive (using numerical values) ' ) ,
( ' checkbox ' , ' Checkbox (done or not-done) ' ) ,
] ,
string = " Displayed as " , required = True ) ,
2013-04-15 14:57:43 +00:00
2013-02-18 15:16:07 +00:00
' model_id ' : fields . many2one ( ' ir.model ' ,
2013-02-19 13:23:06 +00:00
string = ' Model ' ,
2013-03-11 11:04:23 +00:00
help = ' The model object for the field to evaluate ' ) ,
2013-02-18 15:16:07 +00:00
' field_id ' : fields . many2one ( ' ir.model.fields ' ,
2013-04-08 13:17:29 +00:00
string = ' Field to Sum ' ,
2013-03-11 11:04:23 +00:00
help = ' The field containing the value to evaluate ' ) ,
2013-02-18 15:16:07 +00:00
' field_date_id ' : fields . many2one ( ' ir.model.fields ' ,
2013-04-08 13:17:29 +00:00
string = ' Date Field ' ,
2013-02-15 09:16:38 +00:00
help = ' The date to use for the time period evaluated ' ) ,
2013-04-08 13:17:29 +00:00
' domain ' : fields . char ( " Filter Domain " ,
2013-02-18 09:56:44 +00:00
help = " Technical filters rules to apply " ,
2013-04-10 08:49:40 +00:00
required = True ) ,
2013-04-15 14:57:43 +00:00
' compute_code ' : fields . char ( ' Compute Code ' ,
help = " The name of the python method that will be executed to verify if a user can receive this badge. " ) ,
2013-03-11 11:04:23 +00:00
' condition ' : fields . selection ( [
2013-04-08 13:17:29 +00:00
( ' higher ' , ' The higher the better ' ) ,
( ' lower ' , ' The lower the better ' )
2013-02-20 12:33:28 +00:00
] ,
2013-04-08 13:17:29 +00:00
string = ' Goal Performance ' ,
2013-02-15 09:16:38 +00:00
help = ' A goal is considered as completed when the current value is compared to the value to reach ' ,
required = True ) ,
2013-03-11 11:04:23 +00:00
' sequence ' : fields . integer ( ' Sequence ' ,
2013-02-15 09:16:38 +00:00
help = ' Sequence number for ordering ' ,
2013-02-14 17:07:36 +00:00
required = True ) ,
2013-04-02 13:45:01 +00:00
2013-04-08 13:17:29 +00:00
' action_id ' : fields . many2one ( ' ir.actions.act_window ' ,
string = " Action " ,
help = " The action that will be called to update the goal value. " ) ,
' res_id_field ' : fields . char ( " ID Field of user " ,
help = " The field name on the user profile (res.users) containing the value for res_id for action. " )
2013-02-14 08:20:38 +00:00
}
2013-03-26 15:45:53 +00:00
2013-02-15 14:54:15 +00:00
_order = ' sequence '
2013-02-14 17:07:36 +00:00
_defaults = {
2013-02-18 12:44:03 +00:00
' sequence ' : 1 ,
2013-03-06 14:45:44 +00:00
' condition ' : ' higher ' ,
2013-03-26 15:45:53 +00:00
' computation_mode ' : ' manually ' ,
' domain ' : " [] " ,
' monetary ' : False ,
2013-04-16 10:27:24 +00:00
' display_mode ' : ' progress ' ,
2013-02-14 08:20:38 +00:00
}
2013-02-20 12:33:28 +00:00
2013-02-14 17:07:36 +00:00
class gamification_goal ( osv . Model ) :
""" Goal instance for a user
2013-02-21 13:50:00 +00:00
An individual goal for a user on a specified time period """
2013-02-14 17:07:36 +00:00
_name = ' gamification.goal '
_description = ' Gamification goal instance '
_inherit = ' mail.thread '
2013-02-15 14:54:15 +00:00
def _get_completeness ( self , cr , uid , ids , field_name , arg , context = None ) :
2013-02-27 10:59:56 +00:00
""" Return the percentage of completeness of the goal, between 0 and 100 """
2013-02-15 14:54:15 +00:00
res = { }
for goal in self . browse ( cr , uid , ids , context ) :
2013-02-27 10:59:56 +00:00
if goal . current > 0 :
res [ goal . id ] = min ( 100 , round ( 100.0 * goal . current / goal . target_goal , 2 ) )
else :
res [ goal . id ] = 0.0
2013-03-07 11:15:57 +00:00
2013-02-15 14:54:15 +00:00
return res
2013-02-20 16:17:55 +00:00
2013-02-20 16:05:17 +00:00
def on_change_type_id ( self , cr , uid , ids , type_id = False , context = None ) :
goal_type = self . pool . get ( ' gamification.goal.type ' )
if not type_id :
2013-04-15 15:50:14 +00:00
return { ' value ' : { ' type_id ' : False } }
2013-02-20 16:05:17 +00:00
goal_type = goal_type . browse ( cr , uid , type_id , context = context )
2013-04-15 15:50:14 +00:00
ret = { ' value ' : { ' computation_mode ' : goal_type . computation_mode } }
2013-02-20 16:05:17 +00:00
return ret
2013-02-15 14:54:15 +00:00
2013-02-14 17:07:36 +00:00
_columns = {
2013-03-22 15:41:30 +00:00
' type_id ' : fields . many2one ( ' gamification.goal.type ' ,
2013-02-18 13:06:56 +00:00
string = ' Goal Type ' ,
2013-02-19 14:37:22 +00:00
required = True ,
ondelete = " cascade " ) ,
2013-03-12 14:15:31 +00:00
' user_id ' : fields . many2one ( ' res.users ' , string = ' User ' , required = True ) ,
2013-02-20 12:33:28 +00:00
' planline_id ' : fields . many2one ( ' gamification.goal.planline ' ,
2013-02-21 12:57:59 +00:00
string = ' Goal Planline ' ,
2013-02-19 14:37:22 +00:00
ondelete = " cascade " ) ,
2013-03-12 14:15:31 +00:00
' plan_id ' : fields . related ( ' planline_id ' , ' plan_id ' ,
string = " Plan " ,
type = ' many2one ' ,
2013-03-12 14:34:51 +00:00
relation = ' gamification.goal.plan ' ,
2013-03-12 14:15:31 +00:00
store = True ) ,
' start_date ' : fields . date ( ' Start Date ' ) ,
2013-04-12 12:19:13 +00:00
' end_date ' : fields . date ( ' End Date ' ) , # no start and end = always active
2013-03-12 14:15:31 +00:00
' target_goal ' : fields . float ( ' To Reach ' ,
2013-02-14 17:07:36 +00:00
required = True ,
2013-04-12 12:19:13 +00:00
track_visibility = ' always ' ) , # no goal = global index
2013-03-12 14:15:31 +00:00
' current ' : fields . float ( ' Current ' ,
2013-02-14 17:07:36 +00:00
required = True ,
2013-04-12 12:19:13 +00:00
track_visibility = ' always ' ) ,
2013-02-15 14:54:15 +00:00
' completeness ' : fields . function ( _get_completeness ,
type = ' float ' ,
2013-02-20 12:33:28 +00:00
string = ' Completeness ' ) ,
' state ' : fields . selection ( [
2013-02-22 13:40:24 +00:00
( ' draft ' , ' Draft ' ) ,
2013-02-20 12:33:28 +00:00
( ' inprogress ' , ' In progress ' ) ,
( ' inprogress_update ' , ' In progress (to update) ' ) ,
( ' reached ' , ' Reached ' ) ,
( ' failed ' , ' Failed ' ) ,
( ' canceled ' , ' Canceled ' ) ,
] ,
2013-02-15 14:16:51 +00:00
string = ' State ' ,
2013-02-14 17:07:36 +00:00
required = True ,
2013-03-11 11:04:23 +00:00
track_visibility = ' always ' ) ,
2013-02-20 16:05:17 +00:00
2013-03-13 09:51:54 +00:00
' computation_mode ' : fields . related ( ' type_id ' , ' computation_mode ' ,
2013-04-15 15:50:14 +00:00
type = ' char ' ,
2013-02-20 16:05:17 +00:00
string = " Type computation mode " ) ,
2013-03-12 14:15:31 +00:00
' remind_update_delay ' : fields . integer ( ' Remind delay ' ,
2013-02-20 16:05:17 +00:00
help = " The number of days after which the user assigned to a manual goal will be reminded. Never reminded if no value is specified. " ) ,
2013-04-12 12:19:13 +00:00
' last_update ' : fields . date ( ' Last Update ' ,
2013-03-07 09:54:53 +00:00
help = " In case of manual goal, reminders are sent if the goal as not been updated for a while (defined in goal plan). Ignored in case of non-manual goal or goal not linked to a plan. " ) ,
2013-03-11 11:04:23 +00:00
2013-04-12 10:09:33 +00:00
' type_description ' : fields . related ( ' type_id ' , ' description ' ,
type = ' char ' , string = ' Type Description ' , readonly = True ) ,
2013-03-07 13:44:47 +00:00
' type_unit ' : fields . related ( ' type_id ' , ' unit ' ,
2013-04-12 10:09:33 +00:00
type = ' char ' , string = ' Type Description ' , readonly = True ) ,
2013-03-07 13:44:47 +00:00
' type_condition ' : fields . related ( ' type_id ' , ' condition ' ,
2013-04-12 10:09:33 +00:00
type = ' char ' , string = ' Type Condition ' , readonly = True ) ,
2013-04-10 15:30:31 +00:00
' type_suffix ' : fields . related ( ' type_id ' , ' full_suffix ' ,
2013-04-12 10:09:33 +00:00
type = " char " , string = " Suffix " , readonly = True ) ,
2013-04-16 10:27:24 +00:00
' type_display ' : fields . related ( ' type_id ' , ' display_mode ' ,
type = " char " , string = " Display Mode " , readonly = True ) ,
2013-02-14 17:07:36 +00:00
}
2013-02-14 08:20:38 +00:00
2013-02-14 17:07:36 +00:00
_defaults = {
' current ' : 0 ,
2013-02-22 13:40:24 +00:00
' state ' : ' draft ' ,
2013-02-18 13:48:03 +00:00
' start_date ' : fields . date . today ,
2013-02-14 17:07:36 +00:00
}
2013-04-18 16:05:12 +00:00
_order = ' create_date, end_date desc, type_id, id '
2013-02-14 08:20:38 +00:00
2013-02-22 09:56:15 +00:00
def update ( self , cr , uid , ids , context = None ) :
2013-02-20 16:05:17 +00:00
""" Update the goals to recomputes values and change of states
2013-03-01 11:27:20 +00:00
If a manual goal is not updated for enough time , the user will be
reminded to do so ( done only once , in ' inprogress ' state ) .
If a goal reaches the target value , the status is set to reached
2013-02-20 16:05:17 +00:00
If the end date is passed ( at least + 1 day , time not considered ) without
2013-02-22 09:56:15 +00:00
the target value being reached , the goal is set as failed . """
2013-03-22 11:13:44 +00:00
if context is None : context = { }
2013-03-12 14:15:31 +00:00
2013-03-22 11:13:44 +00:00
for goal in self . browse ( cr , uid , ids , context = context ) :
2013-03-12 14:15:31 +00:00
if goal . state in ( ' draft ' , ' canceled ' ) :
2013-03-06 14:45:44 +00:00
# skip if goal draft or canceled
2013-02-22 09:47:45 +00:00
continue
2013-02-20 14:22:48 +00:00
2013-02-20 16:17:55 +00:00
if goal . type_id . computation_mode == ' manually ' :
2013-03-12 14:15:31 +00:00
towrite = { ' current ' : goal . current }
2013-02-20 16:17:55 +00:00
# check for remind to update
if goal . remind_update_delay and goal . last_update :
delta_max = timedelta ( days = goal . remind_update_delay )
2013-03-12 14:15:31 +00:00
last_update = datetime . strptime ( goal . last_update , ' % Y- % m- %d ' ) . date ( )
2013-02-27 16:56:00 +00:00
if date . today ( ) - last_update > delta_max and goal . state == ' inprogress ' :
2013-02-20 16:17:55 +00:00
towrite [ ' state ' ] = ' inprogress_update '
2013-02-20 14:22:48 +00:00
2013-02-27 10:59:56 +00:00
# generate a remind report
2013-02-28 13:53:29 +00:00
template_env = TemplateHelper ( )
body_html = template_env . get_template ( ' reminder.mako ' ) . render ( { ' object ' : goal } )
2013-02-28 09:42:00 +00:00
self . message_post ( cr , uid , goal . id ,
2013-02-27 17:04:42 +00:00
body = body_html ,
partner_ids = [ goal . user_id . partner_id . id ] ,
2013-02-28 09:42:00 +00:00
context = context ,
subtype = ' mail.mt_comment ' )
2013-03-11 11:04:23 +00:00
2013-04-15 14:57:43 +00:00
elif goal . type_id . computation_mode == ' python ' :
values = { ' cr ' : cr , ' uid ' : uid , ' context ' : context , ' self ' : self . pool . get ( ' gamification.goal.type ' ) }
result = safe_eval ( goal . type_id . compute_code , values , { } )
if type ( result ) == float or type ( result ) == int :
towrite = { ' current ' : result }
else :
_logger . exception ( _ ( ' Unvalid return content from the evaluation of %s ' % str ( goal . type_id . compute_code ) ) )
# raise osv.except_osv(_('Error!'), _('Unvalid return content from the evaluation of %s' % str(goal.type_id.compute_code)))
2013-03-11 11:04:23 +00:00
else : # count or sum
2013-02-20 14:22:48 +00:00
obj = self . pool . get ( goal . type_id . model_id . model )
field_date_name = goal . type_id . field_date_id . name
2013-03-11 11:04:23 +00:00
domain = safe_eval ( goal . type_id . domain ,
2013-03-12 14:15:31 +00:00
{ ' user_id ' : goal . user_id . id } )
2013-03-13 16:43:25 +00:00
if goal . start_date and field_date_name :
2013-02-20 14:22:48 +00:00
domain . append ( ( field_date_name , ' >= ' , goal . start_date ) )
2013-03-13 16:43:25 +00:00
if goal . end_date and field_date_name :
2013-02-20 14:22:48 +00:00
domain . append ( ( field_date_name , ' <= ' , goal . end_date ) )
2013-02-20 16:17:55 +00:00
if goal . type_id . computation_mode == ' sum ' :
field_name = goal . type_id . field_id . name
res = obj . read_group ( cr , uid , domain , [ field_name ] ,
2013-03-12 14:15:31 +00:00
[ ' ' ] , context = context )
if res [ 0 ] [ field_name ] :
towrite = { ' current ' : res [ 0 ] [ field_name ] }
else :
# avoid false when no result
towrite = { ' current ' : 0 }
2013-03-11 11:04:23 +00:00
else : # computation mode = count
2013-02-20 16:17:55 +00:00
res = obj . search ( cr , uid , domain , context = context )
towrite = { ' current ' : len ( res ) }
2013-03-11 11:04:23 +00:00
2013-02-20 16:17:55 +00:00
# check goal target reached
2013-03-06 14:45:44 +00:00
if ( goal . type_id . condition == ' higher ' \
2013-02-20 16:17:55 +00:00
and towrite [ ' current ' ] > = goal . target_goal ) \
2013-03-06 14:45:44 +00:00
or ( goal . type_id . condition == ' lower ' \
2013-02-20 16:17:55 +00:00
and towrite [ ' current ' ] < = goal . target_goal ) :
2013-02-20 14:22:48 +00:00
towrite [ ' state ' ] = ' reached '
2013-02-20 16:17:55 +00:00
# check goal failure
2013-02-20 16:05:17 +00:00
elif goal . end_date and fields . date . today ( ) > goal . end_date :
2013-02-20 14:22:48 +00:00
towrite [ ' state ' ] = ' failed '
self . write ( cr , uid , [ goal . id ] , towrite , context = context )
return True
2013-02-27 11:12:20 +00:00
def action_start ( self , cr , uid , ids , context = None ) :
2013-03-01 11:27:20 +00:00
""" Mark a goal as started.
This should only be used when creating goals manually ( in draft state ) """
2013-02-27 11:12:20 +00:00
self . write ( cr , uid , ids , { ' state ' : ' inprogress ' } , context = context )
return self . update ( cr , uid , ids , context = context )
2013-02-21 10:46:46 +00:00
2013-02-15 16:02:04 +00:00
def action_reach ( self , cr , uid , ids , context = None ) :
2013-03-01 11:27:20 +00:00
""" Mark a goal as reached.
If the target goal condition is not met , the state will be reset to In
Progress at the next goal update until the end date . """
2013-02-15 16:02:04 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' reached ' } , context = context )
def action_fail ( self , cr , uid , ids , context = None ) :
2013-03-01 11:27:20 +00:00
""" Set the state of the goal to failed.
A failed goal will be ignored in future checks . """
2013-02-15 16:02:04 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' failed ' } , context = context )
def action_cancel ( self , cr , uid , ids , context = None ) :
2013-03-01 11:27:20 +00:00
""" Reset the completion after setting a goal as reached or failed.
This is only the current state , if the date and / or target criterias
2013-03-12 14:15:31 +00:00
match the conditions for a change of state , this will be applied at the
2013-03-01 11:27:20 +00:00
next goal update . """
2013-02-15 16:02:04 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' inprogress ' } , context = context )
2013-02-28 10:14:46 +00:00
def create ( self , cr , uid , vals , context = None ) :
""" Overwrite the create method to add a ' just_created ' field to True """
context = context or { }
context [ ' just_created ' ] = True
return super ( gamification_goal , self ) . create ( cr , uid , vals , context = context )
2013-03-12 14:15:31 +00:00
2013-02-26 15:23:25 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2013-03-01 11:27:20 +00:00
""" Overwrite the write method to update the last_update field to today
2013-03-12 14:15:31 +00:00
If the current value is changed and the report frequency is set to On
2013-03-13 10:12:58 +00:00
change , a report is generated """
2013-03-12 14:15:31 +00:00
vals [ ' last_update ' ] = fields . date . today ( )
for goal in self . browse ( cr , uid , ids , context = context ) :
2013-02-28 10:14:46 +00:00
2013-02-28 11:28:30 +00:00
if ' current ' in vals :
2013-02-28 10:14:46 +00:00
if ' just_created ' in context :
# new goals should not be reported
continue
2013-03-12 14:15:31 +00:00
if goal . plan_id and goal . plan_id . report_message_frequency == ' onchange ' :
2013-02-28 09:22:47 +00:00
plan_obj = self . pool . get ( ' gamification.goal.plan ' )
2013-03-12 14:15:31 +00:00
plan_obj . report_progress ( cr , uid , goal . plan_id , users = [ goal . user_id ] , context = context )
2013-02-28 10:14:46 +00:00
return super ( gamification_goal , self ) . write ( cr , uid , ids , vals , context = context )
2013-03-24 13:46:58 +00:00
def get_action ( self , cr , uid , goal_id , context = None ) :
""" Get the ir.action related to update the goal
In case of a manual goal , should return a wizard to update the value
2013-03-24 18:05:57 +00:00
: return : action description in a dictionnary
2013-03-24 13:46:58 +00:00
"""
goal = self . browse ( cr , uid , goal_id , context = context )
2013-03-27 10:50:40 +00:00
if goal . type_id . action_id :
2013-04-16 13:48:47 +00:00
action = goal . type_id . action_id . read ( ) [ 0 ]
2013-04-08 13:17:29 +00:00
if goal . type_id . res_id_field :
current_user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context )
# eg : company_id.id
field_names = goal . type_id . res_id_field . split ( ' . ' )
res = current_user . __getitem__ ( field_names [ 0 ] )
for field_name in field_names [ 1 : ] :
res = res . __getitem__ ( field_name )
action [ ' res_id ' ] = res
2013-04-16 13:48:47 +00:00
# if one element to display, should see it in form mode if possible
views = action [ ' views ' ]
for ( view_id , mode ) in action [ ' views ' ] :
if mode == " form " :
views = [ ( view_id , mode ) ]
action [ ' views ' ] = views
2013-04-08 13:17:29 +00:00
action [ ' target ' ] = ' new '
return action
2013-03-27 10:50:40 +00:00
2013-03-24 13:46:58 +00:00
if goal . computation_mode == ' manually ' :
2013-04-02 12:15:14 +00:00
action = {
' name ' : " Update %s " % goal . type_id . name ,
' id ' : goal_id ,
' type ' : ' ir.actions.act_window ' ,
' views ' : [ [ False , ' form ' ] ] ,
' target ' : ' new ' ,
}
2013-03-25 09:00:10 +00:00
action [ ' context ' ] = { ' default_goal_id ' : goal_id , ' default_current ' : goal . current }
2013-03-24 13:46:58 +00:00
action [ ' res_model ' ] = ' gamification.goal.wizard '
2013-03-27 10:50:40 +00:00
return action
return False
2013-03-24 13:46:58 +00:00
2013-03-29 15:45:35 +00:00
def changed_users_avatar ( self , cr , uid , ids , context ) :
""" Warning a user changed his avatar.
The goal_type type_base_avatar requires a change of avatar . As an
avatar is randomly generated by default , trigger warning when change .
Set the state of every type_base_avatar linked to ids to ' reached ' and
set current value to 1.
"""
goal_type_id = self . pool [ ' ir.model.data ' ] . get_object ( cr , uid , ' gamification ' , ' type_base_avatar ' , context )
goal_ids = self . search ( cr , uid , [ ( ' type_id ' , ' = ' , goal_type_id . id ) , ( ' user_id ' , ' in ' , ids ) ] , context = context )
values = { ' state ' : ' reached ' , ' current ' : 1 }
self . write ( cr , uid , goal_ids , values , context = context )
2013-03-24 13:46:58 +00:00
class goal_manual_wizard ( osv . TransientModel ) :
""" Wizard type to update a manual goal """
_name = ' gamification.goal.wizard '
_columns = {
' goal_id ' : fields . many2one ( " gamification.goal " , string = ' Goal ' ) ,
2013-03-24 18:05:57 +00:00
' current ' : fields . float ( ' Current ' ) ,
2013-03-24 13:46:58 +00:00
}
2013-03-24 18:05:57 +00:00
def action_update_current ( self , cr , uid , ids , context = None ) :
""" Wizard action for updating the current value """
if context is None :
context = { }
goal_obj = self . pool . get ( ' gamification.goal ' )
for wiz in self . browse ( cr , uid , ids , context = context ) :
towrite = {
' current ' : wiz . current ,
' goal_id ' : wiz . goal_id . id ,
}
goal_obj . write ( cr , uid , [ wiz . goal_id . id ] , towrite , context = context )
goal_obj . update ( cr , uid , [ wiz . goal_id . id ] , context = context )
return { }