database.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2013 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. SQLAlchemy storage backend.
  20. """
  21. import time
  22. from datetime import datetime
  23. from contextlib import contextmanager
  24. from sqlalchemy import create_engine, Column, String, DateTime, ForeignKey
  25. from sqlalchemy import func
  26. from sqlalchemy.orm import sessionmaker, relationship
  27. from sqlalchemy.ext.declarative import declarative_base
  28. from .. import config, ical
  29. Base = declarative_base()
  30. Session = sessionmaker()
  31. Session.configure(bind=create_engine(config.get("storage", "database_url")))
  32. class DBCollection(Base):
  33. __tablename__ = "collection"
  34. path = Column(String, primary_key=True)
  35. parent_path = Column(String, ForeignKey("collection.path"))
  36. parent = relationship("DBCollection", backref="children", remote_side=[path])
  37. class DBItem(Base):
  38. __tablename__ = "item"
  39. name = Column(String, primary_key=True)
  40. tag = Column(String)
  41. collection_path = Column(String, ForeignKey("collection.path"))
  42. collection = relationship("DBCollection", backref="items")
  43. class DBHeader(Base):
  44. __tablename__ = "header"
  45. key = Column(String, primary_key=True)
  46. value = Column(String)
  47. collection_path = Column(String, ForeignKey("collection.path"), primary_key=True)
  48. collection = relationship("DBCollection", backref="headers")
  49. class DBLine(Base):
  50. __tablename__ = "line"
  51. key = Column(String, primary_key=True)
  52. value = Column(String)
  53. item_name = Column(String, ForeignKey("item.name"), primary_key=True)
  54. timestamp = Column(DateTime, default=datetime.now)
  55. item = relationship(
  56. "DBItem", backref="lines", order_by=timestamp)
  57. class DBProperty(Base):
  58. __tablename__ = "property"
  59. key = Column(String, primary_key=True)
  60. value = Column(String)
  61. collection_path = Column(String, ForeignKey("collection.path"), primary_key=True)
  62. collection = relationship(
  63. "DBCollection", backref="properties", cascade="delete")
  64. class Collection(ical.Collection):
  65. """Collection stored in a database."""
  66. def __init__(self, path, principal=False):
  67. self.session = Session()
  68. super(Collection, self).__init__(path, principal)
  69. def __del__(self):
  70. self.session.commit()
  71. #super(Collection, self).__del__()
  72. def _query(self, item_types):
  73. item_objects = []
  74. for item_type in item_types:
  75. items = (
  76. self.session.query(DBItem)
  77. .filter_by(collection_path=self.path, tag=item_type.tag)
  78. .order_by("name").all())
  79. for item in items:
  80. text = "\n".join(
  81. "%s:%s" % (line.key, line.value) for line in item.lines)
  82. item_objects.append(item_type(text, item.name))
  83. return item_objects
  84. @property
  85. def _modification_time(self):
  86. return (
  87. self.session.query(func.max(DBLine.timestamp))
  88. .join(DBItem).filter_by(collection_path=self.path).first())[0]
  89. @property
  90. def _db_collection(self):
  91. return self.session.query(DBCollection).get(self.path)
  92. def write(self, headers=None, items=None):
  93. headers = headers or self.headers or (
  94. ical.Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
  95. ical.Header("VERSION:%s" % self.version))
  96. items = items if items is not None else self.items
  97. if self._db_collection:
  98. for item in self._db_collection.items:
  99. for line in item.lines:
  100. self.session.delete(line)
  101. self.session.delete(item)
  102. for header in self._db_collection.headers:
  103. self.session.delete(header)
  104. else:
  105. db_collection = DBCollection()
  106. db_collection.path = self.path
  107. self.session.add(db_collection)
  108. for header in headers:
  109. db_header = DBHeader()
  110. db_header.key, db_header.value = header.text.split(":", 1)
  111. db_header.collection_path = self.path
  112. self.session.add(db_header)
  113. for item in items:
  114. db_item = DBItem()
  115. db_item.name = item.name
  116. db_item.tag = item.tag
  117. db_item.collection_path = self.path
  118. self.session.add(db_item)
  119. for line in ical.unfold(item.text):
  120. db_line = DBLine()
  121. db_line.key, db_line.value = line.split(":", 1)
  122. db_line.item_name = item.name
  123. self.session.add(db_line)
  124. def delete(self):
  125. self.session.delete(self._db_collection)
  126. @property
  127. def text(self):
  128. return ical.serialize(self.tag, self.headers, self.items)
  129. @property
  130. def etag(self):
  131. return '"%s"' % hash(self._modification_time)
  132. @property
  133. def headers(self):
  134. headers = (
  135. self.session.query(DBHeader)
  136. .filter_by(collection_path=self.path)
  137. .order_by("key").all())
  138. return [
  139. ical.Header("%s:%s" % (header.key, header.value))
  140. for header in headers]
  141. @classmethod
  142. def children(cls, path):
  143. session = Session()
  144. if path:
  145. children = session.query(DBCollection).get(path).children
  146. else:
  147. children = session.query(DBCollection).filter_by(parent=None).all()
  148. collections = [cls(child.path) for child in children]
  149. session.close()
  150. return collections
  151. @classmethod
  152. def is_node(cls, path):
  153. if not path:
  154. return True
  155. session = Session()
  156. result = (
  157. session.query(DBCollection)
  158. .filter_by(parent_path=path).count() > 0)
  159. session.close()
  160. return result
  161. @classmethod
  162. def is_leaf(cls, path):
  163. if not path:
  164. return False
  165. session = Session()
  166. result = (
  167. session.query(DBItem)
  168. .filter_by(collection_path=path).count() > 0)
  169. session.close()
  170. return result
  171. @property
  172. def last_modified(self):
  173. return time.strftime(
  174. "%a, %d %b %Y %H:%M:%S +0000", self._modification_time.timetuple())
  175. @property
  176. @contextmanager
  177. def props(self):
  178. # On enter
  179. properties = {}
  180. db_properties = (
  181. self.session.query(DBProperty)
  182. .filter_by(collection_path=self.path).all())
  183. for prop in db_properties:
  184. properties[prop.key] = prop.value
  185. old_properties = properties.copy()
  186. yield properties
  187. # On exit
  188. if self._db_collection and old_properties != properties:
  189. for prop in db_properties:
  190. self.session.delete(prop)
  191. for key, value in properties.items():
  192. prop = DBProperty()
  193. prop.key = key
  194. prop.value = value
  195. prop.collection_path = self.path
  196. self.session.add(prop)
  197. @property
  198. def items(self):
  199. return self._query(
  200. (ical.Event, ical.Todo, ical.Journal, ical.Card, ical.Timezone))
  201. @property
  202. def components(self):
  203. return self._query((ical.Event, ical.Todo, ical.Journal, ical.Card))
  204. @property
  205. def events(self):
  206. return self._query((ical.Event,))
  207. @property
  208. def todos(self):
  209. return self._query((ical.Todo,))
  210. @property
  211. def journals(self):
  212. return self._query((ical.Journal,))
  213. @property
  214. def timezones(self):
  215. return self._query((ical.Timezone,))
  216. @property
  217. def cards(self):
  218. return self._query((ical.Card,))