Commit df49470e394f57e9e27730b881bfe4b20f9250b1

timdb2.py: Added documentExists method. The method getDocumentAsHtmlBlocks will load the requested document in cache if it is not there already.
timApp/timdb/timdb2.py
(21 / 5)
  
1515import gitpylib.sync
1616import gitpylib.file
1717import gitpylib.common
18from ephemeralclient import EphemeralClient, EphemeralException
18from ephemeralclient import EphemeralClient, EphemeralException, NotInCacheException
1919import collections
2020
2121class TimDbException(Exception):
105105 def addNote(self, usergroup_id: 'int', content : 'str', block_id : 'int', block_specifier : 'int'):
106106 """Adds a note to the document.
107107
108 :param user_id: The user who created the note.
108 :param usergroup_id: The usergroup who owns the note.
109109 :param content: The content of the note.
110110 :param block_id: The block to which the comment is added.
111111 :param block_specifier: A specifier that tells a more accurate position of the note.
401401 blockIndex += 1
402402 return blocks
403403
404 @contract
405 def documentExists(self, document_id : 'int') -> 'bool':
406 """Checks whether a document with the specified id exists.
407
408 :param document_id: The id of the document.
409 :returns: True if the documents exists, false otherwise.
410 """
411
412 cursor = self.db.cursor()
413 cursor.execute('select count(id) from Block where id = ? and type_id = ?', [document_id, blocktypes.DOCUMENT])
414 result = cursor.fetchall()
415 assert len(result) <= 1, 'len(result) was more than 1'
416 return len(result) == 1
417
404418 def getDocumentAsHtmlBlocks(self, document_id : 'int') -> 'list(str)':
405419 """Gets the specified document in HTML form."""
406420
422422
423423 try:
424424 blocks = ec.getDocumentAsHtmlBlocks(document_id)
425 except EphemeralException as e:
426 raise TimDbException(str(e))
427
425 except NotInCacheException as e:
426 if self.documentExists(document_id):
427 with open(self.getBlockPath(document_id), 'rb') as f:
428 ec.loadDocument(document_id, f.read())
429 blocks = ec.getDocumentAsHtmlBlocks(document_id)
428430 return blocks
429431
430432 @contract