Skip to content

Commit

Permalink
added VkontakteTimelineManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ramusus committed Feb 5, 2014
1 parent bd898e5 commit e869309
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vkontakte_api/__init__.py
@@ -1,2 +1,2 @@
VERSION = (0, 5, 2)
VERSION = (0, 5, 3)
__version__ = '.'.join(map(str, VERSION))
35 changes: 35 additions & 0 deletions vkontakte_api/models.py
Expand Up @@ -198,6 +198,41 @@ def parse_response_list(self, response_list, extra_fields=None):
return instances


class VkontakteTimelineManager(VkontakteManager):
'''
Manager class, child of VkontakteManager for fetching objects with arguments `after`, `before`
'''
@transaction.commit_on_success
def fetch(self, *args, **kwargs):
'''
Retrieve and save object to local DB
Return queryset with respect to parameters:
* 'after' - excluding all items before.
* 'before' - excluding all items after.
'''
after = kwargs.pop('after', None)
before = kwargs.pop('before', None)

result = self.get(*args, **kwargs)
if isinstance(result, list):
instances = self.model.objects.none()
for instance in result:

if after and after > getattr(instance, 'date'):
break

if before and before < getattr(instance, 'date'):
continue

instance = self.get_or_create_from_instance(instance)
instances |= instance.__class__.objects.filter(pk=instance.pk)
return instances
elif isinstance(result, QuerySet):
return result
else:
return self.get_or_create_from_instance(result)


class VkontakteModel(models.Model):
class Meta:
abstract = True
Expand Down

0 comments on commit e869309

Please sign in to comment.