import tornado.ioloop
import tornado.web
from sdp import SDP, method, sub, before_insert, can_update, can_insert, Collection
import rethinkdb as r
from datetime import datetime, timezone
import jwt
class App(SDP):
@method
def add(self, a, b):
return a + b
@method
def login(self, encoded_jwt):
payload = jwt.decode(encoded_jwt, 'secret', algorithms=['HS256'])
user_id = payload.get('user_id')
if user_id:
self.user_id = user_id
return user_id
@method
def logout(self):
self.close()
@can_insert('cars')
def is_logged(self, doc):
return self.user_id is not None
@can_update('cars')
def is_owner(self, doc, old_doc):
return old_doc['owner'] == self.user_id
@before_insert('cars')
def created_at(self, doc):
doc['created_at'] = datetime.now(timezone.utc)
doc['owner'] = self.user_id
@method
def change_color(self, id, color):
self.check(id, str)
self.check(color, str)
yield self.update('cars', id, {'color': color})
@method
def create_car_of_color(self, color, matricula):
self.check(color, str)
self.check(matricula, str)
yield self.insert('cars', {'matricula': matricula, 'color': color})
@sub
def cars_of_color(self, color):
return Collection('cars').filter({'color': color})
def make_app():
return tornado.web.Application([
(r"/ws", App),
], debug=True)
if __name__ == "__main__":
print('init')
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
-
Notifications
You must be signed in to change notification settings - Fork 0
miguelalarcos/tornado-sdp
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
a implementation of subscription data protocol with tornado, python
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published