Launchpad automatic translations update.
[odoo/odoo.git] / addons / document / directory_content.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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 osv import osv, fields
23
24 import netsvc
25 # import os
26 import nodes
27 # import StringIO
28
29 class document_directory_content_type(osv.osv):
30     _name = 'document.directory.content.type'
31     _description = 'Directory Content Type'
32     _columns = {
33         'name': fields.char('Content Type', size=64, required=True),
34         'code': fields.char('Extension', size=4),
35         'active': fields.boolean('Active'),
36         'mimetype': fields.char('Mime Type',size=32)
37     }
38     _defaults = {
39         'active': lambda *args: 1
40     }
41 document_directory_content_type()
42
43 class document_directory_content(osv.osv):
44     _name = 'document.directory.content'
45     _description = 'Directory Content'
46     _order = "sequence"
47     def _extension_get(self, cr, uid, context=None):
48         cr.execute('select code,name from document_directory_content_type where active')
49         res = cr.fetchall()
50         return res
51     _columns = {
52         'name': fields.char('Content Name', size=64, required=True),
53         'sequence': fields.integer('Sequence', size=16),
54         'prefix': fields.char('Prefix', size=16),
55         'suffix': fields.char('Suffix', size=16),
56         'report_id': fields.many2one('ir.actions.report.xml', 'Report'),
57         'extension': fields.selection(_extension_get, 'Document Type', required=True, size=4),
58         'include_name': fields.boolean('Include Record Name', 
59                 help="Check this field if you want that the name of the file to contain the record name." \
60                     "\nIf set, the directory will have to be a resource one."),
61         'directory_id': fields.many2one('document.directory', 'Directory'),
62     }
63     _defaults = {
64         'extension': lambda *args: '.pdf',
65         'sequence': lambda *args: 1,
66         'include_name': lambda *args: 1,
67     }
68     
69     def _file_get(self, cr, node, nodename, content, context=None):
70         """ return the nodes of a <node> parent having a <content> content
71             The return value MUST be false or a list of node_class objects.
72         """
73     
74         # TODO: respect the context!
75         model = node.res_model
76         if content.include_name and not model:
77             return False
78         
79         res2 = []
80         tname = ''
81         if content.include_name:
82             content_name = node.displayname or ''
83             # obj = node.context._dirobj.pool.get(model)
84             if content_name:
85                 tname = (content.prefix or '') + content_name + (content.suffix or '') + (content.extension or '')
86         else:
87             tname = (content.prefix or '') + (content.suffix or '') + (content.extension or '')
88         if tname.find('/'):
89             tname=tname.replace('/', '_')
90         act_id = False
91         if 'dctx_res_id' in node.dctx:
92             act_id = node.dctx['dctx_res_id']
93         elif hasattr(node, 'res_id'):
94             act_id = node.res_id
95         else:
96             act_id = node.context.context.get('res_id',False)
97         if not nodename:
98             n = nodes.node_content(tname, node, node.context,content, act_id=act_id)
99             res2.append( n)
100         else:
101             if nodename == tname:
102                 n = nodes.node_content(tname, node, node.context,content, act_id=act_id)
103                 n.fill_fields(cr)
104                 res2.append(n)
105         return res2
106
107     def process_write(self, cr, uid, node, data, context=None):
108         if node.extension != '.pdf':
109             raise Exception("Invalid content: %s" % node.extension)
110         return True
111     
112     def process_read(self, cr, uid, node, context=None):
113         if node.extension != '.pdf':
114             raise Exception("Invalid content: %s" % node.extension)
115         report = self.pool.get('ir.actions.report.xml').browse(cr, uid, node.report_id, context=context)
116         srv = netsvc.Service._services['report.'+report.report_name]
117         ctx = node.context.context.copy()
118         ctx.update(node.dctx)
119         pdf,pdftype = srv.create(cr, uid, [node.act_id,], {}, context=ctx)
120         return pdf
121 document_directory_content()
122
123 #eof
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: