Browse Source

Add filtering capabilities

11.0
Andrea 5 years ago
parent
commit
ad328a132e
  1. 17
      base_directory_file_download/models/ir_filesystem_directory.py
  2. 1
      base_directory_file_download/models/ir_filesystem_file.py
  3. 1
      base_directory_file_download/views/ir_filesystem_directory.xml

17
base_directory_file_download/models/ir_filesystem_directory.py

@ -3,8 +3,9 @@
import logging
from os import listdir
from os.path import isfile, join, exists, normpath, realpath
from glob import glob
from os.path import basename, isfile, join, exists, normpath, realpath
from odoo import api, fields, models, _
from odoo.exceptions import UserError
@ -26,6 +27,7 @@ class IrFilesystemDirectory(models.Model):
compute='_compute_file_count',
string="# Files",
)
files_filter = fields.Char()
@api.multi
def get_dir(self):
@ -61,10 +63,11 @@ class IrFilesystemDirectory(models.Model):
@api.multi
def _get_directory_files(self):
def get_files(directory, files):
for file_name in listdir(directory):
full_path = join(directory, file_name)
def get_files(directory, files_filter):
glob_path = join(directory, files_filter)
content = glob(glob_path)
for full_path in content:
file_name = basename(full_path)
# Symbolic links and up-level references are not considered
norm_path = normpath(realpath(full_path))
if norm_path in full_path:
@ -75,7 +78,7 @@ class IrFilesystemDirectory(models.Model):
files = []
if self.get_dir() and exists(self.get_dir()):
try:
get_files(self.get_dir(), files)
get_files(self.get_dir(), self.files_filter or '*')
except (IOError, OSError):
_logger.info(
"_get_directory_files reading %s",

1
base_directory_file_download/models/ir_filesystem_file.py

@ -14,6 +14,7 @@ _logger = logging.getLogger(__name__)
class IrFilesystemDirectoryLine(models.TransientModel):
_name = 'ir.filesystem.file'
_description = 'File in filesystem'
name = fields.Char(required=True)
filename = fields.Char()

1
base_directory_file_download/views/ir_filesystem_directory.xml

@ -18,6 +18,7 @@
</h1>
<group>
<field name="directory" required="1"/>
<field name="files_filter" placeholder="Enter a pattern to filter file names, eg.: *.txt"/>
</group>
<notebook name="notebook">
<page name="files" string="Files">

Loading…
Cancel
Save