Skip to content

Commit

Permalink
[fix] Decoupled admin LogEntry from Template Model openwisp#364
Browse files Browse the repository at this point in the history
Moved the LogEntry section of the code from `template.py` to
the clone method implemented in `admin.py`.
This allows to avoid depending on the django-admin, making the admin interface optional.

Fixes openwisp#364
  • Loading branch information
ManishShah120 committed Mar 6, 2021
1 parent ce5473b commit 4005d9f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
15 changes: 15 additions & 0 deletions openwisp_controller/config/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from django.conf.urls import url
from django.contrib import admin, messages
from django.contrib.admin import helpers
from django.contrib.admin.models import ADDITION, LogEntry
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import (
FieldDoesNotExist,
ObjectDoesNotExist,
Expand Down Expand Up @@ -566,6 +568,17 @@ class TemplateAdmin(MultitenantAdminMixin, BaseConfigAdmin, SystemDefinedVariabl
def clone_selected_templates(self, request, queryset):
selectable_orgs = None
user = request.user

def create_log_entry(user, clone):
ct = ContentType.objects.get(model='template')
LogEntry.objects.log_action(
user_id=user.id,
content_type_id=ct.pk,
object_id=clone.pk,
object_repr=clone.name,
action_flag=ADDITION,
)

if user.is_superuser:
all_orgs = Organization.objects.all()
if all_orgs.count() > 1:
Expand All @@ -581,6 +594,7 @@ def clone_selected_templates(self, request, queryset):
clone.organization = Organization.objects.get(
pk=request.POST.get('organization')
)
create_log_entry(user, clone)
clone.save()
self.message_user(
request,
Expand All @@ -605,6 +619,7 @@ def clone_selected_templates(self, request, queryset):
else:
for template in queryset:
clone = template.clone(user)
create_log_entry(user, clone)
clone.save()

actions = ['clone_selected_templates']
Expand Down
10 changes: 0 additions & 10 deletions openwisp_controller/config/base/template.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from collections import OrderedDict
from copy import copy

from django.contrib.admin.models import ADDITION, LogEntry
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -197,14 +195,6 @@ def clone(self, user):
clone.default = False
clone.full_clean()
clone.save()
ct = ContentType.objects.get(model='template')
LogEntry.objects.log_action(
user_id=user.id,
content_type_id=ct.pk,
object_id=clone.pk,
object_repr=clone.name,
action_flag=ADDITION,
)
return clone

def __get_clone_name(self):
Expand Down
7 changes: 7 additions & 0 deletions openwisp_controller/config/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from unittest.mock import patch

from django.contrib.admin.models import LogEntry
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
Expand Down Expand Up @@ -973,6 +974,12 @@ def test_clone_template(self):
self.assertContains(response, '{} (Clone 2)'.format(t.name))
response = self.client.post(path, data, follow=True)
self.assertContains(response, '{} (Clone 3)'.format(t.name))
path = reverse('admin:index')
response = self.client.get(path)
self.assertIn('test-template (Clone 3)', str(response.content))
self.assertIn('test-template (Clone 2)', str(response.content))
self.assertIn('test-template (Clone)', str(response.content))
self.assertEqual(LogEntry.objects.all().count(), 3)

def test_get_template_default_values(self):
t1 = self._create_template(name='t1', default_values={'name1': 'test1'})
Expand Down

0 comments on commit 4005d9f

Please sign in to comment.