feat: dynamic main icon
This commit is contained in:
+47
-96
@@ -1,22 +1,8 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views.generic import DetailView, ListView, TemplateView
|
||||
|
||||
from .models import INSTALLABLE_PLATFORM_SPECS, MainProduct, SubProduct
|
||||
|
||||
|
||||
def _featured_version(qs):
|
||||
ordered = qs.order_by("order", "pk")
|
||||
cand = ordered.filter(is_featured_stable=True).first()
|
||||
return cand if cand else ordered.first()
|
||||
|
||||
|
||||
def _install_specs_from_versions(version_list):
|
||||
present = set()
|
||||
for ver in version_list:
|
||||
for field_name, *_ in INSTALLABLE_PLATFORM_SPECS:
|
||||
if (getattr(ver, field_name) or "").strip():
|
||||
present.add(field_name)
|
||||
return tuple(s for s in INSTALLABLE_PLATFORM_SPECS if s[0] in present)
|
||||
from .models import MainProduct, SubProduct
|
||||
from .release_context import build_archive_context, build_release_context
|
||||
|
||||
|
||||
class ProductOverviewView(ListView):
|
||||
@@ -37,6 +23,39 @@ class MainProductDetailView(DetailView):
|
||||
"sub_products"
|
||||
)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["articles"] = self.object.articles.prefetch_related(
|
||||
"sections", "citations"
|
||||
).all()
|
||||
release_context = build_release_context(
|
||||
self.object.distribution,
|
||||
self.object.versions.filter(is_active=True),
|
||||
)
|
||||
release_context["versions_archive_url"] = self.object.get_versions_archive_url()
|
||||
context.update(release_context)
|
||||
return context
|
||||
|
||||
|
||||
class MainProductOlderVersionsView(TemplateView):
|
||||
template_name = "products/versions_archive.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
main_product = get_object_or_404(
|
||||
MainProduct,
|
||||
slug=self.kwargs["main_slug"],
|
||||
is_active=True,
|
||||
)
|
||||
active_versions = main_product.versions.filter(is_active=True)
|
||||
context["main_product"] = main_product
|
||||
context["sub_product"] = None
|
||||
context["product_detail_url"] = main_product.get_absolute_url()
|
||||
context.update(
|
||||
build_archive_context(main_product.distribution, active_versions)
|
||||
)
|
||||
return context
|
||||
|
||||
|
||||
class SubProductDetailView(TemplateView):
|
||||
template_name = "products/sub_detail.html"
|
||||
@@ -56,63 +75,25 @@ class SubProductDetailView(TemplateView):
|
||||
)
|
||||
context["main_product"] = main_product
|
||||
context["sub_product"] = sub_product
|
||||
context["articles"] = sub_product.articles.prefetch_related("sections").all()
|
||||
context["articles"] = sub_product.articles.prefetch_related(
|
||||
"sections", "citations"
|
||||
).all()
|
||||
context["siblings"] = (
|
||||
SubProduct.objects.filter(main_product=main_product, is_active=True)
|
||||
.exclude(pk=sub_product.pk)
|
||||
.order_by("order", "name")
|
||||
)
|
||||
|
||||
active_versions = sub_product.versions.filter(is_active=True)
|
||||
|
||||
context["distribution_installable"] = (
|
||||
sub_product.distribution == SubProduct.DISTRIBUTION_INSTALLABLE
|
||||
release_context = build_release_context(
|
||||
sub_product.distribution, active_versions
|
||||
)
|
||||
context["distribution_package"] = (
|
||||
sub_product.distribution == SubProduct.DISTRIBUTION_PACKAGE
|
||||
)
|
||||
context["show_releases_section"] = False
|
||||
context["featured_version"] = None
|
||||
context["featured_install_cells"] = []
|
||||
context["show_older_versions_link"] = False
|
||||
context["package_versions"] = []
|
||||
|
||||
if sub_product.distribution == SubProduct.DISTRIBUTION_INSTALLABLE:
|
||||
featured = _featured_version(active_versions)
|
||||
if (
|
||||
featured
|
||||
and not featured.has_any_install_asset()
|
||||
and not (featured.package_resource_url or "").strip()
|
||||
):
|
||||
for cand in active_versions.exclude(pk=featured.pk).order_by("order", "pk"):
|
||||
if cand.has_any_install_asset() or (cand.package_resource_url or "").strip():
|
||||
featured = cand
|
||||
break
|
||||
context["featured_version"] = featured
|
||||
if featured and (
|
||||
featured.has_any_install_asset()
|
||||
or (featured.package_resource_url or "").strip()
|
||||
):
|
||||
if featured.has_any_install_asset():
|
||||
specs = _install_specs_from_versions([featured])
|
||||
context["featured_install_cells"] = featured.install_urls_for_specs(specs)
|
||||
context["show_releases_section"] = True
|
||||
older_list = list(
|
||||
active_versions.exclude(pk=featured.pk).order_by("order", "pk")
|
||||
if featured else active_versions.order_by("order", "pk")
|
||||
)
|
||||
context["show_older_versions_link"] = len(older_list) > 0
|
||||
|
||||
elif sub_product.distribution == SubProduct.DISTRIBUTION_PACKAGE:
|
||||
pkg_versions = list(active_versions.order_by("order", "pk"))
|
||||
context["package_versions"] = pkg_versions
|
||||
context["show_releases_section"] = len(pkg_versions) > 0
|
||||
|
||||
release_context["versions_archive_url"] = sub_product.get_versions_archive_url()
|
||||
context.update(release_context)
|
||||
return context
|
||||
|
||||
|
||||
class SubProductOlderVersionsView(TemplateView):
|
||||
template_name = "products/sub_versions.html"
|
||||
template_name = "products/versions_archive.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
@@ -128,40 +109,10 @@ class SubProductOlderVersionsView(TemplateView):
|
||||
is_active=True,
|
||||
)
|
||||
active_versions = sub_product.versions.filter(is_active=True)
|
||||
featured = _featured_version(active_versions)
|
||||
archive = list(
|
||||
active_versions.exclude(pk=featured.pk).order_by("order", "pk")
|
||||
if featured
|
||||
else active_versions.order_by("order", "pk")
|
||||
)
|
||||
context["main_product"] = main_product
|
||||
context["sub_product"] = sub_product
|
||||
context["featured_version"] = featured
|
||||
context["archive_versions"] = archive
|
||||
|
||||
if sub_product.distribution == SubProduct.DISTRIBUTION_INSTALLABLE:
|
||||
specs = _install_specs_from_versions(archive)
|
||||
context["archive_specs"] = specs
|
||||
archive_rows_installable = []
|
||||
for ver in archive:
|
||||
cells = []
|
||||
for field_name, label, icon in specs:
|
||||
u = getattr(ver, field_name, "") or ""
|
||||
u = u.strip()
|
||||
cells.append(
|
||||
{"field": field_name, "label": label, "icon": icon, "url": u}
|
||||
)
|
||||
archive_rows_installable.append(
|
||||
{"version_obj": ver, "cells": cells}
|
||||
)
|
||||
context["archive_rows_installable"] = archive_rows_installable
|
||||
context["archive_rows_package"] = False
|
||||
context["distribution_installable"] = True
|
||||
context["distribution_package"] = False
|
||||
else:
|
||||
context["archive_specs"] = ()
|
||||
context["archive_rows_installable"] = []
|
||||
context["archive_rows_package"] = True
|
||||
context["distribution_installable"] = False
|
||||
context["distribution_package"] = True
|
||||
context["product_detail_url"] = sub_product.get_absolute_url()
|
||||
context.update(
|
||||
build_archive_context(sub_product.distribution, active_versions)
|
||||
)
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user