[MERGE]
[odoo/odoo.git] / addons / document / directory_content.py
index cfe5797..5b8ec3d 100644 (file)
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 #
 ##############################################################################
-import base64
 
 from osv import osv, fields
-from osv.orm import except_orm
-import urlparse
 
 import netsvc
-import os
+# import os
 import nodes
-import StringIO
+# import StringIO
 
 class document_directory_content_type(osv.osv):
     _name = 'document.directory.content.type'
@@ -36,10 +33,10 @@ class document_directory_content_type(osv.osv):
         'name': fields.char('Content Type', size=64, required=True),
         'code': fields.char('Extension', size=4),
         'active': fields.boolean('Active'),
-        'mimetype': fields.char('Mime Type', size=32)
+        'mimetype': fields.char('Mime Type',size=32)
     }
     _defaults = {
-        'active': lambda * args: 1
+        'active': lambda *args: 1
     }
 document_directory_content_type()
 
@@ -47,7 +44,7 @@ class document_directory_content(osv.osv):
     _name = 'document.directory.content'
     _description = 'Directory Content'
     _order = "sequence"
-    def _extension_get(self, cr, uid, context={}):
+    def _extension_get(self, cr, uid, context=None):
         cr.execute('select code,name from document_directory_content_type where active')
         res = cr.fetchall()
         return res
@@ -58,42 +55,51 @@ class document_directory_content(osv.osv):
         'suffix': fields.char('Suffix', size=16),
         'report_id': fields.many2one('ir.actions.report.xml', 'Report'),
         'extension': fields.selection(_extension_get, 'Document Type', required=True, size=4),
-        '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."),
+        'include_name': fields.boolean('Include Record Name', 
+                help="Check this field if you want that the name of the file to contain the record name." \
+                    "\nIf set, the directory will have to be a resource one."),
         'directory_id': fields.many2one('document.directory', 'Directory'),
     }
     _defaults = {
-        'extension': lambda * args: '.pdf',
-        'sequence': lambda * args: 1,
-        'include_name': lambda * args: 1,
+        'extension': lambda *args: '.pdf',
+        'sequence': lambda *args: 1,
+        'include_name': lambda *args: 1,
     }
-
+    
     def _file_get(self, cr, node, nodename, content, context=None):
         """ return the nodes of a <node> parent having a <content> content
             The return value MUST be false or a list of node_class objects.
         """
-
+    
         # TODO: respect the context!
         model = node.res_model
         if content.include_name and not model:
             return False
-
+        
         res2 = []
         tname = ''
         if content.include_name:
             content_name = node.displayname or ''
-            obj = node.context._dirobj.pool.get(model)
+            # obj = node.context._dirobj.pool.get(model)
             if content_name:
                 tname = (content.prefix or '') + content_name + (content.suffix or '') + (content.extension or '')
         else:
             tname = (content.prefix or '') + (content.suffix or '') + (content.extension or '')
         if tname.find('/'):
-            tname = tname.replace('/', '_')
+            tname=tname.replace('/', '_')
+        act_id = False
+        if 'dctx_res_id' in node.dctx:
+            act_id = node.dctx['dctx_res_id']
+        elif hasattr(node, 'res_id'):
+            act_id = node.res_id
+        else:
+            act_id = node.context.context.get('res_id',False)
         if not nodename:
-            n = nodes.node_content(tname, node, node.context, content)
-            res2.append(n)
+            n = nodes.node_content(tname, node, node.context,content, act_id=act_id)
+            res2.append( n)
         else:
             if nodename == tname:
-                n = nodes.node_content(tname, node, node.context, content)
+                n = nodes.node_content(tname, node, node.context,content, act_id=act_id)
                 n.fill_fields(cr)
                 res2.append(n)
         return res2
@@ -102,12 +108,17 @@ class document_directory_content(osv.osv):
         if node.extension != '.pdf':
             raise Exception("Invalid content: %s" % node.extension)
         return True
-
+    
     def process_read(self, cr, uid, node, context=None):
         if node.extension != '.pdf':
             raise Exception("Invalid content: %s" % node.extension)
-        report = self.pool.get('ir.actions.report.xml').browse(cr, uid, node.report_id)
-        srv = netsvc.Service._services['report.' + report.report_name]
-        pdf, pdftype = srv.create(cr, uid, [node.context.context['res_id']], {}, {})
+        report = self.pool.get('ir.actions.report.xml').browse(cr, uid, node.report_id, context=context)
+        srv = netsvc.Service._services['report.'+report.report_name]
+        ctx = node.context.context.copy()
+        ctx.update(node.dctx)
+        pdf,pdftype = srv.create(cr, uid, [node.act_id,], {}, context=ctx)
         return pdf
 document_directory_content()
+
+#eof
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: