# Path: privagal/timeline/models.py
# class Timeline(AuthTokenPageMixin, Page):
# 
#     parent_page_types = []
#     subpage_types = ['gallery.Gallery']
# 
#     paginate_by = 20
# 
#     class Meta:
#         db_table = 'privagal_timeline'
# 
#     def get_context(self, request, *args, **kwargs):
#         context = super(Timeline, self).get_context(request, *args, **kwargs)
# 
#         search_query = request.GET.get('query', None)
#         context['search_query'] = search_query
# 
#         paginator, page = self.get_galleries(request, search_query)
#         context['page_obj'] = page
#         context['object_list'] = page.object_list
#         context['is_paginated'] = page.has_other_pages()
# 
#         return context
# 
#     def get_galleries(self, request, search_query):
#         qs = (
#             Gallery.objects
#             .live()
#             .prefetch_related(
#                 Prefetch(
#                     'images',
#                     queryset=ImageGallery.objects.select_related('image'),
#                 )
#             )
#             .order_by('-date')
#         )
# 
#         if search_query:
#             qs = qs.search(search_query, order_by_relevance=False)
# 
#         return paginate(request, qs, per_page=self.paginate_by)
# 
# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()


from privagal.timeline.models import Timeline
from .models import Token
# -*- coding: utf-8 -*-
from __future__ import unicode_literals




class AuthTokenMiddleware(object):

    def process_view(self, request, view_func, view_args, view_kwargs):
        key = request.GET.get('token', None)
        if key is None:
            return

        try:
            Token.objects.get(key=key)

# Path: privagal/timeline/models.py
# class Timeline(AuthTokenPageMixin, Page):
# 
#     parent_page_types = []
#     subpage_types = ['gallery.Gallery']
# 
#     paginate_by = 20
# 
#     class Meta:
#         db_table = 'privagal_timeline'
# 
#     def get_context(self, request, *args, **kwargs):
#         context = super(Timeline, self).get_context(request, *args, **kwargs)
# 
#         search_query = request.GET.get('query', None)
#         context['search_query'] = search_query
# 
#         paginator, page = self.get_galleries(request, search_query)
#         context['page_obj'] = page
#         context['object_list'] = page.object_list
#         context['is_paginated'] = page.has_other_pages()
# 
#         return context
# 
#     def get_galleries(self, request, search_query):
#         qs = (
#             Gallery.objects
#             .live()
#             .prefetch_related(
#                 Prefetch(
#                     'images',
#                     queryset=ImageGallery.objects.select_related('image'),
#                 )
#             )
#             .order_by('-date')
#         )
# 
#         if search_query:
#             qs = qs.search(search_query, order_by_relevance=False)
# 
#         return paginate(request, qs, per_page=self.paginate_by)
# 
# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()


# Path: privagal/core/middleware.py
from privagal.timeline.models import Timeline
from .models import Token

# -*- coding: utf-8 -*-
from __future__ import unicode_literals




class AuthTokenMiddleware(object):

    def process_view(self, request, view_func, view_args, view_kwargs):
        key = request.GET.get('token', None)
        if key is None:
            return

        try:
            Token.objects.get(key=key)

        except Token.DoesNotExist: