Skip to content

Commit

Permalink
Merge server into master.
Browse files Browse the repository at this point in the history
  • Loading branch information
All-less committed Jun 12, 2016
2 parents fad253b + d6e71bf commit cc91be8
Show file tree
Hide file tree
Showing 53 changed files with 27,376 additions and 3 deletions.
14 changes: 11 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down Expand Up @@ -68,17 +67,26 @@ bin/
# MacOS
.DS_Store

# Intellij
.idea
# JetBrains IDE
.idea/

# swap file
.swp

# Javascript Module
node_modules/

# Temporary file
tmp/

# Database
*.db

# log file
*.log

# merge backup file
*.orig

# cofiguration
config_override.py
79 changes: 79 additions & 0 deletions server/AdminHandler.py
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()
38 changes: 38 additions & 0 deletions server/BaseHttpHandler.py
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
72 changes: 72 additions & 0 deletions server/ExDict.py
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()
Loading

0 comments on commit cc91be8

Please sign in to comment.