Permalink
Browse files

First Commit

  • Loading branch information...
1 parent 888d08d commit 8ed1d498fc939fc628814f6c1fea8b291ae6a553 @LBiNationalTrust LBiNationalTrust committed Sep 7, 2012
Showing with 6,478 additions and 0 deletions.
  1. +3 −0 .hgignore
  2. +34 −0 Mojo/Auth/AuthManager.py
  3. +75 −0 Mojo/Auth/SessionManager.py
  4. +1 −0 Mojo/Auth/__init__.py
  5. +36 −0 Mojo/Auth/models.py
  6. +1 −0 Mojo/Backends/AsyncmongoBackend/__init__.py
  7. +78 −0 Mojo/Backends/AsyncmongoBackend/asyncmongo_backend.py
  8. +1 −0 Mojo/Backends/PyMongoBackend/__init__.py
  9. +53 −0 Mojo/Backends/PyMongoBackend/pymongo_backend.py
  10. +41 −0 Mojo/Backends/PyMongoBackend/pymongo_backend_test.py
  11. +1 −0 Mojo/Backends/__init__.py
  12. +42 −0 Mojo/Backends/base_interface.py
  13. +153 −0 Mojo/Documentation/Makefile
  14. BIN Mojo/Documentation/_build/doctrees/apps.doctree
  15. BIN Mojo/Documentation/_build/doctrees/authentication.doctree
  16. BIN Mojo/Documentation/_build/doctrees/backends.doctree
  17. BIN Mojo/Documentation/_build/doctrees/docs/apps.doctree
  18. BIN Mojo/Documentation/_build/doctrees/docs/authentication.doctree
  19. BIN Mojo/Documentation/_build/doctrees/docs/backends.doctree
  20. BIN Mojo/Documentation/_build/doctrees/docs/intro.doctree
  21. BIN Mojo/Documentation/_build/doctrees/docs/models.doctree
  22. BIN Mojo/Documentation/_build/doctrees/docs/quickstart.doctree
  23. BIN Mojo/Documentation/_build/doctrees/docs/socketio.doctree
  24. BIN Mojo/Documentation/_build/doctrees/docs/urls.doctree
  25. BIN Mojo/Documentation/_build/doctrees/environment.pickle
  26. BIN Mojo/Documentation/_build/doctrees/index.doctree
  27. BIN Mojo/Documentation/_build/doctrees/intro.doctree
  28. BIN Mojo/Documentation/_build/doctrees/models.doctree
  29. BIN Mojo/Documentation/_build/doctrees/quickstart.doctree
  30. BIN Mojo/Documentation/_build/doctrees/socketio.doctree
  31. BIN Mojo/Documentation/_build/doctrees/urls.doctree
  32. +4 −0 Mojo/Documentation/_build/html/.buildinfo
  33. +3 −0 Mojo/Documentation/_build/html/_sources/apps.txt
  34. +3 −0 Mojo/Documentation/_build/html/_sources/authentication.txt
  35. +3 −0 Mojo/Documentation/_build/html/_sources/backends.txt
  36. +3 −0 Mojo/Documentation/_build/html/_sources/docs/apps.txt
  37. +3 −0 Mojo/Documentation/_build/html/_sources/docs/authentication.txt
  38. +3 −0 Mojo/Documentation/_build/html/_sources/docs/backends.txt
  39. +4 −0 Mojo/Documentation/_build/html/_sources/docs/intro.txt
  40. +3 −0 Mojo/Documentation/_build/html/_sources/docs/models.txt
  41. +3 −0 Mojo/Documentation/_build/html/_sources/docs/quickstart.txt
  42. +3 −0 Mojo/Documentation/_build/html/_sources/docs/socketio.txt
  43. +3 −0 Mojo/Documentation/_build/html/_sources/docs/urls.txt
  44. +29 −0 Mojo/Documentation/_build/html/_sources/index.txt
  45. +4 −0 Mojo/Documentation/_build/html/_sources/intro.txt
  46. +3 −0 Mojo/Documentation/_build/html/_sources/models.txt
  47. +3 −0 Mojo/Documentation/_build/html/_sources/quickstart.txt
  48. +3 −0 Mojo/Documentation/_build/html/_sources/socketio.txt
  49. +3 −0 Mojo/Documentation/_build/html/_sources/urls.txt
  50. BIN Mojo/Documentation/_build/html/_static/ajax-loader.gif
  51. +540 −0 Mojo/Documentation/_build/html/_static/basic.css
  52. BIN Mojo/Documentation/_build/html/_static/comment-bright.png
  53. BIN Mojo/Documentation/_build/html/_static/comment-close.png
  54. BIN Mojo/Documentation/_build/html/_static/comment.png
  55. +256 −0 Mojo/Documentation/_build/html/_static/default.css
  56. +247 −0 Mojo/Documentation/_build/html/_static/doctools.js
  57. BIN Mojo/Documentation/_build/html/_static/down-pressed.png
  58. BIN Mojo/Documentation/_build/html/_static/down.png
  59. BIN Mojo/Documentation/_build/html/_static/file.png
  60. +154 −0 Mojo/Documentation/_build/html/_static/jquery.js
  61. BIN Mojo/Documentation/_build/html/_static/minus.png
  62. BIN Mojo/Documentation/_build/html/_static/plus.png
  63. +62 −0 Mojo/Documentation/_build/html/_static/pygments.css
  64. +560 −0 Mojo/Documentation/_build/html/_static/searchtools.js
  65. +151 −0 Mojo/Documentation/_build/html/_static/sidebar.js
  66. +23 −0 Mojo/Documentation/_build/html/_static/underscore.js
  67. BIN Mojo/Documentation/_build/html/_static/up-pressed.png
  68. BIN Mojo/Documentation/_build/html/_static/up.png
  69. +808 −0 Mojo/Documentation/_build/html/_static/websupport.js
  70. +92 −0 Mojo/Documentation/_build/html/apps.html
  71. +92 −0 Mojo/Documentation/_build/html/authentication.html
  72. +92 −0 Mojo/Documentation/_build/html/backends.html
  73. +92 −0 Mojo/Documentation/_build/html/docs/apps.html
  74. +92 −0 Mojo/Documentation/_build/html/docs/authentication.html
  75. +92 −0 Mojo/Documentation/_build/html/docs/backends.html
  76. +93 −0 Mojo/Documentation/_build/html/docs/intro.html
  77. +92 −0 Mojo/Documentation/_build/html/docs/models.html
  78. +92 −0 Mojo/Documentation/_build/html/docs/quickstart.html
  79. +92 −0 Mojo/Documentation/_build/html/docs/socketio.html
  80. +92 −0 Mojo/Documentation/_build/html/docs/urls.html
  81. +95 −0 Mojo/Documentation/_build/html/genindex.html
  82. +131 −0 Mojo/Documentation/_build/html/index.html
  83. +93 −0 Mojo/Documentation/_build/html/intro.html
  84. +92 −0 Mojo/Documentation/_build/html/models.html
  85. BIN Mojo/Documentation/_build/html/objects.inv
  86. +92 −0 Mojo/Documentation/_build/html/quickstart.html
  87. +99 −0 Mojo/Documentation/_build/html/search.html
  88. +1 −0 Mojo/Documentation/_build/html/searchindex.js
  89. +92 −0 Mojo/Documentation/_build/html/socketio.html
  90. +92 −0 Mojo/Documentation/_build/html/urls.html
  91. +242 −0 Mojo/Documentation/conf.py
  92. +3 −0 Mojo/Documentation/docs/apps.rst
  93. +3 −0 Mojo/Documentation/docs/authentication.rst
  94. +3 −0 Mojo/Documentation/docs/backends.rst
  95. +4 −0 Mojo/Documentation/docs/intro.rst
  96. +3 −0 Mojo/Documentation/docs/models.rst
  97. +3 −0 Mojo/Documentation/docs/quickstart.rst
  98. +3 −0 Mojo/Documentation/docs/socketio.rst
  99. +3 −0 Mojo/Documentation/docs/urls.rst
  100. +29 −0 Mojo/Documentation/index.rst
  101. +76 −0 Mojo/ObjectMapper/FieldPrototype.py
  102. +137 −0 Mojo/ObjectMapper/Fields.py
  103. +293 −0 Mojo/ObjectMapper/ModelPrototype.py
  104. +1 −0 Mojo/ObjectMapper/__init__.py
  105. +61 −0 Mojo/ObjectMapper/tdb.py
  106. +77 −0 Mojo/RequestHandlers/MojoHandler.py
  107. +1 −0 Mojo/RequestHandlers/__init__.py
  108. +122 −0 Mojo/ServerHelpers/RunServer.py
  109. +1 −0 Mojo/ServerHelpers/__init__.py
  110. +5 −0 Mojo/SocketHandlers/ControlConnection.py
  111. +1 −0 Mojo/SocketHandlers/__init__.py
  112. +1 −0 Mojo/__init__.py
  113. +8 −0 Mojo/requirements.txt
  114. +1 −0 SampleMojoProject/Apps/SampleApp/__init__.py
  115. +5 −0 SampleMojoProject/Apps/SampleApp/socket_handlers.py
  116. +1 −0 SampleMojoProject/Apps/SampleApp/templates/UI_modules/module_entry.html
  117. +13 −0 SampleMojoProject/Apps/SampleApp/templates/base.html
  118. +11 −0 SampleMojoProject/Apps/SampleApp/templates/test.html
  119. +6 −0 SampleMojoProject/Apps/SampleApp/ui_modules.py
  120. +4 −0 SampleMojoProject/Apps/SampleApp/urls.py
  121. +110 −0 SampleMojoProject/Apps/SampleApp/views.py
  122. +1 −0 SampleMojoProject/Apps/__init__.py
  123. +1 −0 SampleMojoProject/__init__.py
  124. +7 −0 SampleMojoProject/runserver.py
  125. +27 −0 SampleMojoProject/settings.py
  126. +8 −0 SampleMojoProject/test.py
View
@@ -1,2 +1,5 @@
syntax: glob
.DS_Store
+venv
+\.idea
+*.pyc
View
@@ -0,0 +1,34 @@
+import bcrypt, string, random, datetime
+
+def authenticate(userObj, password):
+ """
+ Will check if a user password matches the input to authenticate the user,
+ this will NOT log them in
+ """
+ if bcrypt.hashpw(password, userObj.password) == userObj.password:
+ return True
+ else:
+ return False
+
+def set_password(userObj, new_password):
+ """
+ Sets the user password - requires bcrypt
+ """
+ new_pw = bcrypt.hashpw(new_password, bcrypt.gensalt())
+ userObj.password = new_pw
+
+ return UserObj
+
+def make_random_password():
+ """
+ Returns a simple random password as string
+ """
+ return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
+
+def login(userObj):
+ """
+ Set the appropriate state of the user object
+ """
+ userObj.last_login = datetime.datetime.now()
+
+ return userObj
@@ -0,0 +1,75 @@
+from Mojo.Auth.models import *
+from AuthManager import authenticate, login
+from uuid import uuid4
+import base64, json, datetime
+
+class SessionManager(object):
+ def __init__(self, request_handler):
+ self.request_handler = request_handler
+ self.session_model = None
+
+ def get_or_create_session(self):
+ if self.request_handler.get_secure_cookie('session_id'):
+ return self.request_handler.get_secure_cookie('session_id'), False
+ else:
+ new_session = self._create_new_session()
+ return new_session, True
+
+ def _logout(self):
+ if self.session_model:
+ self._set_session_key('logged_in', False)
+
+ return self.session_model
+
+ def _login(self):
+ if self.session_model:
+ self._set_session_key('logged_in', True)
+
+ def _is_logged_in(self):
+ if self.session_model:
+ if self._get_session_key('logged_in'):
+ return self._get_session_key('logged_in')
+ else:
+ return False
+ else:
+ return False
+
+ def _is_session_valid(self):
+ if self.session_model:
+ if self.session_model.session_expires > datetime.datetime.now():
+ return True
+ else:
+ return False
+ else:
+ return False
+
+ def _set_session_key(self, key, value):
+ session_data = self._decode_session(self.session_model.session_data)
+ session_data[key] = value
+ enc_data = self._encode_session(session_data)
+ self.session_model.session_data = enc_data
+
+ def _get_session_key(self, key):
+ session_data = self._decode_session(self.session_model.session_data)
+ if key in session_data:
+ return session_data[key]
+ else:
+ return False
+
+ def _decode_session(self, session_data):
+ jsonStr = base64.b64decode(session_data)
+ session_dict = json.loads(jsonStr)
+
+ return session_dict
+
+ def _encode_session(self, session_data):
+ session_json_str = json.dumps(session_data)
+ encoded_str = base64.b64encode(session_json_str)
+
+ return encoded_str
+
+ def _create_new_session(self):
+ new_key = str(uuid4())
+ new_session = Session()._setup_session()
+ self.request_handler.set_secure_cookie('session_id', new_key)
+ return new_session
View
@@ -0,0 +1 @@
+__author__ = 'buhrm'
View
@@ -0,0 +1,36 @@
+from Mojo.ObjectMapper.ModelPrototype import Model, EmbeddedModelField
+from Mojo.ObjectMapper.Fields import *
+import uuid, datetime
+
+class Profile(Model):
+ _id = StringField()
+ first_name = StringField()
+ last_name = StringField()
+
+
+class Group(Model):
+ _id = StringField()
+ group_name = StringField(allow_empty=False)
+
+class User(Model):
+ _id = StringField()
+ username = StringField(allow_empty=False)
+ password = StringField(allow_empty=False)
+ email = StringField()
+ groups = ListField()
+ active = BooleanField(default = True)
+ profile = EmbeddedModelField(to=Profile)
+ is_superuser = BooleanField(default = False)
+ last_login = DateTimeField()
+ date_joined = DateTimeField()
+
+class Session(Model):
+ _id = StringField()
+ session_key = StringField(allow_empty=False)
+ session_data = StringField()
+ session_expires = DateTimeField()
+
+ def _setup_session(self, expiry_days=30, expiry_hours=0, expiry_minutes=0):
+ self.session_key = str(uuid.uuid4())
+ expiry_date = datetime.datetime.now() +datetime.timedelta(days=expiry_days, hours=expiry_hours, minutes = expiry_minutes)
+ self.session_expires = expiry_date
@@ -0,0 +1 @@
+__author__ = 'buhrm'
@@ -0,0 +1,78 @@
+from Mojo.Backends.base_interface import CollectionModelInterface, SessionInterface
+import asyncmongo
+from tornado import gen
+import logging
+
+class Session(SessionInterface):
+ def _setup_connection(self):
+ self._db = asyncmongo.Client(
+ pool_id='mydb',
+ maxcached=10,
+ maxconnections=50,
+ host=self.host,
+ port=self.port,
+ dbname=self.db_name
+ )
+
+class Collection(CollectionModelInterface):
+
+ @gen.engine
+ def find_one(self, *args, **kwargs):
+
+ cb = kwargs['callback']
+ del(kwargs['callback'])
+
+ return_dict = yield gen.Task(self.session._db[self.collection_name].find_one,*args, **kwargs)
+
+ cb(self._return_model_object(return_dict[0][0]))
+
+
+ @gen.engine
+ def find(self, *args, **kwargs):
+ cb = kwargs['callback']
+ del(kwargs['callback'])
+
+ return_cursor = yield gen.Task(self.session._db[self.collection_name].find, *args, **kwargs)
+ ret_list = return_cursor[0][0]
+ return_model_list = [self._return_model_object(i) for i in ret_list]
+ cb(return_model_list)
+
+ @gen.engine
+ def insert(self, documents, *args, **kwargs):
+
+ cb = kwargs['callback']
+ del(kwargs['callback'])
+
+ if type(documents) != list:
+ raise ValueError("Insert requires a list as input, for single query use [document]")
+ else:
+ clean_docs = []
+ for doc in documents:
+ del doc.__data__['_id']
+ clean_docs.append(doc.get_value())
+
+ ids = yield gen.Task(self.session._db[self.collection_name].insert, clean_docs)
+
+ cb(ids)
+
+ @gen.engine
+ def save(self, document, callback):
+ if document._id.get_value():
+ temp_doc = document.get_value()
+ del(temp_doc['_id'])
+ return_tuple, error = yield gen.Task(self.session._db[self.collection_name].update, {'_id': document._id.get_value()}, {"$set":temp_doc})
+ else:
+ logging.info('Asyncmongo does not return ObjectID on insert, please update _id property of model manually before further manipulation')
+ return_tuple, error = yield gen.Task(self.session._db[self.collection_name].insert, document.get_value())
+
+ callback(document)
+
+ @gen.engine
+ def delete(self, documents, callback):
+ return_values = []
+ for document in documents:
+ print 'Deleting: ', document
+ ret_val = yield gen.Task(self.session._db[self.collection_name].remove, document._id.get_value())
+ return_values.append(ret_val)
+
+ callback(return_values)
@@ -0,0 +1 @@
+__author__ = 'buhrm'
@@ -0,0 +1,53 @@
+from Mojo.Backends.base_interface import CollectionModelInterface, SessionInterface
+import pymongo
+
+class Session(SessionInterface):
+ def _setup_connection(self):
+ self._db = pymongo.Connection(
+ host=self.host,
+ port=self.port,
+ )[self.db_name]
+
+class Collection(CollectionModelInterface):
+ def find_one(self, *args, **kwargs):
+ return_dict = self.session._db[self.collection_name].find_one(*args, **kwargs)
+ return self._return_model_object(return_dict)
+
+ def find(self, *args, **kwargs):
+ return_cursor = self.session._db[self.collection_name].find(*args, **kwargs)
+ return_model_list = [self._return_model_object(i) for i in return_cursor]
+ return return_model_list
+
+ def insert(self, documents, *args, **kwargs):
+ ret_vals = []
+ if type(documents) != list:
+ raise ValueError("Insert requires a list as input, for single query use [document]")
+ else:
+ insert_list = []
+ for doc in documents:
+ del doc.__data__['_id']
+ insert_list.append(doc.get_value())
+
+ ids = self.session._db[self.collection_name].insert(insert_list)
+
+ index = 0
+ for doc in documents:
+ doc.__data__['_id'] = ids[index]
+ index += 1
+
+ ret_vals.append(doc)
+
+ return ret_vals
+
+ def save(self, document, *args, **kwargs):
+ returnID = self.session._db[self.collection_name].save(document.get_value(), *args, **kwargs)
+ document._id = returnID
+ return document
+
+ def delete(self, documents, *args, **kwargs):
+ return_values = []
+ for document in documents:
+ ret_val = self.session._db[self.collection_name].remove(document._id.get_value())
+ return_values.append(ret_val)
+
+ return return_values
@@ -0,0 +1,41 @@
+from Mojo.ObjectMapper.ModelPrototype import Model
+from Mojo.ObjectMapper.Fields import StringField, ObjectIDField
+from pymongo_backend import Session, Collection
+
+class messages(Model):
+ _id = ObjectIDField()
+ msg = StringField()
+
+
+s = Session(db_name='test')
+c = Collection(s, messages)
+
+m = messages()
+m.msg = u"New message!!!"
+
+docs = c.insert([m])
+for a in docs:
+ print 'Doc', a
+
+single_doc = docs[0]
+
+print 'Single doc: ', single_doc
+
+from bson.objectid import ObjectId
+print 'finding: ', type(single_doc._id.get_value())
+#ObjectId('504875716b7e15388f088a7c')}
+obj = c.find_one({"_id":single_doc._id.get_value()})
+
+print "Returned: ", obj
+
+print 'Modifying and saving:'
+obj.msg = u"I've changed!"
+print c.save(obj)
+
+print 'Finding many'
+items = c.find({})
+
+print items
+
+print 'deleting'
+print c.delete([obj])
@@ -0,0 +1 @@
+__author__ = 'buhrm'
@@ -0,0 +1,42 @@
+
+
+class SessionInterface(object):
+ def __init__(self, host='127.0.0.1', port=27017, db_name = None):
+ self.host = host
+ self.port = port
+ self.db_name = db_name
+
+ self._db = None
+
+ self._setup_connection()
+
+ def _setup_connection(self):
+ pass
+
+class CollectionModelInterface(object):
+ def __init__(self, session, model):
+ self.session = session
+ self.model = model
+ if type(self.model) == type:
+ self.collection_name = self.model.__name__
+ else:
+ self.collection_name = type(self.model).__name__
+
+ def _return_model_object(self,dict):
+ return self.model(dict)
+
+ def find(self, *args, **kwargs):
+ pass
+
+ def find_one(self, *args, **kwargs):
+ pass
+
+ def save(self, document, *args, **kwargs):
+ pass
+
+ def delete(self, documents, *args, **kwargs):
+ pass
+
+ def insert(self, documents, *args, **kwargs):
+ pass
+
Oops, something went wrong.

0 comments on commit 8ed1d49

Please sign in to comment.