Permalink
Browse files

prefix id filtering working now

  • Loading branch information...
1 parent 907d4ae commit 94d24fb0fc0d549b4e7c74716f9256a74e3c0507 @camd camd committed Apr 12, 2012
View
@@ -193,7 +193,8 @@ class CaseVersionFilterSet(filters.FilterSet):
filters = [
filters.ChoicesFilter("status", choices=model.CaseVersion.STATUS),
- filters.KeywordExactFilter("id", lookup="case__id", coerce=int),
+ #filters.KeywordExactFilter("id", lookup="case__id", coerce=int),
+ filters.DelimitedStringIntFilter("id", lookup=["case__idprefix","case__id"]),
filters.KeywordFilter("name"),
filters.ModelFilter(
"tag", lookup="tags", queryset=model.Tag.objects.all()),
@@ -388,3 +388,47 @@ def filter(self, queryset, values):
return queryset.distinct()
return queryset
+
+
+
+class DelimitedStringIntFilter(Filter):
+ """
+ A string and an int, separated by a delimiter.
+ Values are split by the right-most occurrance of delimeter and ANDed
+ across fields. Must match exactly by either the prefix or prefix + suffix
+ ``lookup`` must be a pair of fields to lookup for prefix and suffix
+ ``key`` must also be a pair of keys used
+ """
+
+ delimiter = "-"
+
+ def filter(self, queryset, values):
+
+ for value in values:
+
+ # split the prefix from the id
+ prefix, sep, suffix = value.rpartition(self.delimiter)
+
+ # if there is a prefix of abc-xyz, then we don't want to
+ # try searching in the int field for xyz, presume it's all
+ # the prefix
+ # also, if the user put the delimiter at the end, strip it off
+ if not suffix.isdecimal():
+ prefix = value.rstrip(self.delimiter)
+ suffix = None
+
+ kwargs = {}
+
+ if prefix:
+ kwargs["{0}__exact".format(self.lookup[0])] = prefix
+ if suffix:
+ kwargs["{0}__exact".format(self.lookup[1])] = suffix
+
+ queryset = queryset.filter(**kwargs)
+
+ if values:
+ return queryset.distinct()
+
+ return queryset
+
+
@@ -258,6 +258,7 @@ def save(self):
assert self.is_valid()
product = self.cleaned_data["product"]
+ idprefix = self.cleaned_data["idprefix"]
self.save_new_tags(product)
@@ -271,7 +272,11 @@ def save(self):
cases = []
for case_data in self.cleaned_data["cases"]:
- case = model.Case.objects.create(product=product, user=self.user)
+ case = model.Case.objects.create(
+ product=product,
+ user=self.user,
+ idprefix=idprefix,
+ )
version_kwargs = case_data.copy()
steps_data = version_kwargs.pop("steps")
@@ -109,7 +109,7 @@
.caseinfo-fields
+columns(11,24)
- .name-field, .description-field
+ .name-field, .description-field, .id-prefix-field
.type
+columns(2,11)
.value, .errorlist
@@ -1,4 +1,4 @@
{% extends 'lists/_byline.html' %}
{% block id %}<strong>
- <a href="{% url manage_case case_id=item.case.id %}">#{% if item.case.idprefix %}{{ item.case.idprefix }}{% endif %}{{ item.case.id }}</a></strong>{% endblock id %}
+ <a href="{% url manage_case case_id=item.case.id %}">#{% if item.case.idprefix %}{{ item.case.idprefix }}-{% endif %}{{ item.case.id }}</a></strong>{% endblock id %}
@@ -610,3 +610,15 @@ def test_filter_doesnt_touch_queryset_if_no_values(self):
self.assertEqual(qs.filter.call_count, 0)
self.assertEqual(qs.distinct.call_count, 0)
+
+class DelimitedStringIntFilterTest(FiltersTestCase):
+ """
+ prefix and ID
+ prefix only
+ ID only
+ id when case has no prefix
+ 2 cases have 2 different prefixes OR'ed
+ 2 cases with no prefixes, IDs OR'ed
+
+
+ """

0 comments on commit 94d24fb

Please sign in to comment.