# Path: sequere/backends/base.py
# class BaseBackend(object):
#     def follow(self, from_instance, to_instance):
#         raise NotImplemented
# 
#     def unfollow(self, from_instance, to_instance):
#         raise NotImplemented
# 
#     def get_followers(self, instance):
#         raise NotImplemented
# 
#     def get_followings(self, instance):
#         raise NotImplemented
# 
#     def is_following(self, from_instance, to_instance):
#         raise NotImplemented
# 
#     def get_followings_count(self, instance):
#         raise NotImplemented
# 
#     def get_followers_count(self, instance):
#         raise NotImplemented
# 
#     def clear(self):
#         raise NotImplemented
# 
# Path: sequere/registry.py
# class SequereRegistry(dict):
#     def __init__(self):
#     def for_model(self, model):
#     def get_identifier(self, instance):
#     def identifiers(self):
#     def unregister(self, name):
#     def register(self, *args, **kwargs):
#     def _register_model(self, model, sequere=None,
#                         name=None, **kwargs):
#     def _register(self, sequere):
# def autodiscover():
# def register(*args, **kwargs):
# 
# Path: sequere/signals.py

# 
# Path: sequere/backends/database/query.py
# class DatabaseQuerySetTransformer(QuerySetTransformer):
#     def __init__(self, qs, count):
#         super(DatabaseQuerySetTransformer, self).__init__(qs, count)
# 
#         self.keys = []
# 
#     def order_by(self, key):
#         self.order_by = key
#         self.desc = False
#         self.sorting_key = key
# 
#         if key.startswith('-'):
#             self.desc = True
#             self.sorting_key = key[1:]
# 
#         self.keys.append(self.sorting_key)
# 
#         return self
# 
#     def aggregate_by(self, key):
#         self.aggregate_key = key
# 
#         self.keys.append(self.aggregate_key)
# 
#         return self
# 
#     def pivot_by(self, key):
#         self.pivot_key = key
# 
#         self.keys.append(self.pivot_key)
# 
#         return self
# 
#     def transform(self, qs):
#         values = qs[self.start:self.stop].values(*self.keys)
# 
#         identifier_ids = defaultdict(list)
# 
#         orders = {}
# 
#         for value in values:
#             identifier_ids[value[self.aggregate_key]].append(value[self.pivot_key])
#             orders[value[self.pivot_key]] = value[self.sorting_key]
# 
#         for identifier, ids in six.iteritems(identifier_ids):
#             model = registry.identifiers.get(identifier)
# 
#             results = model.objects.filter(pk__in=ids)
# 
#             for result in results:
#                 created = orders[result.pk]
# 
#                 del orders[result.pk]
# 
#                 orders[result] = created
# 
#         return sorted(orders.items(), key=itemgetter(1), reverse=self.desc)


from django.core.exceptions import ImproperlyConfigured
from django.db.models import Q
from sequere.backends.base import BaseBackend
from sequere.registry import registry
from sequere.signals import followed, unfollowed
from .query import DatabaseQuerySetTransformer
        from .models import Follow




class DatabaseBackend(BaseBackend):
    def __init__(self, *args, **kwargs):

        self.model = Follow

        if not self.model._meta.installed:
            raise ImproperlyConfigured(
                "The sequere.backends.database app isn't installed "
                "correctly. Make sure it's in your INSTALLED_APPS setting.")

    def _params(self, from_instance=None, to_instance=None):
        params = {}

        if from_instance:
            from_identifier = registry.get_identifier(from_instance)

            params.update({
                'from_identifier': from_identifier,
                'from_object_id': from_instance.pk,
            })

        if to_instance:

# Path: sequere/backends/base.py
# class BaseBackend(object):
#     def follow(self, from_instance, to_instance):
#         raise NotImplemented
# 
#     def unfollow(self, from_instance, to_instance):
#         raise NotImplemented
# 
#     def get_followers(self, instance):
#         raise NotImplemented
# 
#     def get_followings(self, instance):
#         raise NotImplemented
# 
#     def is_following(self, from_instance, to_instance):
#         raise NotImplemented
# 
#     def get_followings_count(self, instance):
#         raise NotImplemented
# 
#     def get_followers_count(self, instance):
#         raise NotImplemented
# 
#     def clear(self):
#         raise NotImplemented
# 
# Path: sequere/registry.py
# class SequereRegistry(dict):
#     def __init__(self):
#     def for_model(self, model):
#     def get_identifier(self, instance):
#     def identifiers(self):
#     def unregister(self, name):
#     def register(self, *args, **kwargs):
#     def _register_model(self, model, sequere=None,
#                         name=None, **kwargs):
#     def _register(self, sequere):
# def autodiscover():
# def register(*args, **kwargs):
# 
# Path: sequere/signals.py

# 
# Path: sequere/backends/database/query.py
# class DatabaseQuerySetTransformer(QuerySetTransformer):
#     def __init__(self, qs, count):
#         super(DatabaseQuerySetTransformer, self).__init__(qs, count)
# 
#         self.keys = []
# 
#     def order_by(self, key):
#         self.order_by = key
#         self.desc = False
#         self.sorting_key = key
# 
#         if key.startswith('-'):
#             self.desc = True
#             self.sorting_key = key[1:]
# 
#         self.keys.append(self.sorting_key)
# 
#         return self
# 
#     def aggregate_by(self, key):
#         self.aggregate_key = key
# 
#         self.keys.append(self.aggregate_key)
# 
#         return self
# 
#     def pivot_by(self, key):
#         self.pivot_key = key
# 
#         self.keys.append(self.pivot_key)
# 
#         return self
# 
#     def transform(self, qs):
#         values = qs[self.start:self.stop].values(*self.keys)
# 
#         identifier_ids = defaultdict(list)
# 
#         orders = {}
# 
#         for value in values:
#             identifier_ids[value[self.aggregate_key]].append(value[self.pivot_key])
#             orders[value[self.pivot_key]] = value[self.sorting_key]
# 
#         for identifier, ids in six.iteritems(identifier_ids):
#             model = registry.identifiers.get(identifier)
# 
#             results = model.objects.filter(pk__in=ids)
# 
#             for result in results:
#                 created = orders[result.pk]
# 
#                 del orders[result.pk]
# 
#                 orders[result] = created
# 
#         return sorted(orders.items(), key=itemgetter(1), reverse=self.desc)


# Path: sequere/backends/database/backend.py
from django.core.exceptions import ImproperlyConfigured
from django.db.models import Q
from sequere.backends.base import BaseBackend
from sequere.registry import registry
from sequere.signals import followed, unfollowed
from .query import DatabaseQuerySetTransformer
        from .models import Follow





class DatabaseBackend(BaseBackend):
    def __init__(self, *args, **kwargs):

        self.model = Follow

        if not self.model._meta.installed:
            raise ImproperlyConfigured(
                "The sequere.backends.database app isn't installed "
                "correctly. Make sure it's in your INSTALLED_APPS setting.")

    def _params(self, from_instance=None, to_instance=None):
        params = {}

        if from_instance:
            from_identifier = registry.get_identifier(from_instance)

            params.update({
                'from_identifier': from_identifier,
                'from_object_id': from_instance.pk,
            })

        if to_instance:

            to_identifier = registry.get_identifier(to_instance)