Skip to content

Commit

Permalink
Rework tagging infrastructure
Browse files Browse the repository at this point in the history
Solve getpatchwork#113 and getpatchwork#57 GitHub issues, allow tags on comments, fix up
returning tags in the API.

Signed-off-by: Veronika Kabatova <vkabatov@redhat.com>
  • Loading branch information
veruu committed Mar 16, 2018
1 parent 8465e33 commit 56aeff2
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 133 deletions.
16 changes: 8 additions & 8 deletions docs/deployment/management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ retag

.. program:: manage.py retag

Update the tag (Ack/Review/Test) counts on existing patches.
Update the tag (Ack/Review/Test) counts on existing submissions.

.. code-block:: shell
./manage.py retag [<patch_id>...]
./manage.py retag [<submission_id>...]
Patchwork extracts :ref:`tags <overview-tags>` from each patch it receives. By
default, three tags are extracted, but it's possible to change this on a
per-instance basis. Should you add additional tags, you may wish to scan older
patches for these new tags.
Patchwork extracts :ref:`tags <overview-tags>` from each submission it
receives. By default, three tags are extracted, but it's possible to change
this on a per-instance basis. Should you add additional tags, you may wish to
scan older submissions for these new tags.

.. option:: patch_id
.. option:: submission_id

a patch ID number. If not supplied, all patches will be updated.
a submission ID number. If not supplied, all submissions will be updated.
32 changes: 31 additions & 1 deletion patchwork/api/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

from django.db.models import Q

import email.parser

from rest_framework.generics import ListAPIView
Expand All @@ -28,7 +30,9 @@
from patchwork.api.embedded import PersonSerializer
from patchwork.api.embedded import ProjectSerializer
from patchwork.api.embedded import SeriesSerializer
from patchwork.models import Comment
from patchwork.models import CoverLetter
from patchwork.models import RelatedTag


class CoverLetterListSerializer(HyperlinkedModelSerializer):
Expand All @@ -37,15 +41,41 @@ class CoverLetterListSerializer(HyperlinkedModelSerializer):
submitter = PersonSerializer(read_only=True)
mbox = SerializerMethodField()
series = SeriesSerializer(many=True, read_only=True)
tags = SerializerMethodField()

def get_mbox(self, instance):
request = self.context.get('request')
return request.build_absolute_uri(instance.get_mbox_url())

def get_tags(self, instance):
tags = instance.project.tags
if not tags:
return {}

all_tags = {tag.name: [] for tag in tags}

related_tags = RelatedTag.objects.filter(
Q(submission__id=instance.id)
| Q(comment__id__in=[comment.id for comment in
instance.comments.all()])
)

for related_tag in related_tags:
all_tags[related_tag.tag.name].extend([value.value for value in
related_tag.values.all()])

# Sanitize the values -- remove possible duplicates and unused tags
for tag in tags:
if all_tags[tag.name]:
all_tags[tag.name] = set(all_tags[tag.name])
else:
del(all_tags[tag.name])
return all_tags

class Meta:
model = CoverLetter
fields = ('id', 'url', 'project', 'msgid', 'date', 'name', 'submitter',
'mbox', 'series')
'mbox', 'series', 'tags')
read_only_fields = fields
extra_kwargs = {
'url': {'view_name': 'api-cover-detail'},
Expand Down
41 changes: 38 additions & 3 deletions patchwork/api/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

from django.db.models import Q

import email.parser

from django.utils.translation import ugettext_lazy as _
Expand All @@ -34,6 +36,8 @@
from patchwork.api.embedded import SeriesSerializer
from patchwork.api.embedded import UserSerializer
from patchwork.models import Patch
from patchwork.models import RelatedTag
from patchwork.models import SeriesPatch
from patchwork.models import State
from patchwork.parser import clean_subject

Expand Down Expand Up @@ -92,9 +96,40 @@ def get_mbox(self, instance):
return request.build_absolute_uri(instance.get_mbox_url())

def get_tags(self, instance):
# TODO(stephenfin): Make tags performant, possibly by reworking the
# model
return {}
tags = instance.project.tags
if not tags:
return {}

all_tags = {tag.name: [] for tag in tags}

patch_tags = RelatedTag.objects.filter(
Q(submission__id=instance.id)
| Q(comment__id__in=[comment.id for comment in
instance.comments.all()])
)
cover = SeriesPatch.objects.get(
patch_id=instance.id).series.cover_letter
if cover:
cover_tags = RelatedTag.objects.filter(
Q(submission__id=cover.submission_ptr_id)
| Q(comment__id__in=[comment.id for comment in
cover.comments.all()])
)
else:
cover_tags = RelatedTag.objects.none()

for related_tag in (patch_tags | cover_tags):
all_tags[related_tag.tag.name].extend([value.value for value in
related_tag.values.all()])

# Sanitize the values -- remove possible duplicates and unused tags
for tag in tags:
if all_tags[tag.name]:
all_tags[tag.name] = set(all_tags[tag.name])
else:
del(all_tags[tag.name])

return all_tags

def get_check(self, instance):
return instance.combined_check_state
Expand Down
14 changes: 8 additions & 6 deletions patchwork/management/commands/retag.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

from django.core.management.base import BaseCommand

from patchwork.models import Patch
from patchwork.models import Submission


class Command(BaseCommand):
help = 'Update the tag (Ack/Review/Test) counts on existing patches'
args = '[<patch_id>...]'
help = 'Update tags on existing submissions and related comments'
args = '[<submission_id>...]'

def handle(self, *args, **options):
query = Patch.objects
query = Submission.objects

if args:
query = query.filter(id__in=args)
Expand All @@ -36,8 +36,10 @@ def handle(self, *args, **options):

count = query.count()

for i, patch in enumerate(query.iterator()):
patch.refresh_tag_counts()
for i, submission in enumerate(query.iterator()):
submission.refresh_tags()
for comment in submission.comments.all():
comment.refresh_tags()
if (i % 10) == 0:
self.stdout.write('%06d/%06d\r' % (i, count), ending='')
self.stdout.flush()
Expand Down
Loading

0 comments on commit 56aeff2

Please sign in to comment.