From 9d87f1bb41ad30dec4346ff1fcf0b07d2835e769 Mon Sep 17 00:00:00 2001 From: Mantavya Gajjar Date: Wed, 5 May 2010 20:15:41 +0530 Subject: [PATCH] [REVIEW]: review idea module make it clean [IMP]: implement new features for the same module bzr revid: mga@tinyerp.com-20100505144541-1taaprsq35d83sak --- addons/idea/__openerp__.py | 4 +- addons/idea/idea.py | 105 +++++++++++----- addons/idea/idea_data.xml | 19 +++ addons/idea/idea_view.xml | 135 +++++++++++---------- addons/idea/wizard/idea_post_vote.py | 68 ++++++++--- addons/idea/wizard/idea_post_vote_view.xml | 65 +++++----- 6 files changed, 251 insertions(+), 145 deletions(-) create mode 100644 addons/idea/idea_data.xml diff --git a/addons/idea/__openerp__.py b/addons/idea/__openerp__.py index c5794346f4d..9f840cf0612 100644 --- a/addons/idea/__openerp__.py +++ b/addons/idea/__openerp__.py @@ -42,7 +42,9 @@ 'security/idea_security.xml', 'security/ir.model.access.csv', ], - 'demo_xml': [], + 'demo_xml': [ + "idea_data.xml" + ], 'installable': True, 'certificate': '0071515601309', } diff --git a/addons/idea/idea.py b/addons/idea/idea.py index e698c85d0b4..2a6aafe2989 100644 --- a/addons/idea/idea.py +++ b/addons/idea/idea.py @@ -19,7 +19,9 @@ # ############################################################################## -from osv import osv, fields +from osv import osv +from osv import fields +from tools.translate import _ VoteValues = [('-1', 'Not Voted'), ('0', 'Very Bad'), ('25', 'Bad'), \ ('50', 'Normal'), ('75', 'Good'), ('100', 'Very Good') ] @@ -35,7 +37,8 @@ class idea_category(osv.osv): 'name': fields.char('Category', size=64, required=True), 'summary': fields.text('Summary'), 'parent_id': fields.many2one('idea.category', 'Parent Categories', ondelete='set null'), - 'child_ids': fields.one2many('idea.category', 'parent_id', 'Child Categories') + 'child_ids': fields.one2many('idea.category', 'parent_id', 'Child Categories'), + 'visibility':fields.boolean('Open Idea?', required=False), } _sql_constraints = [ ('name', 'unique(parent_id,name)', 'The name of the category must be unique' ) @@ -44,7 +47,6 @@ class idea_category(osv.osv): idea_category() - class idea_idea(osv.osv): """ Idea """ _name = 'idea.idea' @@ -62,10 +64,10 @@ class idea_idea(osv.osv): return {} 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 - """ + 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 + """ cr.execute(sql, (ids,)) return dict(cr.fetchall()) @@ -82,10 +84,10 @@ class idea_idea(osv.osv): return {} 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 - """ + 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 + """ cr.execute(sql, (ids,)) return dict(cr.fetchall()) @@ -102,10 +104,10 @@ class idea_idea(osv.osv): return {} 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 - """ + 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 + """ cr.execute(sql, (ids,)) return dict(cr.fetchall()) @@ -156,29 +158,77 @@ class idea_idea(osv.osv): '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), + '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"), + '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, - 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\'.'), + '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\'.' + ), + 'visibility':fields.boolean('Open Idea?', required=False), 'stat_vote_ids': fields.one2many('idea.vote.stat', 'idea_id', 'Statistics', readonly=True), } _defaults = { 'user_id': lambda self,cr,uid,context: uid, 'my_vote': lambda *a: '-1', - 'state': lambda *a: 'draft' + 'state': lambda *a: 'draft', + 'visibility': lambda *a: True, } _order = 'id desc' + def create(self, cr, user, vals, context={}): + """ + Create a new record for a model idea_idea + @param cr: A database cursor + @param user: ID of the user currently logged in + @param vals: provides data for new record + @param context: context arguments, like lang, time zone + + @return: Returns an id of the new record + """ + visibility = False + + if vals.get('category_id', False): + category_pool = self.pool.get('idea.category') + category = category_pool.browse(cr, user, vals.get('category_id'), context) + visibility = category.visibility + + vals.update({ + 'visibility':visibility + }) + res_id = super(idea_idea, self).create(cr, user, vals, context) + return res_id + + def write(self, cr, user, ids, vals, context=None): + """ + Update redord(s) exist in {ids}, with new value provided in {vals} + + @param cr: A database cursor + @param user: ID of the user currently logged in + @param ids: list of record ids to update + @param vals: dict of new values to be set + @param context: context arguments, like lang, time zone + + @return: Returns True on success, False otherwise + """ + + state = self.browse(cr, user, ids[0]).state + + if vals.get('my_vote', False): + if vals.get('state', state) != 'open': + raise osv.except_osv(_("Warning !"), _("Draft/Accepted/Cancelled ideas Could not be voted")) + + res = super(idea_idea, self).write(cr, user, ids, vals, context) + return res + def idea_cancel(self, cr, uid, ids): self.write(cr, uid, ids, { 'state': 'cancel' }) return True @@ -190,7 +240,7 @@ class idea_idea(osv.osv): def idea_close(self, cr, uid, ids): self.write(cr, uid, ids, { 'state': 'close' }) return True - + def idea_draft(self, cr, uid, ids): self.write(cr, uid, ids, { 'state': 'draft' }) return True @@ -238,7 +288,6 @@ class idea_vote(osv.osv): idea_vote() - class idea_vote_stat(osv.osv): """ Idea votes Statistics """ diff --git a/addons/idea/idea_data.xml b/addons/idea/idea_data.xml new file mode 100644 index 00000000000..8c147c35d32 --- /dev/null +++ b/addons/idea/idea_data.xml @@ -0,0 +1,19 @@ + + + + + + General + General Ideas + True + + + + TODO: Need to put a good idea on OpenERP + TODO: Need to put a good idea on OpenERP + + True + + + + diff --git a/addons/idea/idea_view.xml b/addons/idea/idea_view.xml index 7dc0b5f7178..38ac9eda0a7 100644 --- a/addons/idea/idea_view.xml +++ b/addons/idea/idea_view.xml @@ -2,8 +2,7 @@ - - + idea.category.form @@ -11,16 +10,18 @@ form
- - - - + + + + + + +
- - + idea.category.tree idea.category @@ -33,7 +34,7 @@ - + Categories @@ -47,8 +48,7 @@ - + @@ -61,11 +61,6 @@ - - @@ -137,21 +132,18 @@ form
- - - - - - - - - -