Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2011
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. # -*- mode: python; coding: utf-8 -*-
  2. from __future__ import division, print_function, unicode_literals, absolute_import
  3.  
  4. import os, sys
  5. import wx
  6. import wx.grid
  7.  
  8. from sqlalchemy import and_, or_
  9. from models import *
  10. from violation import ShowViolationForm
  11. #####################################################################################
  12. dtRender = wx.grid.GridCellDateTimeRenderer()
  13. strRender = wx.grid.GridCellStringRenderer()
  14. boolRender = wx.grid.GridCellBoolRenderer()
  15. #####################################################################################
  16.  
  17. class ViolationTable(wx.grid.PyGridTableBase):
  18.  
  19. columns = (
  20. ("Начало", wx.grid.GRID_VALUE_DATETIME, "begin", 110, dtRender),
  21. ("Конец", wx.grid.GRID_VALUE_DATETIME, "end", 110, dtRender),
  22. ("Время", wx.grid.GRID_VALUE_STRING, "duration", 50, strRender),
  23. ("Оборудование", wx.grid.GRID_VALUE_STRING, "dev4grid", 120, strRender),
  24. ("Состав", wx.grid.GRID_VALUE_STRING, "_content.note", 150, strRender),
  25. ("Причина", wx.grid.GRID_VALUE_STRING, "cause", 150, strRender),
  26. ("Дополнительно", wx.grid.GRID_VALUE_STRING, "note", 150, strRender),
  27. ("ОП", wx.grid.GRID_VALUE_BOOL, "special", 22, boolRender))
  28.  
  29. def __init__(self, category=None, period=None, enterprise=None):
  30. """параметры:
  31. @category категория (связь/телемеханика)
  32. @period кортеж (начало периода, конец периода)
  33. если None, то все не завершенные записи
  34. @enterprise предприятие
  35. """
  36. super(ViolationTable, self).__init__()
  37.  
  38. self.dataTypes = [c[1] for c in self.columns]
  39. self.refresh(category, period=period, enterprise=enterprise)
  40.  
  41. def refresh(self, category, period=None, enterprise=None):
  42. special = False
  43. query = session.query(Disturbance)
  44. if period is None:
  45. query = query.filter(Disturbance.end == None)
  46. else:
  47. cond = []
  48. cond2 = []
  49. if period[1] is not None:
  50. cond.append(Disturbance.begin < period[1])
  51. if period[0] is not None:
  52. cond.append(Disturbance.begin >= period[0])
  53. if period[1]:
  54. cond2.append(Disturbance.begin < period[0])
  55. cond2.append(Disturbance.end == None)
  56. else:
  57. cond.append(Disturbance.end != None)
  58. special = True
  59. if cond2:
  60. query = query.filter(or_(and_(*cond), and_(*cond2)))
  61. else:
  62. query = query.filter(and_(*cond))
  63.  
  64. cond = []
  65. if category:
  66. if special:
  67. cond.append(and_(Disturbance.begin_content_id == Content.id,
  68. Content.category_id == category,
  69. Content.note.like("Полное%")))
  70. else:
  71. cond.append(and_(Disturbance.begin_content_id == Content.id,
  72. Content.category_id == category))
  73. if enterprise:
  74. if isinstance(enterprise, list):
  75. cond.append(Disturbance.enterprise_id.in_(enterprise))
  76. else:
  77. cond.append(Disturbance.enterprise_id == enterprise)
  78.  
  79. if cond:
  80. self.data = query.filter(and_(*cond)).order_by(
  81. Disturbance.begin, Disturbance.end).all()
  82. else:
  83. self.data = query.order_by(Disturbance.begin, Disturbance.end).all()
  84.  
  85. def GetNumberRows(self):
  86. return len(self.data)
  87.  
  88. def GetNumberCols(self):
  89. return len(self.columns)
  90.  
  91. def GetColLabelValue(self, col):
  92. return self.columns[col][0]
  93.  
  94. def GetRowLabelValue(self, row):
  95. return str(row + 1)
  96.  
  97. def IsEmptyCell(self, row, col):
  98. return self.GetValue(row, col) is None
  99.  
  100. def GetValue(self, row, col):
  101. v = self.data[row]
  102. for name in self.columns[col][2].split("."):
  103. v = getattr(v, name)
  104. return v if v is not None else ""
  105.  
  106. def SetValue(self, row, col, value):
  107. pass
  108.  
  109. def GetTypeName(self, row, col):
  110. return self.dataTypes[col]
  111.  
  112. def CanGetValueAs(self, row, col, typeName):
  113. colType = self.dataTypes[col].split(':')[0]
  114. if typeName == colType:
  115. return True
  116. else:
  117. return False
  118.  
  119. def CanSetValueAs(self, row, col, typeName):
  120. return self.CanGetValueAs(row, col, typeName)
  121.  
  122. def updateColAttrs (self, grid) :
  123. for row in xrange(len(self.data)):
  124. attr = wx.grid.GridCellAttr()
  125. if row % 2:
  126. attr.SetBackgroundColour((0xF0, 0xF0, 0xF0))
  127. grid.SetRowAttr(row, attr)
  128. for i, c in enumerate(self.columns):
  129. grid.SetCellRenderer(row, i, c[4])
  130.  
  131. for i, c in enumerate(self.columns):
  132. attr = wx.grid.GridCellAttr()
  133. if i in (2, 3, 7):
  134. attr.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
  135. else:
  136. attr.SetAlignment(wx.ALIGN_LEFT, wx.ALIGN_CENTRE)
  137. attr.SetReadOnly(True)
  138. grid.SetColAttr(i, attr)
  139. ##___________________________________________________________________________________
  140.  
  141. class ViolationGrid(wx.grid.Grid):
  142. def __init__(self, parent, Editable=True, continue_=False):
  143. super(ViolationGrid, self).__init__(parent, -1)
  144.  
  145. self._continue= continue_
  146.  
  147. self.Editable = Editable
  148. self.SetRowLabelSize(30)
  149.  
  150. self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
  151. self.Bind(wx.grid.EVT_GRID_CMD_CELL_LEFT_DCLICK, self.OnDoubleClick)
  152.  
  153. def UpdateData(self, category=None, period=None, enterprise=None):
  154. self.BeginBatch()
  155. self.SetTable(ViolationTable(category=category, period=period,
  156. enterprise=enterprise))
  157.  
  158. for i, c in enumerate(self.GetTable().columns):
  159. self.SetColSize(i, c[3])
  160. self.GetTable().updateColAttrs(self)
  161. self.AutoSizeRows(True)
  162. self.EndBatch()
  163.  
  164. def OnKeyDown(self, event):
  165. if wx.WXK_RETURN == event.KeyCode:
  166. self.OnReturnPress(event)
  167. event.Skip()
  168.  
  169. def OnReturnPress(self, event):
  170. if self.Editable:
  171. ShowViolationForm(self.GetParent(),
  172. self.GetTable().data[self.GetGridCursorRow()],
  173. continue_=self._continue)
  174. self.BeginBatch()
  175. self.Update()
  176. self.EndBatch()
  177.  
  178. def OnDoubleClick(self, event):
  179. if self.Editable:
  180. ShowViolationForm(self.GetParent(),
  181. self.GetTable().data[event.GetRow()],
  182. continue_=self._continue)
  183. self.BeginBatch()
  184. self.Update()
  185. self.EndBatch()
  186. #################################### End Of File ####################################
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement