data.py 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # -*- coding: utf-8 -*-
  2. """
  3. """
  4. import os
  5. import re
  6. from datetime import datetime
  7. from hashlib import sha256
  8. __all__ = ['User', 'Group', 'Permission']
  9. from sqlalchemy import Table, ForeignKey, Column, Sequence
  10. from sqlalchemy.types import Unicode, Integer, DateTime, Text
  11. from sqlalchemy.orm import relation, synonym
  12. from pboard.model import DeclarativeBase, metadata, DBSession
  13. # This is the association table for the many-to-many relationship between
  14. # groups and permissions.
  15. pb_node_table = Table('pb_nodes', metadata,
  16. Column('node_id', Integer, Sequence('pb_nodes__node_id__sequence'), primary_key=True),
  17. Column('parent_id', Integer, ForeignKey('pb_nodes.node_id'), nullable=True, default=None),
  18. Column('node_order', Integer, nullable=True, default=1),
  19. Column('node_type', Unicode(16), unique=False, nullable=False, default=u'data'),
  20. Column('node_status', Unicode(16), unique=False, nullable=False, default=u'new'),
  21. Column('created_at', DateTime, unique=False, nullable=False),
  22. Column('updated_at', DateTime, unique=False, nullable=False),
  23. Column('data_label', Unicode(1024), unique=False, nullable=False, default=u''),
  24. Column('data_content', Text(), unique=False, nullable=False, default=u''),
  25. Column('data_datetime', DateTime, unique=False, nullable=False),
  26. Column('data_reminder_datetime', DateTime, unique=False, nullable=True),
  27. )
  28. """
  29. - node_type
  30. - node_created_at
  31. - node_updated_at
  32. - data_label
  33. - data_content
  34. - data_source_url
  35. - data_status_id
  36. """
  37. class PBNodeStatusItem(object):
  38. def __init__(self, psStatusId, psStatusLabel, psStatusFamily, psIconId, psCssClass): #, psBackgroundColor):
  39. self._sStatusId = psStatusId
  40. self._sStatusLabel = psStatusLabel
  41. self._sStatusFamily = psStatusFamily
  42. self._sIconId = psIconId
  43. self._sCssClass = psCssClass
  44. # self._sBackgroundColor = psBackgroundColor
  45. def getLabel(self):
  46. return self._sStatusLabel
  47. @property
  48. def icon(self):
  49. return self._sIconId
  50. def getId(self):
  51. return self._sStatusId
  52. @property
  53. def css(self):
  54. return self._sCssClass
  55. @property
  56. def status_id(self):
  57. return self._sStatusId
  58. @property
  59. def icon_id(self):
  60. return self._sIconId
  61. @property
  62. def label(self):
  63. return self._sStatusLabel
  64. class PBNodeStatus(object):
  65. StatusList = dict()
  66. StatusList['immortal'] = PBNodeStatusItem('immortal', 'Information', 'normal', 'icon-g-circle-info', 'pod-status-grey-light')
  67. StatusList['new'] = PBNodeStatusItem('new', 'New', 'open', 'icon-g-lightbulb icon-g-white', 'btn-success')
  68. StatusList['inprogress'] = PBNodeStatusItem('inprogress', 'In progress', 'open', ' icon-g-roundabout icon-g-white', 'btn-info')
  69. StatusList['standby'] = PBNodeStatusItem('standby', 'In Standby', 'open', 'icon-g-clock icon-g-white', 'btn-warning')
  70. StatusList['hot'] = PBNodeStatusItem('hot', 'Hot', 'open', 'icon-g-warning-sign icon-g-white', 'btn-danger')
  71. StatusList['done'] = PBNodeStatusItem('done', 'Done', 'closed', 'icon-g-ok-2', 'pod-status-grey-light')
  72. StatusList['closed'] = PBNodeStatusItem('closed', 'Closed', 'closed', 'icon-g-lightbulb', 'pod-status-grey-middle')
  73. StatusList['archived'] = PBNodeStatusItem('archived', 'Archived', 'invisible', 'icon-g-wallet', 'pod-status-grey-dark')
  74. StatusList['deleted'] = PBNodeStatusItem('deleted', 'Deleted', 'invisible', 'icon-g-bin', 'pod-status-grey-dark')
  75. @classmethod
  76. def getList(cls):
  77. return [
  78. PBNodeStatus.StatusList['immortal'],
  79. PBNodeStatus.StatusList['new'],
  80. PBNodeStatus.StatusList['inprogress'],
  81. PBNodeStatus.StatusList['standby'],
  82. PBNodeStatus.StatusList['hot'],
  83. PBNodeStatus.StatusList['done'],
  84. PBNodeStatus.StatusList['closed'],
  85. PBNodeStatus.StatusList['archived'],
  86. PBNodeStatus.StatusList['deleted']
  87. ]
  88. PBNodeStatus.StatusList.values()
  89. @classmethod
  90. def getStatusItem(cls, psStatusId):
  91. print "====> ID=", psStatusId
  92. print "====> ITEM=", PBNodeStatus.StatusList[psStatusId]
  93. return PBNodeStatus.StatusList[psStatusId]
  94. class PBNodeType(object):
  95. Node = 'node'
  96. Folder = 'folder'
  97. Data = 'data'
  98. File = 'file'
  99. Event = 'event'
  100. Contact = 'contact'
  101. Comment = 'comment'
  102. class PBNode(object):
  103. def getChildrenOfType(self, plNodeTypeList, plSortingCriteria):
  104. """return all children nodes of type 'data' or 'node' or 'folder'"""
  105. print "NODE = ", self.node_id
  106. print "######"
  107. print plNodeTypeList
  108. print "######"
  109. print "######"
  110. return DBSession.query(PBNode).filter(PBNode.parent_id==self.node_id).filter(PBNode.node_type.in_(plNodeTypeList)).order_by(plSortingCriteria).all()
  111. def getChildNb(self):
  112. liChildNb = DBSession.query(PBNode).filter(PBNode.parent_id==self.node_id).filter(PBNode.node_type==PBNodeType.Data).count()
  113. print "CHILDREN of ", self.node_id, " are ", liChildNb
  114. return liChildNb
  115. def getChildren(self):
  116. """return all children nodes of type 'data' or 'node' or 'folder'"""
  117. return self.getChildrenOfType([PBNodeType.Node, PBNodeType.Folder, PBNodeType.Data], PBNode.node_order.asc())
  118. def getContacts(self):
  119. """return all children nodes of type 'data' or 'node' or 'folder'"""
  120. return self.getChildrenOfType([PBNodeType.Contact], PBNode.data_label.asc())
  121. def getEvents(self):
  122. """print "---------------------------"
  123. print self.getChildrenOfType((PBNodeType.Event,), PBNode.data_datetime.desc())
  124. print "---------------------------"
  125. print "---------------------------"
  126. return self.getChildrenOfType((PBNodeType.Event,), PBNode.data_datetime.desc())
  127. """
  128. return DBSession.query(PBNode).filter(PBNode.parent_id==self.node_id).filter(PBNode.node_type==PBNodeType.Event).order_by(PBNode.data_datetime.desc()).all()
  129. return []
  130. def getIconClass(self):
  131. laIconClass = dict()
  132. laIconClass['node'] = 'icon-g-folder-open'
  133. laIconClass['folder'] = 'icon-g-folder-open'
  134. laIconClass['data'] = 'icon-g-file'
  135. laIconClass['file'] = 'icon-file'
  136. laIconClass['event'] = 'icon-time' # icon-calendar
  137. laIconClass['contact'] = 'icon-user'
  138. laIconClass['comment'] = 'icon-comment'
  139. if self.node_type==PBNodeType.Data and self.getChildNb()>0:
  140. return laIconClass['folder']
  141. else:
  142. return laIconClass[self.node_type]
  143. def getFormattedDateTime(self, poDateTime, psDateTimeFormat = '%d/%m/%Y @ %H:%M'):
  144. return poDateTime.strftime(psDateTimeFormat)
  145. def getFormattedDate(self, poDateTime, psDateTimeFormat = '%d/%m/%Y'):
  146. return poDateTime.strftime(psDateTimeFormat)
  147. def getFormattedTime(self, poDateTime, psDateTimeFormat = '%H:%M'):
  148. return poDateTime.strftime(psDateTimeFormat)
  149. def getStatus(self):
  150. return PBNodeStatus.getStatusItem(self.node_status)
  151. def getTruncatedLabel(self, piCharNb):
  152. lsTruncatedLabel = u''
  153. liMaxLength = int(piCharNb)
  154. if len(self.data_label)>liMaxLength:
  155. lsTruncatedLabel = self.data_label[0:liMaxLength-1]+u'…'
  156. else:
  157. lsTruncatedLabel = self.data_label
  158. return lsTruncatedLabel
  159. def getTagList(self):
  160. loPattern = re.compile('(^|\s|@)@(\w+)')
  161. loResults = re.findall(loPattern, self.data_content)
  162. lsResultList = []
  163. for loResult in loResults:
  164. lsResultList.append(loResult[1].replace('@', '').replace('_', ' '))
  165. return lsResultList
  166. @classmethod
  167. def addTagReplacement(cls, matchobj):
  168. return " <span class='badge'>%s</span> " %(matchobj.group(0).replace('@', '').replace('_', ' '))
  169. def getContentWithTags(self):
  170. lsTemporaryResult = re.sub('(^|\s)@@(\w+)', '', self.data_content) # tags with @@ are explicitly removed from the body
  171. return re.sub('(^|\s)@(\w+)', PBNode.addTagReplacement, lsTemporaryResult) # then, 'normal tags are transformed as labels'
  172. # FIXME - D.A. - 2013-09-12
  173. # Does not match @@ at end of content.
  174. from sqlalchemy.orm import mapper
  175. mapper(PBNode, pb_node_table)