From 1dd47359a8c348b3aaec00fbd1847d9d3416751d Mon Sep 17 00:00:00 2001 From: AleksandrKosmylev Date: Sat, 15 Jun 2024 17:51:46 +0300 Subject: [PATCH] edit_achievements_addition --- .../views/contributor_achievements.py | 109 +++++ ...tributor_achievement_percentage_field.html | 18 + .../tables/contributor_achievements_list.html | 380 ++++++++++++++++++ .../contributor_achievements_list.html | 19 + 4 files changed, 526 insertions(+) create mode 100644 contributors/views/contributor_achievements.py create mode 100644 templates/components/tables/contributor_achievement_percentage_field.html create mode 100644 templates/components/tables/contributor_achievements_list.html create mode 100644 templates/contributor/contributor_achievements_list.html diff --git a/contributors/views/contributor_achievements.py b/contributors/views/contributor_achievements.py new file mode 100644 index 0000000..873f08f --- /dev/null +++ b/contributors/views/contributor_achievements.py @@ -0,0 +1,109 @@ +from django.db.models import Count, Q, Sum # noqa: WPS235, WPS347 +from django.db.models.functions import Coalesce +from django.views import generic + +from contributors.models import Contributor, Repository + +ID = 'id' + + +class ContributorAchievementListView(generic.ListView): + """Achievement list.""" + + template_name = 'contributor/contributor_achievements_list.html' + model = Contributor + contributors = Contributor.objects.with_contributions() + + pull_request_ranges_for_achievements = [100, 50, 25, 10, 1] + commit_ranges_for_achievements = [200, 100, 50, 25, 1] + issue_ranges_for_achievements = [50, 25, 10, 5, 1] + comment_ranges_for_achievements = [200, 100, 50, 25, 1] + edition_ranges_for_achievements = [1000, 500, 250, 100, 1] + + def get_context_data(self, **kwargs): + """Add context data for achievement list.""" + self.contributors_amount = Contributor.objects.count() + context = super().get_context_data(**kwargs) + contributors = Contributor.objects.with_contributions() + current_contributor = ( + Contributor.objects.get(login=self.kwargs['slug']) + ) + + repositories = Repository.objects.select_related( + 'organization', + ).filter( + is_visible=True, + contribution__contributor=current_contributor, + ).annotate( + commits=Count('id', filter=Q(contribution__type='cit')), + additions=Coalesce(Sum('contribution__stats__additions'), 0), + deletions=Coalesce(Sum('contribution__stats__deletions'), 0), + pull_requests=Count( + 'contribution', filter=Q(contribution__type='pr'), + ), + issues=Count('contribution', filter=Q(contribution__type='iss')), + comments=Count('contribution', filter=Q(contribution__type='cnt')), + ).order_by('organization', 'name') + + contributions = repositories.values().aggregate( + contributor_deletions=Sum('deletions'), + contributor_additions=Sum('additions'), + contributor_commits=Sum('commits'), + contributor_pull_requests=Sum('pull_requests'), + contributor_issues=Sum('issues'), + contributor_comments=Sum('comments'), + ) + + context['commits'] = contributions['contributor_commits'] + context['pull_requests'] = contributions['contributor_pull_requests'] + context['issues'] = contributions['contributor_issues'] + context['comments'] = contributions['contributor_commits'] + context['total_editions'] = ( + contributions['contributor_additions'] + contributions['contributor_deletions'] # noqa: E501 + ) + context['total_actions'] = sum(contributions.values()) + context['pull_request_ranges_for_achievements'] = ( + self.pull_request_ranges_for_achievements + ) + context['current_contributor'] = current_contributor + context['contributors_amount'] = self.contributors_amount + context['contributors_with_any_contribution'] = ( + contributors.filter(contribution_amount__gte=1).count() + ) + + # Pull request achievements: + for pr_num in self.pull_request_ranges_for_achievements: + context[f'contributor_pull_requests_gte_{pr_num}'] = pr_num + context[f'contributors_pull_requests_gte_{pr_num}'] = ( + contributors.filter(pull_requests__gte=pr_num).count() + ) + + # Commit achievements: + for commit_num in self.commit_ranges_for_achievements: + context[f'contributor_commits_gte_{commit_num}'] = commit_num + context[f'contributors_commits_gte_{commit_num}'] = ( + contributors.filter(commits__gte=commit_num).count() + ) + + # Issue achievements: + for issue_num in self.issue_ranges_for_achievements: + context[f'contributor_issues_gte_{issue_num}'] = issue_num + context[f'contributors_issues_gte_{issue_num}'] = ( + contributors.filter(issues__gte=issue_num).count() + ) + + # Comment achievements: + for comment_num in self.comment_ranges_for_achievements: + context[f'contributor_comments_gte_{comment_num}'] = comment_num + context[f'contributors_comments_gte_{comment_num}'] = ( + contributors.filter(comments__gte=comment_num).count() + ) + + # Edition achievements: + for ed_num in self.edition_ranges_for_achievements: + context[f'contributor_editions_gte_{ed_num}'] = ed_num + context[f'contributors_editions_gte_{ed_num}'] = ( + contributors.filter(editions__gte=ed_num).count() + ) + + return context diff --git a/templates/components/tables/contributor_achievement_percentage_field.html b/templates/components/tables/contributor_achievement_percentage_field.html new file mode 100644 index 0000000..bf68ebf --- /dev/null +++ b/templates/components/tables/contributor_achievement_percentage_field.html @@ -0,0 +1,18 @@ +{% load i18n static mathfilters %} + +
+
+
+
{% trans achievement_name %}
+
{% trans achievement_description %}
+
+
+ +
+ {% if contribution|div:achievement_made_count|mul:100 > 100 %} + 100.0% + {% else %} + {{ contribution|div:achievement_made_count|mul:100|floatformat:1 }}% + {% endif %} +
+
diff --git a/templates/components/tables/contributor_achievements_list.html b/templates/components/tables/contributor_achievements_list.html new file mode 100644 index 0000000..d5006b1 --- /dev/null +++ b/templates/components/tables/contributor_achievements_list.html @@ -0,0 +1,380 @@ +{% extends './list_as_table.html' %} +{% load i18n static mathfilters %} + +{% block tbody %} + + + {% trans 'Hexlet friend' %} + + + + {% with achievement_name="Hexlet friend" achievement_description="Make any contribution to Hexlet projects" achievement_made_count=1 contribution=total_actions %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + + + + {% trans 'Pull requests (equal to or more than 1)' %} + + + {% with achievement_name="Pull requests (equal to or more than 1)" achievement_description="Make pull requests in amount of equal to or more than 1" achievement_made_count=contributor_pull_requests_gte_1 contribution=pull_requests %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Pull requests (equal to or more than 10)' %} + + + {% with achievement_name="Pull requests (equal to or more than 10)" achievement_description="Make pull requests in amount of equal to or more than 10" achievement_made_count=contributor_pull_requests_gte_10 contribution=pull_requests %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Pull requests (equal to or more than 25)' %} + + + {% with achievement_name="Pull requests (equal to or more than 25)" achievement_description="Make pull requests in amount of equal to or more than 25" achievement_made_count=contributor_pull_requests_gte_25 contribution=pull_requests %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Pull requests (equal to or more than 50)' %} + + + {% with achievement_name="Pull requests (equal to or more than 50)" achievement_description="Make pull requests in amount of equal to or more than 50" achievement_made_count=contributor_pull_requests_gte_50 contribution=pull_requests %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Pull requests (equal to or more than 100)' %} + + + {% with achievement_name="Pull requests (equal to or more than 100)" achievement_description="Make pull requests in amount of equal to or more than 100" achievement_made_count=contributor_pull_requests_gte_100 contribution=pull_requests %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + + + {% trans 'Commits (equal to or more than 1)' %} + + + {% with achievement_name="Commits (equal to or more than 1)" achievement_description="Make commits in amount of equal to or more than 1" achievement_made_count=contributor_commits_gte_1 contribution=commits %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Commits (equal to or more than 25)' %} + + + {% with achievement_name="Commits (equal to or more than 25)" achievement_description="Make commits in amount of equal to or more than 25" achievement_made_count=contributor_commits_gte_25 contribution=commits %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Commits (equal to or more than 50)' %} + + + {% with achievement_name="Commits (equal to or more than 50)" achievement_description="Make commits in amount of equal to or more than 50" achievement_made_count=contributor_commits_gte_50 contribution=commits %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Commits (equal to or more than 100)' %} + + + {% with achievement_name="Commits (equal to or more than 100)" achievement_description="Make commits in amount of equal to or more than 100" achievement_made_count=contributor_commits_gte_100 contribution=commits %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Commits (equal to or more than 200)' %} + + + {% with achievement_name="Commits (equal to or more than 200)" achievement_description="Make commits in amount of equal to or more than 200" achievement_made_count=contributor_commits_gte_200 contribution=commits %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + + + {% trans 'Issues (equal to or more than 1)' %} + + + {% with achievement_name="Issues (equal to or more than 1)" achievement_description="Make issues in amount of equal to or more than 1" achievement_made_count=contributor_issues_gte_1 contribution=issues %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Issues (equal to or more than 5)' %} + + + {% with achievement_name="Issues (equal to or more than 5)" achievement_description="Make issues in amount of equal to or more than 5" achievement_made_count=contributor_issues_gte_5 contribution=issues %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Issues (equal to or more than 10)' %} + + + {% with achievement_name="Issues (equal to or more than 10)" achievement_description="Make issues in amount of equal to or more than 10" achievement_made_count=contributor_issues_gte_10 contribution=issues %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Issues (equal to or more than 25)' %} + + + {% with achievement_name="Issues (equal to or more than 25)" achievement_description="Make issues in amount of equal to or more than 25" achievement_made_count=contributor_issues_gte_25 contribution=issues %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Issues (equal to or more than 50)' %} + + + {% with achievement_name="Issues (equal to or more than 50)" achievement_description="Make issues in amount of equal to or more than 50" achievement_made_count=contributor_issues_gte_50 contribution=issues %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + + + {% trans 'Comments (equal to or more than 1)' %} + + + {% with achievement_name="Comments (equal to or more than 1)" achievement_description="Make comments in amount of equal to or more than 1" achievement_made_count=contributor_comments_gte_1 contribution=comments %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Comments (equal to or more than 25)' %} + + + {% with achievement_name="Comments (equal to or more than 25)" achievement_description="Make comments in amount of equal to or more than 25" achievement_made_count=contributor_comments_gte_25 contribution=comments %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Comments (equal to or more than 50)' %} + + + {% with achievement_name="Comments (equal to or more than 50)" achievement_description="Make comments in amount of equal to or more than 50" achievement_made_count=contributor_comments_gte_50 contribution=comments %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Comments (equal to or more than 100)' %} + + + {% with achievement_name="Comments (equal to or more than 100)" achievement_description="Make comments in amount of equal to or more than 100" achievement_made_count=contributor_comments_gte_100 contribution=comments %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Comments (equal to or more than 200)' %} + + + {% with achievement_name="Comments (equal to or more than 200)" achievement_description="Make comments in amount of equal to or more than 200" achievement_made_count=contributor_comments_gte_200 contribution=comments %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + + {% trans 'Additions and deletions (equal to or more than 1)' %} + + + {% with achievement_name="Additions and deletions (equal to or more than 1)" achievement_description="Make additions and deletions in amount of equal to or more than 1" achievement_made_count=contributor_editions_gte_1 contribution=total_editions %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Additions and deletions (equal to or more than 100)' %} + + + {% with achievement_name="Additions and deletions (equal to or more than 100)" achievement_description="Make additions and deletions in amount of equal to or more than 100" achievement_made_count=contributor_editions_gte_100 contribution=total_editions %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Additions and deletions (equal to or more than 250)' %} + + + {% with achievement_name="Additions and deletions (equal to or more than 250)" achievement_description="Make additions and deletions in amount of equal to or more than 250" achievement_made_count=contributor_editions_gte_250 contribution=total_editions %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Additions and deletions (equal to or more than 500)' %} + + + {% with achievement_name="Additions and deletions (equal to or more than 500)" achievement_description="Make additions and deletions in amount of equal to or more than 500" achievement_made_count=contributor_editions_gte_500 contribution=total_editions %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + + + + {% trans 'Additions and deletions (equal to or more than 1000)' %} + + + {% with achievement_name="Additions and deletions (equal to or more than 1000)" achievement_description="Make additions and deletions in amount of equal to or more than 1000" achievement_made_count=contributor_editions_gte_1000 contribution=total_editions %} + {% include './contributor_achievement_percentage_field.html' %} + {% endwith %} + + +{% endblock %} diff --git a/templates/contributor/contributor_achievements_list.html b/templates/contributor/contributor_achievements_list.html new file mode 100644 index 0000000..213a42a --- /dev/null +++ b/templates/contributor/contributor_achievements_list.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} +{% load i18n static %} + +{% block content %} + +

{% trans "Achievements" %}

+ + + + +{% endblock %}