diff --git a/alembic/env.py b/alembic/env.py index 36e604e..30aa597 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -22,7 +22,11 @@ # add your model's MetaData object here # for 'autogenerate' support -from gengine.models import Base +from gengine.metadata import init_session,init_declarative_base +init_session() +init_declarative_base() + +from gengine.metadata import Base target_metadata = Base.metadata # target_metadata = None diff --git a/gengine/__init__.py b/gengine/__init__.py index 792065f..6d5c381 100644 --- a/gengine/__init__.py +++ b/gengine/__init__.py @@ -29,7 +29,9 @@ def main(global_config, **settings): config.include("pyramid_tm") - from gengine.models import init_db + from gengine.metadata import init_session, init_declarative_base, init_db + init_session() + init_declarative_base() init_db(engine) config.include('pyramid_chameleon') diff --git a/gengine/flask_static/admin.js b/gengine/flask_static/admin.js new file mode 100644 index 0000000..5dac964 --- /dev/null +++ b/gengine/flask_static/admin.js @@ -0,0 +1,39 @@ +jQuery().ready(function($) { + var defaultcall = "progress"; + + var fields=["userid","variable","value","key","achievementid","level", + "lat","lon","friends","groups","timezone","country","region","city"]; + + var api_funcs = { + "progress" : { + "fields":["userid"], + "url":"/progress/{userid}", + "method":"GET" + }, + "increase_value" : { + "fields":["variable","userid","value","key"], + "url":"/increase_value/{variable}/{userid}{/key}", + "method":"POST", + "postparams":["value"] + }, + "add_or_update_user" : { + "fields":["userid","lat","lon","friends","groups","timezone","country","region","city"], + "url":"/add_or_update_user/{userid}", + "method":"POST", + "postparams":["lat","lon","friends","groups","timezone","country","region","city"] + }, + "delete_user" : { + "fields":["userid"], + "url":"/delete_user/{userid}", + "method":"DELETE" + }, + "achievement_level" : { + "fields":["achievementid","level"], + "url":"/achievement/{achievementid}/level/{level}", + "method":"GET" + } + }; + + setupAPIForm($,defaultcall,fields,api_funcs); + +}); \ No newline at end of file diff --git a/gengine/static/admin_layout.css b/gengine/flask_static/admin_layout.css similarity index 100% rename from gengine/static/admin_layout.css rename to gengine/flask_static/admin_layout.css diff --git a/gengine/static/admin.js b/gengine/flask_static/api.js similarity index 74% rename from gengine/static/admin.js rename to gengine/flask_static/api.js index 1a62490..0606eea 100644 --- a/gengine/static/admin.js +++ b/gengine/flask_static/api.js @@ -1,8 +1,5 @@ -jQuery().ready(function($) { +var setupAPIForm = function($, defaultcall, fields, api_funcs) { - var defaultcall = "progress"; - var fields=["userid","variable","value","key","achievementid","level", - "lat","lon","friends","groups","timezone","country","region","city"]; var container_fields = {}; for(var i=0; i', + endpoint='static_gengine', + view_func=get_static_view('gengine:flask_static',flaskadminapp)) @flaskadminapp.context_processor def inject_version(): return { "gamification_engine_version" : pkg_resources.get_distribution("gamification-engine").version } - admin = Admin(flaskadminapp, - name="Gamification Engine - Admin Control Panel", - base_template='admin_layout.html', - url=urlprefix+"/admin" - ) - + if not override_admin: + admin = Admin(flaskadminapp, + name="Gamification Engine - Admin Control Panel", + base_template='admin_layout.html', + url=urlprefix+"/admin" + ) + else: + admin = override_admin + admin.add_view(ModelViewAchievement(DBSession, category="Rules")) admin.add_view(ModelViewGoal(DBSession, category="Rules")) admin.add_view(ModelView(AchievementProperty, DBSession, category="Rules")) @@ -136,7 +175,6 @@ class ClearCacheForm(Form): clear_check = BooleanField(label="Delete all caches?") class MaintenanceView(BaseView): - @expose('/',methods=('GET','POST',)) def index(self): self._template_args['msgs'] = [] @@ -148,4 +186,4 @@ def index(self): clear_all_caches() self._template_args['msgs'].append("All caches cleared!") return self.render(template="admin_maintenance.html") - + \ No newline at end of file diff --git a/gengine/metadata.py b/gengine/metadata.py new file mode 100644 index 0000000..249de08 --- /dev/null +++ b/gengine/metadata.py @@ -0,0 +1,37 @@ +from sqlalchemy.orm.session import Session, sessionmaker +import transaction +from sqlalchemy.orm.scoping import scoped_session +from zope.sqlalchemy.datamanager import ZopeTransactionExtension +from sqlalchemy.ext.declarative.api import declarative_base + +class MySession(Session): + """This allow us to use the flask-admin sqla extension, which uses DBSession.commit() rather than transaction.commit()""" + def commit(self,*args,**kw): + transaction.commit(*args,**kw) + + def rollback(self,*args,**kw): + transaction.abort(*args,**kw) + +DBSession=None + +def init_session(override_session=None): + global DBSession + if override_session: + DBSession = override_session + else: + DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension(), class_=MySession)) + +Base=None + +def init_declarative_base(override_base=None): + global Base + if override_base: + Base=override_base + else: + Base = declarative_base() + +def init_db(engine): + DBSession.configure(bind=engine) + Base.metadata.bind = engine + + \ No newline at end of file diff --git a/gengine/models.py b/gengine/models.py index 7f05daf..c64396c 100644 --- a/gengine/models.py +++ b/gengine/models.py @@ -36,17 +36,7 @@ import hashlib import warnings - -class MySession(Session): - """This allow us to use the flask-admin sqla extension, which uses DBSession.commit() rather than transaction.commit()""" - def commit(self,*args,**kw): - transaction.commit(*args,**kw) - - def rollback(self,*args,**kw): - transaction.abort(*args,**kw) - -DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension(), class_=MySession)) -Base = declarative_base() +from gengine.metadata import Base, DBSession try: cache_general = get_region('general') @@ -63,10 +53,6 @@ def rollback(self,*args,**kw): warnings.warn("Warning: cache objects are in memory, are you creating docs?") -def init_db(engine): - DBSession.configure(bind=engine) - Base.metadata.bind = engine - t_users = Table("users", Base.metadata, Column('id', ty.BigInteger, primary_key = True), Column("lat", ty.Float(Precision=64), nullable=True), @@ -1167,7 +1153,6 @@ def insert_variable_for_property(mapper,connection,target): variable.group = "day" DBSession.add(variable) - #some query helpers def calc_distance(latlong1, latlong2): diff --git a/gengine/scripts/initializedb.py b/gengine/scripts/initializedb.py index cde3790..f21f8d0 100644 --- a/gengine/scripts/initializedb.py +++ b/gengine/scripts/initializedb.py @@ -13,9 +13,6 @@ from pyramid.scripts.common import parse_vars import pyramid_dogpile_cache from pyramid.config import Configurator -from gengine.models import AchievementProperty, AchievementReward - - def usage(argv): @@ -45,9 +42,19 @@ def main(argv=sys.argv): config = Configurator(settings=settings) pyramid_dogpile_cache.includeme(config) - from ..models import ( - DBSession, + from ..metadata import ( + init_session, + init_declarative_base + ) + init_session() + init_declarative_base() + + from ..metadata import ( Base, + DBSession + ) + + from ..models import ( Achievement, Goal, Variable, @@ -56,7 +63,9 @@ def main(argv=sys.argv): TranslationVariable, Translation, Property, - Reward + Reward, + AchievementProperty, + AchievementReward ) DBSession.configure(bind=engine) diff --git a/gengine/templates/admin_layout.html b/gengine/templates/admin_layout.html index 411d38a..cc99b28 100644 --- a/gengine/templates/admin_layout.html +++ b/gengine/templates/admin_layout.html @@ -9,5 +9,6 @@ {% block tail %} {{ super() }} - + + {% endblock %}