Skip to content

Commit

Permalink
rationalizes where templates for read-only and editable elements are …
Browse files Browse the repository at this point in the history
…found.
  • Loading branch information
smirolo committed Nov 25, 2024
1 parent 2e2ee70 commit 40b2cb0
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 50 deletions.
9 changes: 6 additions & 3 deletions pages/api/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
3 changes: 2 additions & 1 deletion pages/api/reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions pages/templates/pages/editables/element.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "base.html" %}

{% block content %}
<section id="app">
<editables-detail inline-template>
<div :id="item.slug">
<h1 class="title editable" data-key="title">[[item.title]]</h1>
<div class="editable edit-formatted" data-key="text" v-html="item.html_formatted" v-if="item.text">
</div>
<div class="editable edit-formatted" data-key="text" v-if="!item.text">
enter text here...
</div>
<hr />
{% include "pages/_follow_vote.html" %}
{% include "pages/_comments.html" %}
</div>
</editables-detail>
</section>
{% endblock %}
File renamed without changes.
73 changes: 32 additions & 41 deletions pages/views/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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),
Expand All @@ -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': {
Expand Down Expand Up @@ -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):
Expand All @@ -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,
Expand All @@ -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
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions testsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def load_config(confpath):
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'debug_toolbar',
'pages',
'testsite',
)
Expand Down Expand Up @@ -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',
Expand All @@ -157,8 +159,6 @@ def load_config(confpath):
'django.middleware.clickjacking.XFrameOptionsMiddleware'
)

MIDDLEWARE_CLASSES = MIDDLEWARE

ROOT_URLCONF = 'testsite.urls'
WSGI_APPLICATION = 'testsite.wsgi.application'

Expand Down
11 changes: 10 additions & 1 deletion testsite/urls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += [
Expand Down

0 comments on commit 40b2cb0

Please sign in to comment.