Skip to content
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

feat: allow reuse of status indicators #319

Merged
merged 51 commits into from
Mar 11, 2023

Conversation

fsbraun
Copy link
Member

@fsbraun fsbraun commented Feb 12, 2023

Description

This PR refactors the indicators.py setup to allow using indicators outside the PageTree (i.e. other versioned models). Existing functionality does not change.

  • The indicator code now works for all versioned object types
  • The indicator mixing for the page content admin has been moved into the page content admin
  • Some pagecontent-based helper functions have been generalized for all versioned models
  • To improve db access a latest_content() has been added to the admin_manager query set. It respects prefetched content objects reducing db accesses
  • Documentation is updated
  • Tests for content indicators for non-page content objects included

Usage

The status indicator for versioning can be added to a ModelAdmin by:

    from djangocms_versioning.admin import StateIndicatorMixin

    class MyModelAdmin(StateIndicatorMixin, admin.Admin):
        # Add indicator column to list_items
         list_items = [..., "status_indicator", ...]

Remarks:

  • Using the mixin will load the cms.pagetree.css (not the js) from the core into the admin form
  • Additionally, the indicators also work with ExtendedVersionAdminMixin.
  • The new ExtendedIndicatorVersionAdminMixin combines state indicators and the extended versioning admin.
  • Test are added
  • Documentation is updated

Example: djangocms-alias

This is the result of relpacing ExtendedVersionAdminMixin with ExtendedIndicatorVersionAdminMixin for djangocms-alias:
image

Checklist

  • I have opened this pull request against master
  • I have added or modified the tests when changing logic
  • I have followed the conventional commits guidelines to add meaningful information into the changelog
  • I have read the contribution guidelines and I have joined #workgroup-pr-review on
    Slack to find a “pr review buddy” who is going to review my pull request.

@codecov
Copy link

codecov bot commented Feb 12, 2023

Codecov Report

Merging #319 (f5c5fcb) into master (7e41d8a) will increase coverage by 0.53%.
The diff coverage is 92.90%.

@@            Coverage Diff             @@
##           master     #319      +/-   ##
==========================================
+ Coverage   89.99%   90.53%   +0.53%     
==========================================
  Files          68       68              
  Lines        2259     2324      +65     
  Branches      301      314      +13     
==========================================
+ Hits         2033     2104      +71     
+ Misses        171      165       -6     
  Partials       55       55              
Impacted Files Coverage Δ
djangocms_versioning/forms.py 100.00% <ø> (ø)
djangocms_versioning/admin.py 89.13% <85.71%> (+0.37%) ⬆️
djangocms_versioning/indicators.py 91.07% <92.85%> (+11.07%) ⬆️
djangocms_versioning/cms_config.py 81.77% <100.00%> (+1.17%) ⬆️
djangocms_versioning/cms_toolbars.py 95.95% <100.00%> (ø)
djangocms_versioning/datastructures.py 95.40% <100.00%> (-0.16%) ⬇️
djangocms_versioning/helpers.py 91.48% <100.00%> (+0.37%) ⬆️
djangocms_versioning/managers.py 85.24% <100.00%> (+2.55%) ⬆️
djangocms_versioning/test_utils/blogpost/admin.py 100.00% <100.00%> (ø)
djangocms_versioning/versionables.py 100.00% <100.00%> (+8.33%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

from djangocms_versioning.test_utils.blogpost import models


class BlogContentAdmin(ExtendedVersionAdminMixin, admin.ModelAdmin):
pass
class BlogContentAdmin(StateIndicatorMixin, ExtendedVersionAdminMixin, admin.ModelAdmin):

Check warning

Code scanning / CodeQL

Conflicting attributes in base classes

Base classes have conflicting values for attribute 'change_list_template': [str u'djangocms_versioning/admin/mixin/change_list.html'](1) and [NoneType None](2). Base classes have conflicting values for attribute 'change_form_template': [str u'djangocms_versioning/admin/mixin/change_form.html'](3) and [NoneType None](2). Base classes have conflicting values for attribute 'Media': [class Media](4) and [class Media](5).


admin.site.register(models.BlogPost)
class BlogPostAdmin(StateIndicatorMixin, ExtendedVersionAdminMixin, admin.ModelAdmin):

Check warning

Code scanning / CodeQL

Conflicting attributes in base classes

Base classes have conflicting values for attribute 'change_list_template': [str u'djangocms_versioning/admin/mixin/change_list.html'](1) and [NoneType None](2). Base classes have conflicting values for attribute 'change_form_template': [str u'djangocms_versioning/admin/mixin/change_form.html'](3) and [NoneType None](2). Base classes have conflicting values for attribute 'Media': [class Media](4) and [class Media](5).
@@ -326,6 +407,14 @@
return list_display


class ExtendedIndicatorVersionAdminMixin(StateIndicatorMixin, ExtendedVersionAdminMixin):

Check warning

Code scanning / CodeQL

Conflicting attributes in base classes

Base classes have conflicting values for attribute 'Media': [class Media](1) and [class Media](2).
djangocms_versioning/admin.py Fixed Show fixed Hide fixed
@fsbraun
Copy link
Member Author

fsbraun commented Feb 21, 2023

@Aiky30 I've updated the PR.

I do not expect additional backwards incompatible behavior. This version of versioning will not work with Django CMS 4.0 anyways since it does not use monkey patching.

All elements were tested, but only indirectly. I added tests to explicitly test especially the new query sets.

djangocms_versioning/helpers.py Show resolved Hide resolved
js = ("admin/js/jquery.init.js", "djangocms_versioning/js/indicators.js",)
# css for indicators and context menu
css = {
"all": (static_with_version("cms/css/cms.pagetree.css"),),
Copy link
Contributor

Choose a reason for hiding this comment

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

A ticket to create for a future change in the core and here is to separate the indicator styling out of this file.

djangocms_versioning/admin.py Outdated Show resolved Hide resolved
"""
grouping_filters = {}
for field in versionable.extra_grouping_fields:
if hasattr(self, f"get_{field}_from_request"):
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not seeing unit test coverage for this new condition that checks that the get_field_form_request. Is it covered by another test?

djangocms_versioning/helpers.py Outdated Show resolved Hide resolved
tests/test_indicators.py Outdated Show resolved Hide resolved
tests/test_indicators.py Outdated Show resolved Hide resolved
tests/test_indicators.py Outdated Show resolved Hide resolved
tests/test_indicators.py Outdated Show resolved Hide resolved
self.assertEqual(content.versions.first(), version1)

# Now archive
version1.archive(user=self.get_superuser())
Copy link
Contributor

Choose a reason for hiding this comment

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

Not that important but each check could have been a separate test in the Class, with the version being defined in setup. would have been able to keep the AAA test pattern that's used elsewhere then too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed it.

fsbraun and others added 4 commits March 2, 2023 20:36
Co-authored-by: Andrew Aikman <Aiky30@users.noreply.github.com>
Co-authored-by: Andrew Aikman <Aiky30@users.noreply.github.com>
Co-authored-by: Andrew Aikman <Aiky30@users.noreply.github.com>
fsbraun and others added 6 commits March 2, 2023 20:53
Co-authored-by: Andrew Aikman <Aiky30@users.noreply.github.com>
Co-authored-by: Andrew Aikman <Aiky30@users.noreply.github.com>
Co-authored-by: Andrew Aikman <Aiky30@users.noreply.github.com>
Co-authored-by: Andrew Aikman <Aiky30@users.noreply.github.com>
get_preview_url,
proxy_model,
version_list_url,
)
from .indicators import content_indicator, content_indicator_menu

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [djangocms_versioning.indicators](1) begins an import cycle.
@fsbraun
Copy link
Member Author

fsbraun commented Mar 6, 2023

@Aiky30 OK, I'm ready for round three! Thanks for asking for the test for get_{field}_from_request. The test (now added) revealed that I was looking for that method in the ChangeList class while it should be in the model admin class. 🙌

The only open point is the import of cms.pagetree.css which I believe is ok:

  • cms.pagetree.css contains: icons.scss, dropdown.scss, legend.scss, and node-state.scss which are needed by this PR
  • cms.pagetree.css also contains tree.scss which is not needed by this PR.

I figured that css.pagetree.css will be available in the browser cache with a high probability, and there is little chance of erroneous cross-styling as long as you do not have .jstree or .cms-tree-item-xxx or .cms-pagetree-xxx in your changelist admin templates. (If you have to have you will not be able to use the mixin.)

Copy link
Contributor

@Aiky30 Aiky30 left a comment

Choose a reason for hiding this comment

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

Looks good and great test coverage. Good job. Thanks for taking the time to consider and act on the comments. :-)

@fsbraun
Copy link
Member Author

fsbraun commented Mar 11, 2023

Thanks for talking the time to look through this. I appreciate the comments (and the additional coverage prevented a bug 🙂).

@fsbraun fsbraun merged commit 1e8a48c into django-cms:master Mar 11, 2023
@fsbraun fsbraun deleted the feat/code-reusability branch March 15, 2023 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants