From 64b0ec5f619ae8be4790afae3363c63529e52dcb Mon Sep 17 00:00:00 2001 From: Artem Kostiuk Date: Wed, 7 Oct 2015 13:20:21 +0300 Subject: [PATCH 1/2] Demo User.last_login processor --- ramses_example/__init__.py | 13 +++++ schemas/user.json | 3 ++ test.ini | 98 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 test.ini diff --git a/ramses_example/__init__.py b/ramses_example/__init__.py index 321d7e4..433d23e 100644 --- a/ramses_example/__init__.py +++ b/ramses_example/__init__.py @@ -63,6 +63,19 @@ def encrypt(**kwargs): return new_value +@registry.add +def set_last_login(event): + from datetime import datetime + from nefertari import engine + User = engine.get_document_cls('User') + login = event.fields['login'].new_value + field = 'email' if '@' in login else 'username' + user = User.get_item(**{field: login}) + if user is not None: + user.update({'last_login': datetime.now()}, + event.view.request) + + def main(global_config, **settings): config = Configurator(settings=settings) config.include('ramses') diff --git a/schemas/user.json b/schemas/user.json index 7f7b972..99a52c7 100644 --- a/schemas/user.json +++ b/schemas/user.json @@ -7,6 +7,9 @@ "_hidden_fields": ["password"], "_nested_relationships": ["profile"], "_auth_model": true, + "_event_handlers": { + "after_login": ["set_last_login"] + }, "required": ["username", "email", "password"], "properties": { "created_at": { diff --git a/test.ini b/test.ini new file mode 100644 index 0000000..9e5c50b --- /dev/null +++ b/test.ini @@ -0,0 +1,98 @@ +[app:ramses_example] +use = egg:ramses_example +nefertari.engine = nefertari_sqla +; nefertari.engine = nefertari_mongodb + +# Ramses settings +ramses.raml_schema = example.raml +auth = false +database_acls = false + +auth_tkt_secret = verysecret +enable_get_tunneling = true +static_cache_max_age = 7200 +public_max_limit = 100 + +system.user = system +system.password = 123456 +system.email = user@domain.com + +# SQLA +sqlalchemy.url = postgresql://post:post@localhost:5432/ramex_test + +# MongoDB settings +mongodb.host = localhost +mongodb.port = 27017 +mongodb.db = ramex_test + +# ElasticSearch +elasticsearch.hosts = localhost:9200 +elasticsearch.sniff = false +elasticsearch.index_name = ramex_test +elasticsearch.index.disable = false +elasticsearch.enable_refresh_query = true +elasticsearch.enable_aggregations = true +elasticsearch.enable_polymorphic_query = true + +# ramses_example +host = localhost +base_url = http://%(host)s + +# CORS +cors.enable = false +cors.allow_origins = %(base_url)s +cors.allow_credentials = true + +request_timing.enable = true + +[composite:main] +use = egg:Paste#urlmap +/api/ = ramses_example + +[server:main] +use = egg:waitress#main +host = 0.0.0.0 +port = 6543 +threads = 3 + +[loggers] +keys = root, ramses_example, nefertari, ramses + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = INFO +handlers = console + +[logger_ramses_example] +level = INFO +handlers = console +qualname = ramses_example + +[logger_nefertari] +level = DEBUG +handlers = +qualname = nefertari + +[logger_ramses] +level = DEBUG +handlers = +qualname = ramses + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[logger_elasticsearch] +level = DEBUG +handlers = +qualname = elasticsearch.trace + +[formatter_generic] +format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(module)s.%(funcName)s: %(message)s From 60677f782513cbb343b05e411a7c3da58e61d6b8 Mon Sep 17 00:00:00 2001 From: Artem Kostiuk Date: Wed, 7 Oct 2015 13:23:11 +0300 Subject: [PATCH 2/2] Remove test.ini --- test.ini | 98 -------------------------------------------------------- 1 file changed, 98 deletions(-) delete mode 100644 test.ini diff --git a/test.ini b/test.ini deleted file mode 100644 index 9e5c50b..0000000 --- a/test.ini +++ /dev/null @@ -1,98 +0,0 @@ -[app:ramses_example] -use = egg:ramses_example -nefertari.engine = nefertari_sqla -; nefertari.engine = nefertari_mongodb - -# Ramses settings -ramses.raml_schema = example.raml -auth = false -database_acls = false - -auth_tkt_secret = verysecret -enable_get_tunneling = true -static_cache_max_age = 7200 -public_max_limit = 100 - -system.user = system -system.password = 123456 -system.email = user@domain.com - -# SQLA -sqlalchemy.url = postgresql://post:post@localhost:5432/ramex_test - -# MongoDB settings -mongodb.host = localhost -mongodb.port = 27017 -mongodb.db = ramex_test - -# ElasticSearch -elasticsearch.hosts = localhost:9200 -elasticsearch.sniff = false -elasticsearch.index_name = ramex_test -elasticsearch.index.disable = false -elasticsearch.enable_refresh_query = true -elasticsearch.enable_aggregations = true -elasticsearch.enable_polymorphic_query = true - -# ramses_example -host = localhost -base_url = http://%(host)s - -# CORS -cors.enable = false -cors.allow_origins = %(base_url)s -cors.allow_credentials = true - -request_timing.enable = true - -[composite:main] -use = egg:Paste#urlmap -/api/ = ramses_example - -[server:main] -use = egg:waitress#main -host = 0.0.0.0 -port = 6543 -threads = 3 - -[loggers] -keys = root, ramses_example, nefertari, ramses - -[handlers] -keys = console - -[formatters] -keys = generic - -[logger_root] -level = INFO -handlers = console - -[logger_ramses_example] -level = INFO -handlers = console -qualname = ramses_example - -[logger_nefertari] -level = DEBUG -handlers = -qualname = nefertari - -[logger_ramses] -level = DEBUG -handlers = -qualname = ramses - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = NOTSET -formatter = generic - -[logger_elasticsearch] -level = DEBUG -handlers = -qualname = elasticsearch.trace - -[formatter_generic] -format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(module)s.%(funcName)s: %(message)s