database.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. # These are classes, not constants
  30. # pylint: disable=C0103
  31. Base = declarative_base()
  32. Session = sessionmaker()
  33. Session.configure(bind=create_engine(config.get("storage", "database_url")))
  34. # pylint: enable=C0103
  35. class DBCollection(Base):
  36. """Table of collections."""
  37. __tablename__ = "collection"
  38. path = Column(String, primary_key=True)
  39. parent_path = Column(String, ForeignKey("collection.path"))
  40. parent = relationship(
  41. "DBCollection", backref="children", remote_side=[path])
  42. class DBItem(Base):
  43. """Table of collection's items."""
  44. __tablename__ = "item"
  45. name = Column(String, primary_key=True)
  46. tag = Column(String)
  47. collection_path = Column(String, ForeignKey("collection.path"))
  48. collection = relationship("DBCollection", backref="items")
  49. class DBHeader(Base):
  50. """Table of item's headers."""
  51. __tablename__ = "header"
  52. key = Column(String, primary_key=True)
  53. value = Column(String)
  54. collection_path = Column(
  55. String, ForeignKey("collection.path"), primary_key=True)
  56. collection = relationship("DBCollection", backref="headers")
  57. class DBLine(Base):
  58. """Table of item's lines."""
  59. __tablename__ = "line"
  60. key = Column(String)
  61. value = Column(String)
  62. item_name = Column(String, ForeignKey("item.name"))
  63. timestamp = Column(DateTime, default=datetime.now, primary_key=True)
  64. item = relationship(
  65. "DBItem", backref="lines", order_by=timestamp)
  66. class DBProperty(Base):
  67. """Table of collection's properties."""
  68. __tablename__ = "property"
  69. key = Column(String, primary_key=True)
  70. value = Column(String)
  71. collection_path = Column(
  72. String, ForeignKey("collection.path"), primary_key=True)
  73. collection = relationship(
  74. "DBCollection", backref="properties", cascade="delete")
  75. class Collection(ical.Collection):
  76. """Collection stored in a database."""
  77. def __init__(self, path, principal=False):
  78. self.session = Session()
  79. super(Collection, self).__init__(path, principal)
  80. def __del__(self):
  81. self.session.commit()
  82. def _query(self, item_types):
  83. """Get collection's items matching ``item_types``."""
  84. item_objects = []
  85. for item_type in item_types:
  86. items = (
  87. self.session.query(DBItem)
  88. .filter_by(collection_path=self.path, tag=item_type.tag)
  89. .order_by(DBItem.name).all())
  90. for item in items:
  91. text = "\n".join(
  92. "%s:%s" % (line.key, line.value) for line in item.lines)
  93. item_objects.append(item_type(text, item.name))
  94. return item_objects
  95. @property
  96. def _modification_time(self):
  97. """Collection's last modification time."""
  98. return (
  99. self.session.query(func.max(DBLine.timestamp))
  100. .join(DBItem).filter_by(collection_path=self.path).first()[0]
  101. or datetime.now())
  102. @property
  103. def _db_collection(self):
  104. """Collection's object mapped to the table line."""
  105. return self.session.query(DBCollection).get(self.path)
  106. def write(self, headers=None, items=None):
  107. headers = headers or self.headers or (
  108. ical.Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
  109. ical.Header("VERSION:%s" % self.version))
  110. items = items if items is not None else self.items
  111. if self._db_collection:
  112. for item in self._db_collection.items:
  113. for line in item.lines:
  114. self.session.delete(line)
  115. self.session.delete(item)
  116. for header in self._db_collection.headers:
  117. self.session.delete(header)
  118. else:
  119. db_collection = DBCollection()
  120. db_collection.path = self.path
  121. self.session.add(db_collection)
  122. for header in headers:
  123. db_header = DBHeader()
  124. db_header.key, db_header.value = header.text.split(":", 1)
  125. db_header.collection_path = self.path
  126. self.session.add(db_header)
  127. for item in items:
  128. db_item = DBItem()
  129. db_item.name = item.name
  130. db_item.tag = item.tag
  131. db_item.collection_path = self.path
  132. self.session.add(db_item)
  133. for line in ical.unfold(item.text):
  134. db_line = DBLine()
  135. db_line.key, db_line.value = line.split(":", 1)
  136. db_line.item_name = item.name
  137. self.session.add(db_line)
  138. def delete(self):
  139. self.session.delete(self._db_collection)
  140. @property
  141. def text(self):
  142. return ical.serialize(self.tag, self.headers, self.items)
  143. @property
  144. def etag(self):
  145. return '"%s"' % hash(self._modification_time)
  146. @property
  147. def headers(self):
  148. headers = (
  149. self.session.query(DBHeader)
  150. .filter_by(collection_path=self.path)
  151. .order_by(DBHeader.key).all())
  152. return [
  153. ical.Header("%s:%s" % (header.key, header.value))
  154. for header in headers]
  155. @classmethod
  156. def children(cls, path):
  157. session = Session()
  158. if path:
  159. children = session.query(DBCollection).get(path).children
  160. else:
  161. children = session.query(DBCollection).filter_by(parent=None).all()
  162. collections = [cls(child.path) for child in children]
  163. session.close()
  164. return collections
  165. @classmethod
  166. def is_node(cls, path):
  167. if not path:
  168. return True
  169. session = Session()
  170. result = (
  171. session.query(DBCollection)
  172. .filter_by(parent_path=path).count() > 0)
  173. session.close()
  174. return result
  175. @classmethod
  176. def is_leaf(cls, path):
  177. if not path:
  178. return False
  179. session = Session()
  180. result = (
  181. session.query(DBItem)
  182. .filter_by(collection_path=path).count() > 0)
  183. session.close()
  184. return result
  185. @property
  186. def last_modified(self):
  187. return time.strftime(
  188. "%a, %d %b %Y %H:%M:%S +0000", self._modification_time.timetuple())
  189. @property
  190. @contextmanager
  191. def props(self):
  192. # On enter
  193. properties = {}
  194. db_properties = (
  195. self.session.query(DBProperty)
  196. .filter_by(collection_path=self.path).all())
  197. for prop in db_properties:
  198. properties[prop.key] = prop.value
  199. old_properties = properties.copy()
  200. yield properties
  201. # On exit
  202. if self._db_collection and old_properties != properties:
  203. for prop in db_properties:
  204. self.session.delete(prop)
  205. for key, value in properties.items():
  206. prop = DBProperty()
  207. prop.key = key
  208. prop.value = value
  209. prop.collection_path = self.path
  210. self.session.add(prop)
  211. @property
  212. def items(self):
  213. return self._query(
  214. (ical.Event, ical.Todo, ical.Journal, ical.Card, ical.Timezone))
  215. @property
  216. def components(self):
  217. return self._query((ical.Event, ical.Todo, ical.Journal, ical.Card))
  218. @property
  219. def events(self):
  220. return self._query((ical.Event,))
  221. @property
  222. def todos(self):
  223. return self._query((ical.Todo,))
  224. @property
  225. def journals(self):
  226. return self._query((ical.Journal,))
  227. @property
  228. def timezones(self):
  229. return self._query((ical.Timezone,))
  230. @property
  231. def cards(self):
  232. return self._query((ical.Card,))
  233. def save(self):
  234. """Save the text into the collection.
  235. This method is not used for databases.
  236. """