From df04efdabb634cbfd4f0b8e14960d078d7d90e70 Mon Sep 17 00:00:00 2001 From: Iman Karimi Date: Mon, 31 Jul 2023 14:35:43 +0330 Subject: [PATCH] add search field by grep --- log_reader/admin.py | 3 ++- .../static/log_reader/css/log_reader.css | 3 +++ .../log_reader/admin/change_list.html | 7 ++++++ log_reader/utils.py | 23 +++++++++++++------ setup.py | 2 +- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/log_reader/admin.py b/log_reader/admin.py index 20dcc5e..5da428e 100644 --- a/log_reader/admin.py +++ b/log_reader/admin.py @@ -20,7 +20,8 @@ def get_urls(self): def changelist_view(self, request, extra_context=None): filename = request.GET.get('file_name', settings.LOG_READER_DEFAULT_FILE) - is_valid, file_contents = read_file_lines(file_name=filename) + search = request.GET.get('q', None) or None + is_valid, file_contents = read_file_lines(file_name=filename, search=search) if not is_valid: self.message_user(request, file_contents, level=messages.ERROR) log_files = get_log_files(settings.LOG_READER_DIR_PATH) diff --git a/log_reader/static/log_reader/css/log_reader.css b/log_reader/static/log_reader/css/log_reader.css index ea54d0b..50bd7ed 100644 --- a/log_reader/static/log_reader/css/log_reader.css +++ b/log_reader/static/log_reader/css/log_reader.css @@ -5,4 +5,7 @@ color: #ffffff; cursor: pointer; border: 1px solid #d0d0d0; + position: fixed; + right: 315px; + top: 156px; } \ No newline at end of file diff --git a/log_reader/templates/log_reader/admin/change_list.html b/log_reader/templates/log_reader/admin/change_list.html index ebbd5b2..f29ad25 100644 --- a/log_reader/templates/log_reader/admin/change_list.html +++ b/log_reader/templates/log_reader/admin/change_list.html @@ -30,6 +30,13 @@

File Logs

{% endif %} {% if file_contents %}
+
{% endif %} diff --git a/log_reader/utils.py b/log_reader/utils.py index 8af2398..f714af1 100644 --- a/log_reader/utils.py +++ b/log_reader/utils.py @@ -24,18 +24,27 @@ def get_log_files(directory): return [] -def read_file_lines(file_name): +def read_file_lines(file_name, search=None): if file_name not in get_log_files(settings.LOG_READER_DIR_PATH): return False, _("%s file, not found. Please try again." % file_name) try: file_path = '%s/%s' % (settings.LOG_READER_DIR_PATH, file_name) - result = subprocess.run( - ['tail', '-%s' % settings.LOG_READER_MAX_READ_LINES, file_path], - stdout=PIPE, - stderr=PIPE, - encoding="utf8", - ) + + if search: + result = subprocess.run( + ['grep', '-m %s' % settings.LOG_READER_MAX_READ_LINES, search, file_path], + stdout=PIPE, + stderr=PIPE, + encoding="utf8", + ) + else: + result = subprocess.run( + ['tail', '-%s' % settings.LOG_READER_MAX_READ_LINES, file_path], + stdout=PIPE, + stderr=PIPE, + encoding="utf8", + ) content = repr(result.stdout) if result.stdout else None except Exception as e: return False, str(e) diff --git a/setup.py b/setup.py index c9dfb15..ae77016 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name='django-log-reader', - version='1.1.6', + version='1.1.7', zip_safe=False, packages=find_packages(), include_package_data=True,