From 40b2cb079293884cca5d24e1d69d1fff42a094af Mon Sep 17 00:00:00 2001 From: Sebastien Mirolo Date: Mon, 25 Nov 2024 13:58:15 -0800 Subject: [PATCH] rationalizes where templates for read-only and editable elements are found. --- pages/api/elements.py | 9 ++- pages/api/reactions.py | 3 +- pages/models.py | 2 +- pages/templates/pages/editables/element.html | 19 +++++ .../{editables.html => editables/index.html} | 0 pages/views/elements.py | 73 ++++++++----------- pyproject.toml | 3 +- testsite/settings.py | 4 +- testsite/urls/__init__.py | 11 ++- 9 files changed, 74 insertions(+), 50 deletions(-) create mode 100644 pages/templates/pages/editables/element.html rename pages/templates/pages/{editables.html => editables/index.html} (100%) diff --git a/pages/api/elements.py b/pages/api/elements.py index b7c041e..0b3bce8 100644 --- a/pages/api/elements.py +++ b/pages/api/elements.py @@ -577,9 +577,12 @@ def perform_create(self, serializer): orig_element=parent, dest_element=element, rank=rank) def get_success_headers(self, data): - path = data.get('path').strip(self.URL_PATH_SEP) + self.URL_PATH_SEP - return {'Location': reverse('pages_editables_element', - args=(self.element.account, path))} + path = data.get('path') + if path: + return {'Location': reverse('pages_editables_element', args=( + self.element.account, + path.strip(self.URL_PATH_SEP) + self.URL_PATH_SEP))} + return {} def update(self, request, *args, **kwargs): with transaction.atomic(): diff --git a/pages/api/reactions.py b/pages/api/reactions.py index 52580da..debecf5 100644 --- a/pages/api/reactions.py +++ b/pages/api/reactions.py @@ -293,7 +293,8 @@ class NewsFeedListAPIView(UserMixin, generics.ListAPIView): "next": null, "previous": null, "results": [{ - "path": "/metal/boxes-and-enclosures/production/energy-efficiency/process-heating/combustion/adjust-air-fuel-ratio", + "path": "/metal/boxes-and-enclosures/production/\ +energy-efficiency/process-heating/combustion/adjust-air-fuel-ratio", "text_updated_at": "2024-01-01T00:00:00Z", "last_read_at": "2023-12-01T00:00:00Z", "nb_comments_since_last_read": 5, diff --git a/pages/models.py b/pages/models.py index b536684..85a2d87 100644 --- a/pages/models.py +++ b/pages/models.py @@ -198,7 +198,7 @@ class PageElement(models.Model): help_text=_("Reading time of the material (in hh:mm:ss)")) lang = models.CharField(default=settings.LANGUAGE_CODE, max_length=8, help_text=_("Language the material is written in")) - text_updated_at = models.DateTimeField(blank=True, null=True, + text_updated_at = models.DateTimeField(auto_now_add=True, help_text=_("Last updated at date for the text field")) extra = get_extra_field_class()(null=True, blank=True) relationships = models.ManyToManyField("self", diff --git a/pages/templates/pages/editables/element.html b/pages/templates/pages/editables/element.html new file mode 100644 index 0000000..f11690c --- /dev/null +++ b/pages/templates/pages/editables/element.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block content %} +
+ +
+

[[item.title]]

+
+
+
+ enter text here... +
+
+ {% include "pages/_follow_vote.html" %} + {% include "pages/_comments.html" %} +
+
+
+{% endblock %} diff --git a/pages/templates/pages/editables.html b/pages/templates/pages/editables/index.html similarity index 100% rename from pages/templates/pages/editables.html rename to pages/templates/pages/editables/index.html diff --git a/pages/views/elements.py b/pages/views/elements.py index fabd209..5899f9e 100644 --- a/pages/views/elements.py +++ b/pages/views/elements.py @@ -21,11 +21,11 @@ # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import logging +import logging, os from django.http import Http404 +from django.utils._os import safe_join from django.views.generic import TemplateView -from deployutils.apps.django.mixins import AccessiblesMixin from .. import settings from ..compat import NoReverseMatch, reverse, six @@ -37,13 +37,13 @@ LOGGER = logging.getLogger(__name__) -class PageElementView(TrailMixin, AccessiblesMixin, TemplateView): +class PageElementView(TrailMixin, TemplateView): """ When {path} points to an internal node in the content DAG, an index page is created that contains the children (up to `pagebreak`) of that node that are both visible and searchable. """ - template_name = 'pages/index.html' + template_name = 'pages/element.html' account_url_kwarg = settings.ACCOUNT_URL_KWARG direct_text_load = False @@ -85,9 +85,10 @@ def get_template_names(self): for layout in get_extra(self.element, 'layouts', [])] if self.is_prefix: # It is not a leaf, let's return the list view - candidates += super(PageElementView, self).get_template_names() + candidates += [safe_join(os.path.dirname( + self.template_name), 'index.html')] else: - candidates += ['pages/element.html'] + candidates += super(PageElementView, self).get_template_names() return candidates def get_context_data(self, **kwargs): @@ -104,10 +105,6 @@ def get_context_data(self, **kwargs): if isinstance(path, six.string_types): path = path.strip(self.URL_PATH_SEP) if path: - if self.manages(self.element.account): - context.update({ - 'edit_perm': self.manages(self.element.account) - }) url_kwargs = {'path': path} update_context_urls(context, { 'api_content': reverse('api_content', kwargs=url_kwargs), @@ -121,33 +118,9 @@ def get_context_data(self, **kwargs): }) else: url_kwargs = {'path': self.element.slug} - if self.manages(self.element.account): - context.update({ - 'edit_perm': self.manages(self.element.account) - }) - url_kwargs.update({ - self.account_url_kwarg: self.element.account}) - update_context_urls(context, { - 'api_content': reverse('pages_api_edit_element', - kwargs=url_kwargs), - }) - try: - update_context_urls(context, { - 'edit': { - 'api_medias': reverse( - 'uploaded_media_elements', - args=(self.element.account, self.element)), - }}) - except NoReverseMatch: - # There is no API end-point to upload POD assets (images, - # etc.) - pass - - else: - update_context_urls(context, { - 'api_content': reverse('api_content', - kwargs=url_kwargs), - }) + update_context_urls(context, { + 'api_content': reverse('api_content', kwargs=url_kwargs), + }) if self.direct_text_load: context.update({ 'element': { @@ -179,7 +152,7 @@ class PageElementEditableView(AccountMixin, PageElementView): page is created that contains the direct children of that belongs to the `account`. """ - template_name = 'pages/editables.html' + template_name = 'pages/editables/element.html' breadcrumb_url = 'pages_editables_element' def get_reverse_kwargs(self): @@ -204,9 +177,6 @@ def get_context_data(self, **kwargs): if isinstance(path, six.string_types): path = path.strip(self.URL_PATH_SEP) if path: - context.update({ - 'edit_perm': self.manages(self.element.account) - }) url_kwargs = { 'path': path, self.account_url_kwarg: self.element.account, @@ -220,4 +190,25 @@ def get_context_data(self, **kwargs): 'api_content': reverse('pages_api_editables_index', kwargs=url_kwargs), }) + else: + url_kwargs = { + 'path': self.element.slug, + self.account_url_kwarg: self.element.account, + } + update_context_urls(context, { + 'api_content': reverse('pages_api_edit_element', + kwargs=url_kwargs), + }) + try: + update_context_urls(context, { + 'edit': { + 'api_medias': reverse( + 'uploaded_media_elements', + args=(self.element.account, self.element)), + }}) + except NoReverseMatch: + # There is no API end-point to upload POD assets (images, + # etc.) + pass + return context diff --git a/pyproject.toml b/pyproject.toml index 2d3765d..f40b213 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,7 +51,8 @@ pages = [ 'static/js/*', 'static/vendor/css/*', 'static/vendor/js/*', - 'templates/pages/*.html' + 'templates/pages/*.html', + 'templates/pages/editables/*.html', ] [tool.setuptools.dynamic] diff --git a/testsite/settings.py b/testsite/settings.py index 40f8420..f899222 100644 --- a/testsite/settings.py +++ b/testsite/settings.py @@ -78,6 +78,7 @@ def load_config(confpath): 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', + 'debug_toolbar', 'pages', 'testsite', ) @@ -149,6 +150,7 @@ def load_config(confpath): MIDDLEWARE = ( 'whitenoise.middleware.WhiteNoiseMiddleware', + 'debug_toolbar.middleware.DebugToolbarMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', @@ -157,8 +159,6 @@ def load_config(confpath): 'django.middleware.clickjacking.XFrameOptionsMiddleware' ) -MIDDLEWARE_CLASSES = MIDDLEWARE - ROOT_URLCONF = 'testsite.urls' WSGI_APPLICATION = 'testsite.wsgi.application' diff --git a/testsite/urls/__init__.py b/testsite/urls/__init__.py index 177e1d5..2b9f88e 100644 --- a/testsite/urls/__init__.py +++ b/testsite/urls/__init__.py @@ -31,7 +31,16 @@ from ..views.app import IndexView -urlpatterns = staticfiles_urlpatterns() \ +if settings.DEBUG: + import debug_toolbar + urlpatterns = [ + path('__debug__/', include(debug_toolbar.urls)), + ] +else: + urlpatterns = [] + + +urlpatterns += staticfiles_urlpatterns() \ + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += [