Skip to content

Commit

Permalink
fix caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sgmdlt committed Dec 25, 2024
1 parent 13d7f0a commit 8ec60ba
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 62 deletions.
82 changes: 50 additions & 32 deletions contributors/views/home.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,84 @@
from django.db.models import Prefetch
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.generic.base import TemplateView

from contributors.models import Contribution, Contributor

LATEST_COUNT = 10


@method_decorator(cache_page(60 * 15), name='dispatch') # кэширование на 15 минут
class HomeView(TemplateView):
"""Home page view."""

template_name = 'home.html'
template_name = "home.html"

def get_context_data(self, **kwargs):
"""Add context."""
context = super().get_context_data(**kwargs)

contributors_for_month = (
Contributor.objects.visible_with_monthly_stats()
.prefetch_related(
Contributor.objects.visible_with_monthly_stats().prefetch_related(
Prefetch(
'contribution_set',
queryset=Contribution.objects.select_related('repository'),
to_attr='prefetched_contributions'
"contribution_set",
queryset=Contribution.objects.select_related("repository"),
to_attr="prefetched_contributions",
)
)
)
contributors_for_week = (
Contributor.objects.visible_with_weekly_stats()
.prefetch_related(
Contributor.objects.visible_with_weekly_stats().prefetch_related(
Prefetch(
'contribution_set',
queryset=Contribution.objects.select_related('repository'),
to_attr='prefetched_contributions'
"contribution_set",
queryset=Contribution.objects.select_related("repository"),
to_attr="prefetched_contributions",
)
)
)

latest_contributions = (
Contribution.objects.select_related('contributor', 'repository')
.order_by('-created_at')[:LATEST_COUNT * 2]
)
latest_contributions = Contribution.objects.select_related(
"contributor", "repository"
).order_by("-created_at")[: LATEST_COUNT * 2]

context.update({
'top10_committers_of_month': self.get_top10(contributors_for_month, 'commits'),
'top10_requesters_of_month': self.get_top10(contributors_for_month, 'pull_requests'),
'top10_reporters_of_month': self.get_top10(contributors_for_month, 'issues'),
'top10_commentators_of_month': self.get_top10(contributors_for_month, 'comments'),
'top10_committers_of_week': self.get_top10(contributors_for_week, 'commits'),
'top10_requesters_of_week': self.get_top10(contributors_for_week, 'pull_requests'),
'top10_reporters_of_week': self.get_top10(contributors_for_week, 'issues'),
'top10_commentators_of_week': self.get_top10(contributors_for_week, 'comments'),
'contributions_for_year': Contribution.objects.for_year(),
'latest_time_issues': [c for c in latest_contributions if c.type == 'iss'][:LATEST_COUNT],
'latest_time_pr': [c for c in latest_contributions if c.type == 'pr'][:LATEST_COUNT],
})
context.update(
{
"top10_committers_of_month": self.get_top10(
contributors_for_month, "commits"
),
"top10_requesters_of_month": self.get_top10(
contributors_for_month, "pull_requests"
),
"top10_reporters_of_month": self.get_top10(
contributors_for_month, "issues"
),
"top10_commentators_of_month": self.get_top10(
contributors_for_month, "comments"
),
"top10_committers_of_week": self.get_top10(
contributors_for_week, "commits"
),
"top10_requesters_of_week": self.get_top10(
contributors_for_week, "pull_requests"
),
"top10_reporters_of_week": self.get_top10(
contributors_for_week, "issues"
),
"top10_commentators_of_week": self.get_top10(
contributors_for_week, "comments"
),
"contributions_for_year": Contribution.objects.for_year(),
"latest_time_issues": [
c for c in latest_contributions if c.type == "iss"
][:LATEST_COUNT],
"latest_time_pr": [c for c in latest_contributions if c.type == "pr"][
:LATEST_COUNT
],
}
)

return context

@staticmethod
def get_top10(dataset, contrib_type):
"""Return top 10 contributors of the type from the dataset."""
return sorted(dataset, key=lambda x: getattr(x, contrib_type), reverse=True)[:LATEST_COUNT]
return sorted(dataset, key=lambda x: getattr(x, contrib_type), reverse=True)[
:LATEST_COUNT
]
24 changes: 17 additions & 7 deletions contributors/views/organizations_views/organizations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db.models import Count
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views import generic
from django.views.decorators.cache import cache_page

from contributors.models import Organization
from contributors.views.mixins import TableSortSearchAndPaginationMixin
Expand All @@ -9,13 +11,21 @@
class ListView(TableSortSearchAndPaginationMixin, generic.ListView):
"""A list of organizations."""

queryset = Organization.objects.filter(
repository__is_visible=True,
).distinct().annotate(repository_count=Count('repository'))
template_name = 'contributors_sections/organizations/organizations_list.html' # noqa: E501
@method_decorator(cache_page(60 * 15)) # кэширование на 15 минут
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)

queryset = (
Organization.objects.filter(
repository__is_visible=True,
)
.distinct()
.annotate(repository_count=Count("repository"))
)
template_name = "contributors_sections/organizations/organizations_list.html" # noqa: E501
sortable_fields = (
'name',
('repository_count', _("Repositories")),
"name",
("repository_count", _("Repositories")),
)
searchable_fields = ('name',)
searchable_fields = ("name",)
ordering = sortable_fields[0]
52 changes: 29 additions & 23 deletions contributors/views/repositories_views/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,56 @@ def dispatch(self, *args, **kwargs):

def get_queryset(self):
queryset = (
Repository.objects.select_related('organization', 'project').filter(
Repository.objects.select_related("organization", "project")
.filter(
is_visible=True,
).distinct().annotate(
)
.distinct()
.annotate(
pull_requests=Count(
'contribution',
filter=Q(contribution__type='pr') & self.for_visible_contributor,
"contribution",
filter=Q(contribution__type="pr") & self.for_visible_contributor,
),
issues=Count(
'contribution',
filter=Q(contribution__type='iss') & self.for_visible_contributor,
"contribution",
filter=Q(contribution__type="iss") & self.for_visible_contributor,
),
contributors_count=Count(
'contribution__contributor',
"contribution__contributor",
filter=self.for_visible_contributor,
distinct=True,
),
).prefetch_related(
Prefetch('labels', queryset=Label.objects.only('name'))
)
.prefetch_related(Prefetch("labels", queryset=Label.objects.only("name")))
)
return queryset

template_name = 'contributors_sections/repositories/repositories_list.html'
template_name = "contributors_sections/repositories/repositories_list.html"
sortable_fields = (
'name',
'organization',
'project',
'pull_requests',
'issues',
('contributors_count', _("Contributors")),
"name",
"organization",
"project",
"pull_requests",
"issues",
("contributors_count", _("Contributors")),
)
searchable_fields = ('name', 'organization__name', 'project__name')
searchable_fields = ("name", "organization__name", "project__name")
ordering = sortable_fields[0]

def get_context_data(self, **kwargs):
"""Add context."""
context = super().get_context_data(**kwargs)

all_labels = Label.objects.only('name')
labels = Label.objects.filter(
repository__in=self.get_queryset(),
).distinct().only('name')
all_labels = Label.objects.only("name")
labels = (
Label.objects.filter(
repository__in=self.get_queryset(),
)
.distinct()
.only("name")
)

context['all_labels'] = all_labels
context['labels'] = labels
context["all_labels"] = all_labels
context["labels"] = labels

return context

0 comments on commit 8ec60ba

Please sign in to comment.