Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- mode: python; coding: utf-8 -*-
- from __future__ import division, print_function, unicode_literals, absolute_import
- import os, sys
- import wx
- import wx.grid
- from sqlalchemy import and_, or_
- from models import *
- from violation import ShowViolationForm
- #####################################################################################
- dtRender = wx.grid.GridCellDateTimeRenderer()
- strRender = wx.grid.GridCellStringRenderer()
- boolRender = wx.grid.GridCellBoolRenderer()
- #####################################################################################
- class ViolationTable(wx.grid.PyGridTableBase):
- columns = (
- ("Начало", wx.grid.GRID_VALUE_DATETIME, "begin", 110, dtRender),
- ("Конец", wx.grid.GRID_VALUE_DATETIME, "end", 110, dtRender),
- ("Время", wx.grid.GRID_VALUE_STRING, "duration", 50, strRender),
- ("Оборудование", wx.grid.GRID_VALUE_STRING, "dev4grid", 120, strRender),
- ("Состав", wx.grid.GRID_VALUE_STRING, "_content.note", 150, strRender),
- ("Причина", wx.grid.GRID_VALUE_STRING, "cause", 150, strRender),
- ("Дополнительно", wx.grid.GRID_VALUE_STRING, "note", 150, strRender),
- ("ОП", wx.grid.GRID_VALUE_BOOL, "special", 22, boolRender))
- def __init__(self, category=None, period=None, enterprise=None):
- """параметры:
- @category категория (связь/телемеханика)
- @period кортеж (начало периода, конец периода)
- если None, то все не завершенные записи
- @enterprise предприятие
- """
- super(ViolationTable, self).__init__()
- self.dataTypes = [c[1] for c in self.columns]
- self.refresh(category, period=period, enterprise=enterprise)
- def refresh(self, category, period=None, enterprise=None):
- special = False
- query = session.query(Disturbance)
- if period is None:
- query = query.filter(Disturbance.end == None)
- else:
- cond = []
- cond2 = []
- if period[1] is not None:
- cond.append(Disturbance.begin < period[1])
- if period[0] is not None:
- cond.append(Disturbance.begin >= period[0])
- if period[1]:
- cond2.append(Disturbance.begin < period[0])
- cond2.append(Disturbance.end == None)
- else:
- cond.append(Disturbance.end != None)
- special = True
- if cond2:
- query = query.filter(or_(and_(*cond), and_(*cond2)))
- else:
- query = query.filter(and_(*cond))
- cond = []
- if category:
- if special:
- cond.append(and_(Disturbance.begin_content_id == Content.id,
- Content.category_id == category,
- Content.note.like("Полное%")))
- else:
- cond.append(and_(Disturbance.begin_content_id == Content.id,
- Content.category_id == category))
- if enterprise:
- if isinstance(enterprise, list):
- cond.append(Disturbance.enterprise_id.in_(enterprise))
- else:
- cond.append(Disturbance.enterprise_id == enterprise)
- if cond:
- self.data = query.filter(and_(*cond)).order_by(
- Disturbance.begin, Disturbance.end).all()
- else:
- self.data = query.order_by(Disturbance.begin, Disturbance.end).all()
- def GetNumberRows(self):
- return len(self.data)
- def GetNumberCols(self):
- return len(self.columns)
- def GetColLabelValue(self, col):
- return self.columns[col][0]
- def GetRowLabelValue(self, row):
- return str(row + 1)
- def IsEmptyCell(self, row, col):
- return self.GetValue(row, col) is None
- def GetValue(self, row, col):
- v = self.data[row]
- for name in self.columns[col][2].split("."):
- v = getattr(v, name)
- return v if v is not None else ""
- def SetValue(self, row, col, value):
- pass
- def GetTypeName(self, row, col):
- return self.dataTypes[col]
- def CanGetValueAs(self, row, col, typeName):
- colType = self.dataTypes[col].split(':')[0]
- if typeName == colType:
- return True
- else:
- return False
- def CanSetValueAs(self, row, col, typeName):
- return self.CanGetValueAs(row, col, typeName)
- def updateColAttrs (self, grid) :
- for row in xrange(len(self.data)):
- attr = wx.grid.GridCellAttr()
- if row % 2:
- attr.SetBackgroundColour((0xF0, 0xF0, 0xF0))
- grid.SetRowAttr(row, attr)
- for i, c in enumerate(self.columns):
- grid.SetCellRenderer(row, i, c[4])
- for i, c in enumerate(self.columns):
- attr = wx.grid.GridCellAttr()
- if i in (2, 3, 7):
- attr.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
- else:
- attr.SetAlignment(wx.ALIGN_LEFT, wx.ALIGN_CENTRE)
- attr.SetReadOnly(True)
- grid.SetColAttr(i, attr)
- ##___________________________________________________________________________________
- class ViolationGrid(wx.grid.Grid):
- def __init__(self, parent, Editable=True, continue_=False):
- super(ViolationGrid, self).__init__(parent, -1)
- self._continue= continue_
- self.Editable = Editable
- self.SetRowLabelSize(30)
- self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
- self.Bind(wx.grid.EVT_GRID_CMD_CELL_LEFT_DCLICK, self.OnDoubleClick)
- def UpdateData(self, category=None, period=None, enterprise=None):
- self.BeginBatch()
- self.SetTable(ViolationTable(category=category, period=period,
- enterprise=enterprise))
- for i, c in enumerate(self.GetTable().columns):
- self.SetColSize(i, c[3])
- self.GetTable().updateColAttrs(self)
- self.AutoSizeRows(True)
- self.EndBatch()
- def OnKeyDown(self, event):
- if wx.WXK_RETURN == event.KeyCode:
- self.OnReturnPress(event)
- event.Skip()
- def OnReturnPress(self, event):
- if self.Editable:
- ShowViolationForm(self.GetParent(),
- self.GetTable().data[self.GetGridCursorRow()],
- continue_=self._continue)
- self.BeginBatch()
- self.Update()
- self.EndBatch()
- def OnDoubleClick(self, event):
- if self.Editable:
- ShowViolationForm(self.GetParent(),
- self.GetTable().data[event.GetRow()],
- continue_=self._continue)
- self.BeginBatch()
- self.Update()
- self.EndBatch()
- #################################### End Of File ####################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement