Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ussues labels and filtering #268

Merged
merged 5 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from contributors.admin import (
contribution,
contribution_label,
contributor,
custom,
label,
Expand Down
9 changes: 9 additions & 0 deletions contributors/admin/contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class CommitStatsInline(admin.StackedInline):
extra = 0


class ContributionLabelInline(admin.StackedInline):
"""Repository label."""

model = Contribution.labels.through
extra = 1
verbose_name = "relation"
verbose_name_plural = "relations"


class IssueInfoInline(admin.StackedInline):
"""Issue or pull request additional info."""

Expand Down
14 changes: 14 additions & 0 deletions contributors/admin/contribution_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.contrib import admin

from contributors.admin.contribution import ContributionLabelInline
from contributors.admin.custom import site
from contributors.models import ContributionLabel


class ContributionLabelAdmin(admin.ModelAdmin):
"""Label representation."""

inlines = (ContributionLabelInline,)


site.register(ContributionLabel, ContributionLabelAdmin)
16 changes: 16 additions & 0 deletions contributors/fixtures/contributionlabel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"model": "contributors.contributionlabel",
"pk": 1,
"fields": {
"name": "bug"
}
},
{
"model": "contributors.contributionlabel",
"pk": 2,
"fields": {
"name": "first_good_ussue"
}
}
]
30 changes: 30 additions & 0 deletions contributors/fixtures/contributions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"fields": {
"repository": 1,
"contributor": 1,
"labels": [
1
],
"type": "cit",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -16,6 +19,9 @@
"fields": {
"repository": 2,
"contributor": 1,
"labels": [
1
],
"type": "cit",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -27,6 +33,9 @@
"fields": {
"repository": 2,
"contributor": 2,
"labels": [
1
],
"type": "cit",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -38,6 +47,9 @@
"fields": {
"repository": 1,
"contributor": 1,
"labels": [
1
],
"type": "iss",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -49,6 +61,9 @@
"fields": {
"repository": 1,
"contributor": 2,
"labels": [
1
],
"type": "iss",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -60,6 +75,9 @@
"fields": {
"repository": 2,
"contributor": 1,
"labels": [
1, 2
],
"type": "iss",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -71,6 +89,9 @@
"fields": {
"repository": 1,
"contributor": 1,
"labels": [
1
],
"type": "pr",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -82,6 +103,9 @@
"fields": {
"repository": 1,
"contributor": 2,
"labels": [
1
],
"type": "pr",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -93,6 +117,9 @@
"fields": {
"repository": 2,
"contributor": 1,
"labels": [
1
],
"type": "pr",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand All @@ -104,6 +131,9 @@
"fields": {
"repository": 2,
"contributor": 2,
"labels": [
1, 2
],
"type": "iss",
"html_url": "https://github.com/#",
"created_at": "2022-04-29T10:24:44Z"
Expand Down
7 changes: 7 additions & 0 deletions contributors/management/commands/fetchdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from contributors.models import (
CommitStats,
Contribution,
ContributionLabel,
Contributor,
IssueInfo,
Label,
Expand Down Expand Up @@ -88,6 +89,12 @@ def create_contributions( # noqa: C901,WPS231,WPS210
session,
) else contrib['state']

for label in contrib['labels']:
label_name, _ = ContributionLabel.objects.get_or_create(
name=label["name"],
)
contribution.labels.add(label_name)

IssueInfo.objects.update_or_create(
issue=contribution,
defaults={
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.1.5 on 2023-02-11 10:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('contributors', '0010_alter_project_html_url'),
]

operations = [
migrations.CreateModel(
name='ContributionLabel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=45)),
],
),
migrations.AddField(
model_name='contribution',
name='labels',
field=models.ManyToManyField(to='contributors.contributionlabel', verbose_name='contribution labels'),
),
]
1 change: 1 addition & 0 deletions contributors/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contributors.models.base import CommonFields
from contributors.models.commit_stats import CommitStats
from contributors.models.contribution import Contribution
from contributors.models.contribution_label import ContributionLabel
from contributors.models.contributor import Contributor
from contributors.models.issue_info import IssueInfo
from contributors.models.label import Label
Expand Down
6 changes: 6 additions & 0 deletions contributors/models/contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.translation import gettext_lazy as _
from django_cte import CTEManager

from contributors.models.contribution_label import ContributionLabel
from contributors.models.contributor import Contributor
from contributors.models.repository import Repository
from contributors.utils import misc
Expand Down Expand Up @@ -68,6 +69,11 @@ class Contribution(models.Model):

objects = ContributionManager() # noqa: WPS110

labels = models.ManyToManyField(
ContributionLabel,
verbose_name=_("contribution labels"),
)

class Meta(object):
verbose_name = _("contribution")
verbose_name_plural = _("contributions")
Expand Down
18 changes: 18 additions & 0 deletions contributors/models/contribution_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import models
from django.utils.translation import gettext_lazy as _


class ContributionLabel(models.Model):
"""Model representing a label."""

NAME_LENGTH = 45 # noqa: WPS115

name = models.CharField(max_length=NAME_LENGTH)

def __str__(self):
"""Represent an instance as a string."""
return self.name

class Meta(object):
verbose_name = _("contribution label")
verbose_name_plural = _("contribution label")
21 changes: 21 additions & 0 deletions contributors/templatetags/contrib_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def get_query_string(context, qs_param, qs_param_value):
with suppress(KeyError):
if qs_param == 'labels':
get_params.pop('page')
if qs_param == 'contribution_labels':
get_params.pop('page')
with suppress(KeyError):
get_params.pop(qs_param)
get_params[qs_param] = (
Expand Down Expand Up @@ -80,3 +82,22 @@ def prepare_labels_param_value(labels_param): # noqa: WPS430
return delimiter.join(labels)

return get_query_string(context, 'labels', prepare_labels_param_value)


@register.simple_tag(takes_context=True)
def get_contribution_label_query_string(context, passed_label):
"""Get labels query string."""
def prepare_contribution_labels_param_value(labels_param): # noqa: WPS430
delimiter = '.'
labels = labels_param.split(delimiter) if labels_param else []
if passed_label in labels:
labels.remove(passed_label)
else:
labels.append(passed_label)
return delimiter.join(labels)

return get_query_string(
context,
'contribution_labels',
prepare_contribution_labels_param_value,
)
9 changes: 9 additions & 0 deletions contributors/tests/test_contributors_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from faker.generator import Generator

from contributors.models.base import CommonFields
from contributors.models.contribution_label import ContributionLabel
from contributors.models.label import Label
from contributors.models.organization import Organization
from contributors.models.project import Project
Expand Down Expand Up @@ -53,6 +54,14 @@ def test_common_fields_methods(self):
common_field = CommonFieldTestClass(name=common_field_name)
self.assertEqual(str(common_field), common_field.name)

def test_contributions_label_methods(self):
"""Create a test contribution label and test its methods."""
contribution_label_name: str = self.faker.domain_word()
contribution_label = ContributionLabel.objects.create(
name=contribution_label_name,
)
self.assertEqual(str(contribution_label), contribution_label.name)


class CommonFieldTestClass(CommonFields):
"""Empty class to test an abstract class."""
Expand Down
17 changes: 15 additions & 2 deletions contributors/tests/test_contributors_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
class TestContributorDetailView(TestCase):
"""Test the methods for the contributor's details view."""

fixtures = ["contributions", "contributors", "labels", "repositories"]
fixtures = [
"contributions",
"contributionlabel",
"contributors",
"labels",
"repositories",
]

def setUp(self):
"""Create a test database."""
Expand Down Expand Up @@ -44,6 +50,7 @@ class TestContributorIssuesView(TestCase):

fixtures = [
"contributions",
"contributionlabel",
"contributors",
"issues",
"labels",
Expand Down Expand Up @@ -72,7 +79,13 @@ def test_contributor_issues_listview_methods(self):
class TestContributorPrView(TestCase):
"""Test the methods for the list of pull requests."""

fixtures = ["contributions", "contributors", "labels", "repositories"]
fixtures = [
"contributions",
"contributionlabel",
"contributors",
"labels",
"repositories",
]

def setUp(self):
"""Create a test database."""
Expand Down
10 changes: 6 additions & 4 deletions contributors/tests/test_issue_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from django.test import Client, TestCase
from django.urls import reverse

EXPECTED_ISSUES_COUNT = 1
EXPECTED_ISSUES_COUNT = 2


class TestIssuesListViewTestCase(TestCase):
"""Test the methods for a list of open issues."""

fixtures = [
"contributions",
"contributionlabel",
"contributors",
"issues",
"labels",
Expand All @@ -22,9 +23,10 @@ def setUp(self):
self.client: Client = Client()

def test_issues_listview_methods(self):
response = self.client.get(reverse("contributors:open_issues_list"))
response = self.client.get(reverse("contributors:open_issues"))
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertIn("repos_with_issues", response.context)
self.assertIn("contribution_labels", response.context)
self.assertEqual(
len(response.context["repos_with_issues"]), EXPECTED_ISSUES_COUNT,
len(response.context["contribution_labels"]),
EXPECTED_ISSUES_COUNT,
)
2 changes: 1 addition & 1 deletion contributors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
path(
"issues",
views.issues.ListView.as_view(),
name="open_issues_list",
name="open_issues",
),
path(
"pull_requests",
Expand Down