Skip to content

Commit

Permalink
Merge pull request awesto#423 from rfleschenberg/jsonfield-wrapper
Browse files Browse the repository at this point in the history
Jsonfield wrapper
  • Loading branch information
rfleschenberg authored Sep 22, 2016
2 parents a080061 + ad5b0bf commit 8ccbcc9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
32 changes: 25 additions & 7 deletions example/tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
# -*- coding: utf-8
from __future__ import unicode_literals

import decimal

from django.contrib.auth import get_user_model
from django.db import models
from django.test import TestCase

from shop.models.defaults.cart_item import CartItem
from shop.models.fields import JSONField

from myshop.models import Cart, CartItem, Customer
from myshop.models.manufacturer import Manufacturer
from myshop.models.polymorphic.commodity import Commodity


class JsonModel(models.Model):

class Meta:
app_label = 'JsonModel'

json = JSONField()
default_json = JSONField(default={"check": 12})
default_json = JSONField(default=lambda: {"check": 12})


class JSONFieldTest(TestCase):
"""JSONField Tests"""

fixtures = ['myshop-polymorphic.json']

def setUp(self):
super(JSONFieldTest, self).setUp()
self.sample = CartItem.objects.get(id=8)
user = get_user_model().objects.create()
customer = Customer.objects.create(number=1, user=user)
manufacturer = Manufacturer.objects.create()
product = Commodity.objects.create(
product_code="testproduct",
unit_price=decimal.Decimal("0"),
order=1,
manufacturer=manufacturer
)
cart = Cart.objects.create(customer=customer)
self.sample = CartItem.objects.create(
quantity=1,
cart=cart,
product=product,
extra={'product_code': 'foo'},
)
self.assertIsNotNone(self.sample)

def test_json_field_create(self):
"""Test saving a JSON object in our JSONField"""
extra = {"product_code":"1121"}

extra = {'product_code': 'foo'}
self.assertEqual(self.sample.extra, extra)
25 changes: 4 additions & 21 deletions shop/models/fields.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import connection
from django.conf import settings

from shop.apps import get_tuple_version

try:
if str(connection.vendor) == 'postgresql':
import psycopg2

psycopg2_version = get_tuple_version(psycopg2.__version__[:5])
with connection.cursor() as cursor:
cursor.execute("SELECT version()")
row = cursor.fetchone()[:17]
postgres_version = get_tuple_version(str(row[0][:17].split(' ')[1]))
# To be able to use the Django version of JSONField, it requires to have
# PostgreSQL ≥ 9.4 and psycopg2 ≥ 2.5.4, otherwise some issues could be faced.
if (psycopg2_version) >= (2, 5, 4) and (postgres_version >= (9, 4)):
from django.contrib.postgres.fields import JSONField as _JSONField
else:
raise ImportError
else:
raise ImportError

except ImportError:
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql':
from django.contrib.postgres.fields import JSONField as _JSONField
else:
from jsonfield.fields import JSONField as _JSONField


Expand Down

0 comments on commit 8ccbcc9

Please sign in to comment.