Browse Source

Merge pull request #27 from mgaitan/py3dj10

Added Python 3 compatibility
Fixes #18
remotes/zorun/master
Alireza Savand 2 years ago
parent
commit
da5079be12

+ 2
- 0
.travis.yml View File

@@ -5,6 +5,8 @@ env:
- DJANGO_VERSION=">=1.10.0,<1.11.0"
python:
- 2.7
- 3.4
- 3.5
install:
- pip install -q Django$DJANGO_VERSION
- pip install -r requirements.txt

+ 1
- 0
AUTHORS.rst View File

@@ -3,3 +3,4 @@
- Yohann Gabory <yohann.gabory@outscale.com>
- Stathis Kanterakis
- Thomas Sorrel <thomas@pandeiro.fr>
- Martín Gaitán <gaitan@gmail.com>

+ 24
- 17
django_databrowse/datastructures.py View File

@@ -6,10 +6,12 @@ convenience functionality and permalink functions for the databrowse app.
from django.db import models
from django.utils import formats
from django.utils.text import capfirst
from django.utils.encoding import smart_unicode, smart_str, iri_to_uri
from django.utils.encoding import smart_text, smart_text, iri_to_uri
from django.utils.safestring import mark_safe
from django.db.models.query import QuerySet
from django.core.exceptions import ObjectDoesNotExist
from django.utils.encoding import python_2_unicode_compatible


EMPTY_VALUE = '(None)'
DISPLAY_SIZE = 100
@@ -25,7 +27,7 @@ class EasyModel(object):

def __repr__(self):
return '<EasyModel for %s>' % \
smart_str(self.model._meta.object_name)
smart_text(self.model._meta.object_name)

def model_databrowse(self):
"Returns the ModelDatabrowse class for this model."
@@ -70,7 +72,7 @@ class EasyField(object):
self.model, self.field = easy_model, field

def __repr__(self):
return smart_str(u'<EasyField for %s.%s>' %
return smart_text(u'<EasyField for %s.%s>' %
(self.model.model._meta.object_name,
self.field.name))

@@ -97,7 +99,7 @@ class EasyChoice(object):
self.value, self.label = value, label

def __repr__(self):
return smart_str(u'<EasyChoice for %s.%s>' %
return smart_text(u'<EasyChoice for %s.%s>' %
(self.model.model._meta.object_name,
self.field.name))

@@ -109,25 +111,22 @@ class EasyChoice(object):
self.field.field.name,
iri_to_uri(self.value)))

@python_2_unicode_compatible
class EasyInstance(object):
def __init__(self, easy_model, instance):
self.model, self.instance = easy_model, instance

def __repr__(self):
return smart_str(u'<EasyInstance for %s (%s)>' %
return smart_text(u'<EasyInstance for %s (%s)>' %
(self.model.model._meta.object_name,
self.instance._get_pk_val()))

def __unicode__(self):
val = smart_unicode(self.instance)
def __str__(self):
val = smart_text(self.instance)
if len(val) > DISPLAY_SIZE:
return val[:DISPLAY_SIZE] + u'...'
return val

def __str__(self):
return self.__unicode__().encode('utf-8')

def pk(self):
return self.instance._get_pk_val()

@@ -153,16 +152,24 @@ class EasyInstance(object):
EasyInstance's model as a ForeignKey or ManyToManyField, along with
lists of related objects.
"""
for rel_object in \
self.model.model._meta.get_all_related_objects() +\
self.model.model._meta.get_all_related_many_to_many_objects():

related_objects = [
f for f in self.model.model._meta.get_fields()
if (f.one_to_many or f.one_to_one)
and f.auto_created and not f.concrete
]
related_m2m = [
f for f in self.model.model._meta.get_fields(include_hidden=True)
if f.many_to_many and f.auto_created
]
for rel_object in related_objects + related_m2m:
if rel_object.model not in self.model.model_list:
continue # Skip models that aren't in the model_list
em = EasyModel(self.model.site, rel_object.model)
try:
rel_accessor = getattr(self.instance, rel_object.get_accessor_name())
except ObjectDoesNotExist:
continue
continue
if rel_object.field.rel.multiple:
object_list = [EasyInstance(em, i) for i in rel_accessor.all()]
else: # for one-to-one fields
@@ -180,7 +187,7 @@ class EasyInstanceField(object):
self.raw_value = getattr(instance.instance, field.name)

def __repr__(self):
return smart_str(u'<EasyInstanceField for %s.%s>' %
return smart_text(u'<EasyInstanceField for %s.%s>' %
(self.model.model._meta.object_name,
self.field.name))

@@ -245,7 +252,7 @@ class EasyInstanceField(object):
m.model._meta.app_label,
m.model._meta.model_name,
iri_to_uri(value._get_pk_val())))
lst.append((smart_unicode(value), url))
lst.append((smart_text(value), url))
else:
lst = [(value, None) for value in self.values()]
elif self.field.choices:

+ 5
- 5
django_databrowse/plugins/calendars.py View File

@@ -2,10 +2,10 @@ from django import http
from django.db import models
from django_databrowse.datastructures import EasyModel
from django_databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.template import RequestContext
from django.utils.text import capfirst
from django.utils.encoding import force_unicode
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe
from django.views.generic import dates
from django.utils import datetime_safe
@@ -71,7 +71,7 @@ class CalendarPlugin(DatabrowsePlugin):
u'<p class="filter"><strong>View calendar by:</strong> %s</p>' % \
u', '.join(
['<a href="calendars/%s/">%s</a>' %
(f.name,force_unicode(capfirst(f.verbose_name)))
(f.name,force_text(capfirst(f.verbose_name)))
for f in fields.values()])
)

@@ -109,13 +109,13 @@ class CalendarPlugin(DatabrowsePlugin):
easy_model = EasyModel(self.site, self.model)
field_list = self.fields.values()
field_list.sort(key=lambda k:k.verbose_name)
return render_to_response(
return render(request,
'databrowse/calendar_homepage.html',
{
'root_url': self.site.root_url,
'model': easy_model,
'field_list': field_list
}, context_instance=RequestContext(request)
}
)

def calendar_view(self, request, field, year=None, month=None, day=None):

+ 16
- 12
django_databrowse/plugins/fieldchoices.py View File

@@ -2,12 +2,16 @@ from django import http
from django.db import models
from django_databrowse.datastructures import EasyModel
from django_databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.template import RequestContext
from django.utils.text import capfirst
from django.utils.encoding import smart_str, force_unicode
from django.utils.encoding import smart_str, force_text
from django.utils.safestring import mark_safe
import urllib
try:
from urllib import quote
except ImportError:
from urllib.parse import quote
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

class FieldChoicePlugin(DatabrowsePlugin):
@@ -46,7 +50,7 @@ class FieldChoicePlugin(DatabrowsePlugin):
u'<p class="filter"><strong>View by:</strong> %s</p>' % \
u', '.join(
['<a href="fields/%s/">%s</a>' %
(f.name, force_unicode(capfirst(f.verbose_name)))
(f.name, force_text(capfirst(f.verbose_name)))
for f in fields.values()])
)

@@ -57,7 +61,7 @@ class FieldChoicePlugin(DatabrowsePlugin):
return [mark_safe(u'%s%s/%s/%s/' % (
easy_instance_field.model.url(),
plugin_name, easy_instance_field.field.name,
urllib.quote(field_value, safe='')))]
quote(field_value, safe='')))]

def model_view(self, request, model_databrowse, url):
self.model, self.site = model_databrowse.model, model_databrowse.site
@@ -84,13 +88,13 @@ class FieldChoicePlugin(DatabrowsePlugin):
easy_model = EasyModel(self.site, self.model)
field_list = self.fields.values()
field_list.sort(key=lambda k: k.verbose_name)
return render_to_response(
return render(request,
'databrowse/fieldchoice_homepage.html',
{
'root_url': self.site.root_url,
'model': easy_model,
'field_list': field_list
}, context_instance=RequestContext(request)
}
)

def field_view(self, request, field, value=None):
@@ -111,7 +115,7 @@ class FieldChoicePlugin(DatabrowsePlugin):
else:
# fall back to default
paginator = Paginator(obj_list, items_per_page[0])
page = request.GET.get('page')
try:
obj_list_page = paginator.page(page)
@@ -121,9 +125,9 @@ class FieldChoicePlugin(DatabrowsePlugin):
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page.
obj_list_page = paginator.page(paginator.num_pages)
if value is not None:
return render_to_response(
return render(request,
'databrowse/fieldchoice_detail.html',
{
'root_url': self.site.root_url,
@@ -132,10 +136,10 @@ class FieldChoicePlugin(DatabrowsePlugin):
'value': value,
'object_list': obj_list_page,
'items_per_page': items_per_page,
}, context_instance=RequestContext(request)
}
)

return render_to_response(
return render(request,
'databrowse/fieldchoice_list.html',
{
'root_url': self.site.root_url,
@@ -143,5 +147,5 @@ class FieldChoicePlugin(DatabrowsePlugin):
'field': easy_field,
'object_list': obj_list_page,
'items_per_page': items_per_page,
}, context_instance=RequestContext(request)
}
)

+ 9
- 6
django_databrowse/plugins/objects.py View File

@@ -2,9 +2,12 @@ from django import http
from django.core.exceptions import ObjectDoesNotExist
from django_databrowse.datastructures import EasyModel
from django_databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.template import RequestContext
import urlparse
try:
import urlparse
except ImportError:
import urllib.parse as urlparse

class ObjectDetailPlugin(DatabrowsePlugin):
def model_view(self, request, model_databrowse, url):
@@ -20,14 +23,14 @@ class ObjectDetailPlugin(DatabrowsePlugin):
)
try:
obj = easy_model.object_by_pk(url)
except ObjectDoesNotExist, e:
except ObjectDoesNotExist:
raise http.Http404('Id not found')
except ValueError, e:
except ValueError:
raise http.Http404('Invalid format key provided')
return render_to_response(
return render(request,
'databrowse/object_detail.html',
{
'object': obj,
'root_url': model_databrowse.site.root_url
}, context_instance=RequestContext(request)
}
)

+ 5
- 6
django_databrowse/sites.py View File

@@ -2,10 +2,10 @@ from django import http
try:
from django.apps import apps
get_model = apps.get_model
except ImportError, e:
except ImportError:
from django.db.models import get_model

from django.shortcuts import render_to_response
from django.shortcuts import render
from django.template import RequestContext
from django.utils.safestring import mark_safe
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
@@ -99,7 +99,7 @@ class ModelDatabrowse(object):
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page.
obj_list_page = paginator.page(paginator.num_pages)
return render_to_response(
return render(request,
'databrowse/model_detail.html',
{
'model': easy_model,
@@ -107,7 +107,7 @@ class ModelDatabrowse(object):
'plugin_html': html_snippets,
'object_list': obj_list_page,
'items_per_page': items_per_page,
}, context_instance=RequestContext(request)
}
)


@@ -165,10 +165,9 @@ class DatabrowseSite(object):

def index(self, request):
m_list = [EasyModel(self, m) for m in self.registry.keys()]
return render_to_response(
return render(request,
'databrowse/homepage.html',
{'model_list': m_list, 'root_url': self.root_url},
context_instance=RequestContext(request)
)

def model_page(self, request, app_label, model_name, rest_of_url=None):

+ 2
- 2
django_databrowse/tests/__init__.py View File

@@ -28,5 +28,5 @@ class YetAnotherModel(models.Model):
class SomeInheritedModel(SomeModel):
special = models.CharField(max_length=30)

from datastructures import *
from sites import *
from .datastructures import *
from .sites import *

+ 1
- 1
django_databrowse/tests/datastructures.py View File

@@ -55,7 +55,7 @@ class EasyModelTest(TestCase):
ei_child = EasyInstance(EasyModel(django_databrowse.site,
SomeInheritedModel), child)
self.assertEqual(
ei.related_objects().next()['object_list'][0].instance,
next(ei.related_objects())['object_list'][0].instance,
ei_child.instance)

def test_model_inheritance_no_child(self):

+ 5
- 6
django_databrowse/views.py View File

@@ -1,5 +1,5 @@
from django.http import Http404
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.template import RequestContext
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

@@ -9,10 +9,9 @@ from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def choice_list(request, app_label, model_name, field_name, models):
m, f = lookup_field(app_label, model_name, field_name, models)
return render_to_response(
return render(request,
'databrowse/choice_list.html',
{'model': m, 'field': f},
context_instance=RequestContext(request)
)

def choice_detail(request, app_label, model_name, field_name,
@@ -30,7 +29,7 @@ def choice_detail(request, app_label, model_name, field_name,
else:
# fall back to default
paginator = Paginator(obj_list, items_per_page[0])
page = request.GET.get('page')
try:
obj_list_page = paginator.page(page)
@@ -41,7 +40,7 @@ def choice_detail(request, app_label, model_name, field_name,
# If page is out of range (e.g. 9999), deliver last page.
obj_list_page = paginator.page(paginator.num_pages)

return render_to_response(
return render(request,
'databrowse/choice_detail.html',
{
'model': m,
@@ -49,5 +48,5 @@ def choice_detail(request, app_label, model_name, field_name,
'value': label,
'object_list': obj_list_page,
'items_per_page': items_per_page,
}, context_instance=RequestContext(request)
}
)

+ 5
- 1
run_tests.py View File

@@ -22,7 +22,11 @@ def main():
),
TEST_RUNNER='django.test.runner.DiscoverRunner',
MIDDLEWARE_CLASSES=[],
ROOT_URLCONF='test_urls'
ROOT_URLCONF='test_urls',
TEMPLATES=[{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
}]
)

django.setup()

Loading…
Cancel
Save