-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
27,376 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
__author__ = 'lmzqwer2' | ||
|
||
import tornado.web | ||
import json | ||
import config | ||
from BaseHttpHandler import BaseHttpHandler | ||
import models | ||
from models import FPGA | ||
|
||
|
||
def require_admin(handler_class): | ||
def wrap_execute(handler_execute): | ||
def require_admin(handler): | ||
valid = handler.is_admin() | ||
if not valid: | ||
handler.set_status(401) | ||
handler._transforms = [] | ||
handler.write(str(tornado.web.HTTPError(401))) | ||
handler.finish() | ||
return valid | ||
|
||
def _execute(self, transforms, *args, **kwargs): | ||
if not require_admin(self): | ||
return False | ||
return handler_execute(self, transforms, *args, **kwargs) | ||
|
||
return _execute | ||
|
||
handler_class._execute = wrap_execute(handler_class._execute) | ||
return handler_class | ||
|
||
|
||
@require_admin | ||
class BaseAdminHttpHandler(BaseHttpHandler): | ||
pass | ||
|
||
|
||
class FPGAQueryHttpHandler(BaseAdminHttpHandler): | ||
def get(self): | ||
device_id = self.get_argument('device_id', None) | ||
d = dict(device_id=device_id) | ||
if device_id is not None: | ||
fpga = FPGA.get(device_id) | ||
if fpga is not None: | ||
d['status'] = 0 | ||
d['auth_key'] = fpga.auth_key | ||
else: | ||
d['status'] = 1 | ||
d['message'] = 'not found' | ||
else: | ||
d['status'] = 2 | ||
d['message'] = 'missing device_id' | ||
self.write(json.dumps(d)) | ||
self.finish() | ||
|
||
|
||
class FPGAAddHttpHandler(BaseAdminHttpHandler): | ||
def get(self): | ||
self.render('FPGAadd.html') | ||
|
||
def post(self): | ||
device_id = self.get_argument('device_id', None) | ||
d = dict(device_id=device_id) | ||
if device_id is not None: | ||
newf = FPGA.new(device_id) | ||
if newf is not None: | ||
d['status'] = 0 | ||
d['auth_key'] = newf.auth_key | ||
else: | ||
d['status'] = 1 | ||
d['message'] = 'Already exist' | ||
else: | ||
d['status'] = 2 | ||
d['message'] = 'missing device_id' | ||
self.write(json.dumps(d)) | ||
self.finish() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
__author__ = 'lmzqwer2' | ||
|
||
import config | ||
import tornado.web | ||
from models import User | ||
|
||
|
||
class BaseHttpHandler(tornado.web.RequestHandler): | ||
def set_default_headers(self): | ||
self.set_header('Server', 'ExoticServer/%s' % config.version) | ||
self.set_header('X-Frame-Options', 'SAMEORIGIN') | ||
self.set_header('X-XSS-Protection', '1; mode=block') | ||
self.set_header('x-content-type-options', 'nosniff') | ||
|
||
def get_current_name(self): | ||
identity = self.get_secure_cookie(config._identity) | ||
if identity is not None and len(identity) == 32: | ||
return self.get_secure_cookie(config._user) | ||
return None | ||
|
||
def set_current_name(self, name): | ||
self.set_secure_cookie(config._user, name) | ||
|
||
def get_current_user(self): | ||
identity = self.get_secure_cookie(config._identity) | ||
if identity is not None and len(identity) == 32: | ||
return self.get_secure_cookie(config._nickname) | ||
return None | ||
|
||
def set_current_user(self, user): | ||
self.set_secure_cookie(config._nickname, user) | ||
|
||
def is_admin(self): | ||
user = User.get(self.get_current_name()) | ||
return user is not None and user.admin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
__author__ = 'lmzqwer2' | ||
|
||
|
||
class ExDict(dict): | ||
''' | ||
A dict regard instance.keyname as instance['keyname']. | ||
and raise error or return default value when not found. | ||
>>> a = ExDict(); | ||
>>> print a.notfound | ||
Traceback (most recent call last): | ||
... | ||
AttributeError: 'Dict' object has no attribute 'notfound' | ||
>>> a.value = 1 | ||
>>> print a.value | ||
1 | ||
>>> a.__raiseError__ = False | ||
>>> print a.notfound | ||
None | ||
>>> a.__default__ = 'defaultvalue' | ||
>>> print a.notfound | ||
defaultvalue | ||
''' | ||
|
||
def __init__(self, **kw): | ||
super(ExDict, self).__init__(**kw) | ||
self.__raiseError__ = True | ||
self.__default__ = None | ||
|
||
def __getattr__(self, key): | ||
if key.startswith('_'): | ||
return object.__getattr__(self, key) | ||
try: | ||
return self[key] | ||
except KeyError: | ||
if self.__raiseError__: | ||
raise AttributeError(r"'Dict' object has no attribute '%s'" % key) | ||
else: | ||
return self.__default__ | ||
|
||
def __setattr__(self, key, value): | ||
if key.startswith('_'): | ||
object.__setattr__(self, key, value) | ||
else: | ||
self[key] = value | ||
|
||
|
||
class DefaultDict(ExDict): | ||
''' | ||
a dict return a preset value when not found. | ||
>>> a = DefaultDict() | ||
>>> print a.value | ||
None | ||
>>> a.value = 2 | ||
>>> print a.value | ||
2 | ||
''' | ||
|
||
def __init__(self, **kw): | ||
super(DefaultDict, self).__init__(**kw) | ||
self.__raiseError__ = False | ||
|
||
|
||
if __name__ == '__main__': | ||
import doctest | ||
|
||
print 'Testing ', __file__ | ||
print doctest.testmod() |
Oops, something went wrong.