businesstrips/include/database.py

171 lines
7.0 KiB
Python
Raw Normal View History

2014-04-18 23:43:54 +04:00
'''
Created on 29.11.2010
@author: morozov
'''
import sys, MySQLdb, datetime
sys.path.append('.')
import functions
config = functions.readConfig()
class dbOperations():
class journalRecord():
def __init__(self,
key = None,
name = None,
position = None,
department = None,
date = None,
startTime = None,
endTime = None,
description = None,
chief = None,
userName = None):
self.key = key
self.name = name
self.position = position
self.department = department
self.date = date
self.startTime = startTime
self.endTime = endTime
self.description = description
self.chief = chief
self.userName = userName
def __str__(self):
return('key: \'%s\'\n'
'name: \'%s\'\n'
'position: \'%s\'\n'
'department: \'%s\'\n'
'date: \'%s\'\n'
'startTime: \'%s\'\n'
'endTime: \'%s\'\n'
'description: \'%s\'\n'
'chief: \'%s\'\n'
'userName: \'%s\'\n' % (self.key,
self.name,
self.position,
self.department,
self.date,
self.startTime,
self.endTime,
self.description,
self.chief,
self.userName))
def __repr__(self):
return(self.__str__())
def __init__(self, dbname, user, password, host='localhost', port=3306):
'''
Constructor
'''
self.db = MySQLdb.connect(
host = host,
user = user,
passwd = password,
db = dbname,
port = int(port),
connect_timeout = 10)
self.cursor = self.db.cursor()
def setProperty(self, userName, propertyName, propertyValue):
self.delProperty(userName, propertyName)
q = "INSERT INTO `user_settings` (`username`, `setting`, `value`) VALUES ('%s', '%s', '%s')" % (userName, propertyName, propertyValue)
self.cursor.execute(q)
self.db.commit()
def getProperty(self, userName, propertyName):
q = "SELECT `value` FROM `user_settings` WHERE `username` = '%s' AND `setting` = '%s'" % (userName, propertyName)
self.cursor.execute(q)
result = self.cursor.fetchone()
self.db.commit()
try:
return(result[0])
except:
return(None)
def delProperty(self, userName, propertyName):
q = "DELETE FROM `user_settings` WHERE `username` = '%s' AND `setting` = '%s'" % (userName, propertyName)
self.cursor.execute(q)
self.db.commit()
def journalGetRecord(self, key):
self.cursor.execute("SELECT `key`, `name`, `position`, `department`, `date`, `start_time`, `end_time`, `description`, `chief`, `username` FROM `journal` WHERE `key` = '%s' LIMIT 1" % key)
record = self.cursor.fetchone()
self.db.commit()
try:
result = self.journalRecord(key = record[0],
name = record[1],
position = record[2],
department = record[3],
date = record[4],
startTime = (datetime.datetime.min + record[5]).time(),
endTime = (datetime.datetime.min + record[6]).time(),
description = record[7],
chief = record[8],
userName = record[9])
except TypeError:
return(None)
else:
return(result)
def journalGetRecords(self, date):
self.cursor.execute("SELECT `key`, `name`, `position`, `department`, `date`, `start_time`, `end_time`, `description`, `chief`, `username` FROM `journal` WHERE `date` = '%s' ORDER BY `start_time`, `key`" % date)
rows = self.cursor.fetchall()
self.db.commit()
result = []
for i in xrange(len(rows)):
result.append(self.journalRecord(key = rows[i][0],
name = rows[i][1],
position = rows[i][2],
department = rows[i][3],
date = rows[i][4],
startTime = (datetime.datetime.min + rows[i][5]).time(),
endTime = (datetime.datetime.min + rows[i][6]).time(),
description = rows[i][7],
chief = rows[i][8],
userName = rows[i][9]))
return(result)
def journalSaveRecord(self, journalRecord):
self.cursor.execute("INSERT INTO `journal` (`name`, `position`, `department`, `date`, `start_time`, `end_time`, `description`, `chief`, `username`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", (journalRecord.name, journalRecord.position, journalRecord.department, journalRecord.date, journalRecord.startTime, journalRecord.endTime, journalRecord.description, journalRecord.chief, journalRecord.userName))
self.db.commit()
def journalDeleteRecord(self, userName, key):
if self.journalCheckDeletePermissions(userName, key):
q = "DELETE FROM `journal` WHERE `key` = '%s'" % key
self.cursor.execute(q)
self.db.commit()
else:
pass
def journalCheckDeletePermissions(self, userName, key):
record = self.journalGetRecord(key)
self.db.commit()
if userName == record.userName or userName in config.allow_delete_any_record:
return(True)
else:
return(False)
def journalListDays(self, limit = None):
if limit == None:
limit = ''
else:
limit = 'LIMIT %s' % limit
q = "SELECT DISTINCT `date` FROM `journal` ORDER BY `date` DESC %s" % limit
self.cursor.execute(q)
days = self.cursor.fetchall()
self.db.commit()
result = []
for i in xrange(len(days)):
result.append(days[i][0])
return(result)
def close(self):
self.cursor.close()
self.db.close()
pass