Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.
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
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
.DS_Store
*.pyc
*.pyc

# Setuptools distribution folder.
/dist/

# Python egg metadata, regenerated from source files by setuptools.
/*.egg-info
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
sudo: false
language: python
python:
- "2.7"

before_install:
- pip install codecov

install:
- python setup.py -q develop

script:
- cd test_project
- coverage run --source ../versionfield manage.py test versionfield

after_success:
- codecov
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
django-versionfield
====

[![Build Status](https://travis-ci.org/tonioo/django-versionfield.svg?branch=master)](https://travis-ci.org/tonioo/django-versionfield)
[![codecov](https://codecov.io/gh/tonioo/django-versionfield/branch/master/graph/badge.svg)](https://codecov.io/gh/tonioo/django-versionfield)

Usage:
from versionfield import VersionField

Expand All @@ -16,6 +19,7 @@ License

django-versionfield is distributed under a BSD-style license.

Copyright (c) 2014-2017 Antoine Nguyen <tonio@ngyn.org>
Copyright (c) 2011-2013 MindSnacks (http://mindsnacks.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
25 changes: 14 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
ez_setup.use_setuptools()
from setuptools import setup, find_packages

import os

setup(
name = "django-versionfield",
version = "0.3.2",
url = 'https://github.com/mindsnacks/django-versionfield',
license = 'BSD',
description = "A DB Independent Custom Django Field for storing Version numbers for fast indexing",
author = 'Tom Hoddes',
packages = find_packages(),
include_package_data = True,
classifiers = [
name="django-versionfield2",
version="0.5.0",
url='https://github.com/tonioo/django-versionfield',
license='BSD',
description="A DB Independent Custom Django Field for storing Version numbers for fast indexing",
author='Antoine Nguyen',
author_email='tonio@ngyn.org',
packages=find_packages(),
include_package_data=True,
install_requires=[
'Django<=1.10.99',
'six>=1.9.0',
],
classifiers=[
'Development Status :: 3 - Alpha',
'Framework :: Django',
'Intended Audience :: Developers',
Expand Down
10 changes: 10 additions & 0 deletions test_project/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
Empty file.
122 changes: 122 additions & 0 deletions test_project/test_project/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"""
Django settings for test_project project.

Generated by 'django-admin startproject' using Django 1.9.7.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '33%!r(6)*y1@f0=(2(!*fwoz5@=mi#$6&cc-ze@2#ws3(gx(5p'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'versionfield',
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'test_project.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'test_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'
21 changes: 21 additions & 0 deletions test_project/test_project/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""test_project URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
url(r'^admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions test_project/test_project/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for test_project project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")

application = get_wsgi_application()
115 changes: 67 additions & 48 deletions versionfield/__init__.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,88 @@
from __future__ import unicode_literals

import six

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from . import forms
from .constants import DEFAULT_NUMBER_BITS
from .version import Version
from utils import convert_version_string_to_int, convert_version_int_to_string
import forms
from .utils import convert_version_int_to_string


@python_2_unicode_compatible
class VersionField(models.Field):

"""
A Field where version numbers are input/output as strings (e.g. 3.0.1)
but stored in the db as converted integers for fast indexing
"""

class VersionField(models.PositiveIntegerField):
"""
A Field where version numbers are input/output as strings (e.g. 3.0.1)
but stored in the db as converted integers for fast indexing
"""
description = "A version number (e.g. 3.0.1)"
description = "A version number (e.g. 3.0.1)"

__metaclass__ = models.SubfieldBase
def __init__(self, number_bits=DEFAULT_NUMBER_BITS, *args, **kwargs):
self.number_bits = number_bits
super(VersionField, self).__init__(*args, **kwargs)

def __init__(self, number_bits=DEFAULT_NUMBER_BITS, *args, **kwargs):
self.number_bits = number_bits
super(VersionField, self).__init__(*args, **kwargs)
def db_type(self, connection):
"""Use integer as internal representation."""
return "integer"

def to_python(self,value):
if isinstance(value, Version):
return value
def to_python(self, value):
if not value:
return None

if isinstance(value,basestring):
return Version(value,self.number_bits)
if isinstance(value, Version):
return value

if value is None:
return None
if isinstance(value, six.string_types):
return Version(value, self.number_bits)

return Version(convert_version_int_to_string(value,self.number_bits),self.number_bits)
return Version(
convert_version_int_to_string(value, self.number_bits),
self.number_bits
)

def get_prep_value(self,value):
if isinstance(value,basestring):
return int(Version(value,self.number_bits))
def from_db_value(self, value, expression, connection, context):
"""Convert data from database."""
if value is None:
return value
return Version(
convert_version_int_to_string(value, self.number_bits),
self.number_bits)

if value is None:
return None
def get_prep_value(self, value):
if isinstance(value, six.string_types):
return int(Version(value, self.number_bits))

return int(value)
if value is None:
return None

def formfield(self, **kwargs):
defaults = {
'form_class': forms.VersionField,
'number_bits': self.number_bits
}
defaults.update(kwargs)
return super(VersionField, self).formfield(**defaults)
return int(value)

def __unicode__(self, value):
return unicode(value)
def formfield(self, **kwargs):
defaults = {
'form_class': forms.VersionField,
'number_bits': self.number_bits
}
defaults.update(kwargs)
return super(VersionField, self).formfield(**defaults)

def __str__(self, value):
return six.text_type(value)


try:
from south.modelsinspector import add_introspection_rules
rules = [
(
(VersionField,),
[],
{
"number_bits": ["number_bits", {"default": DEFAULT_NUMBER_BITS}],
},
)
]
add_introspection_rules(rules, ["^versionfield"])
from south.modelsinspector import add_introspection_rules
rules = [(
(VersionField,),
[],
{
"number_bits": ["number_bits", {"default": DEFAULT_NUMBER_BITS}],
},
)]
add_introspection_rules(rules, ["^versionfield"])
except ImportError:
# looks like we aren't using south
pass
# looks like we aren't using south
pass
2 changes: 1 addition & 1 deletion versionfield/constants.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEFAULT_NUMBER_BITS = (8,8,16)
DEFAULT_NUMBER_BITS = (8, 8, 16)
Loading