~ubuntu-branches/ubuntu/oneiric/openerp-server/oneiric

« back to all changes in this revision

Viewing changes to bin/addons/document/document.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2011-01-01 09:45:38 UTC
  • mfrom: (19.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110101094538-lg37tzn110haohp6
Tags: 5.0.15-2
* Removing README.source and headers in copyright file.
* Removing vcs fields.
* Updating year in copyright file.
* Orphaning package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
 
71
71
 
72
72
class node_class(object):
73
 
    def __init__(self, cr, uid, path, object, object2=False, context={}, content=False, type='collection', root=False):
 
73
    def __init__(self, cr, uid, path, object, object2=False, context=None, content=False, type='collection', root=False):
74
74
        self.cr = cr
75
75
        self.uid = uid
76
76
        self.path = path
77
77
        self.object = object
78
78
        self.object2 = object2
79
79
        self.context = context
 
80
        if self.context is None:
 
81
            self.context = {}
80
82
        self.content = content
81
83
        self.type=type
82
84
        self.root=root
101
103
                if content.include_name:
102
104
                    content_name = self.object2.name
103
105
                    obj = pool.get(self.object.ressource_type_id.model)
104
 
                    name_for = obj._name.split('.')[-1]            
 
106
                    name_for = obj._name.split('.')[-1]
105
107
                    if content_name  and content_name.find(name_for) == 0  :
106
108
                        content_name = content_name.replace(name_for,'')
107
109
                    test_nodename = content_name + (content.suffix or '') + (content.extension or '')
123
125
            ids += fobj.search(self.cr, self.uid, where+[ ('parent_id','=',False) ])
124
126
        res = fobj.browse(self.cr, self.uid, ids, context=self.context)
125
127
        return map(lambda x: node_class(self.cr, self.uid, self.path+'/'+eval('x.'+fobj._rec_name, {'x' : x}), x, False, context=self.context, type='file', root=False), res) + res2
126
 
    
 
128
 
127
129
    def get_translation(self,value,lang):
128
130
        result = value
129
 
        #TODO : to get translation term        
130
 
        return result 
131
 
    
 
131
        #TODO : to get translation term
 
132
        return result
 
133
 
132
134
    def directory_list_for_child(self,nodename,parent=False):
133
135
        pool = pooler.get_pool(self.cr.dbname)
134
136
        where = []
135
 
        if nodename:    
136
 
            nodename = self.get_translation(nodename, self.context['lang'])            
 
137
        if nodename:
 
138
            nodename = self.get_translation(nodename, self.context['lang'])
137
139
            where.append(('name','=',nodename))
138
140
        if (self.object and self.object.type=='directory') or not self.object2:
139
141
            where.append(('parent_id','=',self.object and self.object.id or False))
146
148
 
147
149
        ids = pool.get('document.directory').search(self.cr, self.uid, where+[('ressource_id','=',0)])
148
150
        if self.object2:
149
 
            ids += pool.get('document.directory').search(self.cr, self.uid, where+[('ressource_id','=',self.object2.id)])        
 
151
            ids += pool.get('document.directory').search(self.cr, self.uid, where+[('ressource_id','=',self.object2.id)])
150
152
        res = pool.get('document.directory').browse(self.cr, self.uid, ids, self.context)
151
153
        return res
152
154
 
153
 
    def _child_get(self, nodename=False):        
 
155
    def _child_get(self, nodename=False):
154
156
        if self.type not in ('collection','database'):
155
157
            return []
156
158
        res = self.directory_list_for_child(nodename)
167
169
            result +=map(lambda x: node_class(self.cr, self.uid, self.path+'/'+eval('x.'+fobj._rec_name, {'x' : x}), x, False, context=self.context, type='file', root=self.root), res)
168
170
        if self.type=='collection' and self.object.type=="ressource":
169
171
            where = self.object.domain and eval(self.object.domain, {'active_id':self.root, 'uid':self.uid}) or []
170
 
            pool = pooler.get_pool(self.cr.dbname)            
 
172
            pool = pooler.get_pool(self.cr.dbname)
171
173
            obj = pool.get(self.object.ressource_type_id.model)
172
174
            _dirname_field = obj._rec_name
173
175
            if len(obj.fields_get(self.cr, self.uid, ['dirname'])):
174
 
                _dirname_field = 'dirname'            
 
176
                _dirname_field = 'dirname'
175
177
 
176
 
            name_for = obj._name.split('.')[-1]            
 
178
            name_for = obj._name.split('.')[-1]
177
179
            if nodename  and nodename.find(name_for) == 0  :
178
180
                id = int(nodename.replace(name_for,''))
179
181
                where.append(('id','=',id))
187
189
                where.append((_dirname_field,'=',nodename))
188
190
 
189
191
            if self.object.ressource_tree:
190
 
                if obj._parent_name in obj.fields_get(self.cr,self.uid):                    
 
192
                if obj._parent_name in obj.fields_get(self.cr,self.uid):
191
193
                    where.append((obj._parent_name,'=',self.object2 and self.object2.id or False))
192
194
                    ids = obj.search(self.cr, self.uid, where)
193
195
                    res = obj.browse(self.cr, self.uid, ids,self.context)
200
202
                if self.object2:
201
203
                    return result
202
204
 
203
 
            
 
205
 
204
206
            ids = obj.search(self.cr, self.uid, where)
205
207
            res = obj.browse(self.cr, self.uid, ids,self.context)
206
 
            for r in res:                               
 
208
            for r in res:
207
209
                if len(obj.fields_get(self.cr, self.uid, [_dirname_field])):
208
210
                    r.name = eval('r.'+_dirname_field, {'r' : r})
209
211
                else:
210
212
                    r.name = False
211
213
                if not r.name:
212
 
                    r.name = name_for + '%d'%r.id               
 
214
                    r.name = name_for + '%d'%r.id
213
215
                for invalid in INVALID_CHARS:
214
216
                    if r.name.find(invalid) :
215
 
                        r.name = r.name.replace(invalid,INVALID_CHARS[invalid])            
 
217
                        r.name = r.name.replace(invalid,INVALID_CHARS[invalid])
216
218
            result2 = map(lambda x: node_class(self.cr, self.uid, self.path+'/'+x.name.replace('/','__'), self.object, x, context=self.context, root=x.id), res)
217
219
            if result2:
218
220
                if self.object.ressource_tree:
219
221
                    result += result2
220
222
                else:
221
 
                    result = result2                  
 
223
                    result = result2
222
224
        return result
223
225
 
224
226
    def children(self):
321
323
    def onchange_content_id(self, cr, uid, ids, ressource_type_id):
322
324
        return {}
323
325
 
324
 
    def _get_childs(self, cr, uid, node, nodename=False, context={}):
 
326
    def _get_childs(self, cr, uid, node, nodename=False, context=None):
325
327
        where = []
326
328
        if nodename:
327
329
            nodename = self.get_translation(nodename, self.context['lang'])
339
341
            object: the object.directory or object.directory.content
340
342
            object2: the other object linked (if object.directory.content)
341
343
    """
342
 
    def get_object(self, cr, uid, uri, context={}):        
343
 
        #TODO : set user's context_lang in context  
344
 
        context.update({'lang':False})      
 
344
    def get_object(self, cr, uid, uri, context=None):
 
345
        if context is None:
 
346
            context = {}
 
347
        #TODO : set user's context_lang in context
 
348
        context.update({'lang':False})
345
349
        if not uri:
346
350
            return node_class(cr, uid, '', False, context=context, type='database')
347
351
        turi = tuple(uri)
369
373
        self._cache[turi] = (node.path, oo, oo2, node.context, node.content,node.type,node.root)
370
374
        return node
371
375
 
372
 
    def get_childs(self, cr, uid, uri, context={}):
 
376
    def get_childs(self, cr, uid, uri, context=None):
373
377
        node = self.get_object(cr, uid, uri, context)
374
378
        if uri:
375
379
            children = node.children()
448
452
    _name = 'document.directory.content'
449
453
    _description = 'Directory Content'
450
454
    _order = "sequence"
451
 
    def _extension_get(self, cr, uid, context={}):
 
455
    def _extension_get(self, cr, uid, context=None):
452
456
        cr.execute('select code,name from document_directory_content_type where active')
453
457
        res = cr.fetchall()
454
458
        return res
466
470
        'sequence': lambda *args: 1,
467
471
        'include_name': lambda *args: 1,
468
472
    }
469
 
    def process_write_pdf(self, cr, uid, node, context={}):
 
473
    def process_write_pdf(self, cr, uid, node, context=None):
470
474
        return True
471
 
    def process_read_pdf(self, cr, uid, node, context={}):
 
475
    def process_read_pdf(self, cr, uid, node, context=None):
472
476
        report = self.pool.get('ir.actions.report.xml').browse(cr, uid, node.content.report_id.id)
473
477
        srv = netsvc.LocalService('report.'+report.report_name)
474
478
        pdf,pdftype = srv.create(cr, uid, [node.object.id], {}, {})
541
545
    #
542
546
    # This code can be improved
543
547
    #
544
 
    def _data_set(self, cr, uid, id, name, value, args=None, context={}):
 
548
    def _data_set(self, cr, uid, id, name, value, args=None, context=None):
545
549
        if not value:
546
550
            filename = self.browse(cr, uid, id, context).store_fname
547
551
            try:
655
659
            pass
656
660
        return result
657
661
 
658
 
    def create(self, cr, uid, vals, context={}):
 
662
    def create(self, cr, uid, vals, context=None):
 
663
        if context is None:
 
664
            context = {}
659
665
        vals['title']=vals['name']
660
666
        vals['parent_id'] = context.get('parent_id',False) or vals.get('parent_id',False)
661
667
        if not vals.get('res_id', False) and context.get('default_res_id',False):
692
698
            datas=base64.encodestring(urllib.urlopen(vals['link']).read())
693
699
        else:
694
700
            datas = vals.get('datas',False)
695
 
        
 
701
 
696
702
        vals['file_size']= datas and len(datas) or 0
697
703
        if not self._check_duplication(cr,uid,vals):
698
704
            raise except_orm(_('ValidateError'), _('File name must be unique!'))
708
714
            pass
709
715
        return result
710
716
 
711
 
    def unlink(self,cr, uid, ids, context={}):
 
717
    def unlink(self,cr, uid, ids, context=None):
712
718
        for f in self.browse(cr, uid, ids, context):
713
719
            #if f.store_method=='fs':
714
720
            try: