[2/7] REST: Simplify ModelMultiChoiceField

Message ID 20180411161338.420-3-stephen@that.guru
State Accepted
Headers show
Series
  • Add support for multiple filters
Related show

Commit Message

Stephen Finucane April 11, 2018, 4:13 p.m.
We're actually going to remove this shortly but the new technique works
for both.

Signed-off-by: Stephen Finucane <stephen@that.guru>
---
 patchwork/api/filters.py | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)

Comments

Daniel Axtens May 8, 2018, 3:23 p.m. | #1
Stephen Finucane <stephen@that.guru> writes:

> We're actually going to remove this shortly but the new technique works
> for both.

For both what?

Apart from that, this patch leaves ModelMultiChoiceField._get_filters()
as dead code, but it seems you do something with it later, so that's
fine.

Regards,
Daniel

>
> Signed-off-by: Stephen Finucane <stephen@that.guru>
> ---
>  patchwork/api/filters.py | 23 +++++++----------------
>  1 file changed, 7 insertions(+), 16 deletions(-)
>
> diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py
> index 25956e98..030f9af3 100644
> --- a/patchwork/api/filters.py
> +++ b/patchwork/api/filters.py
> @@ -46,7 +46,10 @@ class ModelMultiChoiceField(ModelChoiceField):
>          if value in self.empty_values:
>              return None
>  
> -        filters = self._get_filters(value)
> +        try:
> +            filters = {'pk': int(value)}
> +        except ValueError:
> +            filters = {self.alternate_lookup: value}
>  
>          try:
>              value = self.queryset.get(**filters)
> @@ -58,11 +61,7 @@ class ModelMultiChoiceField(ModelChoiceField):
>  
>  class ProjectChoiceField(ModelMultiChoiceField):
>  
> -    def _get_filters(self, value):
> -        try:
> -            return {'pk': int(value)}
> -        except ValueError:
> -            return {'linkname__iexact': value}
> +    alternate_lookup = 'linkname__iexact'
>  
>  
>  class ProjectFilter(ModelChoiceFilter):
> @@ -72,11 +71,7 @@ class ProjectFilter(ModelChoiceFilter):
>  
>  class PersonChoiceField(ModelMultiChoiceField):
>  
> -    def _get_filters(self, value):
> -        try:
> -            return {'pk': int(value)}
> -        except ValueError:
> -            return {'email__iexact': value}
> +    alternate_lookup = 'email__iexact'
>  
>  
>  class PersonFilter(ModelChoiceFilter):
> @@ -111,11 +106,7 @@ class StateFilter(ModelChoiceFilter):
>  
>  class UserChoiceField(ModelMultiChoiceField):
>  
> -    def _get_filters(self, value):
> -        try:
> -            return {'pk': int(value)}
> -        except ValueError:
> -            return {'username__iexact': value}
> +    alternate_lookup = 'username__iexact'
>  
>  
>  class UserFilter(ModelChoiceFilter):
> -- 
> 2.14.3
>
> _______________________________________________
> Patchwork mailing list
> Patchwork@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork
Stephen Finucane May 8, 2018, 3:36 p.m. | #2
On Wed, 2018-05-09 at 01:23 +1000, Daniel Axtens wrote:
> Stephen Finucane <stephen@that.guru> writes:
> 
> > We're actually going to remove this shortly but the new technique works
> > for both.
> 
> For both what?

Works for both the current approach and the approach we adopt in future
patches.

I think I should have removed '_get_filters' in this patch. Feel free
to do so when applying.

Stephen

> Apart from that, this patch leaves ModelMultiChoiceField._get_filters()
> as dead code, but it seems you do something with it later, so that's
> fine.
> 
> Regards,
> Daniel
> 
> > 
> > Signed-off-by: Stephen Finucane <stephen@that.guru>
> > ---
> >  patchwork/api/filters.py | 23 +++++++----------------
> >  1 file changed, 7 insertions(+), 16 deletions(-)
> > 
> > diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py
> > index 25956e98..030f9af3 100644
> > --- a/patchwork/api/filters.py
> > +++ b/patchwork/api/filters.py
> > @@ -46,7 +46,10 @@ class ModelMultiChoiceField(ModelChoiceField):
> >          if value in self.empty_values:
> >              return None
> >  
> > -        filters = self._get_filters(value)
> > +        try:
> > +            filters = {'pk': int(value)}
> > +        except ValueError:
> > +            filters = {self.alternate_lookup: value}
> >  
> >          try:
> >              value = self.queryset.get(**filters)
> > @@ -58,11 +61,7 @@ class ModelMultiChoiceField(ModelChoiceField):
> >  
> >  class ProjectChoiceField(ModelMultiChoiceField):
> >  
> > -    def _get_filters(self, value):
> > -        try:
> > -            return {'pk': int(value)}
> > -        except ValueError:
> > -            return {'linkname__iexact': value}
> > +    alternate_lookup = 'linkname__iexact'
> >  
> >  
> >  class ProjectFilter(ModelChoiceFilter):
> > @@ -72,11 +71,7 @@ class ProjectFilter(ModelChoiceFilter):
> >  
> >  class PersonChoiceField(ModelMultiChoiceField):
> >  
> > -    def _get_filters(self, value):
> > -        try:
> > -            return {'pk': int(value)}
> > -        except ValueError:
> > -            return {'email__iexact': value}
> > +    alternate_lookup = 'email__iexact'
> >  
> >  
> >  class PersonFilter(ModelChoiceFilter):
> > @@ -111,11 +106,7 @@ class StateFilter(ModelChoiceFilter):
> >  
> >  class UserChoiceField(ModelMultiChoiceField):
> >  
> > -    def _get_filters(self, value):
> > -        try:
> > -            return {'pk': int(value)}
> > -        except ValueError:
> > -            return {'username__iexact': value}
> > +    alternate_lookup = 'username__iexact'
> >  
> >  
> >  class UserFilter(ModelChoiceFilter):
> > -- 
> > 2.14.3
> > 
> > _______________________________________________
> > Patchwork mailing list
> > Patchwork@lists.ozlabs.org
> > https://lists.ozlabs.org/listinfo/patchwork

Patch

diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py
index 25956e98..030f9af3 100644
--- a/patchwork/api/filters.py
+++ b/patchwork/api/filters.py
@@ -46,7 +46,10 @@  class ModelMultiChoiceField(ModelChoiceField):
         if value in self.empty_values:
             return None
 
-        filters = self._get_filters(value)
+        try:
+            filters = {'pk': int(value)}
+        except ValueError:
+            filters = {self.alternate_lookup: value}
 
         try:
             value = self.queryset.get(**filters)
@@ -58,11 +61,7 @@  class ModelMultiChoiceField(ModelChoiceField):
 
 class ProjectChoiceField(ModelMultiChoiceField):
 
-    def _get_filters(self, value):
-        try:
-            return {'pk': int(value)}
-        except ValueError:
-            return {'linkname__iexact': value}
+    alternate_lookup = 'linkname__iexact'
 
 
 class ProjectFilter(ModelChoiceFilter):
@@ -72,11 +71,7 @@  class ProjectFilter(ModelChoiceFilter):
 
 class PersonChoiceField(ModelMultiChoiceField):
 
-    def _get_filters(self, value):
-        try:
-            return {'pk': int(value)}
-        except ValueError:
-            return {'email__iexact': value}
+    alternate_lookup = 'email__iexact'
 
 
 class PersonFilter(ModelChoiceFilter):
@@ -111,11 +106,7 @@  class StateFilter(ModelChoiceFilter):
 
 class UserChoiceField(ModelMultiChoiceField):
 
-    def _get_filters(self, value):
-        try:
-            return {'pk': int(value)}
-        except ValueError:
-            return {'username__iexact': value}
+    alternate_lookup = 'username__iexact'
 
 
 class UserFilter(ModelChoiceFilter):