[IMP]: idea: code optimization and apply doc string

bzr revid: ksa@tinyerp.co.in-20100316140207-32167zmiorb79959
This commit is contained in:
ksa (Open ERP) 2010-03-16 19:32:07 +05:30
parent 7c5449a692
commit 0a46449d0a
4 changed files with 92 additions and 61 deletions

View File

@ -21,10 +21,12 @@
from osv import osv, fields
VoteValues = [('-1','Not Voted'),('0','Very Bad'),('25', 'Bad'),('50','Normal'),('75','Good'),('100','Very Good') ]
VoteValues = [('-1', 'Not Voted'), ('0', 'Very Bad'), ('25', 'Bad'), \
('50', 'Normal'), ('75', 'Good'), ('100', 'Very Good') ]
DefaultVoteValue = '50'
class idea_category(osv.osv):
""" Category of Idea """
_name = "idea.category"
_description = "Category for an idea"
_columns = {
@ -37,17 +39,20 @@ class idea_category(osv.osv):
('name', 'unique(parent_id,name)', 'The name of the category must be unique' )
]
_order = 'parent_id,name asc'
idea_category()
class idea_idea(osv.osv):
_name = 'idea.idea'
_rec_name = 'title'
def _vote_avg_compute(self, cr, uid, ids, name, arg, context = None):
if not len(ids):
""" compute average for voting """
if not ids:
return {}
sql = """SELECT i.id, avg(v.score::integer)
sql = """SELECT i.id, avg(v.score::integer)
FROM idea_idea i LEFT OUTER JOIN idea_vote v ON i.id = v.idea_id
WHERE i.id = ANY(%s)
GROUP BY i.id
@ -57,10 +62,11 @@ class idea_idea(osv.osv):
return dict(cr.fetchall())
def _vote_count(self,cr,uid,ids,name,arg,context=None):
if not len(ids):
""" count number of vote """
if not ids:
return {}
sql = """SELECT i.id, COUNT(1)
sql = """SELECT i.id, COUNT(1)
FROM idea_idea i LEFT OUTER JOIN idea_vote v ON i.id = v.idea_id
WHERE i.id = ANY(%s)
GROUP BY i.id
@ -70,10 +76,11 @@ class idea_idea(osv.osv):
return dict(cr.fetchall())
def _comment_count(self,cr,uid,ids,name,arg,context=None):
if not len(ids):
""" count number of comment """
if not ids:
return {}
sql = """SELECT i.id, COUNT(1)
sql = """SELECT i.id, COUNT(1)
FROM idea_idea i LEFT OUTER JOIN idea_comment c ON i.id = c.idea_id
WHERE i.id = ANY(%s)
GROUP BY i.id
@ -88,7 +95,8 @@ class idea_idea(osv.osv):
res[id] = '-1'
vote_obj = self.pool.get('idea.vote')
votes_ids = vote_obj.search(cr, uid, [('idea_id', 'in', ids), ('user_id', '=', uid)])
for vote in vote_obj.browse(cr, uid, votes_ids, context):
vote_obj_id = vote_obj.browse(cr, uid, votes_ids, context)
for vote in vote_obj_id:
res[vote.idea_id.id] = vote.score
return res
@ -98,12 +106,12 @@ class idea_idea(osv.osv):
textual_value = str(field_value)
if vote:
if int(field_value)>=0:
vote_obj.write(cr,uid, vote, { 'score' : textual_value })
vote_obj.write(cr,uid,vote,{'score': textual_value })
else:
vote_obj.unlink(cr,uid, vote)
vote_obj.unlink(cr,uid,vote)
else:
if int(field_value)>=0:
vote_obj.create(cr,uid, { 'idea_id' : id, 'user_id' : uid, 'score' : textual_value })
vote_obj.create(cr,uid, {'idea_id': id,'user_id': uid,'score': textual_value })
_columns = {
@ -111,14 +119,14 @@ class idea_idea(osv.osv):
'title': fields.char('Idea Summary', size=64, required=True),
'description': fields.text('Description', required=True, help='Content of the idea'),
'comment_ids': fields.one2many('idea.comment', 'idea_id', 'Comments'),
'create_date' : fields.datetime( 'Creation date', readonly=True),
'vote_ids' : fields.one2many('idea.vote', 'idea_id', 'Vote'),
'my_vote' : fields.function(_vote_read, fnct_inv = _vote_save, string="My Vote", method=True, type="selection", selection=VoteValues),
'vote_avg' : fields.function(_vote_avg_compute, method=True, string="Average Score", type="float"),
'count_votes' : fields.function(_vote_count, method=True, string="Count of votes", type="integer"),
'create_date': fields.datetime( 'Creation date', readonly=True),
'vote_ids': fields.one2many('idea.vote', 'idea_id', 'Vote'),
'my_vote': fields.function(_vote_read, fnct_inv = _vote_save, string="My Vote", method=True, type="selection", selection=VoteValues),
'vote_avg': fields.function(_vote_avg_compute, method=True, string="Average Score", type="float"),
'count_votes': fields.function(_vote_count, method=True, string="Count of votes", type="integer"),
'count_comments': fields.function(_comment_count, method=True, string="Count of comments", type="integer"),
'category_id': fields.many2one('idea.category', 'Category', required=True ),
'state': fields.selection([('draft','Draft'),('open','Opened'),('close','Accepted'),('cancel','Cancelled')], 'State', readonly=True,
'state': fields.selection([('draft','Draft'),('open','Opened'),('close','Accepted'),('cancel','Cancelled')], 'State', readonly=True,
help='When the Idea is created the state is \'Draft\'.\n It is opened by the user, the state is \'Opened\'.\
\nIf the idea is accepted, the state is \'Accepted\'.'),
'stat_vote_ids': fields.one2many('idea.vote.stat', 'idea_id', 'Statistics', readonly=True),
@ -132,31 +140,33 @@ class idea_idea(osv.osv):
_order = 'id desc'
def idea_cancel(self, cr, uid, ids):
self.write(cr, uid, ids, { 'state' : 'cancel' })
self.write(cr, uid, ids, { 'state': 'cancel' })
return True
def idea_open(self, cr, uid, ids):
self.write(cr, uid, ids, { 'state' : 'open' })
self.write(cr, uid, ids, { 'state': 'open' })
return True
def idea_close(self, cr, uid, ids):
self.write(cr, uid, ids, { 'state' : 'close' })
self.write(cr, uid, ids, { 'state': 'close' })
return True
def idea_draft(self, cr, uid, ids):
self.write(cr, uid, ids, { 'state' : 'draft' })
self.write(cr, uid, ids, { 'state': 'draft' })
return True
idea_idea()
class idea_comment(osv.osv):
""" Apply Idea for Comment """
_name = 'idea.comment'
_description = 'Comments'
_rec_name = 'content'
_columns = {
'idea_id': fields.many2one('idea.idea', 'Idea', required=True, ondelete='cascade' ),
'user_id': fields.many2one('res.users', 'User', required=True ),
'content': fields.text( 'Comment', required=True ),
'create_date' : fields.datetime( 'Creation date', readonly=True),
'content': fields.text('Comment', required=True ),
'create_date' : fields.datetime('Creation date', readonly=True),
}
_defaults = {
'user_id': lambda self, cr, uid, context: uid
@ -164,8 +174,11 @@ class idea_comment(osv.osv):
_order = 'id desc'
idea_comment()
class idea_vote(osv.osv):
""" Apply Idea for Vote """
_name = 'idea.vote'
_description = 'Vote for Idea'
_rec_name = 'score'
_columns = {
'user_id': fields.many2one( 'res.users', 'User'),
@ -173,22 +186,24 @@ class idea_vote(osv.osv):
'score': fields.selection( VoteValues, 'Score', required=True)
}
_defaults = {
#'user_id': lambda self, cr, uid, context: uid,
'score': lambda *a: DefaultVoteValue,
}
idea_vote()
class idea_vote_stat(osv.osv):
""" Idea votes Statistics """
_name = 'idea.vote.stat'
_description = 'Idea Votes Statistics'
_auto = False
_rec_name = 'idea_id'
_columns = {
'idea_id': fields.many2one('idea.idea', 'Idea', readonly=True),
'score': fields.selection( VoteValues, 'Score', readonly=True),
'score': fields.selection(VoteValues, 'Score', readonly=True),
'nbr': fields.integer('Number of Votes', readonly=True),
}
def init(self, cr):
def init(self,cr):
"""
initialize the sql view for the stats
@ -207,6 +222,7 @@ class idea_vote_stat(osv.osv):
GROUP BY
i.id, v.score, i.id )
""")
idea_vote_stat()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -1,6 +1,9 @@
<?xml version="1.0"?>
<openerp>
<data>
<!-- Idea Category -->
<menuitem name="Tools" id="base.menu_tools" icon="STOCK_PREFERENCES" sequence="15"/>
<record model="ir.ui.view" id="view_idea_category_form">
<field name="name">idea.category.form</field>
@ -34,9 +37,11 @@
</record>
<menuitem name="Configuration" parent="base.menu_tools"
id="base.menu_lunch_survey_root" sequence="6"/>
<!-- <menuitem name="Configuration" parent="menu_tools" id="menu_config"/>-->
<menuitem name="Ideas" parent="base.menu_lunch_survey_root" id="menu_ideas"/>
<menuitem name="Categories" parent="menu_ideas" id="menu_idea_category" action="action_idea_category"/>
<menuitem name="Categories" parent="menu_ideas"
id="menu_idea_category" action="action_idea_category" />
<record model="ir.actions.act_window" id="action_idea_category_tree">
<field name="name">Ideas by Categories</field>
@ -44,12 +49,15 @@
<field name="view_type">tree</field>
<field name="domain">[('parent_id','=',False)]</field>
</record>
<menuitem name="Ideas" parent="base.menu_tools" id="menu_ideas1" sequence="4"/>
<menuitem
name="Ideas by Categories" parent="menu_ideas1"
id="menu_idea_category_tree"
action="action_idea_category_tree"/>
<!-- Oepn Ideas -->
<record model="ir.actions.act_window" id="action_idea_idea_categ_open">
<field name="name">Open Ideas</field>
@ -66,6 +74,8 @@
<field name="object" eval="True" />
</record>
<!-- Idea statistics -->
<record model="ir.ui.view" id="view_idea_stat_form">
<field name="name">idea.stat.form</field>
<field name="model">idea.stat</field>
@ -79,6 +89,8 @@
</field>
</record>
<!-- Idea Vote -->
<record model="ir.ui.view" id="view_idea_vote_tree">
<field name="name">idea.vote.tree</field>
<field name="model">idea.vote</field>
@ -104,6 +116,8 @@
</field>
</record>
<!-- New Idea -->
<record model="ir.ui.view" id="view_idea_idea_form">
<field name="name">idea.idea.form</field>
<field name="model">idea.idea</field>
@ -115,7 +129,6 @@
<field name="title" select="1" />
<field name="category_id" select="1"/>
<field name="user_id" select="1"/>
<!--<field name="my_vote"/>-->
<field name="vote_avg" />
<field name="description" colspan="4" select="2" widget="text_wiki"/>
<field name="state" select="2"/>
@ -171,6 +184,8 @@
</field>
</record>
<!-- Search Idea -->
<record model="ir.ui.view" id="view_idea_idea_search">
<field name="name">idea.idea.search</field>
<field name="model">idea.idea</field>
@ -180,8 +195,10 @@
<group col="10" colspan="4">
<filter icon="gtk-execute" string="My" domain="[('user_id','=',uid)]" help="My Ideas"/>
<separator orientation="vertical"/>
<filter icon="gtk-execute" string="Draft" domain="[('state','=','draft')]" help="Draft Ideas"/>
<filter icon="gtk-execute" string="Open" domain="[('state','=','open')]" help="Open Ideas"/>
<filter icon="gtk-execute" string="Draft"
domain="[('state','=','draft')]" help="Draft Ideas" />
<filter icon="gtk-execute" string="Open"
domain="[('state','=','open')]" help="Open Ideas" />
<separator orientation="vertical"/>
<field name="title" select="1"/>
<field name="category_id" select="1"/>
@ -191,6 +208,8 @@
</field>
</record>
<!-- Comments on Idea -->
<record model="ir.ui.view" id="view_idea_comment_tree">
<field name="name">idea.comment.tree</field>
<field name="model">idea.comment</field>
@ -265,17 +284,19 @@
<field name="view_mode">graph,tree</field>
</record>
<menuitem name="Reporting" parent="base.menu_tools" id="base.menu_lunch_reporting" sequence="5"/>
<!-- <menuitem name="Reporting" parent="menu_ideas1" id="menu_reporting"/>-->
<menuitem name="Idea" parent="base.menu_lunch_reporting" id="menu_idea_reporting"/>
<menuitem name="Vote Statistics" parent="menu_idea_reporting" id="menu_idea_vote_stat" action="action_idea_vote_stat"/>
<menuitem name="Vote Statistics" parent="menu_idea_reporting"
id="menu_idea_vote_stat" action="action_idea_vote_stat" />
<record model="ir.actions.act_window" id="action_idea_vote">
<record model="ir.actions.act_window" id="action_idea_vote">
<field name="name">Idea's vote</field>
<field name="res_model">idea.vote</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="All Votes" parent="menu_ideas" id="menu_idea_vote" action="action_idea_vote"/>
</data>

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
##############################################################################
#
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
@ -15,24 +15,24 @@
# 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/>.
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields, osv
class idea_post_vote(osv.osv_memory):
_name = "idea.post.vote"
_description = "Post vote"
_columns = {
'vote': fields.selection([('-1', 'Not Voted'),
('0', 'Very Bad'),
('25', 'Bad'),
('50', 'Normal'),
('75', 'Good'),
'vote': fields.selection([('-1', 'Not Voted'),
('0', 'Very Bad'),
('25', 'Bad'),
('50', 'Normal'),
('75', 'Good'),
('100', 'Very Good') ], 'Post Vote', required=True)
}
def do_vote(self, cr, uid, ids, context):
"""
Create idea vote.
@ -41,10 +41,11 @@ class idea_post_vote(osv.osv_memory):
@param ids: List of Idea Post votes IDs.
@return: Dictionary {}
"""
data = context and context.get('active_id', False) or False
vote_obj = self.pool.get('idea.vote')
for data in self.read(cr, uid, ids):
score = str(data['vote'])
dic = {'idea_id': context['active_id'], 'user_id': uid, 'score': score }
for dovote_obj in self.read(cr, uid, ids):
score = str(dovote_obj['vote'])
dic = {'idea_id': data, 'user_id': uid, 'score': score }
vote = vote_obj.create(cr, uid, dic)
return {}

View File

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Post Idea for vote -->
<record id="view_idea_post_vote" model="ir.ui.view">
<field name="name">idea.post.vote.form</field>
<field name="model">idea.post.vote</field>
@ -18,32 +21,22 @@
</form>
</field>
</record>
<record id="action_idea_post_vote" model="ir.actions.act_window">
<field name="name">Vote</field>
<field name="res_model">idea.post.vote</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="view_idea_post_vote"/>
<field name="view_id" ref="view_idea_post_vote"/>
<field name="target">new</field>
<field name="context">{'record_id':active_id}</field>
</record>
<!-- <record id="action_idea_post_vote_values" model="ir.values">
<field name="model_id" ref="idea.model_idea_idea" />
<field name="object" eval="1" />
<field name="name">vote</field>
<field name="key2">client_action_multi</field>
<field name="value" eval="'ir.actions.act_window,' + str(ref('action_idea_post_vote'))"/>
<field name="key">action</field>
<field name="model">idea.idea</field>
</record> -->
<act_window id="action_idea_post_vote_values"
key2="client_action_multi" name="vote"
res_model="idea.post.vote" src_model="idea.idea"
view_mode="form" target="new" view_type="form" />
</data>
</openerp>