update sqlalchemy storage for values et settings
[tiramisu.git] / tiramisu / storage / sqlalchemy / setting.py
1 # -*- coding: utf-8 -*-
2 "default plugin for setting: set it in a simple dictionary"
3 # Copyright (C) 2014 Team tiramisu (see AUTHORS for all contributors)
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, either version 3 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 # ____________________________________________________________
18 from ..util import Cache
19 from .util import SqlAlchemyBase
20 import util
21 from sqlalchemy import Column, Integer, String, PickleType, ForeignKey
22 from sqlalchemy.orm import relationship
23 from sqlalchemy.ext.associationproxy import association_proxy
24 from sqlalchemy.orm.collections import attribute_mapped_collection
25
26
27 #____________________________________________________________
28 #
29 # properties|permissives
30 class _Property(SqlAlchemyBase):
31     __tablename__ = 'property'
32     id = Column(Integer, primary_key=True)
33     setting = Column(Integer, ForeignKey('settings.id'), nullable=False)
34     path = Column(String)
35     properties = Column(PickleType)
36
37     def __init__(self, path, properties):
38         self.path = path
39         self.properties = properties
40
41
42 class _Permissive (SqlAlchemyBase):
43     __tablename__ = 'permissive'
44     id = Column(Integer, primary_key=True)
45     setting = Column(Integer, ForeignKey('settings.id'), nullable=False)
46     path = Column(String)
47     permissives = Column(PickleType)
48
49     def __init__(self, path, permissives):
50         self.path = path
51         self.permissives = permissives
52
53
54 #____________________________________________________________
55 #FIXME marche pas le cache ... de toute facon je vais faire un storage separe !
56 class Settings(Cache, SqlAlchemyBase):
57     __tablename__ = 'settings'
58     id = Column(Integer, primary_key=True)
59     session_id = Column(String, index=True)
60     _props = relationship("_Property",
61                           collection_class=attribute_mapped_collection('path'),
62                           cascade="all, delete-orphan")
63     _properties = association_proxy("_props", "properties")
64     _perms = relationship("_Permissive",
65                           collection_class=attribute_mapped_collection('path'),
66                           cascade="all, delete-orphan")
67     _permissives = association_proxy("_perms", "permissives")
68
69     def __init__(self, session_id, storage):
70         session = self.getsession()
71         self.session_id = session_id
72         super(Settings, self).__init__(storage)
73         session.commit()
74
75     def getsession(self):
76         return util.Session()
77
78     # properties
79     def setproperties(self, path, properties):
80         session = self.getsession()
81         self._properties[path] = properties
82         session.commit()
83         del(session)
84
85     def getproperties(self, path, default_properties):
86         return self._properties.get(path, set(default_properties))
87
88     def hasproperties(self, path):
89         return path in self._properties
90
91     def reset_all_properties(self):
92         session = self.getsession()
93         self._properties.clear()
94         session.commit()
95         del(session)
96
97     def delproperties(self, path):
98         try:
99             session = self.getsession()
100             del(self._properties[path])
101             session.commit()
102             del(session)
103         except KeyError:
104             pass
105
106     # permissive
107     def setpermissive(self, path, permissive):
108         session = self.getsession()
109         self._permissives[path] = frozenset(permissive)
110         session.commit()
111
112     def getpermissive(self, path=None):
113         ret = self._permissives.get(path, frozenset())
114         #replace None by a frozenset()
115         return {None: frozenset()}.get(ret, ret)
116
117     def get_modified_properties(self):
118         """return all modified settings in a dictionary
119         example: {'path1': set(['prop1', 'prop2'])}
120         """
121         return self._properties
122
123     def get_modified_permissives(self):
124         """return all modified permissives in a dictionary
125         example: {'path1': set(['perm1', 'perm2'])}
126         """
127         return self._permissives
128
129
130 def delete_session(session_id, session):
131     print session.query(_Property).all()
132     print session.query(_Permissive).all()
133     settings_id = session.query(Settings).filter_by(session_id=session_id).first().id
134     for val in session.query(_Property).filter_by(settings=settings_id).all():
135         session.delete(val)
136     for val in session.query(_Permissive).filter_by(settings=settings_id).all():
137         session.delete(val)