Skip to content

Commit

Permalink
Update ajax views (#102)
Browse files Browse the repository at this point in the history
* separating auditlog views

* fixing version
  • Loading branch information
davidslusser committed Apr 20, 2023
1 parent a08dcd2 commit cfeabdc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ ENV/
# intelliJ
*.idea
*.iml
.vscode
2 changes: 1 addition & 1 deletion handyhelpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

__title__ = 'django-handyhelpers'
__version__ = '0.3.0'
__version__ = '0.3.1'
__author__ = 'David Slusser'
__email__ = 'dbslusser@gmail.com'
__license__ = 'GPL-3.0'
Expand Down
2 changes: 1 addition & 1 deletion handyhelpers/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def base_template(request):
return {'BASE_TEMPLATE': getattr(settings, 'BASE_TEMPLATE', 'handyhelpers/handyhelpers_base.htm')}
return {'BASE_TEMPLATE': getattr(settings, 'BASE_TEMPLATE', 'handyhelpers/handyhelpers_base_bs5.htm')}
15 changes: 13 additions & 2 deletions handyhelpers/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from django.conf import settings
from django.urls import path
from handyhelpers.views import action
from handyhelpers.views import ajax
from handyhelpers.views import host
from handyhelpers.views import htmx

if 'auditlog' in settings.INSTALLED_APPS:
from handyhelpers.views import auditlog


app_name = 'handyhelpers'

urlpatterns = [
Expand All @@ -24,10 +29,16 @@
path('host_process_details/', ajax.GetHostProcessDetails.as_view(), name='host_process_details'),
path('host_partition_usage/', ajax.GetHostParitionUsage.as_view(), name='host_partition_usage'),
path('get_host_cpu_stats/', ajax.GetHostCpuStats.as_view(), name='get_host_cpu_stats'),
path('get_auditlog_entries/<str:model_name>/<str:pk>/', ajax.GetAuditLogEntries.as_view(), name='get_auditlog_entries'),
path('get_auditlog_entry/<int:id>/', ajax.GetAuditLogEntry.as_view(), name='get_auditlog_entry'),

# htmx views
path('get_host_processes/', htmx.GetHostProcesses.as_view(), name='get_host_processes'),

]

if 'auditlog' in settings.INSTALLED_APPS:
urlpatterns.extend(
[
path('get_auditlog_entries/<str:model_name>/<str:pk>/', auditlog.GetAuditLogEntries.as_view(), name='get_auditlog_entries'),
path('get_auditlog_entry/<int:id>/', auditlog.GetAuditLogEntry.as_view(), name='get_auditlog_entry'),
]
)
40 changes: 1 addition & 39 deletions handyhelpers/views/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
import psutil

from django.views import View
from django.apps import apps
from django.http import HttpResponse
from django.template import loader
from django.views.decorators.http import require_GET

from auditlog.models import LogEntry


class AjaxGetView(View):
Expand All @@ -27,40 +23,6 @@ def get(self, request, *args, **kwargs):
return HttpResponse(json.dumps({'server_response': self.template.render({'data': None})}),
content_type='application/javascript')


class GetAuditLogEntries(AjaxGetView):
"""
Description:
Get AuditLog entries for a given model and instance.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/get_auditlog_entries.htm')

def get(self, request, *args, **kwargs):
self.data = LogEntry.objects.filter(content_type__model=kwargs['model_name'],
object_pk=kwargs['pk'])
return super().get(request, *args, **kwargs)


class GetAuditLogEntry(AjaxGetView):
"""
Description:
Get details for a LogEntry of auditlog..
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/get_auditlog_entry.htm')

def get(self, request, *args, **kwargs):
self.data = LogEntry.objects.filter(id=kwargs['id'])
return super().get(request, *args, **kwargs)



class GetHostNetworkStats(AjaxGetView):
"""
Expand Down Expand Up @@ -134,4 +96,4 @@ def get(self, request, *args, **kwargs):
time_percent=psutil.cpu_times_percent(percpu=True)[cpu],
frequency=psutil.cpu_freq(percpu=True)[cpu],
)
return super().get(request, *args, **kwargs)
return super().get(request, *args, **kwargs)
36 changes: 36 additions & 0 deletions handyhelpers/views/auditlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.template import loader
from auditlog.models import LogEntry

from .ajax import AjaxGetView

class GetAuditLogEntries(AjaxGetView):
"""
Description:
Get AuditLog entries for a given model and instance.
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/get_auditlog_entries.htm')

def get(self, request, *args, **kwargs):
self.data = LogEntry.objects.filter(content_type__model=kwargs['model_name'],
object_pk=kwargs['pk'])
return super().get(request, *args, **kwargs)


class GetAuditLogEntry(AjaxGetView):
"""
Description:
Get details for a LogEntry of auditlog..
Args:
request: AJAX request object.
Returns:
HttpResponse: JSON formatted response.
"""
template = loader.get_template('handyhelpers/ajax/get_auditlog_entry.htm')

def get(self, request, *args, **kwargs):
self.data = LogEntry.objects.filter(id=kwargs['id'])
return super().get(request, *args, **kwargs)

0 comments on commit cfeabdc

Please sign in to comment.