diff --git a/django_wtf/core/search_view.py b/django_wtf/core/search_view.py index e887fa0..52695ff 100644 --- a/django_wtf/core/search_view.py +++ b/django_wtf/core/search_view.py @@ -2,7 +2,7 @@ from watson.views import SearchView as OriginalSearchView from django_wtf.core.models import Repository -from django_wtf.core.models.category_model import Category +from django_wtf.core.views.index_view import categories_ordered_by_total_repositories class SearchView(OriginalSearchView): @@ -29,18 +29,7 @@ def get_queryset(self): repositories = watson.filter(repositories, search) return repositories - def categories_ordered_by_total_repositories(self): - categories = [] - for c in Category.objects.all(): - count_matching_repositories = Repository.objects.filter( - categories__in=[c] - ).count() - if count_matching_repositories: - setattr(c, "total_repositories", count_matching_repositories) - categories.append(c) - return sorted(categories, key=lambda c: c.total_repositories, reverse=True) - def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) - ctx["categories"] = self.categories_ordered_by_total_repositories() + ctx["categories"] = categories_ordered_by_total_repositories() return ctx diff --git a/django_wtf/core/views/index_view.py b/django_wtf/core/views/index_view.py index 11be1af..c15ac64 100644 --- a/django_wtf/core/views/index_view.py +++ b/django_wtf/core/views/index_view.py @@ -1,7 +1,6 @@ from django.templatetags.static import static from django.views.generic.base import TemplateView from meta.views import MetadataMixin -from watson import search as watson from django_wtf.core.models import Category, Repository, SocialNews from django_wtf.core.queries import ( @@ -11,6 +10,18 @@ ) +def categories_ordered_by_total_repositories(): + categories = [] + for c in Category.objects.all(): + count_matching_repositories = Repository.objects.filter( + categories__in=[c] + ).count() + if count_matching_repositories: + setattr(c, "total_repositories", count_matching_repositories) + categories.append(c) + return sorted(categories, key=lambda c: c.total_repositories, reverse=True) + + class IndexView(MetadataMixin, TemplateView): template_name = "core/index.html" title = "Django.WTF: The Django package index" @@ -36,7 +47,7 @@ class IndexView(MetadataMixin, TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context["categories"] = self.categories_ordered_by_total_repositories() + context["categories"] = categories_ordered_by_total_repositories() context["trending_apps"] = trending_repositories(days_since=14)[0:5] context["trending_developers"] = trending_profiles()[0:5] context["social_news"] = SocialNews.objects.filter( @@ -45,16 +56,5 @@ def get_context_data(self, **kwargs): context["top_apps"] = Repository.valid.order_by("-stars")[0:5] return context - def categories_ordered_by_total_repositories(self): - categories = [] - for c in Category.objects.all(): - count_matching_repositories = Repository.objects.filter( - categories__in=[c] - ).count() - if count_matching_repositories: - setattr(c, "total_repositories", count_matching_repositories) - categories.append(c) - return sorted(categories, key=lambda c: c.total_repositories, reverse=True) - def get_meta_image(self, context=None): return static("images/logo.png") diff --git a/django_wtf/templates/core/components/_navbar.html b/django_wtf/templates/core/components/_navbar.html index 58a361c..9411de8 100644 --- a/django_wtf/templates/core/components/_navbar.html +++ b/django_wtf/templates/core/components/_navbar.html @@ -37,45 +37,38 @@ Django.wtf - - - - {% include "core/search_table.html" %} -
+
{% include "core/search_table.html" %}
{% endblock content %} diff --git a/django_wtf/templates/core/search_table.html b/django_wtf/templates/core/search_table.html new file mode 100644 index 0000000..864db1e --- /dev/null +++ b/django_wtf/templates/core/search_table.html @@ -0,0 +1,25 @@ +{% load admin_urls static %} + +{% if object_list %} + {% for object in object_list %} +
+
+
+ +

+ {{ object.full_name }} + ⭐ {{ object.stars }} +

+
+

+ {% for topic in object.topics|slice:"0:5" %} + {{ topic|truncatechars:30 }} + {% endfor %} +

+

{{ object.description }}

+
+
+
+ {% endfor %} +{% endif %}