Skip to content

Commit

Permalink
Merge pull request #19 from CarliJoy/staging
Browse files Browse the repository at this point in the history
Update to a new version 0.5
  • Loading branch information
CarliJoy committed Nov 29, 2020
2 parents 5f848bd + 7ba0554 commit 4cb8942
Show file tree
Hide file tree
Showing 60 changed files with 1,747 additions and 883 deletions.
27 changes: 27 additions & 0 deletions .converagerc
@@ -0,0 +1,27 @@
[run]
branch = True
source = django-pint

[paths]
source =
src/
*/site-packages/


[report]
# Regexes for lines to exclude from consideration
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

# Don't complain about missing debug-only code:
def __repr__
if self\.debug

# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError

# Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:
51 changes: 48 additions & 3 deletions .gitignore
@@ -1,7 +1,52 @@
/venv
# Temporary and binary files
*~
*.py[cod]
*.so
*.pyc
*.cfg
!.isort.cfg
!setup.cfg
*.orig
*.log
*.pot
__pycache__/*
.cache/*
.*.swp
*/.ipynb_checkpoints/*
.DS_Store

# Project files
.ropeproject
.project
.pydevproject
.settings
.idea
tags

# Package files
*.egg
*.eggs/
.installed.cfg
*.egg-info

# Unittest and coverage
htmlcov/*
.coverage
.tox
junit.xml
coverage.xml
.pytest_cache/

# Build and docs folder/files
build/*
dist/*
.coverage
__pycache__
sdist/*
docs/README.rst
docs/api/*
docs/_rst/*
docs/_build/*
cover/*
MANIFEST

# Per-project virtualenvs
.venv*/
9 changes: 9 additions & 0 deletions .isort.cfg
@@ -0,0 +1,9 @@
[settings]
profile=black
skip=.tox,.venv*,build,dist
known_standard_library=setuptools,pkg_resources
known_test=pytest
known_django=django
known_first_party=django_pint
known_pandas=pandas,numpy
sections=FUTURE,STDLIB,TEST,DJANGO,THIRDPARTY,PANDAS,FIRSTPARTY,LOCALFOLDER
36 changes: 36 additions & 0 deletions .pre-commit-config.yaml
@@ -0,0 +1,36 @@
exclude: '^docs/conf.py'

repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
- id: check-ast
- id: check-json
- id: check-merge-conflict
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: requirements-txt-fixer
- id: mixed-line-ending
args: ['--fix=auto']


- repo: https://github.com/PyCQA/isort
rev: 5.6.4
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
language_version: python3

- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
args: ['--max-line-length=88'] # default of Black
10 changes: 10 additions & 0 deletions .readthedocs.yml
@@ -0,0 +1,10 @@
version: 2

sphinx:
configuration: docs/conf.py
fail_on_warning: true

python:
version: 3.8
install:
- requirements: docs/requirements.txt
74 changes: 54 additions & 20 deletions .travis.yml
@@ -1,28 +1,62 @@
os: linux
arch: arm64-graviton2
virt: lxd
dist: focal
group: edge
language: python
sudo: false
cache: pip
cache:
- directories:
- $HOME/.cache/pre-commit
- $TRAVIS_BUILD_DIR/.eggs
- pip

env:
- DJANGO='https://github.com/django/django/archive/master.tar.gz'
- DJANGO='django>=1.10.0,<1.11.0'
- DJANGO='django>=1.9.0,<1.10.0'
- DJANGO='django>=1.8.0,<1.9.0'
stages:
- lint
- test
- deploy

python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "nightly"
# Taken from https://github.com/encode/django-rest-framework/blob/master/.travis.yml
# But do not check the most up to date version as it seem to be in compatible with django_nose
jobs:
fast_finish: true
include:
- stage: lint
python: "3.9"
install: travis_retry pip install pre-commit
script: pre-commit run --all-files
env: DJANGO="none"

script:
coverage run runtests.py
- { stage: test, python: "3.6", env: DJANGO=2.2 }
- { stage: test, python: "3.6", env: DJANGO=3.0 }
- { stage: test, python: "3.6", env: DJANGO=3.1 }

- { stage: test, python: "3.7", env: DJANGO=2.2 }
- { stage: test, python: "3.7", env: DJANGO=3.0 }
- { stage: test, python: "3.7", env: DJANGO=3.1 }

- { stage: test, python: "3.8", env: DJANGO=2.2 }
- { stage: test, python: "3.8", env: DJANGO=3.0 }
- { stage: test, python: "3.8", env: DJANGO=3.1 }

- { stage: test, python: "3.9", env: DJANGO=3.1 }
- { stage: deploy, python: "3.9", env: STAGE="release", script: skip, install: skip }


install:
- travis_retry pip install $DJANGO
- travis_retry pip install coveralls
- travis_retry pip install -r test_requirements.txt
- travis_retry pip install tox tox-travis

script: tox

after_success:
- coveralls
- bash <(curl -s https://codecov.io/bash)

deploy:
provider: pypi
username: __token__
server: https://upload.pypi.org/legacy/
distributions: sdist bdist_wheel
password:
secure: OwJd2Ikzg/cBbeW0eekMV8aXT/6aOOyf7qi1ow+PvOOTGHzNM3XXvwcwD2TRywBvCEtpBBnGkEnEB8aGEHeF8dITGIVQjTzH0fjPB7ax/3zvg6ok/kY45tv9DpP8cAVxotSXpdgNZ8eHHf4HetWCjAI9tjSxamsC+RyRcINmbpvxrNYRzPLp/5uL5JOPADI98X7zSGRbLKPMSef/hRVeYUyw5TtkzL87/quKlPr2TNl240OQWB/FMxufGie+aLVMpz3eRtZYk5xf8LGxCWb/XPR2J9dmk1DGZSJkoaBUqSveq4NhT8VDL4s4Rl3vTLrPa+rm66crOiqeKgDFP6cXCG2uQpPD4rNtd5fTeGL7ZEdCMHFDqGoFpcHUuZDHprOVXVQ2x0BUI54KR4ntzascxuW5a1jyU5FkKzaUNB+gUW33IyStV0Lalq2Nnm8ASy6JqKJlr3Kj4VX/1QMx1KrwYRM3B9geW2x9DqHaijF75d7ln4zLgiUT6mDy+LeY21M4cUs6nbjzH+XqUG2JFDGhW/SmpSIXE7rEZIYRmUkwcr8jaNI8SRZPSmiE3kSY9C+VRPtR/b/W5m7zMGckSUKscP2tUQEPr5r8Qx+m6yorlgKToNJJ5eAIFnYEuLdA+O8nOSk9hBGSlCZxWicpkd/0qns69MMqPR913MQhslh41jg=
on:
tags: true
condition: $STAGE =~ ^(release)$
2 changes: 0 additions & 2 deletions AUTHORS

This file was deleted.

10 changes: 10 additions & 0 deletions AUTHORS.rst
@@ -0,0 +1,10 @@
============
Contributors
============

* Ben Harling <blrharling@gmail.com> (Original Author and Maintainer 2016-2020)
* Carli* Freudenberg <carli.freudenberg@posteo.de>
* Robert Roskam <raiderrobert@gmail.com>
* Cornel Vaideanu <cornel.vaideanu@gmail.com>
* Alex Bhandari <alexbhandari@gmail.com>
* Jonas Haag <jonas@lophus.org>
30 changes: 30 additions & 0 deletions CHANGELOG.rst
@@ -0,0 +1,30 @@
=========
Changelog
=========

Version 0.5
===========
- Change of Maintainer to `Carli* Freudenberg`_
- Ported code to work with current version of Django (2.2., 3.0, 3.2) and Python (3.6 - 3.9)
- added test for merge requests
- use `black`_ to format code
- using pytest instead of deprecated django-nose
- Allow custom ureg and integer unit field (`merge request #11 from jonashaag`_)
- pass base_unit from field to widget (`merge request #5 from cornelv`_)
- now using PyScaffold for versioned release
- added documentation and uploaded to readthedocs.org
- using pre-commit (also in CI)
- improved travis ci builds
- Created Changelog file

Version 0.4
===========

- Last release of Maintainer `Ben Harling`_


.. _Ben Harling: https://github.com/bharling
.. _Carli* Freudenberg: https://github.com/CarliJoy
.. _merge request #11 from jonashaag: https://github.com/CarliJoy/django-pint/pull/11
.. _merge request #5 from cornelv: https://github.com/CarliJoy/django-pint/pull/5
.. _black: https://github.com/psf/black
18 changes: 11 additions & 7 deletions LICENSE → LICENSE.txt
@@ -1,11 +1,15 @@
Copyright (C) 2012 django-pint authors (see AUTHORS file)
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
Copyright (C) 2020 django-pint authors (see AUTHORS file)

Copyright (c) 2020 Carli* Freudenberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
@@ -1 +1 @@
include *.md LICENSE
include *.md LICENSE
55 changes: 36 additions & 19 deletions README.md
@@ -1,43 +1,43 @@

[![Build Status](https://travis-ci.org/bharling/django-pint.svg?branch=master)](https://travis-ci.org/bharling/django-pint)
[![Coverage Status](https://coveralls.io/repos/github/bharling/django-pint/badge.svg?branch=master)](https://coveralls.io/github/bharling/django-pint?branch=master)
[![PyPI](https://img.shields.io/pypi/dm/django-pint.svg?maxAge=2592000?style=plastic)]()
[![PyPI](https://img.shields.io/pypi/v/django-ping.svg?maxAge=2592000?style=plastic)]()
[![Build Status](https://api.travis-ci.com/CarliJoy/django-pint.svg?branch=master)](https://travis-ci.com/github/CarliJoy/django-pint)
[![codecov](https://codecov.io/gh/CarliJoy/django-pint/branch/master/graph/badge.svg?token=I3M4CLILXE)](https://codecov.io/gh/CarliJoy/django-pint)
[![PyPI](https://img.shields.io/pypi/dm/django-pint.svg?maxAge=2592000?style=plastic)](https://pypi.org/project/django-pint/)
[![Python Versions](https://img.shields.io/pypi/pyversions/django-pint.svg)](https://pypi.org/project/django-pint/)
[![PyPI](https://img.shields.io/pypi/v/django-ping.svg?maxAge=2592000?style=plastic)](https://pypi.org/project/django-pint/)
[![Wheel Build](https://img.shields.io/pypi/wheel/django-pint.svg)](https://pypi.org/project/django-pint/)
[![Code Style Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Documentation Status](https://readthedocs.org/projects/django-pint/badge/?version=latest)](https://django-pint.readthedocs.io/en/latest/?badge=latest)

# Django Quantity Field

Django Quantity Field
================

A Small django field extension allowing you to store quantities in certain units and perform conversions easily. Uses [pint](https://github.com/hgrecco/pint) behind the scenes. Also contains a form field class and form widget that allows a user to choose alternative units to input data. The cleaned_data will output the value in the base_units defined for the field, eg: you specify you want to store a value in grams but will allow users to input either grams or ounces.

New Maintainers Welcome
-----------------------
## Compatibility

Had no idea how many people would end up using this library, great to see but I've nowhere near enough time to maintain it unfortunately. If you'd like to take this project on I'd be honored and grateful.

Compatibility
-------------
Requires django >= 2.2, and python 3.6/3.7/3.8/3.9

Requires django >= 1.8, and python 2.7/3.2/3.3/3.4
## Installation

Installation
------------

pip install django-pint


Simple Example
-----------------------
## Simple Example

Best way to illustrate is with an example

# app/models.py

from django.db import models
from quantityfield.fields import QuantityField

class HayBale(models.Model):
weight = QuantityField('tonne')

Quantities are stored and retrieved like any other field
Quantities are stored as float (Django FloatField) and retrieved like any other field

>> bale = HayBale.objects.create(weight=1.2)
>> bale = HayBale.objects.first()
Expand All @@ -52,6 +52,8 @@ Quantities are stored and retrieved like any other field
>> bale.weight.to('pound')
<Quantity(2645.55, 'pound')>

If your base unit is atomic (i.e. can be represented by an integer), you may also use `IntegerQuantityField` and `BigIntegerQuantityField`.

You can also pass Quantity objects to be stored in models. These are automatically converted to the units defined for the field ( but can be converted to something else when retrieved of course ).

>> from quantityfield import ureg
Expand All @@ -73,3 +75,18 @@ The form will render a float input and a select widget to choose the units. When
For comparative lookups, query values will be coerced into the correct units when comparing values, this means that comparing 1 ounce to 1 tonne should yield the correct results.

less_than_a_tonne = HayBale.objects.filter(weight__lt=Quantity(2000 * ureg.pound))

You can also use a custom Pint unit registry:

# app/models.py

from django.db import models
from quantityfield import DeconstructibleUnitRegistry
from quantityfield.fields import QuantityField

my_ureg = DeconstructibleUnitRegistry('your_units.txt')

class HayBale(models.Model):
custom_unit = QuantityField('tonne', ureg=my_ureg)

Note that in order to use Django's migrations with a custom unit registry, all unit info must be passed to the UnitRegistry constructor via the textfile method shown above. Calls to `.define(...)` aren't considered by Django's migration framework.

0 comments on commit 4cb8942

Please sign in to comment.