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

First draft of extension database. TODO: API for extensions (setter, getter, change event), alembic

parent f4192b26
......@@ -3,6 +3,7 @@
import cherrypy
from vispa.controller import AbstractController
from vispa.controller.filesystem import FSAjaxController
from vispa.models.extension import ExtensionDatabase
from vispa.models.user import User
from vispa.models.workspace import Workspace, WorkspaceState
from vispa.models.preference import VispaPreference, ExtensionPreference
......@@ -222,3 +223,15 @@ class AjaxController(AbstractController):
umail = cherrypy.request.user.email
prefix = "Feedback from user '%s' (%s):\n\n" % (uname, umail)
vispa.send_mail(feedback_address, subject, prefix + content)
@cherrypy.expose
@cherrypy.tools.method(accept="GET")
def getextensiondatabase(self, extensionname):
db = cherrypy.request.db
return ExtensionDatabase.get_data(db, extensionname, decode_json=True) or "{}"
@cherrypy.expose
@cherrypy.tools.method(accept="POST")
def setextensiondatabase(self, extensionname, data):
db = cherrypy.request.db
ExtensionDatabase.update_data(db, extensionname, data)
# -*- coding: utf-8 -*-
# imports
from datetime import datetime
from sqlalchemy import Column, schema
from sqlalchemy.types import DateTime, Text, Unicode
from vispa.models import Base
from vispa import AjaxException
import json
__all__ = ["ExtensionDatabase"]
class ExtensionDatabase(Base):
__tablename__ = "extension_database"
extension_name = Column(Unicode(255), primary_key=True, nullable=False)
data = Column(Text, nullable=True, default=u"{}")
timestamp = Column(DateTime, nullable=False, default=datetime.now)
@staticmethod
def get_by_name(db, extension_name):
extension_database = db.query(ExtensionDatabase).filter_by(extension_name=extension_name).first()
if not isinstance(extension_database, ExtensionDatabase):
# create if doesn't exists
extension_database = ExtensionDatabase(extension_name=extension_name)
db.add(extension_database)
db.commit()
return extension_database
@staticmethod
def get_data(db, extension_name, decode_json=False, extension_database=None):
extension_database = extension_database or ExtensionDatabase.get_by_name(db, extension_name)
if not isinstance(extension_database, ExtensionDatabase):
return None
data = extension_database.data
return data if not decode_json else json.loads(data)
@staticmethod
def update_data(db, extension_name, data, extension_database=None):
extension_database = extension_database or ExtensionDatabase.get_by_name(db, extension_name)
if not isinstance(extension_database, ExtensionDatabase):
return False
if isinstance(data, dict):
current_data = json.loads(extension_database.data)
for id, identifier in data.items():
if identifier is None and id in current_data.keys():
del current_data[id]
elif identifier:
current_data[id] = identifier
extension_database.data = json.dumps(current_data)
else:
extension_database.data = unicode(data)
extension_database.timestamp = datetime.now()
db.commit()
return True
......@@ -10,6 +10,7 @@ import sys
from sqlalchemy.orm import scoped_session, sessionmaker
from vispa.models import Base as sql_base
from vispa.models.extension import *
from vispa.models.preference import *
from vispa.models.stats import *
from vispa.models.user import *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment