# Path: ranking_engine/decorators.py
# def account_required(handler_method):
# 
#     def check_account(self, *args, **kargs):
#         account = self.get_account()
#         if not account:
#             self.redirect(self.url("signup"))
#             return
#         else:
#             handler_method(self, *args, **kargs)
# 
#     return check_account
# 
# def login_required(handler_method):
#   """A decorator to require that a user be logged in to access a handler.
# 
#   To use it, decorate your get() method like this:
# 
#     @login_required
#     def get(self):
#       user = users.get_current_user(self)
#       self.response.out.write('Hello, ' + user.nickname())
# 
#   We will redirect to a login page if the user is not logged in. We always
#   redirect to the request URI, and Google Accounts only redirects back as a GET
#   request, so this should not be used for POSTs.
#   """
#   def check_login(self, *args, **kargs):
#     user = users.get_current_user()
#     if not user:
#         if self.request.method != 'GET':
#             callback = self.url('home')
#         else:
#             callback = self.request.uri
#         self.redirect(users.create_login_url(callback))
#         return
#     else:
#         handler_method(self, *args, **kargs)
#   return check_login
# 
# Path: ranking_engine/forms.py
# class SiteForm(djangoforms.ModelForm):
#     class Meta:
# class KeywordForm(djangoforms.ModelForm):
#     class Meta:
# 
# 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):


import os
import time
import datetime
import logging
from config import TEMPLATES_DIR
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext.db import djangoforms
from google.appengine.ext import db
from google.appengine.api import memcache
from pages import CommonHandler
from ranking_engine.decorators import account_required, login_required
from ranking_engine import forms
from ranking_engine import models






class AddSiteHandler(CommonHandler):

    @login_required
    @account_required
    def get(self):
        if self.account.remaining_sites() <= 0:
            self.redirect(self.url("dashboard"))
            return
        path = os.path.join(TEMPLATES_DIR, 'site_add.html')
        template_values = self.get_common_template_values()
        template_values.update({
                'form_action' : self.url("add_site"),
                'form' : forms.SiteForm(),
                'cancel_url' : self.url("dashboard")
                });
        self.response.out.write(template.render(path, template_values))

    @login_required
    @account_required
    def post(self):
        if self.account.remaining_sites() <= 0:
            self.redirect(self.url("dashboard"))
            return
        # instance param looks ugly, but it's the only solution i found to define a parent entity with a form

# Path: ranking_engine/decorators.py
# def account_required(handler_method):
# 
#     def check_account(self, *args, **kargs):
#         account = self.get_account()
#         if not account:
#             self.redirect(self.url("signup"))
#             return
#         else:
#             handler_method(self, *args, **kargs)
# 
#     return check_account
# 
# def login_required(handler_method):
#   """A decorator to require that a user be logged in to access a handler.
# 
#   To use it, decorate your get() method like this:
# 
#     @login_required
#     def get(self):
#       user = users.get_current_user(self)
#       self.response.out.write('Hello, ' + user.nickname())
# 
#   We will redirect to a login page if the user is not logged in. We always
#   redirect to the request URI, and Google Accounts only redirects back as a GET
#   request, so this should not be used for POSTs.
#   """
#   def check_login(self, *args, **kargs):
#     user = users.get_current_user()
#     if not user:
#         if self.request.method != 'GET':
#             callback = self.url('home')
#         else:
#             callback = self.request.uri
#         self.redirect(users.create_login_url(callback))
#         return
#     else:
#         handler_method(self, *args, **kargs)
#   return check_login
# 
# Path: ranking_engine/forms.py
# class SiteForm(djangoforms.ModelForm):
#     class Meta:
# class KeywordForm(djangoforms.ModelForm):
#     class Meta:
# 
# 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: handlers/sites.py
import os
import time
import datetime
import logging
from config import TEMPLATES_DIR
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext.db import djangoforms
from google.appengine.ext import db
from google.appengine.api import memcache
from pages import CommonHandler
from ranking_engine.decorators import account_required, login_required
from ranking_engine import forms
from ranking_engine import models







class AddSiteHandler(CommonHandler):

    @login_required
    @account_required
    def get(self):
        if self.account.remaining_sites() <= 0:
            self.redirect(self.url("dashboard"))
            return
        path = os.path.join(TEMPLATES_DIR, 'site_add.html')
        template_values = self.get_common_template_values()
        template_values.update({
                'form_action' : self.url("add_site"),
                'form' : forms.SiteForm(),
                'cancel_url' : self.url("dashboard")
                });
        self.response.out.write(template.render(path, template_values))

    @login_required
    @account_required
    def post(self):
        if self.account.remaining_sites() <= 0:
            self.redirect(self.url("dashboard"))
            return
        # instance param looks ugly, but it's the only solution i found to define a parent entity with a form

        form = forms.SiteForm(data=self.request.POST,