# Path: ranking_engine/models.py
# def serialize_entities(models):
# def deserialize_entities(data):
#     def get_cache_namespace(self):
#     def update_cache_namespace(self):
#     def _get_cache_namespace_key(self):
#     def _init_cache_namespace(self):
#     def get_cache_parent(self):
#     def instance_cache_key_by_user(self):
#     def cache_key_by_user(cls, user):
#     def delete(self):
#     def put(self):
#     def cascade_delete(cls, key):
#     def remaining_sites(self):
#     def remaining_keywords(self):
#     def sites(self):
#     def get_user_account(cls, user):
#     def get_cache_parent(self):
#     def put(self):
#     def delete(self):
#     def cascade_delete(cls, key):
#     def keywords(self):
#     def report_url(self):
#     def delete_url(self):
#     def get_cache_parent(self):
#     def delete(self):
#     def put(self):
#     def delete_url(self):
#     def graph_url(self):
#     def cascade_delete(cls, key):
#     def search_engines(self):
#     def get_cache_parent(self):
#     def delete(self):
#     def put(self):
#     def cascade_delete(cls, key):
#     def search_engine(self):
#     def last_log(self):
#     def full_log(self):
#     def variation(self):
#     def enqueue_update_rank_task(self):
#     def enqueue_update_rank_task_key(cls, key):
#     def plot_data(self):
#     def get_cache_parent(self):
#     def delete(self):
#     def put(self):
# class BaseModel(db.Model):
# class Account(BaseModel):
# class Site(BaseModel):
# class Keyword(BaseModel):
# class KeywordSearchEngine(BaseModel):
# class KeywordRankLog(BaseModel):
# 
# Path: ranking_engine/ranking.py
# def get_ranking(url, keyword, server):
# def _google_ajax_api_ranking(url, keyword, server_info):
# def _bing_api_ranking(url, keyword, server_info):


from google.appengine.ext import webapp
from google.appengine.api.labs import taskqueue
from google.appengine.ext import db
from ranking_engine import models
from ranking_engine import ranking
import datetime
import time
import logging
import config
        kw_se = models.KeywordSearchEngine.get(db.Key(key))
        if not kw_se:
            logging.warning("[UpdateKeywordSearchEngineRanksHandler] Ignoring task, unknown Kw_Se with key <" + key + ">");
            return

        if kw_se.last_update and kw_se.last_update.date() >= datetime.date.today():
            logging.info("[UpdateKeywordSearchEngineRanksHandler] Ignoring task, kw_se <" + key + "> is up to date");

        site = kw_se.keyword.site;
        if not site:
            logging.warning("[UpdateKeywordSearchEngineRanksHandler] Ignoring task: Keyword with key <" + unicode(kw_se.keyword.key()) + "> has no site !");
            return

        # Fetch Search Engine Results
        result = ranking.get_ranking(site.url, kw_se.keyword.keyword, kw_se.server)

        db.run_in_transaction(UpdateKeywordSearchEngineRanksHandler.update_trx,
                              self, kw_se, result)



# Cascade delete on Site when deleting an Account
class DeleteSitesHandler(webapp.RequestHandler):
    def post(self, *ar, **kw):
        key = self.request.get('account_key')
        batch_size = 10
        query = models.Site.all(keys_only=True).filter('account = ', db.Key(key))
        count = query.count()
        site_keys = query.fetch(batch_size)
        for site_key in site_keys:

# Path: ranking_engine/models.py
# def serialize_entities(models):
# def deserialize_entities(data):
#     def get_cache_namespace(self):
#     def update_cache_namespace(self):
#     def _get_cache_namespace_key(self):
#     def _init_cache_namespace(self):
#     def get_cache_parent(self):
#     def instance_cache_key_by_user(self):
#     def cache_key_by_user(cls, user):
#     def delete(self):
#     def put(self):
#     def cascade_delete(cls, key):
#     def remaining_sites(self):
#     def remaining_keywords(self):
#     def sites(self):
#     def get_user_account(cls, user):
#     def get_cache_parent(self):
#     def put(self):
#     def delete(self):
#     def cascade_delete(cls, key):
#     def keywords(self):
#     def report_url(self):
#     def delete_url(self):
#     def get_cache_parent(self):
#     def delete(self):
#     def put(self):
#     def delete_url(self):
#     def graph_url(self):
#     def cascade_delete(cls, key):
#     def search_engines(self):
#     def get_cache_parent(self):
#     def delete(self):
#     def put(self):
#     def cascade_delete(cls, key):
#     def search_engine(self):
#     def last_log(self):
#     def full_log(self):
#     def variation(self):
#     def enqueue_update_rank_task(self):
#     def enqueue_update_rank_task_key(cls, key):
#     def plot_data(self):
#     def get_cache_parent(self):
#     def delete(self):
#     def put(self):
# class BaseModel(db.Model):
# class Account(BaseModel):
# class Site(BaseModel):
# class Keyword(BaseModel):
# class KeywordSearchEngine(BaseModel):
# class KeywordRankLog(BaseModel):
# 
# Path: ranking_engine/ranking.py
# def get_ranking(url, keyword, server):
# def _google_ajax_api_ranking(url, keyword, server_info):
# def _bing_api_ranking(url, keyword, server_info):


# Path: handlers/tasks.py
from google.appengine.ext import webapp
from google.appengine.api.labs import taskqueue
from google.appengine.ext import db
from ranking_engine import models
from ranking_engine import ranking
import datetime
import time
import logging
import config

        kw_se = models.KeywordSearchEngine.get(db.Key(key))
        if not kw_se:
            logging.warning("[UpdateKeywordSearchEngineRanksHandler] Ignoring task, unknown Kw_Se with key <" + key + ">");
            return

        if kw_se.last_update and kw_se.last_update.date() >= datetime.date.today():
            logging.info("[UpdateKeywordSearchEngineRanksHandler] Ignoring task, kw_se <" + key + "> is up to date");

        site = kw_se.keyword.site;
        if not site:
            logging.warning("[UpdateKeywordSearchEngineRanksHandler] Ignoring task: Keyword with key <" + unicode(kw_se.keyword.key()) + "> has no site !");
            return

        # Fetch Search Engine Results
        result = ranking.get_ranking(site.url, kw_se.keyword.keyword, kw_se.server)

        db.run_in_transaction(UpdateKeywordSearchEngineRanksHandler.update_trx,
                              self, kw_se, result)



# Cascade delete on Site when deleting an Account
class DeleteSitesHandler(webapp.RequestHandler):
    def post(self, *ar, **kw):
        key = self.request.get('account_key')
        batch_size = 10
        query = models.Site.all(keys_only=True).filter('account = ', db.Key(key))
        count = query.count()
        site_keys = query.fetch(batch_size)
        for site_key in site_keys:

            models.Site.cascade_delete(site_key)