Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for Django 3.0 and added ability for blank fields #15

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ matrix:
env: TOX_ENV=py35-django-2
- python: 3.6
env: TOX_ENV=py36-django-2
- python: 3.6
env: TOX_ENV=py36-django-3
#env:
# - TOX_ENV=py36-django-18
# - TOX_ENV=py35-django-18
Expand Down
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
Binary file added .vs/django-fractions/v15/.suo
Binary file not shown.
Binary file added .vs/slnx.sqlite
Binary file not shown.
6 changes: 2 additions & 4 deletions djfractions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import unicode_literals, absolute_import, division

__version__ = '1.1.0'

from __future__ import unicode_literals, division, absolute_import
__version__ = '1.2.0'
from decimal import Decimal
import fractions
import re
Expand Down
13 changes: 9 additions & 4 deletions djfractions/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from __future__ import unicode_literals, absolute_import, division, print_function
from __future__ import unicode_literals, division, absolute_import, print_function
import django
if django.VERSION[0] < 3:
from django.utils import six
SIX_OR_STR = six.string_types
else:
SIX_OR_STR = str

from django import forms
from django.core.exceptions import ValidationError
from django.core import validators
from django.utils import six
from django.utils.translation import ugettext_lazy as _, ungettext_lazy

from decimal import Decimal, InvalidOperation
Expand Down Expand Up @@ -95,7 +100,7 @@ def to_python(self, value):
if value in validators.EMPTY_VALUES:
return None

if isinstance(value, six.string_types):
if isinstance(value, SIX_OR_STR):
# some really lame validation that we do not have a string like "1 1 1/4" because that
# is not a valid number.
# these regexes should match fractions such as 1 1/4 and 1/4, with any number
Expand Down Expand Up @@ -207,7 +212,7 @@ def to_python(self, value):
# these regexes should match fractions such as 1 1/4 and 1/4, with any number
# of spaces between digits and / and any length of actual digits such as
# 100 1/4 or 1 100/400, etc
if isinstance(value, six.string_types):
if isinstance(value, SIX_OR_STR):
if not is_number(value) and not self.FRACTION_MATCH.match(value) \
and not self.MIXED_NUMBER_MATCH.match(value):
# this second matches optional whitespace, then a digit, then
Expand Down
4 changes: 1 addition & 3 deletions djfractions/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals, division, absolute_import, print_function

from __future__ import unicode_literals, division, absolute_import
from .fields import DecimalFractionField
43 changes: 30 additions & 13 deletions djfractions/models/fields.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals, division, absolute_import, print_function
import django
if django.VERSION[0] < 3:
from django.utils import six
SIX_OR_STR = six.string_types
else:
SIX_OR_STR = str

from django.core import checks
from django.db import connection
from django.db.models import DecimalField, Field
from django.utils import six
from django.utils.translation import ugettext_lazy as _

import decimal
Expand All @@ -30,17 +34,19 @@ class DecimalFractionField(Field):
}
description = _("Fraction number stored in the database as a Decimal")

def __init__(self, verbose_name=None, name=None, max_digits=None,
def __init__(self, verbose_name=None, name=None, max_digits=None, blank=False, null=False,
decimal_places=None, limit_denominator=None, coerce_thirds=True,
**kwargs):
self.limit_denominator = limit_denominator
self.coerce_thirds = coerce_thirds

# for decimal stuff
self.max_digits, self.decimal_places = max_digits, decimal_places
self.max_digits, self.decimal_places, self.blank, self.null = max_digits, decimal_places, blank, null

super(DecimalFractionField, self).__init__(verbose_name=verbose_name,
name=name,
blank=blank,
null=null,
**kwargs)

def check(self, **kwargs):
Expand Down Expand Up @@ -113,14 +119,25 @@ def _check_decimal_places_and_max_digits(self, **kwargs):
]
return []

def from_db_value(self, value, expression, connection, context):
if value is None:
return value

# this probably needs to call to_fraction()
# cann it just call to_python() for now?
#return fractions.Fraction(value)
return self.to_python(value)
# If running pre-django 3.0 use context, depricated in 3.0
if django.VERSION[0] < 3:
def from_db_value(self, value, expression, connection, context):
if value is None:
return value

# this probably needs to call to_fraction()
# cann it just call to_python() for now?
#return fractions.Fraction(value)
return self.to_python(value)
else:
def from_db_value(self, value, expression, connection):
if value is None:
return value

# this probably needs to call to_fraction()
# cann it just call to_python() for now?
#return fractions.Fraction(value)
return self.to_python(value)

def get_db_prep_save(self, value, connection):
# for django 1.9 the following will need used.
Expand Down Expand Up @@ -193,7 +210,7 @@ def get_internal_type(self):
return "DecimalField"

def _format(self, value):
if isinstance(value, six.string_types):
if isinstance(value, SIX_OR_STR):
return value
else:
return self.format_number(value)
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ envlist =
{py27,py34,py35,py36}-django-18
{py27,py34,py35,py36}-django-111
{py34,py35,py36}-django-2
{py36}-django-3
stats

[testenv]
Expand All @@ -13,6 +14,7 @@ deps =
django-18: Django>=1.8,<1.9
django-111: Django>=1.11,<2.0
django-2: Django>=2.0,<2.1
django-3: Django==3.0
-r{toxinidir}/requirements-test.txt
basepython =
py36: python3.6
Expand Down