Personal wiki notebook (not under development)

File.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from Persistent import Persistent, quote
  2. class File( Persistent ):
  3. """
  4. Metadata about an uploaded file. The actual file data is stored on the filesystem instead of in
  5. the database. (Binary conversion to/from PostgreSQL's bytea is too slow, and the version of
  6. psycopg2 I'm using doesn't have large object support.)
  7. """
  8. def __init__( self, object_id, revision = None, notebook_id = None, note_id = None,
  9. filename = None, size_bytes = None, content_type = None ):
  10. """
  11. Create a File with the given id.
  12. @type object_id: unicode
  13. @param object_id: id of the File
  14. @type revision: datetime or NoneType
  15. @param revision: revision timestamp of the object (optional, defaults to now)
  16. @type notebook_id: unicode or NoneType
  17. @param notebook_id: id of the notebook containing the file
  18. @type note_id: unicode or NoneType
  19. @param note_id: id of the note linking to the file
  20. @type filename: unicode
  21. @param filename: name of the file on the client
  22. @type size_bytes: int
  23. @param size_bytes: length of the file data in bytes
  24. @type content_type: unicode
  25. @param content_type: value of the Content-Type HTTP header for this file
  26. @rtype: File
  27. @return: newly constructed File
  28. """
  29. Persistent.__init__( self, object_id, revision )
  30. self.__notebook_id = notebook_id
  31. self.__note_id = note_id
  32. self.__filename = filename
  33. self.__size_bytes = size_bytes
  34. self.__content_type = content_type
  35. @staticmethod
  36. def create( object_id, notebook_id = None, note_id = None, filename = None, size_bytes = None, content_type = None ):
  37. """
  38. Convenience constructor for creating a new File.
  39. @type object_id: unicode
  40. @param object_id: id of the File
  41. @type notebook_id: unicode or NoneType
  42. @param notebook_id: id of the notebook containing the file
  43. @type note_id: unicode or NoneType
  44. @param note_id: id of the note linking to the file
  45. @type filename: unicode
  46. @param filename: name of the file on the client
  47. @type size_bytes: int
  48. @param size_bytes: length of the file data in bytes
  49. @type content_type: unicode
  50. @param content_type: value of the Content-Type HTTP header for this file
  51. @rtype: File
  52. @return: newly constructed File
  53. """
  54. return File( object_id, notebook_id = notebook_id, note_id = note_id, filename = filename,
  55. size_bytes = size_bytes, content_type = content_type )
  56. @staticmethod
  57. def sql_load( object_id, revision = None ):
  58. # Files don't store old revisions
  59. if revision:
  60. raise NotImplementedError()
  61. return \
  62. """
  63. select
  64. file.id, file.revision, file.notebook_id, file.note_id, file.filename, file.size_bytes, file.content_type
  65. from
  66. file
  67. where
  68. file.id = %s;
  69. """ % quote( object_id )
  70. @staticmethod
  71. def sql_id_exists( object_id, revision = None ):
  72. if revision:
  73. raise NotImplementedError()
  74. return "select id from file where id = %s;" % quote( object_id )
  75. def sql_exists( self ):
  76. return File.sql_id_exists( self.object_id )
  77. def sql_create( self ):
  78. return "insert into file ( id, revision, notebook_id, note_id, filename, size_bytes, content_type ) values ( %s, %s, %s, %s, %s, %s, %s );" % \
  79. ( quote( self.object_id ), quote( self.revision ), quote( self.__notebook_id ), quote( self.__note_id ),
  80. quote( self.__filename ), self.__size_bytes or 'null', quote( self.__content_type ) )
  81. def sql_update( self ):
  82. return "update file set revision = %s, notebook_id = %s, note_id = %s, filename = %s, size_bytes = %s, content_type = %s where id = %s;" % \
  83. ( quote( self.revision ), quote( self.__notebook_id ), quote( self.__note_id ), quote( self.__filename ),
  84. self.__size_bytes or 'null', quote( self.__content_type ), quote( self.object_id ) )
  85. def sql_delete( self ):
  86. return "delete from file where id = %s;" % quote( self.object_id )
  87. @staticmethod
  88. def sql_load_note_files( note_id ):
  89. return \
  90. """
  91. select
  92. file.id, file.revision, file.notebook_id, file.note_id, file.filename, file.size_bytes, file.content_type
  93. from
  94. file
  95. where
  96. file.note_id = %s;
  97. """ % quote( note_id )
  98. def to_dict( self ):
  99. d = Persistent.to_dict( self )
  100. d.update( dict(
  101. notebook_id = self.__notebook_id,
  102. note_id = self.__note_id,
  103. filename = self.__filename,
  104. size_bytes = self.__size_bytes,
  105. content_type = self.__content_type,
  106. ) )
  107. return d
  108. def __set_filename( self, filename ):
  109. self.__filename = filename
  110. notebook_id = property( lambda self: self.__notebook_id )
  111. note_id = property( lambda self: self.__note_id )
  112. filename = property( lambda self: self.__filename, __set_filename )
  113. size_bytes = property( lambda self: self.__size_bytes )
  114. content_type = property( lambda self: self.__content_type )