Skip to content

Commit

Permalink
feat: Update info if new django CMS version is available (#474)
Browse files Browse the repository at this point in the history
* fix: use pypi api to check for latest version
* Update tests
  • Loading branch information
fsbraun authored Dec 14, 2022
1 parent 6107883 commit 51f0e7a
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/screenshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.8] # dockerfile uses 3.8
django-version: ['2.2', '3.1']
python-version: [3.9] # dockerfile uses 3.8
django-version: ['2.2', '3.2']
os: [
ubuntu-20.04,
]
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ '3.7', '3.8', '3.9', '3.10']
python-version: [ '3.8', '3.9', '3.10', '3.11']
requirements-file: [
django-2.2.txt,
django-3.0.txt,
django-3.1.txt
django-3.2.txt,
django-4.1.txt
]
os: [
ubuntu-20.04,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
unreleased
==========

* Check updates based on pypi release
* Removed unused css generating conflicts for icons fonts of
djangocms-admin-style and django-cms core.

Expand Down
7 changes: 0 additions & 7 deletions Dockerfile.django-3.1

This file was deleted.

7 changes: 7 additions & 0 deletions Dockerfile.django-3.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM djangocms-admin-style-test:base

RUN pip install -r /app/tests/requirements/django-3.2.txt

ENV SCREENSHOT_REFERENCES="./tests/screenshots/reference-3.2"

COPY . /app
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Documentation
See ``REQUIREMENTS`` in the `setup.py <https://github.com/divio/djangocms-admin-style/blob/master/setup.py>`_
file for additional dependencies:

|python| |django| |djangocms|
|python| |django| |djangocms| |djangocms4|



Installation
Expand Down Expand Up @@ -118,7 +119,9 @@ djangocms-casper-helpers.

.. |python| image:: https://img.shields.io/badge/python-3.5+-blue.svg
:target: https://pypi.org/project/djangocms-admin-style/
.. |django| image:: https://img.shields.io/badge/django-2.2,%203.0,%203.1-blue.svg
.. |django| image:: https://img.shields.io/badge/django-2.2%2B-blue.svg
:target: https://www.djangoproject.com/
.. |djangocms| image:: https://img.shields.io/badge/django%20CMS-3.6%2B-blue.svg
:target: https://www.django-cms.org/
.. |djangocms4| image:: https://img.shields.io/badge/django%20CMS-4-blue.svg
:target: https://www.django-cms.org/

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var $ = require('jquery');
var Cookies = require('js-cookie');
var RELEASES_URL = 'https://releases.django-cms.org/';
var RELEASES_URL = 'https://pypi.org/pypi/django-cms/json';
var MAIN_COOKIE_EXPIRATION = 365; // ~1 year
var REQUEST_COOKIE_EXPIRATION = 14; // check only every two weeks

Expand Down Expand Up @@ -118,17 +118,49 @@ function injectMessage(versionObject, checkType) {
* @returns {Boolean}
*/
function shouldShowMessage(versionObj, currentVersion, checkType) {
var cookie = Cookies.get('cms_upgrade_notification_closed');
if (versionObj !== undefined) {
var cookie = Cookies.get('cms_upgrade_notification_closed');

if (cookie) {
cookie = JSON.parse(cookie);
if (cookie) {
cookie = JSON.parse(cookie);
}
if (cookie && cookie.type === checkType && cookie.version === versionObj.version) {
return false;
}
return greaterThanVersion(versionObj.version, currentVersion);
}
return false;
}

if (cookie && cookie.type === checkType && cookie.version === versionObj.version) {
return false;
/**
* @function getVersionObject
* @private
* @param {Object} versions
* @param {String} currentVersion
* @param {String} checkType
* @returns {Object}
*/
function getVersionObject(versions, currentVersion, checkType) {
var comparison = currentVersion.split('rc')[0].split('.');
var version;
var c;

for (var v in versions) {
if (!v.includes('rc')) {
c = v.split('.');
if (c[0] === comparison[0] && (checkType !== 'patch' || c[1] === comparison[1])) {
if (version === undefined || greaterThanVersion(v, version)) {
version = v;
}
}
}
}
if (version) {
return {
version: version,
url: 'https://github.com/django-cms/django-cms/blob/' + version + '/CHANGELOG.rst'
};
}

return greaterThanVersion(versionObj.version, currentVersion);
}

/**
Expand All @@ -142,7 +174,7 @@ function init() {
return;
}

var currentVersion = metaVersion.attr('content');
var currentVersion = metaVersion.attr('content').split('rc')[0];
var checkType = $('meta[name="djangocms_version_check_type"]').attr('content');

getLatestVersionData({
Expand All @@ -156,15 +188,7 @@ function init() {
} catch (e) { }
}

var versionObj = response.latest;

if (checkType === 'patch') {
response.patches.forEach(function (patch) {
if (patch.version.match(new RegExp('^' + currentVersion.replace(/\.[^\.]+$/, '')))) {
versionObj = patch;
}
});
}
var versionObj = getVersionObject(response.releases, currentVersion, checkType);

if (shouldShowMessage(versionObj, currentVersion, checkType)) {
injectMessage(versionObj, checkType);
Expand Down
2 changes: 1 addition & 1 deletion djangocms_admin_style/templatetags/admin_style_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def current_site_name(context):
def render_update_notification(context):
try:
import cms
except ImportError:
except ImportError: # pragma: no cover
check_type = None
notifications_enabled = False
else:
Expand Down
10 changes: 10 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Framework :: Django',
'Framework :: Django :: 2.2',
'Framework :: Django :: 3.1',
'Framework :: Django :: 3.2',
'Framework :: Django :: 4.0',
'Framework :: Django :: 4.1',
'Framework :: Django CMS',
'Framework :: Django CMS :: 3.6',
'Framework :: Django CMS :: 3.7',
'Framework :: Django CMS :: 3.8',
'Framework :: Django CMS :: 3.8',
'Framework :: Django CMS :: 3.9',
'Framework :: Django CMS :: 3.10',
'Framework :: Django CMS :: 3.11',
'Framework :: Django CMS :: 4.0',
'Framework :: Django CMS :: 4.1',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
'Topic :: Software Development',
Expand Down
3 changes: 0 additions & 3 deletions tests/requirements/django-3.0.txt

This file was deleted.

3 changes: 0 additions & 3 deletions tests/requirements/django-3.1.txt

This file was deleted.

3 changes: 3 additions & 0 deletions tests/requirements/django-3.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r base.txt
Django>=3.2,<4
django-cms>=3.8
3 changes: 3 additions & 0 deletions tests/requirements/django-4.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r base.txt
Django>=4.1,<4.2
django-cms>=3.11.1
40 changes: 40 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.test import TestCase
from django.test.client import RequestFactory

import cms


class Object:
pass


class TemplateTagsTestCase(TestCase):
def setUp(self) -> None:
self.request_factory = RequestFactory()

def test_current_site_name(self):
from djangocms_admin_style.templatetags.admin_style_tags import (
current_site_name,
)
site_name = current_site_name(dict())
self.assertEqual(site_name, "example.com") # Default name

with self.modify_settings(INSTALLED_APPS={
'remove': [
'django.contrib.sites',
],
}):
site_name = current_site_name(dict())
self.assertEqual(site_name, "my site") # Generic name

def test_render_update_notification(self):
from djangocms_admin_style.templatetags.admin_style_tags import (
render_update_notification,
)
self.assertEqual(render_update_notification(dict()), "") # No update notification

request = self.request_factory.get("/")
request.resolver_match = Object()
request.resolver_match.url_name = "index"
self.assertIn(f'<meta name="djangocms_version" content="{cms.__version__}">',
render_update_notification(dict(request=request)))

0 comments on commit 51f0e7a

Please sign in to comment.