Skip to content

Commit

Permalink
refactor handlers structure for #52
Browse files Browse the repository at this point in the history
  • Loading branch information
nzinov committed May 17, 2014
1 parent 9f856e0 commit 1a5f884
Show file tree
Hide file tree
Showing 33 changed files with 247 additions and 272 deletions.
85 changes: 85 additions & 0 deletions handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
from google.appengine.api import users
import webapp2
from environment import JINJA_ENVIRONMENT
from objects.user_devices import get_device_and_user, User

__author__ = 'nikolay'


class GenericHandler(webapp2.RequestHandler):
def __init__(self, *args, **kwargs):
super(GenericHandler, self).__init__(*args, **kwargs)


class WebRequestHandler(GenericHandler):
user_key = None
user = None

def dispatch(self):
self.user = users.get_current_user()
if self.user is None:
if self.login_required:
self.redirect(users.create_login_url(self.request.url))
return
else:
self.user_key = (User.query(User.user_id == self.user.user_id()).get(keys_only=True) or
User(user_id=self.user.user_id(), user_object=self.user).put())
webapp2.RequestHandler.dispatch(self)

def draw_page(self, template_name, **render_data):
template = JINJA_ENVIRONMENT.get_template('templates/{}.html'.format(template_name))
render_data['user_link'] = (users.create_logout_url('/') if self.user
else users.create_login_url(self.request.url))
if self.user:
render_data['user_email'] = users.get_current_user().email()
else:
render_data['user_email'] = None
render_data['is_admin'] = users.is_current_user_admin()
self.response.write(template.render(render_data))

def __init__(self, *args, **kwargs):
self.login_required = False
super(WebRequestHandler, self).__init__(*args, **kwargs)


class AdminRequestHandler(WebRequestHandler):
def dispatch(self):
if users.get_current_user() is None:
self.redirect(users.create_login_url())
if not users.is_current_user_admin():
self.redirect('/')
else:
WebRequestHandler.dispatch(self)

def __init__(self, *args, **kwargs):
super(AdminRequestHandler, self).__init__(*args, **kwargs)
self.login_required = True


class APIRequestHandler(GenericHandler):
def __init__(self, *args, **kwargs):
super(APIRequestHandler, self).__init__(*args, **kwargs)


class AuthorizedAPIRequestHandler(APIRequestHandler):
def __init__(self, *args, **kwargs):
super(APIRequestHandler, self).__init__(*args, **kwargs)
self.device_id = None
self.device_key = None
self.user_key = None

def dispatch(self):
self.device_id = (self.request.route_kwargs.get('device_id', None) or
self.request.headers.get('TheHat-Device-Identity', None))
if self.device_id is None:
self.response.headers.add("WWW-Authenticate", "device-id")
self.abort(401)
self.device_key, self.user_key = get_device_and_user(self.device_id)
super(APIRequestHandler, self).dispatch()


class ServiceRequestHandler(GenericHandler):
def __init__(self, *args, **kwargs):
super(ServiceRequestHandler, self).__init__(*args, **kwargs)

def dispatch(self):
super(ServiceRequestHandler, self).dispatch()
7 changes: 2 additions & 5 deletions handlers/admin_page_handler.py → handlers/admin_page.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
__author__ = 'ivan'
from google.appengine.api import users
import webapp2
from handlers import AdminRequestHandler

from environment import JINJA_ENVIRONMENT
from base_handlers.admin_request_handler import AdminRequestHandler
__author__ = 'ivan'


class AdminPage(AdminRequestHandler):
Expand Down
20 changes: 0 additions & 20 deletions handlers/base_handlers/admin_request_handler.py

This file was deleted.

7 changes: 0 additions & 7 deletions handlers/base_handlers/generic_handler.py

This file was deleted.

13 changes: 0 additions & 13 deletions handlers/base_handlers/service_request_handler.py

This file was deleted.

43 changes: 0 additions & 43 deletions handlers/base_handlers/web_request_handler.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
__author__ = 'nikolay'
import json
from objects.user_devices import get_device_and_user

from handlers import APIRequestHandler, AuthorizedAPIRequestHandler
from objects.user_streams import UserStreams
from objects.dictionaries_packages import PackagesStream, PackageDictionary
from base_handlers.api_request_handlers import APIRequestHandler, AuthorizedAPIRequestHandler


class GetStreamsListHandler(APIRequestHandler):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from google.appengine.api import users
from handlers import AdminRequestHandler

from objects.dictionaries_packages import PackagesStream, PackageDictionary
from environment import *
from base_handlers.admin_request_handler import AdminRequestHandler


def get_streams():
Expand Down
3 changes: 2 additions & 1 deletion handlers/game_log_viewer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from handlers import AdminRequestHandler

__author__ = 'nikolay'
from handlers.base_handlers.admin_request_handler import AdminRequestHandler
from google.appengine.ext import ndb
import json

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

from google.appengine.ext import ndb

from handlers import AdminRequestHandler, AuthorizedAPIRequestHandler
from objects.global_dictionary_word import GlobalDictionaryWord
from objects.complained_word import ComplainedWord
from handlers.base_handlers.admin_request_handler import AdminRequestHandler
from handlers.base_handlers.api_request_handlers import AuthorizedAPIRequestHandler
from objects.user_devices import get_device_and_user


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#TODO: this file is obsolete and must be rewritten
from handlers import AdminRequestHandler

__author__ = 'ivan'

import json
import time

from environment import JINJA_ENVIRONMENT
from objects.global_dictionary_word import GlobalDictionaryWord
from handlers.base_handlers.admin_request_handler import AdminRequestHandler
from objects.GlobalDictionaryJSON import GlobalDictionaryJson


Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

__author__ = 'ivan'

import json

from handlers import AdminRequestHandler, ServiceRequestHandler
from google.appengine.api import taskqueue
from google.appengine.ext import ndb

from handlers.base_handlers.admin_request_handler import AdminRequestHandler
from handlers.base_handlers.service_request_handler import ServiceRequestHandler


class WordFrequency(ndb.Model):
word = ndb.StringProperty()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from google.appengine.ext import ndb
from handlers.base_handlers.admin_request_handler import AdminRequestHandler
from handlers import AdminRequestHandler
from objects.global_dictionary_word import GlobalDictionaryWord

__author__ = 'ivan'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from handlers import AdminRequestHandler

__author__ = 'nikolay'
from objects.global_dictionary_word import WordLookup
from handlers.base_handlers.admin_request_handler import AdminRequestHandler
import json
from google.appengine.ext import ndb


class AddLookups(AdminRequestHandler):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from handlers import AdminRequestHandler, APIRequestHandler, ServiceRequestHandler

__author__ = 'ivan'

import time
Expand All @@ -8,9 +10,6 @@

from objects.global_dictionary_word import GlobalDictionaryWord
from objects.GlobalDictionaryJSON import GlobalDictionaryJson
from handlers.base_handlers.api_request_handlers import APIRequestHandler
from handlers.base_handlers.admin_request_handler import AdminRequestHandler
from handlers.base_handlers.service_request_handler import ServiceRequestHandler


StrategyTypeChooseConstant = 200
Expand Down
6 changes: 3 additions & 3 deletions handlers/link_device.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from handlers import AuthorizedAPIRequestHandler, ServiceRequestHandler

__author__ = 'denspb'

from google.appengine.api import users
from google.appengine.api import taskqueue
from google.appengine.ext import ndb

from handlers.base_handlers.api_request_handlers import AuthorizedAPIRequestHandler
from handlers.base_handlers.service_request_handler import ServiceRequestHandler
from objects.user_devices import get_user
from handlers.userdictionary import merge_user_dictionary_data
from handlers.user_dictionary import merge_user_dictionary_data


class LinkDevice(AuthorizedAPIRequestHandler):
Expand Down
3 changes: 2 additions & 1 deletion handlers/log_n_res_handlers.py → handlers/log_saving.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from handlers import APIRequestHandler, AuthorizedAPIRequestHandler

__author__ = 'konstantin'
import json
import time
Expand All @@ -8,7 +10,6 @@
from objects.user_devices import get_device_and_user
from objects.pin_number import PinNumber
from objects.game_results_log import GameLog, Results, SavedGame
from base_handlers.api_request_handlers import APIRequestHandler, AuthorizedAPIRequestHandler


def make_timestamp():
Expand Down
6 changes: 1 addition & 5 deletions handlers/newsfeed_handlers.py → handlers/newsfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
# v 0.2


import os
import time
import webapp2

from google.appengine.ext import db
from webapp2_extras import json
from base_handlers.admin_request_handler import AdminRequestHandler
from base_handlers.api_request_handlers import APIRequestHandler


from handlers import AdminRequestHandler, APIRequestHandler
from environment import *


Expand Down
3 changes: 2 additions & 1 deletion handlers/pregame_handlers.py → handlers/pregame.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from handlers import AuthorizedAPIRequestHandler

__author__ = 'nikolay'

import random
Expand All @@ -6,7 +8,6 @@
from google.appengine.ext import ndb

from objects.pregame import PreGame, CurrentGame
from base_handlers.api_request_handlers import AuthorizedAPIRequestHandler


class PreGameCreateHandler(AuthorizedAPIRequestHandler):
Expand Down
1 change: 1 addition & 0 deletions handlers/service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'nikolay'
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from handlers import ServiceRequestHandler

__author__ = 'nikolay'
import hashlib

from google.appengine.ext import ndb
from google.appengine.api import taskqueue

from handlers.base_handlers.service_request_handler import ServiceRequestHandler
from objects.legacy_game_history import GameHistory


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from handlers import AdminRequestHandler, ServiceRequestHandler

__author__ = 'nikolay'


Expand All @@ -11,15 +13,13 @@
from google.appengine.api import taskqueue
from google.appengine.api import memcache

from objects.global_dictionary_word import GlobalDictionaryWord, WordLookup
from objects.global_dictionary_word import GlobalDictionaryWord
from environment import TRUESKILL_ENVIRONMENT
from objects.game_results_log import GameLog
from objects.legacy_game_history import GameHistory
from base_handlers.service_request_handler import ServiceRequestHandler
from base_handlers.admin_request_handler import AdminRequestHandler
from objects.total_statistics_object import *
from objects.unknown_word import UnknownWord
from handlers.statistics.game_len_prediction_handler import GameLength
from handlers.statistics.game_len_prediction import GameLength


class BadGameError(Exception):
Expand Down
Loading

0 comments on commit 1a5f884

Please sign in to comment.