[FIX] sale: Removed duplicate field from the list view.
[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 import base64
22
23 from osv import osv, fields
24 from osv.orm import except_orm
25 import urlparse
26
27 import netsvc
28 import os
29 import nodes
30 import StringIO
31
32 class document_directory_content_type(osv.osv):
33     _name = 'document.directory.content.type'
34     _description = 'Directory Content Type'
35     _columns = {
36         'name': fields.char('Content Type', size=64, required=True),
37         'code': fields.char('Extension', size=4),
38         'active': fields.boolean('Active'),
39         'mimetype': fields.char('Mime Type',size=32)
40     }
41     _defaults = {
42         'active': lambda *args: 1
43     }
44 document_directory_content_type()
45
46 class document_directory_content(osv.osv):
47     _name = 'document.directory.content'
48     _description = 'Directory Content'
49     _order = "sequence"
50     def _extension_get(self, cr, uid, context={}):
51         cr.execute('select code,name from document_directory_content_type where active')
52         res = cr.fetchall()
53         return res
54     _columns = {
55         'name': fields.char('Content Name', size=64, required=True),
56         'sequence': fields.integer('Sequence', size=16),
57         'prefix': fields.char('Prefix', size=16),
58         'suffix': fields.char('Suffix', size=16),
59         'report_id': fields.many2one('ir.actions.report.xml', 'Report'),
60         'extension': fields.selection(_extension_get, 'Document Type', required=True, size=4),
61         'include_name': fields.boolean('Include Record Name', help="Check this field if you want that the name of the file start by the record name."),
62         'directory_id': fields.many2one('document.directory', 'Directory'),
63     }
64     _defaults = {
65         'extension': lambda *args: '.pdf',
66         'sequence': lambda *args: 1,
67         'include_name': lambda *args: 1,
68     }
69     
70     def _file_get(self, cr, node, nodename, content, context=None):
71         """ return the nodes of a <node> parent having a <content> content
72             The return value MUST be false or a list of node_class objects.
73         """
74     
75         # TODO: respect the context!
76         model = node.res_model
77         if content.include_name and not model:
78             return False
79         
80         res2 = []
81         tname = ''
82         if content.include_name:
83             content_name = node.displayname or ''
84             obj = node.context._dirobj.pool.get(model)
85             if content_name:
86                 tname = (content.prefix or '') + content_name + (content.suffix or '') + (content.extension or '')
87         else:
88             tname = (content.prefix or '') + (content.suffix or '') + (content.extension or '')
89         if tname.find('/'):
90             tname=tname.replace('/', '_')
91         if not nodename:
92             n = nodes.node_content(tname, node, node.context,content)
93             res2.append( n)
94         else:
95             if nodename == tname:
96                 n = nodes.node_content(tname, node, node.context,content)
97                 n.fill_fields(cr)
98                 res2.append(n)
99         return res2
100
101     def process_write(self, cr, uid, node, data, context=None):
102         if node.extension != '.pdf':
103             raise Exception("Invalid content: %s" % node.extension)
104         return True
105     
106     def process_read(self, cr, uid, node, context=None):
107         if node.extension != '.pdf':
108             raise Exception("Invalid content: %s" % node.extension)
109         report = self.pool.get('ir.actions.report.xml').browse(cr, uid, node.report_id)
110         srv = netsvc.Service._services['report.'+report.report_name]
111         pdf,pdftype = srv.create(cr, uid, [node.context.context['res_id']], {}, {})        
112         return pdf
113 document_directory_content()