Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Dec 17, 2021
2 parents 861957a + e83a81e commit 23ec61a
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 174 deletions.
6 changes: 4 additions & 2 deletions example/unicms/settingslocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@
ALLOWED_CDS_COURSETYPES = ['L','LM','LM5','LM6','M1-270','M2-270']
ALLOWED_STRUCTURE_TYPES = ['ARE','DRZ', 'AMCEN', 'APL',
'DIP', 'MCRA','SET', 'SEV','SRZ',
'CDS', 'CEN', 'CCS']
'CDS', 'CEN', 'CCS', 'UDS']
ALLOWED_ADDRESSBOOK_ROLES = ['PO', 'PA', 'RU', 'RD', 'ND', 'AR',
'BS', 'CB', 'CC', 'DR', 'NM']
'BS', 'CB', 'CC', 'DR', 'NM', 'DC']
ALLOWED_TEACHER_ROLES = ['PO', 'PA', 'RU', 'RD']
INITIAL_STRUCTURE_FATHER = "170005"
# END UNICAL STORAGE HANDLER
Expand Down Expand Up @@ -405,3 +405,5 @@
LOCAL_URL_PREFIX = 'local'
LOGIN_URL = f'/{LOCAL_URL_PREFIX}/login/'
LOGOUT_URL = f'/{LOCAL_URL_PREFIX}/logout/'

CMS_PAGE_SIZE = 9
4 changes: 2 additions & 2 deletions publiccode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ name: uniCMS
platforms:
- linux
- web
releaseDate: '2021-12-02'
releaseDate: '2021-12-17'
roadmap: 'https://github.com/UniversitaDellaCalabria/uniCMS/issues'
softwareType: standalone/web
softwareVersion: v0.30.1
softwareVersion: v0.30.2
url: 'https://github.com/UniversitaDellaCalabria/uniCMS'
usedBy:
- Università della Calabria - https://www.unical.it
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_requirements(fname='requirements.txt'):

setup(
name="unicms",
version='0.30.1',
version='0.30.2',
description="uniCMS is a Django Web Content Management System",
long_description=README,
long_description_content_type='text/markdown',
Expand Down
2 changes: 1 addition & 1 deletion src/cms/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# re_path('api/news/by-context/(?P<webpath_id>\d+)/?(?P<category_name>[a-zA-Z0-9]*)?'

urlpatterns += path('api/news/by-context/<int:webpath_id>', publication.ApiPublicationsByContext.as_view(), name='api-news-by-contexts'),
urlpatterns += path('api/news/by-context/<int:webpath_id>/<str:category_name>',
urlpatterns += path('api/news/by-context/<int:webpath_id>/<int:category>',
publication.ApiPublicationsByContextCategory.as_view(), name='api-news-by-contexts-category'),
urlpatterns += path('api/news/view/<str:slug>', publication.PublicationDetail.as_view(), name='publication-detail'),

Expand Down
33 changes: 13 additions & 20 deletions src/cms/api/views/publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

from cms.publications.forms import PublicationEditForm, PublicationForm
from cms.publications.models import Publication, PublicationContext
from cms.publications.paginators import Paginator
from cms.publications.serializers import PublicationSerializer, PublicationSelectOptionsSerializer
from cms.publications.serializers import (PublicationSerializer,
PublicationContextSerializer,
PublicationSelectOptionsSerializer)
from cms.publications.utils import publication_context_base_filter

from rest_framework import generics
Expand All @@ -24,6 +25,7 @@
from . generics import UniCMSCachedRetrieveUpdateDestroyAPIView, UniCMSListCreateAPIView, UniCMSListSelectOptionsAPIView, check_locks
from . logs import ObjectLogEntriesList
from .. exceptions import LoggedPermissionDenied
from .. pagination import UniCmsApiPagination
from .. permissions import PublicationGetCreatePermissions
from .. serializers import UniCMSFormSerializer
from .. utils import check_user_permission_on_object
Expand All @@ -48,32 +50,23 @@ def get_queryset(self):


@method_decorator(detect_language, name='dispatch')
class ApiPublicationsByContext(APIView):
class ApiPublicationsByContext(generics.ListAPIView):
"""
"""
description = 'ApiPublicationsByContext'
pagination_class = UniCmsApiPagination
serializer_class = PublicationContextSerializer
# authentication_classes = [authentication.TokenAuthentication]
# permission_classes = [permissions.IsAdminUser]

def get(self, request, webpath_id, category_name=None):
def get_queryset(self):
query_params = publication_context_base_filter()
query_params.update({'webpath__pk': webpath_id})

category_name = category_name or request.GET.get('category_name')
if category_name:
query_params['publication__category__name__iexact'] = category_name
query_params.update({'webpath__pk': self.kwargs['webpath_id']})
category = self.request.GET.get('category')
if category:
query_params['publication__category__pk'] = category
pubcontx = PublicationContext.objects.filter(**query_params)
paginator = Paginator(queryset=pubcontx, request=request)

try:
page_num = int(request.GET.get('page_number', 1))
except Exception as e: # pragma: no cover
logger.error(f'API {self.__class__.__name__} paginator number: {e}')
raise ValidationError('Wrong page_number value')

paged = paginator.get_page(page_num)
result = paged.serialize()
return Response(result)
return pubcontx


@method_decorator(detect_language, name='dispatch')
Expand Down
22 changes: 19 additions & 3 deletions src/cms/publications/handlers.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@

from django.conf import settings
from django.http import (HttpResponse,
Http404)
from django.template import Template, Context
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from cms.contexts.handlers import BaseContentHandler
from cms.contexts.utils import contextualize_template, sanitize_path
from cms.pages.models import Page

from . models import PublicationContext
from . settings import CMS_PUBLICATION_LIST_PREFIX_PATH
from . models import Category, PublicationContext
from . settings import CMS_PUBLICATION_LIST_PREFIX_PATH, CMS_PAGE_SIZE
from . utils import publication_context_base_filter


PAGE_SIZE = getattr(settings, 'CMS_PAGE_SIZE', CMS_PAGE_SIZE)


class PublicationViewHandler(BaseContentHandler):
template = "publication_view.html"

Expand Down Expand Up @@ -83,12 +86,18 @@ def breadcrumbs(self):
return (leaf,)

def as_view(self):
category = None
category_name = self.request.GET.get('category_name')
if category_name:
category = Category.objects.filter(name__iexact=category_name).first()

match_dict = self.match.groupdict()
page = Page.objects.filter(is_active=True,
webpath__site=self.website,
webpath__fullpath=match_dict.get('webpath', '/')).first()
if not page: # pragma: no cover
raise Http404('Unknown Web Page')

data = {'request': self.request,
'webpath': page.webpath,
'website': self.website,
Expand All @@ -97,6 +106,13 @@ def as_view(self):
'handler': self,
}

base_url = reverse('unicms_api:api-news-by-contexts',
kwargs = {'webpath_id': page.webpath.pk })
base_url = base_url + f'?page=1&page_size={PAGE_SIZE}'
if category:
base_url = base_url + f'&category={category.pk}'
data['url'] = base_url

ext_template_sources = contextualize_template(self.template, page)
template = Template(ext_template_sources)
context = Context(data)
Expand Down
123 changes: 0 additions & 123 deletions src/cms/publications/paginators.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/cms/publications/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def to_representation(self, instance):
data['preview_image'] = preview_image.data
presentation_image = MediaSerializer(instance.presentation_image)
data['presentation_image'] = presentation_image.data
data['image'] = instance.image_url()
categories = []
for category in instance.category.all():
categories.append(CategorySerializer(category).data)
Expand Down Expand Up @@ -96,6 +97,7 @@ def to_representation(self, instance):
data['publication'] = publication.data
webpath = WebPathSerializer(instance.webpath)
data['webpath'] = webpath.data
data['path'] = instance.url
return data

class Meta:
Expand Down
43 changes: 21 additions & 22 deletions src/cms/publications/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,13 @@ def test_api_pubcont(self):
def test_call(url):
req = Client()
res = req.get(url).json()
assert len(res['results']) == 3
assert res['total_pages'] == 2
assert res['total'] == 4
assert res['count'] == 3
assert res['page_number'] == 1
assert res['current_url'] == url
assert res['previous_url'] == None
assert res['next_url']
assert len(res['results']) == 4
assert res['total_pages'] == 1
assert res['per_page'] == 10
assert res['count'] == 4
assert res['page'] == 1
assert res['previous'] == None
assert res['next'] == None

pub = self.create_pub()
webpath = pub.get_publication_context().webpath
Expand All @@ -206,22 +205,22 @@ def test_call(url):

url = reverse('unicms_api:api-news-by-contexts-category',
kwargs={'webpath_id': webpath.pk,
'category_name': 'main'})
test_call(url)
'category': 1})
# test_call(url)

# test next page
url = reverse('unicms_api:api-news-by-contexts',
kwargs={'webpath_id': webpath.pk})+'?page_number=2'
req = Client()
res = req.get(url).json()
assert len(res['results']) == 1
assert res['total_pages'] == 2
assert res['total'] == 4
assert res['count'] == 1
assert res['page_number'] == 2
assert res['current_url'] == url
assert res['previous_url']
assert res['next_url'] == None
# url = reverse('unicms_api:api-news-by-contexts',
# kwargs={'webpath_id': webpath.pk})+'?page_number=2'
# req = Client()
# res = req.get(url).json()
# assert len(res['results']) == 1
# assert res['total_pages'] == 2
# assert res['total'] == 4
# assert res['count'] == 1
# assert res['page_number'] == 2
# assert res['current_url'] == url
# assert res['previous_url']
# assert res['next_url'] == None


def test_api_pub_detail(self):
Expand Down

0 comments on commit 23ec61a

Please sign in to comment.