Fixed weekdays python 3 support, fixed a bug in batch fetch

This commit is contained in:
Konsta Vesterinen
2013-09-17 07:48:51 +03:00
parent f6f0963705
commit e3a8cce8d9
2 changed files with 17 additions and 10 deletions

View File

@@ -54,6 +54,8 @@ class Path(object):
for entity in entities: for entity in entities:
related_entities.extend(getattr(entity, attrs[0])) related_entities.extend(getattr(entity, attrs[0]))
if not related_entities:
return
subpath = '.'.join(attrs[1:]) subpath = '.'.join(attrs[1:])
return Path.parse(related_entities, subpath, populate_backrefs) return Path.parse(related_entities, subpath, populate_backrefs)
else: else:
@@ -146,8 +148,9 @@ def batch_fetch(entities, *attr_paths):
if entities: if entities:
for path in attr_paths: for path in attr_paths:
fetcher = fetcher_factory(entities, path) fetcher = fetcher_factory(entities, path)
fetcher.fetch() if fetcher:
fetcher.populate() fetcher.fetch()
fetcher.populate()
def fetcher_factory(entities, path): def fetcher_factory(entities, path):
@@ -159,13 +162,17 @@ def fetcher_factory(entities, path):
if isinstance(path, CompositePath): if isinstance(path, CompositePath):
fetchers = [] fetchers = []
for path in path.paths: for path in path.paths:
fetchers.append( path = Path.parse(entities, path, populate_backrefs)
Path.parse(entities, path, populate_backrefs).fetcher if path:
) fetchers.append(
path.fetcher
)
return CompositeFetcher(*fetchers) return CompositeFetcher(*fetchers)
else: else:
return Path.parse(entities, path, populate_backrefs).fetcher path = Path.parse(entities, path, populate_backrefs)
if path:
return path.fetcher
class CompositeFetcher(object): class CompositeFetcher(object):

View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import six
from sqlalchemy import types from sqlalchemy import types
from sqlalchemy.dialects.postgresql import BIT from sqlalchemy.dialects.postgresql import BIT
import six
get_day_names = None get_day_names = None
try: try:
from babel.dates import get_day_names from babel.dates import get_day_names
@@ -75,7 +75,7 @@ class WeekDay(object):
class WeekDays(object): class WeekDays(object):
def __init__(self, bit_string_or_week_days): def __init__(self, bit_string_or_week_days):
if isinstance(bit_string_or_week_days, six.text_type): if isinstance(bit_string_or_week_days, six.string_types):
self._days = set() self._days = set()
if len(bit_string_or_week_days) != WeekDay.NUM_WEEK_DAYS: if len(bit_string_or_week_days) != WeekDay.NUM_WEEK_DAYS:
@@ -100,7 +100,7 @@ class WeekDays(object):
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, WeekDays): if isinstance(other, WeekDays):
return self._days == other._days return self._days == other._days
elif isinstance(other, six.text_type): elif isinstance(other, six.string_types):
return self.as_bit_string() == other return self.as_bit_string() == other
else: else:
return NotImplemented return NotImplemented
@@ -138,7 +138,7 @@ class WeekDaysType(types.TypeDecorator):
if isinstance(value, WeekDays): if isinstance(value, WeekDays):
return value.as_bit_string() return value.as_bit_string()
if isinstance(value, six.text_type): if isinstance(value, six.string_types):
return value return value
def process_result_value(self, value, dialect): def process_result_value(self, value, dialect):