# Path: gogogo/models/loaders.py
# class ListLoader:
#     """
#     Load all entity of a model from memcache or bigtable
#     """
#     
#     cache_key_prefix = "gogogo_list_loader_"
#     
#     def __init__(self,model):
#         self.model = model
#         self.data = None
# 
#     def get_cache_key(self,num):
#         return "%s_%s_%d" % (ListLoader.cache_key_prefix , self.model.kind() , num)
#         
#     def get_info_cache_key(self):
#         return "%s_info_%s" % (ListLoader.cache_key_prefix , self.model.kind() )
# 
#     def load(self,batch_size=1000):
#         if self.data != None: #already loaded
#             return
#             
#         n = 0            
#         batch = self.load_batch(n,batch_size)
#         
#         while len(batch) == batch_size:
#             n+=1
#             batch += self.load_batch(n , batch_size , batch[-1].key() )
#         
#         self.data = batch
#         
#         memcache.add(self.get_info_cache_key(), len(self.data) , _default_cache_time * 2)
#         
#         return self.data
# 
#     def load_batch(self,num,limit = 1000,key = None):
#         cache_key = self.get_cache_key(num)
#         cache = memcache.get(cache_key)
#         if cache == None:
#             if key:
#                 entities = self.model.all().filter('__key__ >',key).fetch(limit)
#             else:
#                 entities = self.model.all().fetch(limit)
#             
#             cache = [entity for entity in entities]
#             memcache.set(cache_key, cache, _default_cache_time)
#         
#         ret = cache
#         return ret
#                 
#     def get_data(self):
#         return self.data
#         
#     def remove_cache(self,limit = 1000):
#         count = memcache.get(self.get_info_cache_key())
#         if count == None:
#             count = 1
#             
#         i = 0
#         while count >0:            
#             cache_key = self.get_cache_key(i)
#             memcache.delete(cache_key)
#             count -= limit


from django.http import HttpResponse
from django.http import Http404
from django.template import Context, loader , RequestContext
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django import forms
from django.forms import ModelForm
from ragendja.auth.decorators import staff_only
from ragendja.template import render_to_response
from gogogo.models import *
from ragendja.dbutils import get_object_or_404
from django.core.urlresolvers import reverse
from gogogo.models.loaders import ListLoader
import cgi
import logging

#from gogogo.views.db import reverse as db_reverse


def search(request):
    """
    Route searching
    """	
    error = None
    agency = None
    if "agency" in request.GET:
        agency_id = request.GET['agency']
        agency = db.Key.from_path(Agency.kind(),agency_id)

    if "keyword" not in request.GET:
        return render_to_response( 
            request,
            'gogogo/route/search.html'
            ,{ 
                'page_title': _("Route searching"),
                'result' : [],
                'error' : _("Error! No keyword provided!")
               })
        
    keyword = request.GET['keyword']
    keyword = keyword.lower()


# Path: gogogo/models/loaders.py
# class ListLoader:
#     """
#     Load all entity of a model from memcache or bigtable
#     """
#     
#     cache_key_prefix = "gogogo_list_loader_"
#     
#     def __init__(self,model):
#         self.model = model
#         self.data = None
# 
#     def get_cache_key(self,num):
#         return "%s_%s_%d" % (ListLoader.cache_key_prefix , self.model.kind() , num)
#         
#     def get_info_cache_key(self):
#         return "%s_info_%s" % (ListLoader.cache_key_prefix , self.model.kind() )
# 
#     def load(self,batch_size=1000):
#         if self.data != None: #already loaded
#             return
#             
#         n = 0            
#         batch = self.load_batch(n,batch_size)
#         
#         while len(batch) == batch_size:
#             n+=1
#             batch += self.load_batch(n , batch_size , batch[-1].key() )
#         
#         self.data = batch
#         
#         memcache.add(self.get_info_cache_key(), len(self.data) , _default_cache_time * 2)
#         
#         return self.data
# 
#     def load_batch(self,num,limit = 1000,key = None):
#         cache_key = self.get_cache_key(num)
#         cache = memcache.get(cache_key)
#         if cache == None:
#             if key:
#                 entities = self.model.all().filter('__key__ >',key).fetch(limit)
#             else:
#                 entities = self.model.all().fetch(limit)
#             
#             cache = [entity for entity in entities]
#             memcache.set(cache_key, cache, _default_cache_time)
#         
#         ret = cache
#         return ret
#                 
#     def get_data(self):
#         return self.data
#         
#     def remove_cache(self,limit = 1000):
#         count = memcache.get(self.get_info_cache_key())
#         if count == None:
#             count = 1
#             
#         i = 0
#         while count >0:            
#             cache_key = self.get_cache_key(i)
#             memcache.delete(cache_key)
#             count -= limit


# Path: gogogo/views/db/route.py
from django.http import HttpResponse
from django.http import Http404
from django.template import Context, loader , RequestContext
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django import forms
from django.forms import ModelForm
from ragendja.auth.decorators import staff_only
from ragendja.template import render_to_response
from gogogo.models import *
from ragendja.dbutils import get_object_or_404
from django.core.urlresolvers import reverse
from gogogo.models.loaders import ListLoader
import cgi
import logging


#from gogogo.views.db import reverse as db_reverse


def search(request):
    """
    Route searching
    """	
    error = None
    agency = None
    if "agency" in request.GET:
        agency_id = request.GET['agency']
        agency = db.Key.from_path(Agency.kind(),agency_id)

    if "keyword" not in request.GET:
        return render_to_response( 
            request,
            'gogogo/route/search.html'
            ,{ 
                'page_title': _("Route searching"),
                'result' : [],
                'error' : _("Error! No keyword provided!")
               })
        
    keyword = request.GET['keyword']
    keyword = keyword.lower()


    route_list_loader = ListLoader(Route)