database.py 8.6 KB

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