public-health-ch/feedler/admin.py

101 lines
3.8 KiB
Python
Raw Permalink Normal View History

2017-10-14 21:31:21 +00:00
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.conf.urls import url
2019-06-14 20:02:15 +00:00
from django.urls import reverse
2017-10-14 21:31:21 +00:00
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _
2017-10-14 21:57:39 +00:00
from django.shortcuts import redirect
2017-10-14 21:31:21 +00:00
2020-12-11 10:32:59 +00:00
from wagtail.admin import messages
2017-10-14 21:31:21 +00:00
from wagtail.contrib.modeladmin.helpers import AdminURLHelper, ButtonHelper
from wagtail.contrib.modeladmin.options import ModelAdmin
from wagtail.contrib.modeladmin.views import IndexView
2020-12-11 10:32:59 +00:00
from wagtail.core.models import Site
2017-10-14 21:31:21 +00:00
from feedler.models import Entry
2017-10-14 21:57:39 +00:00
from feedler.refresh import refresh_streams
from feedler.models.admin import FeedlySettings
2017-10-14 21:31:21 +00:00
2017-10-14 21:57:39 +00:00
class RefreshButtonHelper(ButtonHelper):
2017-10-14 21:31:21 +00:00
"""
2017-10-14 21:57:39 +00:00
This helper constructs a refresh button
2017-10-14 21:31:21 +00:00
"""
2017-10-14 21:57:39 +00:00
button_classnames = ['icon', 'icon-download']
def refresh_button(self, classnames_add=None, classnames_exclude=None):
if classnames_add is None: classnames_add = []
if classnames_exclude is None: classnames_exclude = []
classnames = self.button_classnames + classnames_add
2017-10-14 21:31:21 +00:00
cn = self.finalise_classname(classnames, classnames_exclude)
2017-10-14 21:57:39 +00:00
text = _('Sync {}'.format(self.verbose_name_plural.title()))
2017-10-14 21:31:21 +00:00
return {
2017-10-14 21:57:39 +00:00
'url': self.url_helper.get_action_url('refresh', query_params=self.request.GET),
'label': text, 'classname': cn, 'title': text,
2017-10-14 21:31:21 +00:00
}
2017-10-14 21:57:39 +00:00
class RefreshAdminURLHelper(AdminURLHelper):
2017-10-14 21:31:21 +00:00
"""
2017-10-14 21:57:39 +00:00
This helper constructs the different urls, to overwrite the default behaviour
and append the filters to the action.
2017-10-14 21:31:21 +00:00
"""
2017-10-14 21:57:39 +00:00
non_object_specific_actions = ('create', 'choose_parent', 'index', 'refresh')
2017-10-14 21:31:21 +00:00
def get_action_url(self, action, *args, **kwargs):
query_params = kwargs.pop('query_params', None)
url_name = self.get_action_url_name(action)
if action in self.non_object_specific_actions:
url = reverse(url_name)
else:
url = reverse(url_name, args=args, kwargs=kwargs)
if query_params:
url += '?{params}'.format(params=query_params.urlencode())
return url
def get_action_url_pattern(self, action):
if action in self.non_object_specific_actions:
return self._get_action_url_pattern(action)
return self._get_object_specific_action_url_pattern(action)
2017-10-14 21:57:39 +00:00
class RefreshView(IndexView):
2017-10-14 21:31:21 +00:00
"""
2017-10-14 21:57:39 +00:00
A Class Based View which will handle the button click
2017-10-14 21:31:21 +00:00
"""
2017-10-14 21:57:39 +00:00
# def export_csv(self):
# data = self.queryset.all()
# response = ...
# return response
2017-10-14 21:31:21 +00:00
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
super().dispatch(request, *args, **kwargs)
2020-12-11 10:32:59 +00:00
site = Site.find_for_request(request)
if not refresh_streams(FeedlySettings.for_site(site)):
2017-10-14 21:57:39 +00:00
messages.error(
2017-10-23 08:05:38 +00:00
request, _('Sorry, could not refresh streams. Please try again in a few minutes, then contact support if the issue persists.'))
2017-10-14 21:57:39 +00:00
return redirect('/admin/feedler/entry/')
2017-10-14 21:31:21 +00:00
2017-10-14 21:57:39 +00:00
class EntryModelAdminMixin(object):
2017-10-14 21:31:21 +00:00
"""
A mixin to add to your model admin which hooks the different helpers, the view
and register the new urls.
"""
2017-10-14 21:57:39 +00:00
button_helper_class = RefreshButtonHelper
url_helper_class = RefreshAdminURLHelper
view_class = RefreshView
2017-10-14 21:31:21 +00:00
def get_admin_urls_for_registration(self):
urls = super().get_admin_urls_for_registration()
urls += (
url(
2017-10-14 21:57:39 +00:00
self.url_helper.get_action_url_pattern('refresh'),
self.refresh_view,
name=self.url_helper.get_action_url_name('refresh')
2017-10-14 21:31:21 +00:00
),
)
return urls
2017-10-14 21:57:39 +00:00
def refresh_view(self, request):
2017-10-14 21:31:21 +00:00
kwargs = {'model_admin': self}
2017-10-14 21:57:39 +00:00
view_class = self.view_class
2017-10-14 21:31:21 +00:00
return view_class.as_view(**kwargs)(request)