8000 Log operations by Aiky30 · Pull Request #6419 · django-cms/django-cms · GitHub
[go: up one dir, main page]

Skip to content

Log operations #6419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c393d73
Logging helper, page operation logs and page operation tests implemented
Jun 15, 2018
8309bac
Amended the logs to contain the correct action flag
Jun 18, 2018
3365d4f
Added tests for the action flag, changed the page log messages and te…
Jun 18, 2018
e18af96
Updated the messages and tests to cover more of the log entry fields …
Jun 18, 2018
41af0c5
Added new check for get_change_message to ensure that the view functi…
Jun 18, 2018
8ca4740
Cleaning the comments in the code
Jun 18, 2018
e39fbb5
All tests now passing although the method of getting the page admin w…
Jun 18, 2018
d7677df
Cleaned comments and Todo message
Jun 19, 2018
721948e
Cleaned out and organised better for pep 8 compliance
Jun 19, 2018
d92ba32
Cleaned out comments
Jun 19, 2018
8b79b72
logging page creation shared tests renamed
Jun 19, 2018
27bb832
Clean code to pass py flakes tests
Jun 19, 2018
2c0df27
Added various changes to the logging including changing to a signals …
Jun 21, 2018
801a901
committing progress for discussion
Jun 22, 2018
32a91db
Changed the logging message format and the object of all placeholder …
Jun 22, 2018
322181d
Removed the log creation from the form. Amended logs to treat the pag…
Jun 25, 2018
467ea74
Added all tests, disabled the standard Admin log created, changed and…
Jun 25, 2018
b0d90be
Comment the disabled log entry overrides and remove Manual LogEntry c…
Jun 25, 2018
84c6aa5
Reverse changes to the form file
Jun 25, 2018
6ccfb02
Prepare for the form signal handler via operations. Failing tests.
Jun 26, 2018
d707ce8
Added fake request to continue dev. Amended tests and code to work as…
Jun 26, 2018
c5c032d
Amended the logs to fire for edit title fieldgit status
Jun 26, 2018
9bd9902
Add a page changed flag for when fields on page are changed
Jun 26, 2018
ccc6354
Repair changes made to other files
Jun 26, 2018
e741e04
Removed unused plugin
Jun 26, 2018
e723bd1
Remove space that I've added
Jun 26, 2018
81f9024
Amendments from PR feedback
Jun 27, 2018
9f0a770
Merge branch 'release/4.0.x' of github.com:divio/django-cms into rele…
Jun 27, 2018
6030a71
Repair merge of latest test failure
Jun 27, 2018
b82416d
Removed failing test that checks for publishing logs in the test_admi…
Jun 27, 2018
8bed9d0
Amend the admin form to send through the correct request object and r…
Jun 27, 2018
c192dce
Removed redundnant LogEntry import from test_admin
Jun 27, 2018
89b0249
Format the code as per Paulos PR feedback
Jun 28, 2018
4e4d331
fixed failing test due to cleaning too far
Jun 28, 2018
b57802c
cosmetics
czpython Jun 28, 2018
3b4e002
Updated all of the test docstrings to better describe the purpose of …
Jun 28, 2018
4bf6ce9
Add tests that the correct user is attached to each log
Jun 29, 2018
153a60b
Amended log entry tests by ensuring the correct users are configured …
Jun 29, 2018
2c0a1c6
Changed the users id to pk on the tests£
Jul 2, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cms/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
from cms.models import (CMSPlugin, Page, PageType, PagePermission, PageUser, PageUserGroup, Title,
Placeholder, GlobalPagePermission, TreeNode)
from cms.models.permissionmodels import User
from cms.operations import (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest:

from cms.operations import ADD_PAGE_TRANSLATION, CHANGE_PAGE
from cms.operations.helpers import (
    send_pre_page_operation,
    send_post_page_operation,
)

helpers as operation_helpers,
ADD_PAGE_TRANSLATION,
CHANGE_PAGE,
)
from cms.plugin_pool import plugin_pool
from cms.signals.apphook import set_restart_trigger
from cms.utils.conf import get_cms_setting
Expand All @@ -36,6 +41,9 @@
)
from menus.menu_pool import menu_pool

# FIXME: REMOVEME:
from attrdict import AttrDict


def get_permission_accessor(obj):
User = get_user_model()
Expand Down Expand Up @@ -131,6 +139,9 @@ class BasePageForm(forms.ModelForm):
help_text=_('A description of the page used by search engines.'),
max_length=320)

# FIXME: REMOVEME: Waiting for request to be passed through, temp hack for testing in the UI
request = AttrDict({'user': {'pk': 1 }})

class Meta:
model = Page
fields = []
Expand Down Expand Up @@ -261,6 +272,11 @@ def save(self, *args, **kwargs):
source = self.cleaned_data.get('source')
parent = self.cleaned_data.get('parent_node')

operation_token = operation_helpers.send_pre_page_operation(
request=self.request,
operation=ADD_PAGE_TRANSLATION,
)

if source:
new_page = self.from_source(source, parent=parent)

Expand Down Expand Up @@ -294,6 +310,14 @@ def save(self, *args, **kwargs):
# its the first page. publish it right away
new_page.publish(translation.language)
new_page.set_as_homepage(self._user)

operation_helpers.send_post_page_operation(
request=self.request,
operation=ADD_PAGE_TRANSLATION,
token=operation_token,
obj=new_page,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

8000
return new_page


Expand Down Expand Up @@ -443,6 +467,12 @@ def clean(self):
return data

def save(self, commit=True):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

operation_token = operation_helpers.send_pre_page_operation(
request=self.request,
operation=CHANGE_PAGE,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

data = self.cleaned_data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move data = self.cleaned_data above operation_token

cms_page = super(ChangePageForm, self).save(commit=False)

Expand All @@ -467,6 +497,14 @@ def save(self, commit=True):
else:
cms_page._update_title_path_recursive(self._language)
cms_page.clear_cache(menu=True)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

operation_helpers.send_post_page_operation(
request=self.request,
operation=CHANGE_PAGE,
token=operation_token,
obj=cms_page,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

return cms_page


Expand Down
68 changes: 39 additions & 29 deletions cms/admin/pageadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import copy
import json
import sys
import uuid


import django
from django.contrib.admin.helpers import AdminForm
from django.conf import settings
from django.conf.urls import url
from django.contrib import admin, messages
from django.contrib.admin.models import LogEntry, CHANGE
from django.contrib.admin.models import LogEntry
from django.contrib.admin.options import IS_POPUP_VAR
from django.contrib.admin.utils import get_deleted_objects
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -60,8 +58,8 @@
Title, CMSPlugin, PagePermission,
GlobalPagePermission, StaticPlaceholder,
)
from cms.operations.helpers import send_post_page_operation, send_pre_page_operation
from cms.plugin_pool import plugin_pool
from cms.signals import pre_obj_operation, post_obj_operation
from cms.signals.apphook import set_restart_trigger
from cms.toolbar_pool import toolbar_pool
from cms.utils import permissions, get_current_site, get_language_from_request, copy_plugins
Expand Down Expand Up @@ -120,6 +118,18 @@ class BasePageAdmin(PlaceholderAdminMixin, admin.ModelAdmin):

inlines = PERMISSION_ADMIN_INLINES

def log_addition(self, request, object, object_repr):
# Block the admin log for addition. A signal takes care of this!
return

def log_deletion(self, request, object, object_repr):
# Block the admin log for deletion. A signal takes care of this!
return

def log_change(self, request, object, message):
# Block the admin log for change. A signal takes care of this!
return

def get_admin_url(self, action, *args):
url_name = "{}_{}_{}".format(
self.opts.app_label,
Expand Down Expand Up @@ -200,22 +210,18 @@ def get_urls(self):
return url_patterns + super(BasePageAdmin, self).get_urls()

def _send_pre_page_operation(self, request, operation, **kwargs):
token = str(uuid.uuid4())
pre_obj_operation.send(
sender=self.__class__,
operation=operation,
return send_pre_page_operation(
request=request,
token=token,
**kwargs
)
return token
operation=operation,
sender=self.__class__,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change sender to self.model
also, I suggest:

token = call_the_function(
    arg1='test',
    arg2='test',
    etc..
)
return token

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had initially tried self.model but it wasn't available for some reason. It's possible that one failed so I removed them all for consistency. I'll add them back and test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope that I've interpreted your comment correctly. I've removed the private class methods and just used the functions directly. This required changing a lot of the calls to fire a signal.

**kwargs)

def _send_post_page_operation(self, request, operation, token, **kwargs):
post_obj_operation.send(
sender=self.__class__,
send_post_page_operation(
operation=operation,
request=request,
token=token,
sender=self.__class__,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change sender to self.model

**kwargs
)

Expand Down Expand Up @@ -900,6 +906,7 @@ def move_page(self, request, page_id, extra_context=None):
token=operation_token,
obj=page,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no newline

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

return jsonify_request(HttpResponse(status=200))

def get_permissions(self, request, page_id):
Expand Down Expand Up @@ -1153,13 +1160,6 @@ def publish_page(self, request, page_id, language):
messages.warning(request, _("Page not published! A parent page is not published yet."))
else:
messages.info(request, _('The content was successfully published.'))
LogEntry.objects.log_action(
user_id=request.user.id,
content_type_id=ContentType.objects.get_for_model(Page).pk,
object_id=page_id,
object_repr=page.get_title(language),
action_flag=CHANGE,
)
else:
if page.get_publisher_state(language) == PUBLISHER_STATE_PENDING:
messages.warning(request, _("Page not published! A parent page is not published yet."))
Expand Down Expand Up @@ -1222,14 +1222,7 @@ def unpublish(self, request, page_id, language):
message = _('The %(language)s page "%(page)s" was successfully unpublished') % {
'language': language_name, 'page': page}
messages.info(request, message)
LogEntry.objects.log_action(
user_id=request.user.id,
content_type_id=ContentType.objects.get_for_model(Page).pk,
object_id=page_id,
object_repr=page.get_title(),
action_flag=CHANGE,
change_message=message,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

except RuntimeError:
exc = sys.exc_info()[1]
messages.error(request, exc.message)
Expand Down Expand Up @@ -1614,7 +1607,24 @@ class Meta:
if not cancel_clicked and request.method == 'POST':
form = PageTitleForm(instance=translation, data=request.POST)
if form.is_valid():

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

operation_token = self._send_pre_page_operation(
request,
operation=operations.CHANGE_PAGE_TRANSLATION,
obj=page,
translation=translation,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

form.save()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

self._send_post_page_operation(
request,
operation=operations.CHANGE_PAGE_TRANSLATION,
token=operation_token,
obj=page,
translation=translation,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline

saved_successfully = True
else:
form = PageTitleForm(instance=translation)
Expand Down
3 changes: 3 additions & 0 deletions cms/operations.py → cms/operations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
CLEAR_PLACEHOLDER = 'clear_placeholder'

# Page operations
CHANGE_PAGE = 'change_page'
MOVE_PAGE = 'move_page'
DELETE_PAGE = 'delete_page'

# Page translation operations
ADD_PAGE_TRANSLATION = 'add_page_translation'
CHANGE_PAGE_TRANSLATION = 'change_page_translation'
DELETE_PAGE_TRANSLATION = 'delete_page_translation'
PUBLISH_PAGE_TRANSLATION = 'publish_page_translation'
REVERT_PAGE_TRANSLATION_TO_LIVE = 'revert_page_translation_to_live'
Expand Down
27 changes: 27 additions & 0 deletions cms/operations/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
import uuid

from cms.models import Page
from cms.signals import pre_obj_operation, post_obj_operation


def send_pre_page_operation(request, operation, sender=Page, **kwargs):
token = str(uuid.uuid4())
pre_obj_operation.send(
sender=sender,
operation=operation,
request=request,
token=token,
**kwargs
)
return token


def send_post_page_operation(request, operation, token, sender=Page, **kwargs):
post_obj_operation.send(
sender=sender,
operation=operation,
request=request,
token=token,
**kwargs
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newline at end of file

8 changes: 8 additions & 0 deletions cms/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from cms.signals.apphook import debug_server_restart, trigger_server_restart
from cms.signals.log_entries import log_page_operations, log_placeholder_operations
from cms.signals.permissions import post_save_user, post_save_user_group, pre_save_user, pre_delete_user, pre_save_group, pre_delete_group, pre_save_pagepermission, pre_delete_pagepermission, pre_save_globalpagepermission, pre_delete_globalpagepermission
from cms.utils.conf import get_cms_setting

Expand Down Expand Up @@ -78,6 +79,13 @@
dispatch_uid='aldryn-apphook-reload-handle-urls-need-reloading'
)


###################### log entries #######################


post_obj_operation.connect(log_page_operations)
post_placeholder_operation.connect(log_placeholder_operations)

###################### permissions #######################

if get_cms_setting('PERMISSION'):
Expand Down
Loading
0