Personal wiki notebook (not under development)

Tag.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from Persistent import Persistent, quote
  2. class Tag( Persistent ):
  3. """
  4. A tag for a note or a notebook.
  5. """
  6. def __init__( self, object_id, revision = None, notebook_id = None, user_id = None, name = None, description = None, value = None ):
  7. """
  8. Create a Tag with the given id.
  9. @type object_id: unicode
  10. @param object_id: id of the Tag
  11. @type revision: datetime or NoneType
  12. @param revision: revision timestamp of the object (optional, defaults to now)
  13. @type notebook_id: unicode or NoneType
  14. @param notebook_id: id of the notebook whose namespace this tag is in, if any
  15. @type user_id: unicode or NoneType
  16. @param user_id: id of the user who most recently updated this tag, if any
  17. @type name: unicode or NoneType
  18. @param name: name of the tag (optional)
  19. @type description: unicode or NoneType
  20. @param description: brief description of the tag (optional)
  21. @type value: unicode or NoneType
  22. @param value: per-note or per-notebook value of the tag (optional)
  23. @rtype: Tag
  24. @return: newly constructed Tag
  25. """
  26. Persistent.__init__( self, object_id, revision )
  27. self.__notebook_id = notebook_id
  28. self.__user_id = user_id
  29. self.__name = name
  30. self.__description = description
  31. self.__value = value
  32. @staticmethod
  33. def create( object_id, notebook_id = None, user_id = None, name = None, description = None, value = None ):
  34. """
  35. Convenience constructor for creating a new Tag.
  36. @type object_id: unicode
  37. @param object_id: id of the Tag
  38. @type notebook_id: unicode or NoneType
  39. @param notebook_id: id of the notebook whose namespace this tag is in, if any
  40. @type user_id: unicode or NoneType
  41. @param user_id: id of the user who most recently updated this tag, if any
  42. @type name: unicode or NoneType
  43. @param name: name of the tag (optional)
  44. @type description: unicode or NoneType
  45. @param description: brief description of the tag (optional)
  46. @type value: unicode or NoneType
  47. @param value: per-note or per-notebook value of the tag (optional)
  48. @rtype: Tag
  49. @return: newly constructed Tag
  50. """
  51. return Tag( object_id, notebook_id = notebook_id, user_id = user_id, name = name, description = description, value = value )
  52. @staticmethod
  53. def sql_load( object_id, revision = None ):
  54. # Tags don't store old revisions
  55. if revision:
  56. raise NotImplementedError()
  57. return \
  58. """
  59. select
  60. tag.id, tag.revision, tag.notebook_id, tag.user_id, tag.name, tag.description
  61. from
  62. tag
  63. where
  64. tag.id = %s;
  65. """ % quote( object_id )
  66. @staticmethod
  67. def sql_load_by_name( name, notebook_id = None, user_id = None ):
  68. if notebook_id:
  69. notebook_id_clause = " and tag.notebook_id = %s" % quote( notebook_id )
  70. else:
  71. notebook_id_clause = ""
  72. if user_id:
  73. user_id_clause = " and tag.user_id = %s" % quote( user_id )
  74. else:
  75. user_id_clause = ""
  76. return \
  77. """
  78. select
  79. tag.id, tag.revision, tag.notebook_id, tag.user_id, tag.name, tag.description
  80. from
  81. tag
  82. where
  83. tag.name = %s%s%s;
  84. """ % ( quote( name ), notebook_id_clause, user_id_clause )
  85. @staticmethod
  86. def sql_id_exists( object_id, revision = None ):
  87. if revision:
  88. raise NotImplementedError()
  89. return "select id from tag where id = %s;" % quote( object_id )
  90. def sql_exists( self ):
  91. return Tag.sql_id_exists( self.object_id )
  92. def sql_create( self ):
  93. return "insert into tag ( id, revision, notebook_id, user_id, name, description ) values ( %s, %s, %s, %s, %s, %s );" % \
  94. ( quote( self.object_id ), quote( self.revision ), quote( self.__notebook_id ),
  95. quote( self.__user_id ), quote( self.__name ), quote( self.__description ) )
  96. def sql_update( self ):
  97. return "update tag set revision = %s, notebook_id = %s, user_id = %s, name = %s, description = %s where id = %s;" % \
  98. ( quote( self.revision ), quote( self.__notebook_id ), quote( self.__user_id ),
  99. quote( self.__name ), quote( self.__description ), quote( self.object_id ) )
  100. def sql_delete( self ):
  101. return "delete from tag where id = %s;" % quote( self.object_id )
  102. def to_dict( self ):
  103. d = Persistent.to_dict( self )
  104. d.update( dict(
  105. notebook_id = self.__notebook_id,
  106. user_id = self.__user_id,
  107. name = self.__name,
  108. description = self.__description,
  109. value = self.__value,
  110. ) )
  111. return d
  112. notebook_id = property( lambda self: self.__notebook_id )
  113. user_id = property( lambda self: self.__user_id )
  114. name = property( lambda self: self.__name )
  115. description = property( lambda self: self.__description )
  116. value = property( lambda self: self.__value )