2021-01-16 22:15:10 +08:00
|
|
|
import config
|
|
|
|
|
import sqlite3
|
|
|
|
|
import json
|
2021-01-19 22:20:11 +08:00
|
|
|
import utils
|
|
|
|
|
import threading
|
2021-01-24 14:38:08 +08:00
|
|
|
import logging
|
2021-02-06 15:20:23 +08:00
|
|
|
import dt
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
def SafeDatabaseOperation(func):
|
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
|
with self.mutex:
|
|
|
|
|
# check database and acquire cursor
|
|
|
|
|
try:
|
|
|
|
|
self.check_database()
|
|
|
|
|
self.cursor = self.db.cursor()
|
2021-01-24 14:38:08 +08:00
|
|
|
except Exception as e:
|
2021-01-19 22:20:11 +08:00
|
|
|
self.cursor = None
|
2021-01-24 14:38:08 +08:00
|
|
|
if config.CustomConfig['debug']:
|
|
|
|
|
logging.exception(e)
|
|
|
|
|
return (False, str(e), None)
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
# do real data work
|
|
|
|
|
try:
|
2021-01-25 20:42:06 +08:00
|
|
|
currentTime = utils.GetCurrentTimestamp()
|
|
|
|
|
if currentTime - self.latestClean > config.CustomConfig['auto-token-clean-duration']:
|
|
|
|
|
self.latestClean = currentTime
|
2021-01-24 14:38:08 +08:00
|
|
|
print('Cleaning outdated token...')
|
|
|
|
|
self.tokenOper_clean()
|
|
|
|
|
|
|
|
|
|
result = (True, '', func(self, *args, **kwargs))
|
2021-01-19 22:20:11 +08:00
|
|
|
self.cursor.close()
|
|
|
|
|
self.cursor = None
|
|
|
|
|
self.db.commit()
|
|
|
|
|
return result
|
2021-01-24 14:38:08 +08:00
|
|
|
except Exception as e:
|
2021-01-19 22:20:11 +08:00
|
|
|
self.cursor.close()
|
|
|
|
|
self.cursor = None
|
|
|
|
|
self.db.rollback()
|
2021-01-24 14:38:08 +08:00
|
|
|
if config.CustomConfig['debug']:
|
|
|
|
|
logging.exception(e)
|
|
|
|
|
return (False, str(e), None)
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
return wrapper
|
2021-01-16 22:15:10 +08:00
|
|
|
|
|
|
|
|
class CalendarDatabase(object):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.db = None
|
2021-01-19 22:20:11 +08:00
|
|
|
self.cursor = None
|
|
|
|
|
self.mutex = threading.Lock()
|
2021-01-25 20:42:06 +08:00
|
|
|
self.latestClean = 0
|
2021-01-16 22:15:10 +08:00
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
|
if (self.is_database_valid()):
|
|
|
|
|
raise Exception('Databade is opened')
|
|
|
|
|
|
|
|
|
|
if config.CustomConfig['database-type'] == 'sqlite':
|
2021-01-25 20:42:06 +08:00
|
|
|
self.db = sqlite3.connect(config.CustomConfig['database-config']['url'], check_same_thread = False)
|
2021-02-05 17:07:20 +08:00
|
|
|
self.db.execute('PRAGMA encoding = "UTF-8";')
|
|
|
|
|
self.db.execute('PRAGMA foreign_keys = ON;')
|
2021-01-16 22:15:10 +08:00
|
|
|
elif config.CustomConfig['database-type'] == 'mysql':
|
|
|
|
|
raise Exception('Not implemented database')
|
|
|
|
|
else:
|
|
|
|
|
raise Exception('Unknow database type')
|
|
|
|
|
|
|
|
|
|
def init(self, username, password):
|
|
|
|
|
if (self.is_database_valid()):
|
2021-01-24 14:38:08 +08:00
|
|
|
raise Exception('Database is opened')
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2021-01-19 22:20:11 +08:00
|
|
|
# establish tables
|
2021-01-16 22:15:10 +08:00
|
|
|
self.open()
|
2021-01-19 22:20:11 +08:00
|
|
|
cursor = self.db.cursor()
|
2021-01-16 22:15:10 +08:00
|
|
|
with open('sql/sqlite.sql', 'r', encoding='utf-8') as fsql:
|
|
|
|
|
cursor.executescript(fsql.read())
|
|
|
|
|
|
2021-01-19 22:20:11 +08:00
|
|
|
# finish init
|
2021-01-24 14:38:08 +08:00
|
|
|
cursor.execute('INSERT INTO user VALUES (?, ?, ?, ?);', (
|
2021-01-19 22:20:11 +08:00
|
|
|
username,
|
|
|
|
|
utils.ComputePasswordHash(password),
|
|
|
|
|
1,
|
2021-01-24 14:38:08 +08:00
|
|
|
utils.GenerateSalt()
|
2021-01-19 22:20:11 +08:00
|
|
|
))
|
|
|
|
|
cursor.close()
|
|
|
|
|
self.db.commit()
|
2021-01-16 22:15:10 +08:00
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
self.check_database()
|
|
|
|
|
self.db.close()
|
|
|
|
|
self.db = None
|
|
|
|
|
|
|
|
|
|
def check_database(self):
|
|
|
|
|
if (not self.is_database_valid()):
|
|
|
|
|
raise Exception('Databade is None')
|
|
|
|
|
|
|
|
|
|
def is_database_valid(self):
|
|
|
|
|
return not (self.db == None)
|
|
|
|
|
|
2021-01-24 14:38:08 +08:00
|
|
|
# ======================= token related internal operation
|
|
|
|
|
def tokenOper_clean(self):
|
|
|
|
|
# remove outdated token
|
|
|
|
|
self.cursor.execute('DELETE FROM token WHERE [ccn_tokenExpireOn] <= ?',(utils.GetCurrentTimestamp(), ))
|
|
|
|
|
|
|
|
|
|
def tokenOper_postpone_expireOn(self, token):
|
|
|
|
|
self.cursor.execute('UPDATE token SET [ccn_tokenExpireOn] = ? WHERE [ccn_token] = ?;', (
|
|
|
|
|
utils.GetTokenExpireOn(),
|
|
|
|
|
token
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
def tokenOper_check_valid(self, token):
|
|
|
|
|
self.tokenOper_get_username(token)
|
|
|
|
|
|
|
|
|
|
def tokenOper_is_admin(self, username):
|
|
|
|
|
self.cursor.execute('SELECT [ccn_isAdmin] FROM user WHERE [ccn_name] = ?;',(username, ))
|
|
|
|
|
cache = self.cursor.fetchone()[0]
|
2021-02-03 16:08:40 +08:00
|
|
|
return cache == 1
|
2021-01-24 14:38:08 +08:00
|
|
|
|
|
|
|
|
def tokenOper_get_username(self, token):
|
|
|
|
|
self.cursor.execute('SELECT [ccn_user] FROM token WHERE [ccn_token] = ? AND [ccn_tokenExpireOn] > ?;',(
|
2021-01-19 22:20:11 +08:00
|
|
|
token,
|
|
|
|
|
utils.GetCurrentTimestamp()
|
|
|
|
|
))
|
2021-01-25 20:42:06 +08:00
|
|
|
result = self.cursor.fetchone()[0]
|
|
|
|
|
# need postpone expire on time
|
|
|
|
|
self.tokenOper_postpone_expireOn(token)
|
|
|
|
|
return result
|
2021-01-24 14:38:08 +08:00
|
|
|
|
2021-01-19 22:20:11 +08:00
|
|
|
# =============================== # =============================== operation function
|
|
|
|
|
# =============================== common
|
2021-01-29 11:10:25 +08:00
|
|
|
|
2021-01-19 22:20:11 +08:00
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def common_salt(self, username):
|
|
|
|
|
salt = utils.GenerateSalt()
|
|
|
|
|
self.cursor.execute('UPDATE user SET [ccn_salt] = ? WHERE [ccn_name] = ?;', (
|
|
|
|
|
salt,
|
|
|
|
|
username
|
|
|
|
|
))
|
|
|
|
|
return salt
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
2021-03-08 21:56:03 +08:00
|
|
|
def common_login(self, username, password, clientUa, clientIp):
|
2021-01-19 22:20:11 +08:00
|
|
|
self.cursor.execute('SELECT [ccn_password], [ccn_salt] FROM user WHERE [ccn_name] = ?;', (username, ))
|
|
|
|
|
(gotten_salt, gotten_password) = self.cursor.fetchone()
|
|
|
|
|
|
|
|
|
|
if password == utils.ComputePasswordHashWithSalt(gotten_password, gotten_salt):
|
2021-01-20 22:57:41 +08:00
|
|
|
token = utils.GenerateToken(username)
|
2021-01-24 14:38:08 +08:00
|
|
|
self.cursor.execute('UPDATE user SET [ccn_salt] = ? WHERE [ccn_name] = ?;', (
|
2021-01-20 22:57:41 +08:00
|
|
|
utils.GenerateSalt(), # regenerate a new slat to prevent re-login try
|
|
|
|
|
username
|
|
|
|
|
))
|
2021-03-08 21:56:03 +08:00
|
|
|
self.cursor.execute('INSERT INTO token VALUES (?, ?, ?, ?, ?);', (
|
2021-01-24 14:38:08 +08:00
|
|
|
username,
|
|
|
|
|
token,
|
|
|
|
|
utils.GetTokenExpireOn(), # add 2 day from now
|
2021-03-08 21:56:03 +08:00
|
|
|
clientUa,
|
|
|
|
|
clientIp,
|
2021-01-24 14:38:08 +08:00
|
|
|
))
|
2021-01-20 22:57:41 +08:00
|
|
|
return token
|
|
|
|
|
else:
|
2021-01-23 18:37:12 +08:00
|
|
|
# throw a exception to indicate fail to login
|
2021-01-24 14:38:08 +08:00
|
|
|
raise Exception('Login authentication failed')
|
2021-01-20 22:57:41 +08:00
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
2021-03-08 21:56:03 +08:00
|
|
|
def common_webLogin(self, username, password, clientUa, clientIp):
|
2021-01-20 22:57:41 +08:00
|
|
|
self.cursor.execute('SELECT [ccn_name] FROM user WHERE [ccn_name] = ? AND [ccn_password] = ?;', (username, utils.ComputePasswordHash(password)))
|
|
|
|
|
|
|
|
|
|
if len(self.cursor.fetchall()) != 0:
|
2021-01-19 22:20:11 +08:00
|
|
|
token = utils.GenerateToken(username)
|
2021-03-08 21:56:03 +08:00
|
|
|
self.cursor.execute('INSERT INTO token VALUES (?, ?, ?, ?, ?);', (
|
2021-01-24 14:38:08 +08:00
|
|
|
username,
|
2021-01-19 22:20:11 +08:00
|
|
|
token,
|
2021-01-24 14:38:08 +08:00
|
|
|
utils.GetTokenExpireOn(), # add 2 day from now
|
2021-03-08 21:56:03 +08:00
|
|
|
clientUa,
|
|
|
|
|
clientIp,
|
2021-01-19 22:20:11 +08:00
|
|
|
))
|
|
|
|
|
return token
|
|
|
|
|
else:
|
2021-01-23 18:37:12 +08:00
|
|
|
# throw a exception to indicate fail to login
|
2021-01-24 14:38:08 +08:00
|
|
|
raise Exception('Login authentication failed')
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def common_logout(self, token):
|
2021-01-24 14:38:08 +08:00
|
|
|
self.tokenOper_check_valid(token)
|
|
|
|
|
self.cursor.execute('DELETE FROM token WHERE [ccn_token] = ?;', (token, ))
|
2021-01-31 13:50:20 +08:00
|
|
|
return True
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def common_tokenValid(self, token):
|
2021-01-24 14:38:08 +08:00
|
|
|
self.tokenOper_check_valid(token)
|
2021-01-31 13:50:20 +08:00
|
|
|
return True
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
# =============================== calendar
|
2021-02-02 12:11:13 +08:00
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def calendar_getFull(self, token, startDateTime, endDateTime):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
|
|
|
|
self.cursor.execute('SELECT calendar.* FROM calendar INNER JOIN collection \
|
|
|
|
|
ON collection.ccn_uuid = calendar.ccn_belongTo \
|
2021-02-10 21:18:47 +08:00
|
|
|
WHERE (collection.ccn_user = ? AND calendar.ccn_loopDateTimeEnd >= ? AND calendar.ccn_loopDateTimeStart - (calendar.ccn_eventDateTimeEnd - calendar.ccn_eventDateTimeStart) <= ?);',
|
2021-02-02 12:11:13 +08:00
|
|
|
(username, startDateTime, endDateTime))
|
|
|
|
|
return self.cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def calendar_getList(self, token, startDateTime, endDateTime):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
|
|
|
|
self.cursor.execute('SELECT calendar.ccn_uuid FROM calendar INNER JOIN collection \
|
|
|
|
|
ON collection.ccn_uuid = calendar.ccn_belongTo \
|
2021-02-10 21:18:47 +08:00
|
|
|
WHERE (collection.ccn_user = ? AND calendar.ccn_loopDateTimeEnd >= ? AND calendar.ccn_loopDateTimeStart - (calendar.ccn_eventDateTimeEnd - calendar.ccn_eventDateTimeStart) <= ?);',
|
2021-02-02 12:11:13 +08:00
|
|
|
(username, startDateTime, endDateTime))
|
|
|
|
|
return tuple(map(lambda x: x[0], self.cursor.fetchall()))
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def calendar_getDetail(self, token, uuid):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
|
|
|
|
self.cursor.execute('SELECT * FROM calendar WHERE [ccn_uuid] = ?;', (uuid, ))
|
|
|
|
|
return self.cursor.fetchone()
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def calendar_update(self, token, uuid, lastChange, **optArgs):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
2021-01-19 22:20:11 +08:00
|
|
|
|
2021-02-02 12:11:13 +08:00
|
|
|
# get prev data
|
|
|
|
|
self.cursor.execute('SELECT * FROM calendar WHERE [ccn_uuid] = ? AND [ccn_lastChange] = ?;', (uuid, lastChange))
|
2021-02-09 17:10:05 +08:00
|
|
|
analyseData = list(self.cursor.fetchone())
|
2021-02-02 12:11:13 +08:00
|
|
|
|
|
|
|
|
# construct update data
|
|
|
|
|
lastupdate = utils.GenerateUUID()
|
|
|
|
|
sqlList = [
|
|
|
|
|
'[ccn_lastChange] = ?',
|
|
|
|
|
]
|
|
|
|
|
argumentsList = [
|
|
|
|
|
lastupdate,
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# analyse opt arg
|
|
|
|
|
reAnalyseLoop = False
|
|
|
|
|
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('belongTo', None)
|
2021-02-02 12:11:13 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_belongTo] = ?')
|
|
|
|
|
argumentsList.append(cache)
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('title', None)
|
2021-02-02 12:11:13 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_title] = ?')
|
|
|
|
|
argumentsList.append(cache)
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('description', None)
|
2021-02-02 12:11:13 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_description] = ?')
|
|
|
|
|
argumentsList.append(cache)
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('eventDateTimeStart', None)
|
2021-02-02 12:11:13 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_eventDateTimeStart] = ?')
|
|
|
|
|
argumentsList.append(cache)
|
|
|
|
|
reAnalyseLoop = True
|
|
|
|
|
analyseData[5] = cache
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('eventDateTimeEnd', None)
|
2021-02-02 12:11:13 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_eventDateTimeEnd] = ?')
|
|
|
|
|
argumentsList.append(cache)
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('loopRules', None)
|
2021-02-02 12:11:13 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_loopRules] = ?')
|
|
|
|
|
argumentsList.append(cache)
|
|
|
|
|
reAnalyseLoop = True
|
|
|
|
|
analyseData[8] = cache
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('timezoneOffset', None)
|
2021-02-02 12:11:13 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_timezoneOffset] = ?')
|
|
|
|
|
argumentsList.append(cache)
|
|
|
|
|
reAnalyseLoop = True
|
|
|
|
|
analyseData[7] = cache
|
|
|
|
|
|
|
|
|
|
if reAnalyseLoop:
|
2021-02-07 13:44:16 +08:00
|
|
|
# re-compute loop data and upload it into list
|
|
|
|
|
sqlList.append('[ccn_loopDateTimeStart] = ?')
|
|
|
|
|
argumentsList.append(analyseData[5])
|
|
|
|
|
sqlList.append('[ccn_loopDateTimeEnd] = ?')
|
|
|
|
|
argumentsList.append(dt.ResolveLoopStr(
|
|
|
|
|
analyseData[8],
|
|
|
|
|
analyseData[5],
|
|
|
|
|
analyseData[7]
|
|
|
|
|
))
|
2021-02-02 12:11:13 +08:00
|
|
|
|
|
|
|
|
# execute
|
|
|
|
|
argumentsList.append(uuid)
|
|
|
|
|
self.cursor.execute('UPDATE calendar SET {} WHERE [ccn_uuid] = ?;'.format(', '.join(sqlList)),
|
|
|
|
|
tuple(argumentsList))
|
2021-02-02 20:19:53 +08:00
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to update due to no matched rows or too much rows.')
|
2021-02-02 12:11:13 +08:00
|
|
|
return lastupdate
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def calendar_add(self, token, belongTo, title, description, eventDateTimeStart, eventDateTimeEnd, loopRules, timezoneOffset):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
|
|
|
|
|
|
|
|
|
newuuid = utils.GenerateUUID()
|
|
|
|
|
lastupdate = utils.GenerateUUID()
|
|
|
|
|
|
2021-02-07 13:44:16 +08:00
|
|
|
# analyse loopRules and output following 2 fileds.
|
2021-02-02 12:11:13 +08:00
|
|
|
loopDateTimeStart = eventDateTimeStart
|
2021-02-07 13:44:16 +08:00
|
|
|
loopDateTimeEnd = dt.ResolveLoopStr(loopRules, eventDateTimeStart, timezoneOffset)
|
2021-02-02 12:11:13 +08:00
|
|
|
|
|
|
|
|
self.cursor.execute('INSERT INTO calendar VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
|
|
|
|
|
(newuuid,
|
|
|
|
|
belongTo,
|
|
|
|
|
title,
|
|
|
|
|
description,
|
|
|
|
|
lastupdate,
|
|
|
|
|
eventDateTimeStart,
|
|
|
|
|
eventDateTimeEnd,
|
|
|
|
|
timezoneOffset,
|
|
|
|
|
loopRules,
|
|
|
|
|
loopDateTimeStart,
|
|
|
|
|
loopDateTimeEnd))
|
|
|
|
|
return newuuid
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def calendar_delete(self, token, uuid, lastChange):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
|
|
|
|
self.cursor.execute('DELETE FROM calendar WHERE [ccn_uuid] = ? AND [ccn_lastChange] = ?;', (uuid, lastChange))
|
2021-02-02 20:19:53 +08:00
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to delete due to no matched rows or too much rows.')
|
2021-02-02 12:11:13 +08:00
|
|
|
return True
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
# =============================== collection
|
2021-02-02 20:19:53 +08:00
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_getFullOwn(self, token):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
2021-02-05 17:07:20 +08:00
|
|
|
self.cursor.execute('SELECT [ccn_uuid], [ccn_name], [ccn_lastChange] FROM collection WHERE [ccn_user] = ?;', (username, ))
|
2021-02-02 20:19:53 +08:00
|
|
|
return self.cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_getListOwn(self, token):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
|
|
|
|
self.cursor.execute('SELECT [ccn_uuid] FROM collection WHERE [ccn_user] = ?;', (username, ))
|
|
|
|
|
return tuple(map(lambda x: x[0], self.cursor.fetchall()))
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_getDetailOwn(self, token, uuid):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
2021-02-05 17:07:20 +08:00
|
|
|
self.cursor.execute('SELECT [ccn_uuid], [ccn_name], [ccn_lastChange] FROM collection WHERE [ccn_user] = ? AND [ccn_uuid] = ?;', (username, uuid))
|
2021-02-02 20:19:53 +08:00
|
|
|
return self.cursor.fetchone()
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_addOwn(self, token, newname):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
|
|
|
|
newuuid = utils.GenerateUUID()
|
|
|
|
|
lastupdate = utils.GenerateUUID()
|
|
|
|
|
self.cursor.execute('INSERT INTO collection VALUES (?, ?, ?, ?);',
|
|
|
|
|
(newuuid, newname, username, lastupdate))
|
|
|
|
|
return newuuid
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_updateOwn(self, token, uuid, newname, lastChange):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
|
|
|
|
|
|
|
|
|
lastupdate = utils.GenerateUUID()
|
2021-02-05 17:07:20 +08:00
|
|
|
self.cursor.execute('UPDATE collection SET [ccn_name] = ?, [ccn_lastChange] = ? WHERE [ccn_uuid] = ? AND [ccn_lastChange] = ?;', (
|
2021-02-02 20:19:53 +08:00
|
|
|
newname,
|
|
|
|
|
lastupdate,
|
|
|
|
|
uuid,
|
|
|
|
|
lastChange
|
|
|
|
|
))
|
|
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to update due to no matched rows or too much rows.')
|
|
|
|
|
return lastupdate
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_deleteOwn(self, token, uuid, lastChange):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
2021-01-19 22:20:11 +08:00
|
|
|
|
2021-02-05 17:07:20 +08:00
|
|
|
self.cursor.execute('DELETE FROM collection WHERE [ccn_uuid] = ? AND [ccn_lastChange] = ?;', (
|
2021-02-02 20:19:53 +08:00
|
|
|
uuid,
|
|
|
|
|
lastChange
|
|
|
|
|
))
|
|
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to delete due to no matched rows or too much rows.')
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_getSharing(self, token, uuid):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
|
|
|
|
self.cursor.execute('SELECT [ccn_target] FROM share WHERE [ccn_uuid] = ?;', (uuid, ))
|
|
|
|
|
return tuple(map(lambda x: x[0], self.cursor.fetchall()))
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_deleteSharing(self, token, uuid, target, lastChange):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
|
|
|
|
|
|
|
|
|
lastupdate = utils.GenerateUUID()
|
2021-02-05 17:07:20 +08:00
|
|
|
self.cursor.execute('UPDATE collection SET [ccn_lastChange] = ?, WHERE [ccn_uuid] = ? AND [ccn_lastChange] = ?;', (lastupdate, uuid, lastChange))
|
2021-02-02 20:19:53 +08:00
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to delete due to no matched rows or too much rows.')
|
|
|
|
|
|
|
|
|
|
self.cursor.execute('DELETE FROM share WHERE [ccn_uuid] = ? AND [ccn_target] = ?;', (uuid, target))
|
|
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to delete due to no matched rows or too much rows.')
|
|
|
|
|
|
|
|
|
|
return lastupdate
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_addSharing(self, token, uuid, target, lastChange):
|
|
|
|
|
self.tokenOper_check_valid(token)
|
|
|
|
|
|
|
|
|
|
lastupdate = utils.GenerateUUID()
|
2021-02-05 17:07:20 +08:00
|
|
|
self.cursor.execute('UPDATE collection SET [ccn_lastChange] = ? WHERE [ccn_uuid] = ? AND [ccn_lastChange] = ?;', (lastupdate, uuid, lastChange))
|
2021-02-02 20:19:53 +08:00
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to delete due to no matched rows or too much rows.')
|
|
|
|
|
|
2021-02-05 17:07:20 +08:00
|
|
|
self.cursor.execute('SELECT * FROM share WHERE [ccn_uuid] = ? AND [ccn_target] = ?;', (uuid, target))
|
2021-02-02 20:19:53 +08:00
|
|
|
if len(self.cursor.fetchall()) != 0:
|
|
|
|
|
raise Exception('Fail to insert duplicated item.')
|
|
|
|
|
self.cursor.execute('INSERT INTO share VALUES (?, ?);', (uuid, target))
|
|
|
|
|
|
|
|
|
|
return lastupdate
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def collection_getShared(self, token):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
2021-02-05 17:07:20 +08:00
|
|
|
self.cursor.execute('SELECT collection.ccn_uuid, collection.ccn_name, collection.ccn_user \
|
2021-02-02 20:19:53 +08:00
|
|
|
FROM share INNER JOIN collection \
|
|
|
|
|
ON share.ccn_uuid = collection.ccn_uuid \
|
|
|
|
|
WHERE share.ccn_target = ?;', (username, ))
|
|
|
|
|
return self.cursor.fetchall()
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
# =============================== todo
|
2021-01-20 22:57:41 +08:00
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def todo_getFull(self, token):
|
2021-01-24 14:38:08 +08:00
|
|
|
username = self.tokenOper_get_username(token)
|
2021-01-20 22:57:41 +08:00
|
|
|
self.cursor.execute('SELECT * FROM todo WHERE [ccn_belongTo] = ?;', (username, ))
|
|
|
|
|
return self.cursor.fetchall()
|
2021-01-19 22:20:11 +08:00
|
|
|
|
2021-01-20 22:57:41 +08:00
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def todo_getList(self, token):
|
2021-01-24 14:38:08 +08:00
|
|
|
username = self.tokenOper_get_username(token)
|
2021-01-20 22:57:41 +08:00
|
|
|
self.cursor.execute('SELECT [ccn_uuid] FROM todo WHERE [ccn_belongTo] = ?;', (username, ))
|
|
|
|
|
return tuple(map(lambda x: x[0], self.cursor.fetchall()))
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def todo_getDetail(self, token, uuid):
|
2021-01-24 14:38:08 +08:00
|
|
|
username = self.tokenOper_get_username(token)
|
2021-01-20 22:57:41 +08:00
|
|
|
self.cursor.execute('SELECT * FROM todo WHERE [ccn_belongTo] = ? AND [ccn_uuid] = ?;', (username, uuid))
|
|
|
|
|
return self.cursor.fetchone()
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def todo_add(self, token):
|
2021-01-24 14:38:08 +08:00
|
|
|
username = self.tokenOper_get_username(token)
|
2021-01-20 22:57:41 +08:00
|
|
|
newuuid = utils.GenerateUUID()
|
|
|
|
|
lastupdate = utils.GenerateUUID()
|
2021-01-23 18:37:12 +08:00
|
|
|
returnedData = (
|
2021-01-20 22:57:41 +08:00
|
|
|
newuuid,
|
|
|
|
|
username,
|
|
|
|
|
'',
|
|
|
|
|
lastupdate,
|
2021-01-23 18:37:12 +08:00
|
|
|
)
|
|
|
|
|
self.cursor.execute('INSERT INTO todo VALUES (?, ?, ?, ?);', returnedData)
|
|
|
|
|
return returnedData
|
2021-01-20 22:57:41 +08:00
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def todo_update(self, token, uuid, data, lastChange):
|
|
|
|
|
# check valid token
|
2021-01-24 14:38:08 +08:00
|
|
|
self.tokenOper_check_valid(token)
|
2021-01-20 22:57:41 +08:00
|
|
|
|
|
|
|
|
# update
|
2021-01-23 18:37:12 +08:00
|
|
|
newLastChange = utils.GenerateUUID()
|
2021-02-02 20:19:53 +08:00
|
|
|
self.cursor.execute('UPDATE todo SET [ccn_data] = ?, [ccn_lastChange] = ? WHERE [ccn_uuid] = ? AND [ccn_lastChange] = ?;', (
|
2021-01-20 22:57:41 +08:00
|
|
|
data,
|
2021-01-23 18:37:12 +08:00
|
|
|
newLastChange,
|
2021-02-02 20:19:53 +08:00
|
|
|
uuid,
|
|
|
|
|
lastChange
|
2021-01-20 22:57:41 +08:00
|
|
|
))
|
2021-02-02 20:19:53 +08:00
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to update due to no matched rows or too much rows.')
|
2021-01-23 18:37:12 +08:00
|
|
|
return newLastChange
|
2021-01-20 22:57:41 +08:00
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def todo_delete(self, token, uuid, lastChange):
|
|
|
|
|
# check valid token
|
2021-01-24 14:38:08 +08:00
|
|
|
self.tokenOper_check_valid(token)
|
2021-01-19 22:20:11 +08:00
|
|
|
|
2021-01-20 22:57:41 +08:00
|
|
|
# delete
|
2021-02-02 12:11:13 +08:00
|
|
|
self.cursor.execute('DELETE FROM todo WHERE [ccn_uuid] = ? AND [ccn_lastChange] = ?;', (uuid, lastChange))
|
2021-02-02 20:19:53 +08:00
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to delete due to no matched rows or too much rows.')
|
2021-01-31 13:50:20 +08:00
|
|
|
return True
|
2021-01-19 22:20:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# =============================== admin
|
2021-02-02 20:19:53 +08:00
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def admin_get(self, token):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
2021-02-03 16:08:40 +08:00
|
|
|
if not self.tokenOper_is_admin(username):
|
2021-02-02 20:19:53 +08:00
|
|
|
raise Exception('Permission denied.')
|
2021-01-19 22:20:11 +08:00
|
|
|
|
2021-02-02 20:19:53 +08:00
|
|
|
self.cursor.execute('SELECT [ccn_name], [ccn_isAdmin] FROM user;')
|
|
|
|
|
return tuple(map(lambda x: (x[0], x[1] == 1), self.cursor.fetchall()))
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def admin_add(self, token, newname):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
2021-02-03 16:08:40 +08:00
|
|
|
if not self.tokenOper_is_admin(username):
|
2021-02-02 20:19:53 +08:00
|
|
|
raise Exception('Permission denied.')
|
|
|
|
|
|
|
|
|
|
newpassword = utils.ComputePasswordHash(utils.GenerateUUID())
|
|
|
|
|
self.cursor.execute('INSERT INTO user VALUES (?, ?, ?, ?);', (
|
|
|
|
|
newname,
|
|
|
|
|
newpassword,
|
|
|
|
|
0,
|
|
|
|
|
utils.GenerateSalt()
|
|
|
|
|
))
|
|
|
|
|
return (newname, False)
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
2021-02-03 16:08:40 +08:00
|
|
|
def admin_update(self, token, _username, **optArgs):
|
2021-02-02 20:19:53 +08:00
|
|
|
username = self.tokenOper_get_username(token)
|
2021-02-03 16:08:40 +08:00
|
|
|
if not self.tokenOper_is_admin(username):
|
2021-02-02 20:19:53 +08:00
|
|
|
raise Exception('Permission denied.')
|
|
|
|
|
|
|
|
|
|
# construct data
|
|
|
|
|
sqlList = []
|
|
|
|
|
argumentsList = []
|
|
|
|
|
|
|
|
|
|
# analyse opt arg
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('password', None)
|
2021-02-02 20:19:53 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_password] = ?')
|
|
|
|
|
argumentsList.append(utils.ComputePasswordHash(cache))
|
2021-02-03 16:08:40 +08:00
|
|
|
cache = optArgs.get('isAdmin', None)
|
2021-02-02 20:19:53 +08:00
|
|
|
if cache is not None:
|
|
|
|
|
sqlList.append('[ccn_isAdmin] = ?')
|
|
|
|
|
argumentsList.append(1 if cache else 0)
|
|
|
|
|
|
|
|
|
|
# execute
|
2021-02-03 16:08:40 +08:00
|
|
|
argumentsList.append(_username)
|
2021-02-02 20:19:53 +08:00
|
|
|
self.cursor.execute('UPDATE user SET {} WHERE [ccn_name] = ?;'.format(', '.join(sqlList)),
|
|
|
|
|
tuple(argumentsList))
|
2021-02-03 16:08:40 +08:00
|
|
|
print(cache)
|
|
|
|
|
print(tuple(argumentsList))
|
2021-02-02 20:19:53 +08:00
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to update due to no matched rows or too much rows.')
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def admin_delete(self, token, username):
|
|
|
|
|
_username = self.tokenOper_get_username(token)
|
2021-02-03 16:08:40 +08:00
|
|
|
if not self.tokenOper_is_admin(_username):
|
2021-02-02 20:19:53 +08:00
|
|
|
raise Exception('Permission denied.')
|
|
|
|
|
|
|
|
|
|
# delete
|
|
|
|
|
self.cursor.execute('DELETE FROM user WHERE [ccn_name] = ?;', (username, ))
|
|
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to delete due to no matched rows or too much rows.')
|
|
|
|
|
return True
|
2021-01-16 22:15:10 +08:00
|
|
|
|
2021-03-08 21:56:03 +08:00
|
|
|
# =============================== profile
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def profile_isAdmin(self, token):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
|
|
|
|
return self.tokenOper_is_admin(username)
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def profile_changePassword(self, token, newpassword):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
|
|
|
|
self.cursor.execute('UPDATE user SET [ccn_password] = ? WHERE [ccn_name] = ?;', (
|
|
|
|
|
utils.ComputePasswordHash(newpassword),
|
|
|
|
|
username
|
|
|
|
|
))
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def profile_getToken(self, token):
|
|
|
|
|
username = self.tokenOper_get_username(token)
|
|
|
|
|
|
|
|
|
|
self.cursor.execute('SELECT * FROM token WHERE [ccn_user] = ?;', (
|
|
|
|
|
username,
|
|
|
|
|
))
|
|
|
|
|
return self.cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
@SafeDatabaseOperation
|
|
|
|
|
def profile_deleteToken(self, token, deleteToken):
|
|
|
|
|
_username = self.tokenOper_get_username(token)
|
|
|
|
|
|
|
|
|
|
# delete
|
|
|
|
|
self.cursor.execute('DELETE FROM token WHERE [ccn_user] = ? AND [ccn_token] = ?;', (
|
|
|
|
|
_username,
|
|
|
|
|
deleteToken
|
|
|
|
|
))
|
|
|
|
|
if self.cursor.rowcount != 1:
|
|
|
|
|
raise Exception('Fail to delete due to no matched rows or too much rows.')
|
|
|
|
|
return True
|
|
|
|
|
|