Commit c36a9083 authored by Fabian-Andree Heidemann's avatar Fabian-Andree Heidemann
Browse files

[database] porting to JSONdata

parent 1e4f776d
......@@ -6,7 +6,7 @@ from vispa.controller.filesystem import FSAjaxController
from vispa.controller.usermanagement import UMAjaxController
from vispa.models.user import User
from vispa.models.group import Group, Group_User_Assoc
from vispa.models.workspace import Workspace, WorkspaceState
from vispa.models.workspace import Workspace
from vispa.models.jsondata import JSONData
from vispa import AjaxException
import vispa.workspace
......@@ -81,7 +81,7 @@ class AjaxController(AbstractController):
# json
@cherrypy.expose
@cherrypy.tools.method(accept="POST")
def setjson(self, key, value, wid=-1):
def setjson(self, key, value, wid=None):
db = cherrypy.request.db
user_id = self.get("user_id")
wid = int(wid)
......
......@@ -4,8 +4,7 @@ from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
__all__ = ['group', 'preference', 'project', 'role', 'shortcuts', 'user',
'workgroup', 'workspace']
__all__ = ['group', 'jsondata', 'project', 'role', 'user', 'workgroup', 'workspace']
FORBIDDEN_PHRASES = [u"drop ", u"select ", u"dump ", u"insert ", u"delete ",
......
"""JSON data
Revision ID: d10e867460b9
Revises: 459e6ec6f40d
Create Date: 2016-05-11 13:49:57.723307
"""
# revision identifiers, used by Alembic.
revision = 'd10e867460b9'
down_revision = '459e6ec6f40d'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
def upgrade():
op.create_table('json_data',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('workspace_id', sa.Integer(), nullable=True),
sa.Column('key', sa.Unicode(length=255), nullable=False),
sa.Column('value', sa.UnicodeText(), nullable=False),
sa.Column('timestamp', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.ForeignKeyConstraint(['workspace_id'], ['workspace.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('user_id', 'workspace_id', 'key')
)
op.drop_table('extension_preference')
op.drop_table('vispa_shortcuts')
op.drop_table('vispa_preference')
op.drop_table('workspace_connection')
op.drop_table('extension_shortcuts')
op.drop_table('workspace_state')
def downgrade():
op.create_table('workspace_state',
sa.Column('workspace_id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('state', sa.UnicodeText(), nullable=True),
sa.Column('timestamp', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.ForeignKeyConstraint(['workspace_id'], ['workspace.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('workspace_id', 'user_id'),
)
op.create_table('extension_shortcuts',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('key', sa.Unicode(length=255), nullable=False),
sa.Column('value', sa.UnicodeText(), nullable=False),
sa.Column('timestamp', sa.DateTime(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('user_id', 'key'),
)
op.create_table('workspace_connection',
sa.Column('workspace_id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('host', sa.UnicodeText(), nullable=False),
sa.Column('tempdir', sa.UnicodeText(), nullable=True),
sa.Column('timestamp', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.ForeignKeyConstraint(['workspace_id'], ['workspace.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('workspace_id', 'user_id'),
)
op.create_table('vispa_preference',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('section', sa.Unicode(length=255), nullable=False),
sa.Column('value', sa.UnicodeText(), nullable=False),
sa.Column('timestamp', sa.DateTime(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('user_id', 'section'),
)
op.create_table('vispa_shortcuts',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('key', sa.Unicode(length=255), nullable=False),
sa.Column('value', sa.UnicodeText(), nullable=False),
sa.Column('timestamp', sa.DateTime(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('user_id', 'key'),
)
op.create_table('extension_preference',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('key', sa.Unicode(length=255), nullable=False),
sa.Column('value', sa.UnicodeText(), nullable=False),
sa.Column('timestamp', sa.DateTime(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('user_id', 'key'),
)
op.drop_table('json_data')
......@@ -13,27 +13,25 @@ _all__ = ["JSONData"]
class JSONData(Base):
__tablename__ = "json_data"
id = Column(Integer, primary_key=True)
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
nullable=False)
workspace_id = Column(
Integer,
schema.ForeignKey(
"workspace.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=True,
primary_key=True)
key = Column(Unicode(255), nullable=False, primary_key=True)
nullable=True)
key = Column(Unicode(255), nullable=False)
value = Column(UnicodeText(), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
# created = Column(DateTime, nullable=False, default=datetime.now) # do we need this?
__table_args__ = (schema.PrimaryKeyConstraint(user_id, workspace_id, key),)
__table_args__ = (schema.UniqueConstraint(user_id, workspace_id, key),)
def get_info_data(self):
return {
......
# -*- coding: utf-8 -*-
# imports
from sqlalchemy import Column, schema
from sqlalchemy.types import Integer, Unicode, DateTime, UnicodeText
from datetime import datetime
from vispa.models import Base, insertion_safe
from vispa import AjaxException
import json as JSON
_all__ = ["VispaPreference", "ExtensionPreference"]
class VispaPreference(Base):
__tablename__ = "vispa_preference"
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
section = Column(Unicode(255), nullable=False, primary_key=True)
value = Column(UnicodeText(), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(user_id, section),)
@staticmethod
def get_by_user_id(session, user_id, section=None):
preferences = session.query(
VispaPreference).filter_by(user_id=user_id).all()
if not section:
return preferences
for preference in preferences:
if isinstance(preference, VispaPreference):
if preference.section == section:
return preference
return None
@staticmethod
def get_data_by_user_id(session, user_id, section=None, parse_json=False):
data = {}
preferences = VispaPreference.get_by_user_id(
session,
user_id,
section=section)
if isinstance(preferences, list):
for preference in preferences:
if parse_json:
data[preference.section] = JSON.loads(preference.value)
else:
data[preference.section] = preference.value
return data
elif isinstance(preferences, VispaPreference):
if parse_json:
return JSON.loads(preferences.value)
else:
return preferences.value
return None
@staticmethod
def set_value(session, user_id, section, value, update=True):
safe, key = insertion_safe(section, value)
if not safe:
raise AjaxException(
"Couldn't update preference '%s'!" %
section)
# entry already exists?
preference = VispaPreference.get_by_user_id(
session,
user_id,
section=section)
if isinstance(preference, VispaPreference):
preference.value = value
if update:
preference.timestmap = datetime.now()
else:
preference = VispaPreference(
user_id=user_id,
section=section,
value=value)
session.add(preference)
session.commit()
if not isinstance(preference, VispaPreference):
raise AjaxException(
"Couldn't update preference '%s'!" %
section)
return preference
class ExtensionPreference(Base):
__tablename__ = "extension_preference"
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
key = Column(Unicode(255), nullable=False, primary_key=True)
value = Column(UnicodeText(), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(user_id, key),)
@staticmethod
def get_by_user_id(session, user_id, key=None):
preferences = session.query(
ExtensionPreference).filter_by(user_id=user_id).all()
if not key:
return preferences
for preference in preferences:
if isinstance(preference, ExtensionPreference):
if preference.key == key:
return preference
return None
@staticmethod
def get_data_by_user_id(session, user_id, key=None, parse_json=False):
data = {}
preferences = ExtensionPreference.get_by_user_id(
session,
user_id,
key=key)
if isinstance(preferences, list):
for preference in preferences:
if parse_json:
data[preference.key] = JSON.loads(preference.value)
else:
data[preference.key] = preference.value
return data
elif isinstance(preferences, ExtensionPreference):
if parse_json:
return JSON.loads(preferences.value)
else:
return preferences.value
return None
@staticmethod
def set_value(session, user_id, key, value, update=True):
safe, _key = insertion_safe(key, value)
if not safe:
raise AjaxException("Couldn't update preference '%s'!" % key)
# entry already exists?
preference = ExtensionPreference.get_by_user_id(
session,
user_id,
key=key)
if isinstance(preference, ExtensionPreference):
preference.value = value
if update:
preference.timestmap = datetime.now()
else:
preference = ExtensionPreference(
user_id=user_id,
key=key,
value=value)
session.add(preference)
session.commit()
if not isinstance(preference, ExtensionPreference):
raise AjaxException("Couldn't update preference '%s'!" % key)
return preference
# -*- coding: utf-8 -*-
# imports
from sqlalchemy import Column, schema
from sqlalchemy.types import Integer, Unicode, DateTime, UnicodeText
from datetime import datetime
from vispa.models import Base, insertion_safe
import json as JSON
__all__ = ["VispaShortcuts", "ExtensionShortcuts"]
class VispaShortcuts(Base):
__tablename__ = "vispa_shortcuts"
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
key = Column(Unicode(255), nullable=False, primary_key=True)
value = Column(UnicodeText(), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(user_id, key),)
@staticmethod
def get_by_user_id(session, user_id, key=None):
all_shortcuts = session.query(
VispaShortcuts).filter_by(user_id=user_id).all()
if not key:
return all_shortcuts
for shortcuts in all_shortcuts:
if isinstance(shortcuts, VispaShortcuts):
if shortcuts.key == key:
return shortcuts
return None
@staticmethod
def get_data_by_user_id(session, user_id, key=None, parse_json=False):
data = {}
all_shortcuts = VispaShortcuts.get_by_user_id(
session,
user_id,
key=key)
if isinstance(all_shortcuts, list):
for shortcuts in all_shortcuts:
if parse_json:
data[shortcuts.key] = JSON.loads(shortcuts.value)
else:
data[shortcuts.key] = shortcuts.value
return data
elif isinstance(all_shortcuts, VispaShortcuts):
if parse_json:
return JSON.loads(all_shortcuts.value)
else:
return all_shortcuts.value
return None
@staticmethod
def set_value(session, user_id, key, value, update=True):
safe, _key = insertion_safe(key, value)
if not safe:
raise AjaxException("Couldn't update shortcuts '%s'!" % key)
# entry already exists?
all_shortcuts = VispaShortcuts.get_by_user_id(
session,
user_id,
key=key)
if isinstance(all_shortcuts, VispaShortcuts):
all_shortcuts.value = value
if update:
all_shortcuts.timestmap = datetime.now()
else:
all_shortcuts = VispaShortcuts(
user_id=user_id,
key=key,
value=value)
session.add(all_shortcuts)
session.commit()
if not isinstance(all_shortcuts, VispaShortcuts):
raise AjaxException("Couldn't update shortcuts '%s'!" % key)
return all_shortcuts
class ExtensionShortcuts(Base):
__tablename__ = "extension_shortcuts"
user_id = Column(
Integer,
schema.ForeignKey(
"user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False,
primary_key=True)
key = Column(Unicode(255), nullable=False, primary_key=True)
value = Column(UnicodeText(), nullable=False, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
created = Column(DateTime, nullable=False, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(user_id, key),)
@staticmethod
def get_by_user_id(session, user_id, key=None):
all_shortcuts = session.query(
ExtensionShortcuts).filter_by(user_id=user_id).all()
if not key:
return all_shortcuts
for shortcuts in all_shortcuts:
if isinstance(shortcuts, ExtensionShortcuts):
if shortcuts.key == key:
return shortcuts
return None
@staticmethod
def get_data_by_user_id(session, user_id, key=None, parse_json=False):
data = {}
all_shortcuts = ExtensionShortcuts.get_by_user_id(
session,
user_id,
key=key)
if isinstance(all_shortcuts, list):
for shortcuts in all_shortcuts:
if parse_json:
data[shortcuts.key] = JSON.loads(shortcuts.value)
else:
data[shortcuts.key] = shortcuts.value
return data
elif isinstance(all_shortcuts, ExtensionShortcuts):
if parse_json:
return JSON.loads(all_shortcuts.value)
else:
return all_shortcuts.value
return None
@staticmethod
def set_value(session, user_id, key, value, update=True):
safe, _key = insertion_safe(key, value)
if not safe:
raise AjaxException("Couldn't update shortcuts '%s'!" % key)
# entry already exists?
all_shortcuts = ExtensionShortcuts.get_by_user_id(
session,
user_id,
key=key)
if isinstance(all_shortcuts, ExtensionShortcuts):
all_shortcuts.value = value
if update:
all_shortcuts.timestmap = datetime.now()
else:
all_shortcuts = ExtensionShortcuts(
user_id=user_id,
key=key,
value=value)
session.add(all_shortcuts)
session.commit()
if not isinstance(all_shortcuts, ExtensionShortcuts):
raise AjaxException("Couldn't update shortcuts '%s'!" % key)
return all_shortcuts
......@@ -10,7 +10,7 @@ from vispa.models.user import User
from vispa import AjaxException
import json
__all__ = ["Workspace", "WorkspaceState", "WorkspaceConnection"]
__all__ = ["Workspace"]
class Workspace(Base):
......@@ -145,78 +145,3 @@ class Workspace(Base):
setattr(workspace, key, value)
db.commit()
return True
class WorkspaceState(Base):
__tablename__ = "workspace_state"
workspace_id = Column(Integer, schema.ForeignKey("workspace.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False, primary_key=True)
user_id = Column(Integer, schema.ForeignKey("user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False, primary_key=True)
state = Column(Text, nullable=True, default=u"{}")
timestamp = Column(DateTime, nullable=True, default=datetime.now)
__table_args__ = (schema.PrimaryKeyConstraint(workspace_id, user_id),)
@staticmethod
def get_by_ids(db, workspace_id, user_id):
workspace_state = db.query(WorkspaceState).filter_by(
workspace_id=workspace_id, user_id=user_id).first()
if not isinstance(workspace_state, WorkspaceState):
# create an entry
workspace_state = WorkspaceState(
workspace_id=workspace_id, user_id=user_id)
db.add(workspace_state)
db.commit()
return workspace_state
@staticmethod
def get_state(db, workspace_id, user_id, decode_json=False,
workspace_state=None):
workspace_state = workspace_state or WorkspaceState.get_by_ids(
db, workspace_id, user_id)
if not isinstance(workspace_state, WorkspaceState):
return None
state = workspace_state.state
return state if not decode_json else json.loads(state)
@staticmethod
def update_state(db, workspace_id, user_id, state,
workspace_state=None):
workspace_state = workspace_state or WorkspaceState.get_by_ids(
db, workspace_id, user_id)
if not isinstance(workspace_state, WorkspaceState):
return False
if isinstance(state, dict):
current_state = json.loads(workspace_state.state)
for id, identifier in state.items():
if identifier is None and id in current_state.keys():
del current_state[id]
elif identifier:
current_state[id] = identifier
workspace_state.state = json.dumps(current_state)
else:
workspace_state.state = unicode(state)
workspace_state.timestamp = datetime.now()
db.commit()
return True
class WorkspaceConnection(Base):
__tablename__ = "workspace_connection"
workspace_id = Column(Integer, schema.ForeignKey("workspace.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False, primary_key=True)
user_id = Column(Integer, schema.ForeignKey("user.id",
ondelete="CASCADE",
onupdate="CASCADE"),
nullable=False, primary_key=True)
host = Column(Text, nullable=False)
tempdir = Column(Text, nullable=True)
timestamp = Column(DateTime, nullable=True, default=datetime.now)
......@@ -11,7 +11,6 @@ import sys
from sqlalchemy.orm import scoped_session, sessionmaker
from vispa.models import Base as sql_base
from vispa.models.group import *
from vispa.models.preference import *
from vispa.models.project import *
from vispa.models.role import *
from vispa.models.user import *
......@@ -23,7 +22,6 @@ import vispa.extensions
import vispa.plugins.template
import vispa.url
from vispa.models.shortcuts import *
import vispa.models.alembic
from urlparse import urlparse