#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 17.12.2010 @author: morozov ''' import sys, cgi, datetime sys.path.append('.') import functions from wsgiref.util import application_uri config = functions.readConfig() class pageParts(object): ''' classdocs ''' def __init__(self, environ, db): ''' Constructor ''' self.environ = environ self.userName = environ['REMOTE_USER'] self.fieldStorage = cgi.FieldStorage(fp = environ['wsgi.input'], environ = environ, keep_blank_values = 1) self.db = db self.appUrl = application_uri(environ) self.requestUri = environ['REQUEST_URI'] self.errStatus = False self.errMsg = '' self.now = datetime.datetime.now() if self.fieldStorage.has_key('year') and self.fieldStorage.has_key('month') and self.fieldStorage.has_key('day'): try: self.journalDate = datetime.date(year = int(self.fieldStorage.getfirst('year')), month = int(self.fieldStorage.getfirst('month')), day = int(self.fieldStorage.getfirst('day'))) except ValueError: self.journalDate = self.now.date() else: self.journalDate = self.now.date() try: self.previousDay = self.journalDate - datetime.timedelta(days = 1) except OverflowError: self.previousDayError = True else: self.previousDayError = False try: self.nextDay = self.journalDate + datetime.timedelta(days = 1) except OverflowError: self.nextDayError = True else: self.nextDayError = False self.tomorrow = datetime.datetime.now().date() + datetime.timedelta(days = 1) if self.fieldStorage.has_key('newName')\ and self.fieldStorage.has_key('newPosition')\ and self.fieldStorage.has_key('newDepartment')\ and self.fieldStorage.has_key('newStartTimeHour')\ and self.fieldStorage.has_key('newStartTimeMinute')\ and self.fieldStorage.has_key('newEndTimeHour')\ and self.fieldStorage.has_key('newEndTimeMinute')\ and self.fieldStorage.has_key('newDescription')\ and self.fieldStorage.has_key('newChief'): try: newRecord = self.db.journalRecord(name = self.fieldStorage.getfirst('newName'), position = self.fieldStorage.getfirst('newPosition'), department = self.fieldStorage.getfirst('newDepartment'), date = self.journalDate, startTime = datetime.time(hour = int(self.fieldStorage.getfirst('newStartTimeHour')), minute = int(self.fieldStorage.getfirst('newStartTimeMinute'))), endTime = datetime.time(hour = int(self.fieldStorage.getfirst('newEndTimeHour')), minute = int(self.fieldStorage.getfirst('newEndTimeMinute'))), description = self.fieldStorage.getfirst('newDescription'), chief = self.fieldStorage.getfirst('newChief'), userName = self.userName) for value in newRecord.name, newRecord.position, newRecord.department, newRecord.description: if len(value) == 0: raise ValueError except ValueError: self.errStatus = True self.errMsg = 'Введены неверные данные. Заполняйте форму внимательнее.' else: self.db.setProperty(self.userName, 'latestName', newRecord.name) self.db.setProperty(self.userName, 'latestPosition', newRecord.position) self.db.setProperty(self.userName, 'latestDepartment', newRecord.department) self.db.setProperty(self.userName, 'latestChief', newRecord.chief) self.db.journalSaveRecord(newRecord) if self.fieldStorage.has_key('delete'): try: self.db.journalDeleteRecord(self.userName, int(self.fieldStorage.getfirst('delete'))) except: self.errStatus = True self.errMsg = 'Ошибка при удалении записи. Возможно, вы пытались удалить несуществующую запись.' self.propertyLatestName = self.db.getProperty(self.userName, 'latestName') if self.propertyLatestName == None: self.propertyLatestName = '' self.propertyLatestPosition = self.db.getProperty(self.userName, 'latestPosition') if self.propertyLatestPosition == None: self.propertyLatestPosition = '' self.propertyLatestDepartment = self.db.getProperty(self.userName, 'latestDepartment') if self.propertyLatestDepartment == None: self.propertyLatestDepartment = '' self.propertyLatestChief = self.db.getProperty(self.userName, 'latestChief') if self.propertyLatestChief == None: self.propertyLatestChief = '' def _getWeekDay(self, date): days = {0: 'понедельник', 1: 'вторник', 2: 'среда', 3: 'четверг', 4: 'пятница', 5: 'суббота', 6: 'воскресенье'} return(days[date.weekday()]) def _replaceTags(self, str): str = str.replace('<', '<') str = str.replace('>', '>') return str def header(self): output = '\n'\ '\n'\ '\n'\ ' \n'\ ' Журнал командировок\n'\ '\n'\ '\n' output += '
\n' output += ' \n' output += ' \n' output += ' \n' output += ' \n' output += ' \n' output += '
\n' try: for link in config.links: if link['url'] == '': output += '

%s

\n' % link['name'] else: output += ' %s\n' % (link['url'], link['description'], link['name']) except: None output += '
\n' output += '

Все журналы Сегодня Завтра

\n' % (self.tomorrow.day, self.tomorrow.month, self.tomorrow.year) output += '
\n' output += '

Журнал командировок

\n' output += '
\n' if self.errStatus: output += ' \n' output += ' \n' % self.errMsg output += '
%s
\n' return(output) def headerPrint(self): output = '\n'\ '\n'\ '\n'\ ' \n'\ ' Журнал командировок\n'\ '\n'\ '\n'\ ' \n' output += '

Журнал командировок

\n' return(output) def journalForDay(self): records = self.db.journalGetRecords(self.journalDate) output = ' \n' output += ' \n' output += ' \n' output += '
\n' output += ' \n' % (self.journalDate.day, self.journalDate.month, self.journalDate.year) output += ' \n' output += ' версия для печати\n' % (self.journalDate.day, self.journalDate.month, self.journalDate.year) output += '

\n' output += '

Дата:

\n' output += '
\n' % self.appUrl output += ' \n' output += ' \n' if self.previousDayError == False: output += ' \n' % (self.appUrl, self.previousDay.day, self.previousDay.month, self.previousDay.year) else: output += ' \n' output += ' \n' if self.nextDayError == False: output += ' \n' % (self.appUrl, self.nextDay.day, self.nextDay.month, self.nextDay.year) else: output += ' \n' output += ' \n' output += '
\n' output += ' .\n' % str(self.journalDate.day).zfill(2) output += ' .\n' % str(self.journalDate.month).zfill(2) output += ' \n' % self.journalDate.year output += '
\n' output += ' %s' % self._getWeekDay(self.journalDate) output += '

\n' output += '
\n' if len(records) == 0: output += '

Записи отсутствуют.

\n' else: output += ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n' for i in xrange(len(records)): if i % 2 == 1: bgcolor = ' bgcolor = "#BFD0E0"' else: bgcolor = '' output += ' \n' % bgcolor if self.db.journalCheckDeletePermissions(self.userName, records[i].key): deleteField = '' else: deleteField = '' for field in self._replaceTags(records[i].name), self._replaceTags(records[i].position), self._replaceTags(records[i].department), str(records[i].startTime.hour) + ':' + str(records[i].startTime.minute).zfill(2), str(records[i].endTime.hour) + ':' + str(records[i].endTime.minute).zfill(2), self._replaceTags(records[i].description), self._replaceTags(records[i].chief), deleteField: output += ' \n' % field output += ' \n' output += '
Фамилия И.О.ДолжностьОтделВремя началаВремя окончанияЦель и место встречиРуководительУдалить
%s
\n' return(output) def journalForDayPrint(self): records = self.db.journalGetRecords(self.journalDate) output = '

Дата: %s.%s.%s (%s)

\n' % (str(self.journalDate.day).zfill(2), str(self.journalDate.month).zfill(2), self.journalDate.year, self._getWeekDay(self.journalDate)) output += '

Отчёт сгенерирован: %s.%s.%s %s:%s

\n' % (str(self.now.day).zfill(2), str(self.now.month).zfill(2), self.now.year, self.now.hour, str(self.now.minute).zfill(2)) if len(records) == 0: output += '

Записи отсутствуют.

\n' else: output += ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n' for i in xrange(len(records)): output += ' \n' for field in self._replaceTags(records[i].name), self._replaceTags(records[i].position), self._replaceTags(records[i].department), str(records[i].startTime.hour) + ':' + str(records[i].startTime.minute).zfill(2), str(records[i].endTime.hour) + ':' + str(records[i].endTime.minute).zfill(2), self._replaceTags(records[i].description), self._replaceTags(records[i].chief): output += ' \n' % field output += ' \n' output += '
Фамилия И.О.ДолжностьОтделВремя началаВремя окончанияЦель и место встречиРуководитель
%s
\n' return(output) def addRecord(self): output = '

Добавить запись:

\n'\ '
\n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ '
Фамилия И.О.ДолжностьОтделВремя началаВремя окончанияЦель и место встречиРуководитель
\n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ '
Часы:
Минуты:
\n'\ '
\n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ ' \n'\ '
Часы:
Минуты:
\n'\ '
\n'\ '

- обязательные поля

\n'\ '

\n'\ '
\n' return(output) def allDays(self): allDays = self.db.journalListDays() output = '

Все журналы:

\n' output += '

' if len(allDays) == 0: output += '

Записи отсутствуют.

\n' else: output += ' \n' for i in xrange(len(allDays)): output += ' \n' % (allDays[i].day, allDays[i].month, allDays[i].year, str(allDays[i].day).zfill(2), str(allDays[i].month).zfill(2), allDays[i].year, self._getWeekDay(allDays[i])) output += '
%s.%s.%s%s
\n' output += '

\n' return(output) def footer(self): output = '

© 2010 Сергей Морозов

\n' output += '\n' return(output) def footerPrint(self): output = '

\n'\ '

\n'\ ' \n'\ '
\n'\ '

\n' output += '\n' return(output)