Skip to content

Commit dc72897

Browse files
author
Jan Silhan
committed
core: reordering of list fields
1 parent fee2d5d commit dc72897

File tree

4 files changed

+181
-116
lines changed

4 files changed

+181
-116
lines changed

example/apps/test_security/tests/output_request_log.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_output_request_should_be_logged(self, user):
3434
responses.add(responses.POST, 'https://localhost/test', body='test')
3535
expected_output_request_started_data = {
3636
'request_headers': {
37-
'User-Agent': 'python-requests/2.26.0',
37+
'User-Agent': all_eq_obj,
3838
'Accept-Encoding': 'gzip, deflate',
3939
'Accept': '*/*',
4040
'Connection': 'keep-alive',
@@ -74,7 +74,7 @@ def test_output_request_should_be_logged(self, user):
7474
def test_output_request_error_should_be_logged(self):
7575
expected_output_request_started_data = {
7676
'request_headers': {
77-
'User-Agent': 'python-requests/2.26.0',
77+
'User-Agent': all_eq_obj,
7878
'Accept-Encoding': 'gzip, deflate',
7979
'Accept': '*/*',
8080
'Connection': 'keep-alive',
@@ -163,7 +163,7 @@ def test_output_request_should_be_logged_in_sql_backend(self, user):
163163
assert_equal_model_fields(
164164
sql_output_request_log,
165165
request_headers={
166-
'User-Agent': 'python-requests/2.26.0',
166+
'User-Agent': all_eq_obj,
167167
'Accept-Encoding': 'gzip, deflate',
168168
'Accept': '*/*',
169169
'Connection': 'keep-alive',
@@ -203,7 +203,7 @@ def test_output_request_should_be_logged_in_elasticsearch_backend(self, user):
203203
)
204204
assert_equal_model_fields(
205205
elasticsearch_output_request_log,
206-
request_headers='{"User-Agent": "python-requests/2.26.0", "Accept-Encoding": "gzip, deflate", '
206+
request_headers='{"User-Agent": "python-requests/2.27.1", "Accept-Encoding": "gzip, deflate", '
207207
'"Accept": "*/*", "Connection": "keep-alive", "Content-Length": "16"}',
208208
request_body='{"test": "test"}',
209209
method='POST',
@@ -264,7 +264,7 @@ def test_error_output_request_should_be_logged_in_sql_backend(self,):
264264
assert_equal_model_fields(
265265
sql_output_request_log,
266266
request_headers={
267-
'User-Agent': 'python-requests/2.26.0',
267+
'User-Agent': all_eq_obj,
268268
'Accept-Encoding': 'gzip, deflate',
269269
'Accept': '*/*',
270270
'Connection': 'keep-alive',
@@ -301,7 +301,7 @@ def test_error_output_request_should_be_logged_in_elasticsearch_backend(self):
301301
)
302302
assert_equal_model_fields(
303303
elasticsearch_input_request_log,
304-
request_headers='{"User-Agent": "python-requests/2.26.0", "Accept-Encoding": "gzip, deflate", '
304+
request_headers='{"User-Agent": "python-requests/2.27.1", "Accept-Encoding": "gzip, deflate", '
305305
'"Accept": "*/*", "Connection": "keep-alive", "Content-Length": "16"}',
306306
request_body='{"test": "test"}',
307307
method='POST',

security/backends/common/is_core.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pyston.utils.decorators import filter_by, order_by
1313

1414
from is_core.generic_views.detail_views import DjangoReadonlyDetailView
15+
from is_core.site import get_model_core
1516
from is_core.utils import display_code, render_model_object_with_link, render_model_objects_with_link
1617
from is_core.utils.decorators import short_description
1718

@@ -142,6 +143,18 @@ def __new__(cls, *args, **kwargs):
142143
]
143144
return super().__new__(cls)
144145

146+
@filter_by('id')
147+
@order_by('id')
148+
@short_description(_('short ID'))
149+
def short_id(self, obj):
150+
return truncatechars(str(obj.id), 8)
151+
152+
@filter_by('slug')
153+
@order_by('slug')
154+
@short_description(_('short slug'))
155+
def short_slug(self, obj):
156+
return truncatechars(obj.slug, 20) if obj.slug is not None else None
157+
145158
@short_description(_('error message'))
146159
@filter_by('error_message')
147160
def error_message_code(self, obj):
@@ -150,7 +163,13 @@ def error_message_code(self, obj):
150163
@short_description(_('source'))
151164
def display_source(self, obj, request):
152165
parent_log_obj = self._get_parent_log_obj(obj)
153-
return render_model_object_with_link(request, parent_log_obj) if parent_log_obj else None
166+
if not parent_log_obj:
167+
return None
168+
core_parent_model = get_model_core(parent_log_obj.__class__)
169+
verbose_name_with_last_id_digits = '{} {}'.format(
170+
core_parent_model.verbose_name, core_parent_model.short_id(parent_log_obj)
171+
)
172+
return render_model_object_with_link(request, parent_log_obj, display_value=verbose_name_with_last_id_digits)
154173

155174
def _get_parent_log_obj(self, obj):
156175
raise NotImplementedError
@@ -253,9 +272,9 @@ class InputRequestLogCoreMixin(RequestLogCoreMixin):
253272
menu_group = 'inputrequestlog'
254273

255274
list_fields = (
256-
'id', 'start', 'stop', 'release', 'slug', 'view_slug', 'host', 'short_path', 'state', 'time', 'method',
257-
'short_queries', 'response_code', 'short_request_headers', 'short_request_body', 'short_response_headers',
258-
'short_response_body', 'user', 'ip',
275+
'short_id', 'start', 'stop', 'time', 'view_slug', 'response_code', 'state', 'method', 'host', 'short_path',
276+
'user', 'short_request_body', 'short_response_body', 'short_queries', 'short_request_headers',
277+
'short_response_headers', 'ip', 'release', 'short_slug',
259278
)
260279

261280
verbose_name = _('input request log')
@@ -302,8 +321,9 @@ class OutputRequestLogCoreMixin(RequestLogCoreMixin):
302321
menu_group = 'outputrequestlog'
303322

304323
list_fields = (
305-
'id', 'start', 'stop', 'release', 'slug', 'host', 'short_path', 'state', 'time', 'method', 'short_queries',
306-
'response_code', 'short_request_headers', 'short_request_body', 'short_response_headers', 'short_response_body'
324+
'short_id', 'start', 'stop', 'time', 'response_code', 'state', 'method', 'host', 'short_path',
325+
'short_request_body', 'short_response_headers', 'short_queries', 'short_request_headers', 'short_response_body',
326+
'release'
307327
)
308328

309329
fieldsets = (
@@ -344,7 +364,7 @@ class CommandLogCoreMixin(OutputLogCoreMixin, LogCoreMixin):
344364
menu_group = 'commandlog'
345365

346366
list_fields = (
347-
'id', 'start', 'stop', 'release', 'slug', 'name', 'state', 'time'
367+
'short_id', 'start', 'stop', 'time', 'name', 'state', 'release', 'short_slug'
348368
)
349369

350370
verbose_name = _('command log')
@@ -397,10 +417,12 @@ class CeleryTaskRunLogCoreMixin(OutputLogCoreMixin, CeleryCoreMixin, LogCoreMixi
397417

398418
menu_group = 'celerytaskrunlog'
399419

400-
rest_fields = list_fields = (
401-
'id', 'start', 'stop', 'release', 'slug', 'celery_task_id', 'name', 'state', 'waiting_time', 'time',
402-
'short_input', 'queue_name'
420+
ALL_FIELDS = (
421+
'start', 'stop', 'time', 'name', 'state', 'short_input', 'queue_name', 'celery_task_id', 'waiting_time',
422+
'release',
403423
)
424+
rest_fields = ('id', *ALL_FIELDS, 'slug')
425+
list_fields = ('short_id', *ALL_FIELDS, 'short_slug')
404426

405427
verbose_name = _('celery task run log')
406428
verbose_name_plural = _('celery task run logs')
@@ -440,10 +462,12 @@ class CeleryTaskInvocationLogCoreMixin(CeleryCoreMixin, LogCoreMixin):
440462

441463
menu_group = 'celerytaskinvocationlog'
442464

443-
rest_fields = list_fields = (
444-
'id', 'start', 'stop', 'release', 'slug', 'celery_task_id', 'name', 'state_with_duplicate', 'time',
445-
'short_input', 'queue_name'
465+
ALL_FIELDS = (
466+
'start', 'stop', 'time', 'name', 'state_with_duplicate', 'short_input', 'queue_name', 'is_duplicate',
467+
'celery_task_id', 'release'
446468
)
469+
rest_fields = ('id', *ALL_FIELDS, 'slug')
470+
list_fields = ('short_id', *ALL_FIELDS, 'short_slug')
447471

448472
celery_task_run_inline_table_view = None
449473

security/backends/elasticsearch/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
22

3+
from django.utils.encoding import force_text
4+
from django.utils.timezone import localtime
35
from elasticsearch import NotFoundError
46

57
from django.db import router
@@ -135,6 +137,13 @@ class RequestLog(Log):
135137
state = EnumField(enum=RequestLogState)
136138
error_message = Text()
137139

140+
def __str__(self):
141+
return ' '.join(
142+
(force_text(v) for v in (
143+
self.short_path, self.response_code, localtime(self.start.replace(microsecond=0))
144+
) if v)
145+
)
146+
138147

139148
class InputRequestLog(RequestLog):
140149

0 commit comments

Comments
 (0)