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

Initial implementation for setting CMS version in code #104

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 16 additions & 28 deletions base/templates/admin/cms_upgrade_notification.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
{% load i18n wagtailadmin_tags %}
<div id="w-cms-upgrade" class="w-panel-cms-upgrade panel w-hidden" data-cms-current-version="{{ current_version }}">
<div class="help-block help-warning">
{% icon name='warning' %}
{% translate "CMS upgrade available. Your version:" %} <strong>{{ current_version }}</strong>
{% translate "Latest version:" %} <strong id="w-cms-latest-version"></strong>.
{% if version_upgrade_url %}
<a href="{{ version_upgrade_url }}">{% translate "Upgrade" %}</a>
{% endif %}

{% if cms_upgrade_hook_url and has_new_version %}
<div class="w-panel-cms-upgrade panel" data-cms-current-version="{{ current_version }}">
<div class="help-block help-warning">
{% icon name='warning' %}
{% translate "CMS upgrade available. Your version:" %} <strong>{{ current_version }}. </strong>
{% translate "Latest version:" %} <strong id="w-cms-latest-version">{{ latest_release.version }}</strong>.
{% if version_upgrade_url %}
<a href="{{ version_upgrade_url }}">{% translate "Upgrade" %}.</a>
{% endif %}
</div>
</div>
</div>

<script>
const latestReleaseUrl = "{{ latest_release_url }}";
$(document).ready(function () {
const $cmsUpgrade = $("#w-cms-upgrade")
const $cmsLatestVersion = $("#w-cms-latest-version")
const currentVersion = $cmsUpgrade.data("cmsCurrentVersion")
if (currentVersion && latestReleaseUrl) {
fetch(latestReleaseUrl).then(res => res.json()).then(releaseData => {
const latestVersion = releaseData.name && releaseData.name.replace(/^v+|v+$/gi, '')
if (latestVersion && currentVersion !== latestVersion) {
$cmsLatestVersion.text(latestVersion)
$cmsUpgrade.show()
}
}).catch(err => {
console.log(err)
})
}
})
</script>

{% endif %}




140 changes: 76 additions & 64 deletions base/templates/admin/cms_version.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "wagtailadmin/base.html" %}
{% load i18n %}
{% load l10n %}
{% load wagtailadmin_tags wagtailimages_tags static %}
{% load wagtailadmin_tags wagtailimages_tags static nmhs_cms_tags %}
{% block titletag %}
{% blocktranslate trimmed with title=page.get_admin_display_title %}
{{ title }}
Expand Down Expand Up @@ -35,77 +35,89 @@
{% include "wagtailadmin/shared/header.html" with title=header_str icon="cog" %}

<div class="nice-padding">

{% if current_version %}
<div class="v-item">{% translate "Current installed version" %}: <strong>{{ current_version }}</strong>
{% if error %}
<div class="help-block help-critical">
<svg class="icon icon-warning icon" aria-hidden="true">
<use href="#icon-warning"></use>
</svg>
{{ error_message }}
{% if error_traceback %}
<pre>{{ error_traceback }}</pre>
{% endif %}
</div>
{% endif %}
{% else %}
{% if current_version %}
<div class="v-item">
{% translate "Current installed version" %}: <strong>{% cms_version %}</strong>
</div>
{% endif %}

{% if latest_release %}
<div class="v-item">
{% translate "Latest available version" %}:
<strong>{{ latest_release.version }}</strong></div>
{% endif %}
{% if latest_release and has_new_version %}
<div class="v-item">
{% translate "Latest available version" %}:
<strong>{{ latest_release.version }}</strong></div>
{% endif %}

{% if current_version and latest_release %}
<div class="upgrade-section">
{% if current_version == latest_release.version %}
<div>
{% translate "All good. You are running on the latest version" %}!
</div>
{% else %}
{% if cms_upgrade_pending %}
<div class="help-block help-warning">
<svg class="icon icon-warning icon" aria-hidden="true">
<use href="#icon-warning"></use>
</svg>
{% translate "A CMS upgrade was initiated. Please wait for the upgrade to complete" %}
{% if current_version and latest_release %}
<div class="upgrade-section">
{% if not has_new_version %}
<div>
{% translate "Your CMS version is up to date." %}
</div>
{% else %}
{% if cms_upgrade_hook_url %}
<div class="upgrade-info">
<span style="margin-right: 4px">
{% translate "CMS upgrade available" %}!
</span>
<a href="https://github.com/wmo-raf/nmhs-cms/releases" target="_blank"
rel="noopener noreferrer" style="text-decoration: underline">
{% translate "Release notes" %}
</a>
</div>
<div style="margin-top: 20px">
<form method="POST"
enctype="multipart/form-data">
{% if form.non_field_errors %}
<div class="non-field_errors" style="margin-bottom: 20px">
{% include "wagtailadmin/shared/non_field_errors.html" with form=form %}
</div>
{% endif %}
<ul class="fields">
{% csrf_token %}
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
{% include "wagtailadmin/shared/field_as_li.html" %}
{% endif %}
{% endfor %}
<li>
<button class="button bicolor button--icon" type="submit">
<span class="icon-wrapper">
<svg class="icon icon-rotate icon" aria-hidden="true">
<use href="#icon-rotate"></use>
</svg>
</span>
{% translate "Upgrade now" %}
</button>
</li>
</ul>
</form>
{% if cms_upgrade_pending %}
<div class="help-block help-warning">
<svg class="icon icon-warning icon" aria-hidden="true">
<use href="#icon-warning"></use>
</svg>
{% translate "A CMS upgrade was initiated. Please wait for the upgrade to complete" %}
</div>
{% else %}
{% if cms_upgrade_hook_url %}
<div class="upgrade-info">
<span style="margin-right: 4px">
{% translate "CMS upgrade available" %}!
</span>
<a href="{{ latest_release.html_url }}" target="_blank"
rel="noopener noreferrer" style="text-decoration: underline">
{% translate "Release notes" %}
</a>
</div>
<div style="margin-top: 20px">
<form method="POST"
enctype="multipart/form-data">
{% if form.non_field_errors %}
<div class="non-field_errors" style="margin-bottom: 20px">
{% include "wagtailadmin/shared/non_field_errors.html" with form=form %}
</div>
{% endif %}
<ul class="fields">
{% csrf_token %}
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
{% include "wagtailadmin/shared/field_as_li.html" %}
{% endif %}
{% endfor %}
<li>
<button class="button bicolor button--icon" type="submit">
<span class="icon-wrapper">
<svg class="icon icon-rotate icon" aria-hidden="true">
<use href="#icon-rotate"></use>
</svg>
</span>
{% translate "Upgrade now" %}
</button>
</li>
</ul>
</form>
</div>
{% endif %}
{% endif %}
{% endif %}
{% endif %}
</div>
</div>
{% endif %}
{% endif %}
</div>
{% endblock %}
16 changes: 11 additions & 5 deletions base/templatetags/nmhs_cms_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@
import logging
import os
from datetime import datetime
from html.parser import HTMLParser

from django import template
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.template.defaultfilters import stringfilter, truncatewords_html
from django.utils.safestring import mark_safe
from wagtail.models import Site, Page
from html.parser import HTMLParser

from base.models import LanguageSettings
from base.utils import get_first_non_empty_p_string
from nmhs_cms import __version__

logger = logging.getLogger(__name__)
register = template.Library()


@register.simple_tag
def cms_version():
return __version__


@register.filter
def is_future(self):
return datetime.today().date() < self.date()
Expand Down Expand Up @@ -221,12 +227,10 @@ def __init__(self):
def handle_starttag(self, tag, attrs):
if tag.lower() == 'img':
return

attr_repl = [f"{attr[0]}=\"{attr[1]}\"" for attr in attrs]
self.result.append(f'<{tag} {" ".join(attr_repl)}>')



def handle_endtag(self, tag):
if tag.lower() == 'img':
return
Expand All @@ -235,9 +239,11 @@ def handle_endtag(self, tag):
def handle_data(self, data):
self.result.append(data)


def exclude_images(value):
parser = ExcludeImagesParser()
parser.feed(value)
return ''.join(parser.result)

register.filter('exclude_images', exclude_images)

register.filter('exclude_images', exclude_images)
56 changes: 36 additions & 20 deletions base/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.conf import settings
from django.core.cache import cache
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.utils.translation import gettext as _
from wagtail.admin import messages

from base.forms import CMSUpgradeForm
from base.utils import get_latest_cms_release, send_upgrade_command
from nmhs_cms.utils.version import check_version_greater_than_current, get_main_version


def handler500(request):
Expand All @@ -22,39 +24,52 @@ def cms_version_view(request):
cms_upgrade_pending = cache.get("cms_upgrade_pending")

template_name = "admin/cms_version.html"
current_version = getattr(settings, "CMS_VERSION", None)
cms_upgrade_hook_url = getattr(settings, "CMS_UPGRADE_HOOK_URL", None)

try:
latest_release = get_latest_cms_release()
except Exception:
latest_release = None
latest_version = latest_release.get("version")
except Exception as e:
return render(request, template_name,
context={
"error": True,
"error_message": _("Error fetching latest version. Please try again later."),
"error_traceback": str(e)
})

current_version = get_main_version()

context = {
"latest_release": latest_release,
"current_version": current_version,
"cms_upgrade_hook_url": cms_upgrade_hook_url
"cms_upgrade_hook_url": cms_upgrade_hook_url,
}

if latest_release and current_version:
context.update({
"latest_release": latest_release
})

initial = {
"latest_version": latest_release.get("version"),
"current_version": current_version
}
form = CMSUpgradeForm(initial=initial)

context.update({
"form": form,
})
try:
latest_release_greater_than_current = check_version_greater_than_current(latest_version)
except Exception as e:
return render(request, template_name,
context={
"error": True,
"error_message": _("Error in extracting latest version number from the release"),
"error_traceback": str(e)
})

context.update({
"has_new_version": latest_release_greater_than_current,
})

initial = {
"latest_version": latest_release.get("version"),
"latest_version": latest_version,
"current_version": current_version
}

form = CMSUpgradeForm(initial=initial)

context.update({
"form": form,
})

upgrade_form = CMSUpgradeForm(initial=initial)

if request.POST:
Expand All @@ -72,6 +87,7 @@ def cms_version_view(request):
send_upgrade_command(latest_version)
cache.set("cms_upgrade_pending", True)
messages.success(request, "CMS upgrade initiated successfully")
return redirect("wagtailadmin_home")
except Exception as e:
cache.set("cms_upgrade_pending", False)
messages.error(request, "Error initiating CMS upgrade. Please ensure the "
Expand Down
Loading