File: django-1.10-compat.patch

package info (click to toggle)
python-django-jsonfield 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 200 kB
  • ctags: 143
  • sloc: python: 521; makefile: 7
file content (99 lines) | stat: -rw-r--r-- 3,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
From 68a74fd8f5cc0ce0778bda680b3ca005e68b9530 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
Date: Thu, 18 Aug 2016 14:14:44 +0200
Subject: Register explicit lookup operators

Django 1.10 dropped support for Field.get_prep_lookup() and we need
to register explicit lookup operators overriding the default operators
to have our desired behaviour.

Fix #56

Bug: https://bitbucket.org/schinckel/django-jsonfield/issues/56/unit-test-test_query_object-fails-with
Bug-Debian: https://bugs.debian.org/828668
Applied-Upstream: 1.0.2
---
 jsonfield/fields.py | 59 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 15 deletions(-)

diff --git a/jsonfield/fields.py b/jsonfield/fields.py
index befef2c..dd390ef 100644
--- a/jsonfield/fields.py
+++ b/jsonfield/fields.py
@@ -4,6 +4,7 @@ import json
 from django.core.exceptions import ValidationError
 from django.conf import settings
 from django.db import models
+from django.db.models.lookups import Exact, IExact, In, Contains, IContains
 from django.db.backends.signals import connection_created
 from django.utils.translation import ugettext_lazy as _
 from django.utils import six
@@ -95,25 +96,53 @@ class JSONField(models.Field):
             return None
         return json.dumps(value, **self.encoder_kwargs)
 
-    def get_prep_lookup(self, lookup_type, value):
-        if lookup_type in ["exact", "iexact", "in", "isnull"]:
-            return value
-        if lookup_type in ["contains", "icontains"]:
-            if isinstance(value, (list, tuple)):
-                raise TypeError("Lookup type %r not supported with argument of %s" % (
-                    lookup_type, type(value).__name__
-                ))
-                # Need a way co combine the values with '%', but don't escape that.
-                return self.get_prep_value(value)[1:-1].replace(', ', r'%')
-            if isinstance(value, dict):
-                return self.get_prep_value(value)[1:-1]
-            return self.get_prep_value(value)
-        raise TypeError('Lookup type %r not supported' % lookup_type)
-
     def value_to_string(self, obj):
         return self._get_val_from_obj(obj)
 
 
+class NoPrepareMixin(object):
+    def get_prep_lookup(self):
+        return self.rhs
+
+
+class JSONFieldExactLookup(NoPrepareMixin, Exact):
+    pass
+
+
+class JSONFieldIExactLookup(NoPrepareMixin, IExact):
+    pass
+
+
+class JSONFieldInLookup(NoPrepareMixin, In):
+    pass
+
+
+class ContainsLookupMixin(object):
+    def get_prep_lookup(self):
+        if isinstance(self.rhs, (list, tuple)):
+            raise TypeError("Lookup type %r not supported with %s argument" % (
+                self.lookup_name, type(self.rhs).__name__
+            ))
+        if isinstance(self.rhs, dict):
+            return self.lhs.output_field.get_prep_value(self.rhs)[1:-1]
+        return self.lhs.output_field.get_prep_value(self.rhs)
+
+
+class JSONFieldContainsLookup(ContainsLookupMixin, Contains):
+    pass
+
+
+class JSONFieldIContainsLookup(ContainsLookupMixin, IContains):
+    pass
+
+
+JSONField.register_lookup(JSONFieldExactLookup)
+JSONField.register_lookup(JSONFieldIExactLookup)
+JSONField.register_lookup(JSONFieldInLookup)
+JSONField.register_lookup(JSONFieldContainsLookup)
+JSONField.register_lookup(JSONFieldIContainsLookup)
+
+
 class TypedJSONField(JSONField):
     """