Skip to content

Commit

Permalink
Harmonized GEODJANGO_INSTALLED flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Apr 18, 2015
1 parent ddb28e1 commit 5d6ab87
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 50 deletions.
9 changes: 9 additions & 0 deletions django_hstore/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
""", DeprecationWarning)


# Check if GEODJANGO is being used
GEODJANGO_INSTALLED = False

for database in settings.DATABASES.values():
if 'postgis' in database['ENGINE']:
GEODJANGO_INSTALLED = True
break


class ConnectionCreateHandler(object):
"""
Generic connection handlers manager.
Expand Down
7 changes: 3 additions & 4 deletions django_hstore/hstore.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django_hstore.fields import DictionaryField, ReferencesField, SerializedDictionaryField # noqa
from django_hstore.managers import HStoreManager # noqa
from django_hstore.apps import GEODJANGO_INSTALLED


try:
if GEODJANGO_INSTALLED:
from django_hstore.managers import HStoreGeoManager # noqa
except:
# django.contrib.gis is not configured properly
pass


import django
if django.get_version() < '1.7':
Expand Down
11 changes: 4 additions & 7 deletions django_hstore/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
from django.db import models

from django_hstore.query import HStoreQuerySet

try:
from django.contrib.gis.db import models as geo_models
from django_hstore.query import HStoreGeoQuerySet
GEODJANGO_INSTALLED = True
except:
GEODJANGO_INSTALLED = False
from django_hstore.apps import GEODJANGO_INSTALLED


class HStoreManager(models.Manager):
Expand All @@ -34,6 +28,9 @@ def hslice(self, attr, keys, **params):


if GEODJANGO_INSTALLED:
from django.contrib.gis.db import models as geo_models
from django_hstore.query import HStoreGeoQuerySet

class HStoreGeoManager(geo_models.GeoManager, HStoreManager):
"""
Object manager combining Geodjango and hstore.
Expand Down
60 changes: 21 additions & 39 deletions django_hstore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@
from django.db.models.sql.subqueries import UpdateQuery
from django.db.models.sql.where import EmptyShortCircuit, WhereNode

try:
from django.contrib.gis.db.models.query import GeoQuerySet
from django.contrib.gis.db.models.sql.query import GeoQuery
from django.contrib.gis.db.models.sql.where import \
GeoWhereNode, GeoConstraint
GEODJANGO_INSTALLED = True
except:
GEODJANGO_INSTALLED = False
from django_hstore.apps import GEODJANGO_INSTALLED


class literal_clause(object):
Expand All @@ -38,18 +31,15 @@ def as_sql(self, qn, connection):


def select_query(method):

def selector(self, *args, **params):
query = self.query.clone()
query.default_cols = False
query.clear_select_fields()
return method(self, query, *args, **params)

return selector


def update_query(method):

def updater(self, *args, **params):
self._for_write = True
query = method(self, self.query.clone(UpdateQuery), *args, **params)
Expand All @@ -75,7 +65,6 @@ def updater(self, *args, **params):
def get_cast_for_param(value_annot, key):
if not isinstance(value_annot, dict):
return ''

if value_annot[key] in (True, False):
return '::boolean'
elif issubclass(value_annot[key], datetime):
Expand All @@ -101,7 +90,6 @@ def get_value_annotations(param):


class HStoreWhereNode(WhereNode):

def add(self, data, *args, **kwargs):
# WhereNode will convert params into strings, so we need to record
# the type of the params as part of the value_annotation before calling
Expand Down Expand Up @@ -230,40 +218,15 @@ def make_atom(self, child, qn, connection):
raise TypeError('invalid lookup type')

return super(HStoreWhereNode, self).make_atom(child, qn, connection)

make_hstore_atom = make_atom


if GEODJANGO_INSTALLED:
class HStoreGeoWhereNode(HStoreWhereNode, GeoWhereNode):

def make_atom(self, child, qn, connection):
lvalue, lookup_type, value_annot, params_or_value = child

# if spatial query
if isinstance(lvalue, GeoConstraint):
return GeoWhereNode.make_atom(self, child, qn, connection)

# else might be an HSTORE query
return HStoreWhereNode.make_atom(self, child, qn, connection)


class HStoreQuery(Query):

def __init__(self, model):
super(HStoreQuery, self).__init__(model, HStoreWhereNode)


if GEODJANGO_INSTALLED:
class HStoreGeoQuery(GeoQuery, Query):

def __init__(self, *args, **kwargs):
model = kwargs.pop('model', None) or args[0]
super(HStoreGeoQuery, self).__init__(model, HStoreGeoWhereNode)


class HStoreQuerySet(QuerySet):

def __init__(self, model=None, query=None, using=None, *args, **kwargs):
query = query or HStoreQuery(model)
super(HStoreQuerySet, self).__init__(model=model, query=query, using=using, *args, **kwargs)
Expand Down Expand Up @@ -324,8 +287,27 @@ def hupdate(self, query, attr, updates):


if GEODJANGO_INSTALLED:
class HStoreGeoQuerySet(HStoreQuerySet, GeoQuerySet):
from django.contrib.gis.db.models.query import GeoQuerySet
from django.contrib.gis.db.models.sql.query import GeoQuery
from django.contrib.gis.db.models.sql.where import GeoWhereNode, GeoConstraint

class HStoreGeoWhereNode(HStoreWhereNode, GeoWhereNode):
def make_atom(self, child, qn, connection):
lvalue, lookup_type, value_annot, params_or_value = child

# if spatial query
if isinstance(lvalue, GeoConstraint):
return GeoWhereNode.make_atom(self, child, qn, connection)

# else might be an HSTORE query
return HStoreWhereNode.make_atom(self, child, qn, connection)

class HStoreGeoQuery(GeoQuery, Query):
def __init__(self, *args, **kwargs):
model = kwargs.pop('model', None) or args[0]
super(HStoreGeoQuery, self).__init__(model, HStoreGeoWhereNode)

class HStoreGeoQuerySet(HStoreQuerySet, GeoQuerySet):
def __init__(self, model=None, query=None, using=None, **kwargs):
query = query or HStoreGeoQuery(model)
super(HStoreGeoQuerySet, self).__init__(model=model, query=query, using=using, **kwargs)

0 comments on commit 5d6ab87

Please sign in to comment.