# Path: feincms3/applications.py
# def page_for_app_request(request, *, queryset=None):
#     """
#     Returns the current page if we're inside an app. Should only be called
#     while processing app views. Will pass along exceptions caused by
#     non-existing or duplicated apps (this should never happen inside an app
#     because :func:`~feincms3.applications.apps_urlconf` wouldn't have added the app
#     in the first place if a matching page wouldn't exist, but still.)
# 
#     Example:
# 
#     .. code-block:: python
# 
#         def article_detail(request, slug):
#             page = page_for_app_request(request)
#             page.activate_language(request)
#             instance = get_object_or_404(Article, slug=slug)
#             return render(
#                 request,
#                 "articles/article_detail.html",
#                 {"article": article, "page": page},
#             )
# 
#     It is possible to override the queryset used to fetch a page instance. The
#     default implementation simply uses the first concrete subclass of
#     :class:`~feincms3.applications.PageTypeMixin`.
#     """
# 
#     if queryset is None:
#         queryset = _APPS_MODEL._default_manager.active().with_tree_fields()
#     # Unguarded - if this fails, we shouldn't even be here.
#     return queryset.get(
#         language_code=request.resolver_match.namespaces[0][
#             len(_APPS_MODEL.LANGUAGE_CODES_NAMESPACE) + 1 :
#         ],
#         app_namespace=request.resolver_match.namespaces[1],
#     )
# 
# Path: feincms3/shortcuts.py
# def render_detail(request, object, context=None, *, template_name_suffix="_detail"):
#     """
#     Render a single item
# 
#     Usage example::
# 
#         def article_detail(request, slug):
#             article = get_object_or_404(Article.objects.published(), slug=slug)
#             return render_detail(
#                 request,
#                 article,
#             )
# 
#     An additional context dictionary is also supported, and specifying the
#     template name suffix too.
# 
#     The ``Article`` instance in the example above is passed as ``object``
#     AND ``article`` (lowercased model name) into the template.
#     """
# 
#     context = context or {}
#     context.update({"object": object, object._meta.model_name: object})
#     return TemplateResponse(
#         request, template_name(object, template_name_suffix), context
#     )
# 
# def render_list(
#     request,
#     queryset,
#     context=None,
#     *,
#     model=None,
#     paginate_by=None,
#     template_name_suffix="_list"
# ):
#     """
#     Render a list of items
# 
#     Usage example::
# 
#         def article_list(request, ...):
#             queryset = Article.objects.published()
#             return render_list(
#                 request,
#                 queryset,
#                 paginate_by=10,
#             )
# 
#     You can also pass an additional context dictionary and/or specify the
#     template name suffix. The query parameter ``page`` is hardcoded for
#     specifying the current page if using pagination.
# 
#     The queryset (or the page if using pagination) are passed into the template
#     as ``object_list`` AND ``<model_name>_list``, i.e. ``article_list`` in the
#     example above.
#     """
# 
#     context = context or {}
#     if paginate_by:
#         object_list = Paginator(queryset, paginate_by).get_page(request.GET.get("page"))
#     else:
#         object_list = queryset
# 
#     model = model or queryset.model
#     context.update(
#         {"object_list": object_list, "%s_list" % model._meta.model_name: object_list}
#     )
#     return TemplateResponse(
#         request, template_name(model, template_name_suffix), context
#     )
# 
# Path: tests/testapp/models.py
# class Article(models.Model):
#     title = models.CharField(_("title"), max_length=200)
#     category = models.CharField(
#         _("category"),
#         max_length=100,
#         choices=(("publications", "publications"), ("blog", "blog")),
#     )
# 
#     class Meta:
#         ordering = ["-pk"]
# 
#     def __str__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         return reverse_app(
#             (self.category, "articles"), "article-detail", kwargs={"pk": self.pk}
#         )


from django.shortcuts import get_object_or_404
from django.urls import path
from feincms3.applications import page_for_app_request
from feincms3.shortcuts import render_detail, render_list
from .models import Article




def article_list_all(request):
    page = page_for_app_request(request)
    page.activate_language(request)
    return render_list(
        request, Article.objects.filter(category=page.page_type), {"page": page}
    )


def article_list(request):
    page = page_for_app_request(request)
    page.activate_language(request)
    return render_list(
        request,
        Article.objects.filter(category=page.page_type),
        {"page": page},
        paginate_by=5,
    )


def article_detail(request, pk):
    page = page_for_app_request(request)
    page.activate_language(request)

# Path: feincms3/applications.py
# def page_for_app_request(request, *, queryset=None):
#     """
#     Returns the current page if we're inside an app. Should only be called
#     while processing app views. Will pass along exceptions caused by
#     non-existing or duplicated apps (this should never happen inside an app
#     because :func:`~feincms3.applications.apps_urlconf` wouldn't have added the app
#     in the first place if a matching page wouldn't exist, but still.)
# 
#     Example:
# 
#     .. code-block:: python
# 
#         def article_detail(request, slug):
#             page = page_for_app_request(request)
#             page.activate_language(request)
#             instance = get_object_or_404(Article, slug=slug)
#             return render(
#                 request,
#                 "articles/article_detail.html",
#                 {"article": article, "page": page},
#             )
# 
#     It is possible to override the queryset used to fetch a page instance. The
#     default implementation simply uses the first concrete subclass of
#     :class:`~feincms3.applications.PageTypeMixin`.
#     """
# 
#     if queryset is None:
#         queryset = _APPS_MODEL._default_manager.active().with_tree_fields()
#     # Unguarded - if this fails, we shouldn't even be here.
#     return queryset.get(
#         language_code=request.resolver_match.namespaces[0][
#             len(_APPS_MODEL.LANGUAGE_CODES_NAMESPACE) + 1 :
#         ],
#         app_namespace=request.resolver_match.namespaces[1],
#     )
# 
# Path: feincms3/shortcuts.py
# def render_detail(request, object, context=None, *, template_name_suffix="_detail"):
#     """
#     Render a single item
# 
#     Usage example::
# 
#         def article_detail(request, slug):
#             article = get_object_or_404(Article.objects.published(), slug=slug)
#             return render_detail(
#                 request,
#                 article,
#             )
# 
#     An additional context dictionary is also supported, and specifying the
#     template name suffix too.
# 
#     The ``Article`` instance in the example above is passed as ``object``
#     AND ``article`` (lowercased model name) into the template.
#     """
# 
#     context = context or {}
#     context.update({"object": object, object._meta.model_name: object})
#     return TemplateResponse(
#         request, template_name(object, template_name_suffix), context
#     )
# 
# def render_list(
#     request,
#     queryset,
#     context=None,
#     *,
#     model=None,
#     paginate_by=None,
#     template_name_suffix="_list"
# ):
#     """
#     Render a list of items
# 
#     Usage example::
# 
#         def article_list(request, ...):
#             queryset = Article.objects.published()
#             return render_list(
#                 request,
#                 queryset,
#                 paginate_by=10,
#             )
# 
#     You can also pass an additional context dictionary and/or specify the
#     template name suffix. The query parameter ``page`` is hardcoded for
#     specifying the current page if using pagination.
# 
#     The queryset (or the page if using pagination) are passed into the template
#     as ``object_list`` AND ``<model_name>_list``, i.e. ``article_list`` in the
#     example above.
#     """
# 
#     context = context or {}
#     if paginate_by:
#         object_list = Paginator(queryset, paginate_by).get_page(request.GET.get("page"))
#     else:
#         object_list = queryset
# 
#     model = model or queryset.model
#     context.update(
#         {"object_list": object_list, "%s_list" % model._meta.model_name: object_list}
#     )
#     return TemplateResponse(
#         request, template_name(model, template_name_suffix), context
#     )
# 
# Path: tests/testapp/models.py
# class Article(models.Model):
#     title = models.CharField(_("title"), max_length=200)
#     category = models.CharField(
#         _("category"),
#         max_length=100,
#         choices=(("publications", "publications"), ("blog", "blog")),
#     )
# 
#     class Meta:
#         ordering = ["-pk"]
# 
#     def __str__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         return reverse_app(
#             (self.category, "articles"), "article-detail", kwargs={"pk": self.pk}
#         )


# Path: tests/testapp/articles_urls.py
from django.shortcuts import get_object_or_404
from django.urls import path
from feincms3.applications import page_for_app_request
from feincms3.shortcuts import render_detail, render_list
from .models import Article





def article_list_all(request):
    page = page_for_app_request(request)
    page.activate_language(request)
    return render_list(
        request, Article.objects.filter(category=page.page_type), {"page": page}
    )


def article_list(request):
    page = page_for_app_request(request)
    page.activate_language(request)
    return render_list(
        request,
        Article.objects.filter(category=page.page_type),
        {"page": page},
        paginate_by=5,
    )


def article_detail(request, pk):
    page = page_for_app_request(request)
    page.activate_language(request)

    return render_detail(request, get_object_or_404(Article, pk=pk), {"page": page})