Skip to content

Commit

Permalink
Merge branch 'master' into select-related-unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer committed Nov 10, 2013
2 parents 1dcb7c8 + c94776b commit e8cadbf
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
source = django_pg/
branch = False

[report]
skip_lines =
# Don't complain about defensive lines for turning the
# database off.
if connection.vendor in ('dummy', 'unknown'):

[html]
directory = .coverage-html/
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.2.1
20 changes: 20 additions & 0 deletions django_pg/models/fields/composite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ def create_type(cls, connection):
"""Create the appropriate type in the database, if and only if
it does not already exist.
"""
# Sanity check: Are we using a dummy database?
# An application using django-pgfields may disable their database
# **entirely** for testing purposes. If this happens, this should
# be a no-op, rather than an error.
if connection.vendor in ('dummy', 'unknown'):
return

# Retreive the SQL to create the type.
sql = cls.create_type_sql(connection, only_if_not_exists=True)
cursor = connection.cursor()

# Actually execute the SQL, thereby creating the composite
# type in the database.
for sql_stmt in sql.split(';'):
if not sql_stmt.strip():
continue
Expand Down Expand Up @@ -104,6 +115,15 @@ def get_field_by_name(cls, field_name):
@classmethod
def register_composite(cls, connection, globally=True):
"""Register this composite type with psycopg2."""

# Sanity check: Are we using a dummy database?
# An application using django-pgfields may disable their database
# **entirely** for testing purposes. If this happens, this should
# be a no-op, rather than an error.
if connection.vendor in ('dummy', 'unknown'):
return

# Register the composite type with psycopg2.
return register_composite(str(cls.db_type()), connection.cursor(),
factory=cls.caster,
globally=globally,
Expand Down
10 changes: 9 additions & 1 deletion django_pg/models/fields/composite/adapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import absolute_import, unicode_literals
from django.db import connection
from psycopg2 import extensions
from psycopg2.extensions import adapt, SQL_IN


Expand All @@ -12,9 +14,15 @@ def getquoted(self):
"""Return the appropriate SQL for the given object, typecast
to the registered db_type.
"""
# Prepare an adapted object from the tuple form of this
# composite instance.
adapted = adapt(tuple(self._obj))
adapted.prepare(connection.connection)

# Return the appropriate SQL fragment.
return '{sql}::{db_type}'.format(
db_type=self._db_type,
sql=adapt(tuple(self._obj)).getquoted().decode('utf8'),
sql=adapted.getquoted().decode('utf8'),
).encode('utf8')


Expand Down
7 changes: 4 additions & 3 deletions django_pg/models/fields/composite/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ def __new__(cls, name, bases, attrs):

# Ensure that the type exists in the database.
# This is the final hook for this; it will ensure the presence
# of the type if the syncdb or connection creation hooks fail.
# of the type if the syncdb or connection creation hooks fail.
#
# This is, in particular, needed for testing, since the test
# database types are copied from the main database, but the tests
# don't have any guarantee that the main database ever ran.
# database types are copied from the main database, but the tests
# don't have any guarantee that the main database ever ran.
new_class.create_type(connection)

# Create a "caster class" for converting the value that
Expand Down
5 changes: 4 additions & 1 deletion django_pg/utils/gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
from django.db.utils import DEFAULT_DB_ALIAS


gis_backend = '.gis.' in connections[DEFAULT_DB_ALIAS].__module__
gis_backend = any((
'.gis.' in connections[DEFAULT_DB_ALIAS].__module__,
'.dummy.' in connections[DEFAULT_DB_ALIAS].__module__,
))
6 changes: 6 additions & 0 deletions docs/releases/1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ Features
* UUIDField now supports a ``coerce_to`` argument, defaulting to
``uuid.UUID``. The obvious use case for setting this is if you need
to get back str objects instead.

Bugfixes
--------

* django-pgfields 1.2.1 fixes a bug regarding Unicode values within
``CompositeField`` subclasses; sending unicode values will now work.
2 changes: 2 additions & 0 deletions tests/composite/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import absolute_import, unicode_literals
from collections import namedtuple
from datetime import date
from django.db import connection
from django.test import TestCase
from django.test.utils import override_settings
from django_pg import models
from tests.composite.fields import Monarch, Book, Item
from tests.composite.models import Monarchy, Author, Character
Expand Down

0 comments on commit e8cadbf

Please sign in to comment.