zoo/zoo/zoo_data/lookups.py

69 lines
1.9 KiB
Python
Raw Normal View History

# zoo - versatile objects management
# Copyright (C) 2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import django
from django.db.models import Transform, CharField, DateField
from django.db.models.functions import Lower
2017-01-02 17:48:23 +01:00
from django.contrib.postgres.fields import jsonb
try:
from django.db.models.fields.json import KeyTransform, KeyTransformTextLookupMixin
except ImportError:
from django.contrib.postgres.fields.jsonb import KeyTransform, KeyTransformTextLookupMixin
2017-01-02 17:48:23 +01:00
class Unaccent(Transform):
lookup_name = 'unaccent'
function = 'immutable_unaccent'
output_field = CharField()
2017-01-02 17:48:23 +01:00
CharField.register_lookup(Unaccent)
2017-01-02 17:48:23 +01:00
CharField.register_lookup(Lower)
2017-01-02 17:48:23 +01:00
class Normalize(Transform):
lookup_name = 'normalize'
function = 'immutable_normalize'
output_field = CharField()
2017-01-02 17:48:23 +01:00
CharField.register_lookup(Normalize)
2017-01-02 17:48:23 +01:00
2017-07-17 02:18:23 +02:00
class Date(Transform):
2017-01-02 17:48:23 +01:00
lookup_name = 'timestamp'
2017-07-17 02:18:23 +02:00
function = 'immutable_date'
output_field = DateField()
2017-01-02 17:48:23 +01:00
CharField.register_lookup(Date)
2017-01-02 17:48:23 +01:00
class JSONUnaccent(KeyTransformTextLookupMixin, Unaccent):
pass
2017-07-17 02:18:23 +02:00
class JSONTimestamp(KeyTransformTextLookupMixin, Date):
2017-01-02 17:48:23 +01:00
pass
class JSONNormalize(KeyTransformTextLookupMixin, Normalize):
pass
KeyTransform.register_lookup(JSONUnaccent)
KeyTransform.register_lookup(JSONTimestamp)
KeyTransform.register_lookup(JSONNormalize)