python3.4 support

This commit is contained in:
Benjamin Dauvergne 2017-04-26 14:02:11 +02:00
parent 5379e247cb
commit 01c4b6f571
8 changed files with 31 additions and 26 deletions

View File

@ -79,7 +79,7 @@ setup(
name='zoo',
version=get_version(),
description='Manage datas and their relations',
long_description=file('README').read(),
long_description=open('README').read(),
author='Benjamin Dauvergne',
author_email='bdauvergne@entrouvert.com',
packages=find_packages(),
@ -102,6 +102,7 @@ setup(
'psycopg2',
'jsonschema',
'gadjo',
'six',
'djangorestframework<3.4',
'pytz',
],

View File

@ -37,7 +37,4 @@ def test_new_schema(db):
}
})
qs = Entity.objects.content_search(schema, address__street='chateau')
print qs.query
assert qs.count() == 1
for e in qs:
print e.similarity

View File

@ -19,6 +19,7 @@ usedevelop =
coverage: True
nocoverage: False
deps =
pip>8
pytest-flakes
pg: psycopg2
coverage

View File

@ -1,5 +1,7 @@
from operator import __add__, __or__
import functools
from django.db import models, connection
from django.db.models import F, Value
from django.db.models.query import QuerySet, Q
@ -47,20 +49,20 @@ class EntityQuerySet(QuerySet):
qs = qs.filter(schema=schema)
filters = []
connection.cursor().execute('SELECT SET_LIMIT(%s)', (limit,))
for key, value in kwargs.iteritems():
for key, value in kwargs.items():
filters.append(Q(**{
'content__' + key + '__unaccent__lower__trigram_similar':
Lower(Unaccent(Value(value))),
}))
qs = qs.filter(reduce(__or__, filters))
qs = qs.filter(functools.reduce(__or__, filters))
expressions = []
ordering = []
for key, value in kwargs.iteritems():
for key, value in kwargs.items():
ordering.append(Lower(Unaccent(JSONTextRef(F('content'), *key.split('__')))))
expressions.append(TrigramDistance(
Lower(Unaccent(JSONTextRef(F('content'), *key.split('__')))),
Lower(Unaccent(Value(value)))))
expression = reduce(__add__, expressions)
expression = functools.reduce(__add__, expressions)
qs = qs.annotate(similarity=expression / len(kwargs))
qs = qs.order_by('similarity', *ordering)
return qs
@ -71,7 +73,7 @@ class CommonData(models.Model):
if self.schema:
try:
schema_validator(self.schema.schema)(self.content)
except ValidationError, e:
except ValidationError as e:
raise ValidationError({'content': e})
def __unicode__(self):

View File

@ -23,7 +23,7 @@ class Command(BaseCommand):
def handle(self, *args, **options):
for schema in EntitySchema.objects.all():
if options['verbosity'] >= 1:
print 'Rebuilding index for', unicode(schema),
print('Rebuilding index for', unicode(schema), end=' ')
schema.rebuild_indexes()
if options['verbosity'] >= 1:
print ' Done.'
print(' Done.')

View File

@ -56,7 +56,7 @@ class CommonSchema(models.Model):
def rebuild_string_index(self, cursor, table, path):
expr = 'immutable_normalize((content%s))' % self.path_to_sql_expr(path)
key = md5(expr).hexdigest()[:8]
key = md5(expr.encode('utf-8')).hexdigest()[:8]
sql = ('CREATE INDEX zoo_entity_%s_gin_%s_dynamic_idx ON %s USING gin ((%s) '
' gin_trgm_ops) WHERE schema_id = %s' % (key, self.id, table, expr, self.id))
cursor.execute(sql)
@ -108,7 +108,7 @@ class CommonSchema(models.Model):
if self.caption_template:
try:
return eval(self.caption_template, {}, value.content)
except Exception, e:
except Exception as e:
return unicode(e)
return unicode(value.id)

View File

@ -23,4 +23,4 @@ from zoo.zoo_nanterre.utils import integrity_check
class Command(BaseCommand):
def handle(self, *args, **options):
for error in integrity_check():
print '!!!', error
print('!!!', error)

View File

@ -1,5 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import six
import functools
import sys
import re
import datetime
@ -106,7 +110,7 @@ def adresses(individu):
if relation.schema.slug != HABITE_REL:
continue
adresses.append((relation.right, relation))
adresses.sort(key=lambda (a, r): (not r.content.get('principale', False), a.id))
adresses.sort(key=lambda t: (not t[1].content.get('principale', False), t[0].id))
return adresses
@ -142,7 +146,7 @@ class PersonSearch(object):
applications = getattr(settings, 'ZOO_NANTERRE_APPLICATIONS', {})
def helper():
for application, value in applications.iteritems():
for application, value in applications.items():
yield application, value['name']
return list(helper())
@ -216,7 +220,7 @@ class PersonSearch(object):
filters.append(Q(id=individu_id))
for key, name in self.applications():
filters.append(Q(**{'content__cles_de_federation__%s' % key: identifier}))
q = reduce(Q.__or__, filters)
q = functools.reduce(Q.__or__, filters)
self.key_filters.append(q)
return self
@ -305,7 +309,7 @@ class PersonSearch(object):
@classmethod
def or_filters(self, filters):
return reduce(operator.__or__, filters, Q())
return functools.reduce(operator.__or__, filters, Q())
@classmethod
def add_age(cls, individu):
@ -418,7 +422,7 @@ class PersonSearch(object):
similarities.append(e)
if similarities:
qs = qs.annotate(similarity=reduce(operator.__add__, similarities) /
qs = qs.annotate(similarity=functools.reduce(operator.__add__, similarities) /
Value(len(similarities)))
qs = qs.filter(similarity__gte=self.limit)
qs = qs.order_by('-similarity', 'fullname')
@ -475,7 +479,7 @@ def integrity_check():
for rel in rels.filter(schema__slug=RESPONSABILITE_LEGALE_REL):
count_parent_rels[rel.right] += 1
for key, value in count_union_rels.iteritems():
for key, value in count_union_rels.items():
if value > 1:
if isinstance(key, tuple):
yield ("le couple %s / %s est en union plusieurs fois: %s"
@ -484,12 +488,12 @@ def integrity_check():
yield ("l'individu %s est en union avec plus d'une personne: %s"
% (key, value))
for key, value in count_parent_rels.iteritems():
for key, value in count_parent_rels.items():
if value > 2:
yield ("l'enfant %s a plus de deux parents: %s"
% (key, value))
for key, value in count_habite_rels.iteritems():
for key, value in count_habite_rels.items():
if key.content['statut_legal'] == 'majeur' and value < 1:
yield ("l'adulte %s n'a pas d'adresse" % key)
@ -506,7 +510,7 @@ def integrity_check():
def upper_dict(d):
'''Transform all string values in d to uppercase'''
for key, value in d.items():
if isinstance(value, unicode):
if isinstance(value, six.text_type):
d[key] = value.upper()
@ -560,13 +564,13 @@ class LoadDump(object):
def start(self, msg=None):
if msg and self.verbosity > 0:
print msg,
print(msg, end=' ')
sys.stdout.flush()
self.clock_stack.append(time.time())
def end(self):
if self.verbosity > 0:
print ' DONE (%s s)' % (time.time() - self.clock_stack.pop())
print(' DONE (%s s)' % (time.time() - self.clock_stack.pop()))
def log(msg=None):
def decorator(func):
@ -744,7 +748,7 @@ class LoadDump(object):
Entity.objects.bulk_create(adresse_batch)
relation_batch = []
for (a, b), is_primary in individu_adresse_mapping.iteritems():
for (a, b), is_primary in individu_adresse_mapping.items():
content = {
'principale': is_primary,
}