[IMP] document_page: continuing small cleaning
[odoo/odoo.git] / addons / document_page / document_page.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-Today OpenERP SA (<http://www.openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23 from openerp.tools.translate import _
24 import difflib
25
26
27 class BlogCategory(osv.Model):
28     _name = 'blog.category'
29     _description = 'Blog Category'
30     _inherit = ['mail.thread']
31     _order = 'name'
32
33     _columns = {
34         'name': fields.char('Name', required=True),
35         'description': fields.text('Description'),
36         'template': fields.text('Template'),
37         'blog_ids': fields.one2many(
38             'blog.post', 'category_id',
39             'Blogs',
40         ),
41     }
42
43
44 class BlogTag(osv.Model):
45     _name = 'blog.tag'
46     _description = 'Blog Tag'
47     _order = 'name'
48
49     _columns = {
50         'name': fields.char('Name', required=True),
51     }
52
53
54 class BlogPost(osv.Model):
55     _name = "blog.post"
56     _description = "Blog Post"
57     _inherit = ['mail.thread']
58     _order = 'name'
59
60     _columns = {
61         'name': fields.char('Title', required=True),
62         'category_id': fields.many2one(
63             'blog.category', 'Category',
64             ondelete='set null',
65         ),
66         'tag_ids': fields.many2many(
67             'blog.tag', 'blog_tag_rel',
68             'blog_id', 'tag_id',
69             'Tags',
70         ),
71         'content': fields.text('Content'),
72         # technical stuff: history, menu (to keep ?)
73         'history_ids': fields.one2many(
74             'blog.post.history', 'post_id',
75             'History', help='Last post modifications'
76         ),
77         'menu_id': fields.many2one('ir.ui.menu', "Menu", readonly=True),
78         # creation / update stuff
79         'create_date': fields.datetime("Created on", select=True, readonly=True),
80         'create_uid': fields.many2one('res.users', 'Author', select=True, readonly=True),
81         'write_date': fields.datetime("last Modified on", select=True, readonly=True),
82         'write_uid': fields.many2one('res.users', "Last Contributor", select=True, readonly=True),
83     }
84
85     def create_history(self, cr, uid, ids, vals, context=None):
86         for i in ids:
87             history = self.pool.get('blog.post.history')
88             if vals.get('content'):
89                 res = {
90                     'content': vals.get('content', ''),
91                     'post_id': i,
92                 }
93                 history.create(cr, uid, res)
94
95     def create(self, cr, uid, vals, context=None):
96         if context is None:
97             context = {}
98         create_context = dict(context, mail_create_nolog=True)
99         post_id = super(BlogPost, self).create(cr, uid, vals, context=create_context)
100         self.create_history(cr, uid, [post_id], vals, context)
101         return post_id
102
103     def write(self, cr, uid, ids, vals, context=None):
104         result = super(BlogPost, self).write(cr, uid, ids, vals, context)
105         self.create_history(cr, uid, ids, vals, context)
106         return result
107
108
109 class BlogPostHistory(osv.Model):
110     _name = "blog.post.history"
111     _description = "Document Page History"
112     _order = 'id DESC'
113     _rec_name = "create_date"
114
115     _columns = {
116         'post_id': fields.many2one('blog.post', 'Blog Post'),
117         'summary': fields.char('Summary', size=256, select=True),
118         'content': fields.text("Content"),
119         'create_date': fields.datetime("Date"),
120         'create_uid': fields.many2one('res.users', "Modified By"),
121     }
122
123     def getDiff(self, cr, uid, v1, v2, context=None):
124         history_pool = self.pool.get('document.page.history')
125         text1 = history_pool.read(cr, uid, [v1], ['content'])[0]['content']
126         text2 = history_pool.read(cr, uid, [v2], ['content'])[0]['content']
127         line1 = line2 = ''
128         if text1:
129             line1 = text1.splitlines(1)
130         if text2:
131             line2 = text2.splitlines(1)
132         if (not line1 and not line2) or (line1 == line2):
133             raise osv.except_osv(_('Warning!'), _('There are no changes in revisions.'))
134         diff = difflib.HtmlDiff()
135         return diff.make_table(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=True)
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: