return request.build_absolute_uri(instance.get_mbox_url())
def get_tags(self, instance):
- # TODO(stephenfin): I don't think this is correct - too many queries
- return [{'name': x.tag.name, 'count': x.count}
- for x in instance.patchtag_set.all()]
+ if instance.project.tags:
+ return {x.name: getattr(instance, x.attr_name)
+ for x in instance.project.tags}
+ else:
+ return None
def get_headers(self, instance):
if instance.headers:
serializer_class = PatchSerializer
def get_queryset(self):
- qs = super(PatchViewSet, self).get_queryset(
- ).prefetch_related(
- 'check_set', 'patchtag_set'
- ).select_related('state', 'submitter', 'delegate')
+ qs = super(PatchViewSet, self).get_queryset().with_tag_counts()\
+ .prefetch_related('check_set')\
+ .select_related('state', 'submitter', 'delegate')
if 'pk' not in self.kwargs:
# we are doing a listing, we don't need these fields
qs = qs.defer('content', 'diff', 'headers')
class PatchQuerySet(models.query.QuerySet):
- def with_tag_counts(self, project):
- if not project.use_tags:
+ def with_tag_counts(self, project=None):
+ if project and not project.use_tags:
return self
# We need the project's use_tags field loaded for Project.tags().
qs = self.prefetch_related('project')
select = OrderedDict()
select_params = []
- for tag in project.tags:
+
+ # All projects have the same tags, so we're good to go here
+ if project:
+ tags = project.tags
+ else:
+ tags = Tag.objects.all()
+
+ for tag in tags:
select[tag.attr_name] = (
"coalesce("
"(SELECT count FROM patchwork_patchtag"
content='Reviewed-by: Test User <test@example.com>\n')
resp = self.client.get(self.api_url(patch.id))
tags = resp.data['tags']
- self.assertEqual(1, len(tags))
- self.assertEqual(1, tags[0]['count'])
- self.assertEqual('Reviewed-by', tags[0]['name'])
+ self.assertEqual(3, len(tags))
+ self.assertEqual(1, tags['Reviewed-by'])
def test_anonymous_create(self):
"""Ensure anonymous "POST" operations are rejected."""