Permalink
Browse files

fix for duplicate items in multiselect when order is required

  • Loading branch information...
1 parent aa52420 commit 9f5c7385bf31c2123e68ea58c0cef7716f282d0b @camd camd committed Apr 2, 2013
@@ -6,9 +6,11 @@
<div class="name-tags">
<h5 class="title" title="{{ name }}">{{ name }}</h5>
<ul class="tags">
- {{#tags}}
- <li><a href="#{{ name|slugify }}" title="filter by {{ name }}" class="filter-link tag" data-type="tag">{{ name }}</a></li>
- {{/tags}}
+ {{#versions}}
+ {{#tags}}
+ <li><a href="#{{ name|slugify }}" title="filter by {{ name }}" class="filter-link tag" data-type="tag">{{ name }}</a></li>
+ {{/tags}}
+ {{/versions}}
</ul>
</div>
@@ -1,3 +1,4 @@
+from django.db.models import Max
from tastypie.resources import ModelResource, ALL_WITH_RELATIONS
from tastypie import fields
from tastypie.bundle import Bundle
@@ -300,14 +301,14 @@ class SuiteSelectionResource(BaseSelectionResource):
product = fields.ForeignKey(ProductResource, "product")
runs = fields.ToManyField(RunResource, "runs")
- created_by = fields.ForeignKey(UserResource, "created_by", full=True, null=True)
+ created_by = fields.ForeignKey(
+ UserResource, "created_by", full=True, null=True)
class Meta:
queryset = Suite.objects.all().select_related(
"created_by",
- ).prefetch_related(
- "runsuites",
- ).distinct().order_by("runsuites__order")
+ ).annotate(order=Max("runsuites__order")).order_by("order")
+
list_allowed_methods = ['get']
fields = ["id", "name", "created_by"]
filtering = {
@@ -316,6 +317,10 @@ class Meta:
"created_by": ALL_WITH_RELATIONS,
}
+ @property
+ def order_field(self):
+ return "runsuites__order"
+
def dehydrate(self, bundle):
"""Add some convenience fields to the return JSON."""
@@ -324,14 +329,6 @@ def dehydrate(self, bundle):
bundle.data["suite_id"] = unicode(suite.id)
bundle.data["case_count"] = suite.cases.count()
bundle.data["filter_cases"] = filter_url("manage_cases", suite)
-
- if "runs" in bundle.request.GET.keys():
- run_id = int(bundle.request.GET["runs"])
- s = suite.runsuites.all()
- order = [x.order for x in suite.runsuites.all()
- if x.run_id == run_id][0]
- bundle.data["order"] = order
- else:
- bundle.data["order"] = None
+ bundle.data["order"] = suite.order
return bundle
@@ -1,3 +1,4 @@
+from django.db.models import Max
from tastypie import http, fields
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
@@ -204,12 +205,13 @@ class CaseVersionResource(MTResource):
class Meta(MTResource.Meta):
queryset = CaseVersion.objects.all()
- fields = ["id", "name", "description", "case", "status"]
+ fields = ["id", "name", "description", "case", "latest", "status"]
filtering = {
"environments": ALL,
"productversion": ALL_WITH_RELATIONS,
"case": ALL_WITH_RELATIONS,
"tags": ALL_WITH_RELATIONS,
+ "latest": ALL,
}
authorization = CaseVersionAuthorization()
@@ -316,47 +318,48 @@ class CaseSelectionResource(BaseSelectionResource):
for selecting cases.
"""
- case = fields.ForeignKey(CaseResource, "case")
- productversion = fields.ForeignKey(
- ProductVersionResource, "productversion")
- tags = fields.ToManyField(TagResource, "tags", full=True)
- created_by = fields.ForeignKey(UserResource, "created_by", full=True, null=True)
+ product = fields.ForeignKey(ProductResource, "product")
+ versions = fields.ToManyField(
+ CaseVersionResource,
+ # we only want the latest of the caseversions. this does that filtering
+ lambda bundle: CaseVersion.objects.filter(
+ case=bundle.obj,
+ latest=True
+ ),
+ related_name="versions",
+ full=True,
+ )
+ suites = fields.ToManyField(SuiteResource, "suites")
class Meta:
- queryset = CaseVersion.objects.all().select_related(
- "case",
- "productversion",
- "created_by",
+ queryset = Case.objects.all().select_related(
+ "product",
).prefetch_related(
- "tags",
- "case__suitecases",
- ).distinct().order_by("case__suitecases__order")
+ "versions__tags",
+ ).annotate(
+ order=Max("suitecases__order"),
+ ).order_by("order")
+
list_allowed_methods = ['get']
- fields = ["id", "name", "latest", "created_by"]
+ fields = ["id", "versions", "created_by"]
filtering = {
- "productversion": ALL_WITH_RELATIONS,
- "tags": ALL_WITH_RELATIONS,
- "case": ALL_WITH_RELATIONS,
- "latest": ALL,
- "created_by": ALL_WITH_RELATIONS
+ "product": ALL_WITH_RELATIONS,
+ "versions": ALL_WITH_RELATIONS,
+ "created_by": ALL_WITH_RELATIONS,
+ "suites": ALL_WITH_RELATIONS
}
def dehydrate(self, bundle):
"""Add some convenience fields to the return JSON."""
- case = bundle.obj.case
+ case = bundle.obj
+ cv = bundle.obj.versions.all()[0]
bundle.data["case_id"] = unicode(case.id)
+ bundle.data["name"] = unicode(cv.name)
bundle.data["product_id"] = unicode(case.product_id)
bundle.data["product"] = {"id": unicode(case.product_id)}
-
- if "case__suites" in bundle.request.GET.keys():
- suite_id = int(bundle.request.GET["case__suites"])
- order = [x.order for x in case.suitecases.all()
- if x.suite_id == suite_id][0]
- bundle.data["order"] = order
- else:
- bundle.data["order"] = None
+ bundle.data["order"] = case.order
return bundle
View
@@ -184,11 +184,11 @@ var MT = (function (MT, $) {
container: '#suite-edit-form, #suite-add-form',
trigger_field: '#id_product',
ajax_url_root: "/api/v1/caseselection/?format=json&limit=0",
- ajax_trigger_filter: "productversion__product",
- ajax_for_field: "case__suites",
+ ajax_trigger_filter: "product",
+ ajax_for_field: "suites",
for_type: "suite",
ich_template: ich.case_select_item,
- use_latest: true
+ use_latest: false
});
MT.populateMultiselectItems({
container: '#tag-add-form, #tag-edit-form',
@@ -273,3 +273,29 @@ def test_included_for_one_included_one_not(self):
self.included_param,
exp_objects=exp_objects,
)
+
+
+ def test_available_included_in_other_runs(self):
+ """Get a list of available suites, when suites included elsewhere"""
+
+ s1 = self.factory.create(name="Suite1")
+ s2 = self.factory.create(name="Suite2")
+ run1 = self.F.RunFactory.create()
+ self.F.RunSuiteFactory.create(
+ run=run1, suite=s1, order=0)
+ self.F.RunSuiteFactory.create(
+ run=run1, suite=s2, order=1)
+ run2 = self.F.RunFactory.create()
+ self.F.RunSuiteFactory.create(
+ run=run2, suite=s1, order=0)
+ self.F.RunSuiteFactory.create(
+ run=run2, suite=s2, order=1)
+
+ self._do_test(
+ -1,
+ self.available_param,
+ [self.get_exp_obj(s, runs=[
+ unicode(self.get_detail_url("run", run1.id)),
+ unicode(self.get_detail_url("run", run2.id)),
+ ]) for s in [s1, s2]],
+ )

0 comments on commit 9f5c738

Please sign in to comment.