def logout(): serialNum = request.json['serialNum'] identifier = request.json['identifier'] commit_db('delete from cookies where id = ? and rString = ?', [serialNum, identifier]) r = {'status': "success"} return jsonify(r), 200
def deleteWord(): wordTableName = request.json['username']+'_words' row = query_db('select * from ' + wordTableName +' where id = ?' ,[request.json['wordId']],one=True) if row is not None: commit_db('delete from '+wordTableName+' where id = ?', [request.json['wordId']]) return jsonify({'status':'success'}),200 else: return jsonify({'status':'cant find word:'+ str(request.json['wordId'])}),199
def func_checkTimeStamp(): serialNum = request.json['serialNum'] identifier = request.json['identifier'] stamp = query_db('select * from cookies where id = ? and rString = ?', [serialNum,identifier],one= True) if stamp is None: return jsonify({"status":"without cookie"}),299 elif int(stamp['expiredTime']) < int(time.time()): commit_db('delete from cookies where id = ? and rString = ?', [serialNum,identifier]) return jsonify({"status":"time expired"}),398 else: return func()
def addWord(): wordTableAddr = request.json['username'] + "_words" wordInfo = [] wordInfo.append(request.json['word']) wordInfo.append(request.json['reading']) wordInfo.append(request.json['meaning']) wordInfo.append(request.json['sourceId']) wordInfo.append(request.json['page']) wordInfo.append(request.json['sentence']) commit_db( 'insert into ' + wordTableAddr + ''' (word,reading,description,sourceId,page,sentence) values (?,?,?,?,?,?)''', wordInfo) return jsonify({'status': 'success'}), 200
def updateWord(): wordTableName = request.json['username']+"_words" wordInfo = [] wordInfo.append(request.json['word']) wordInfo.append(request.json['reading']) wordInfo.append(request.json['meaning']) wordInfo.append(request.json['sourceId']) wordInfo.append(request.json['page']) wordInfo.append(request.json['sentence']) wordInfo.append(request.json['wordId']) sql = 'update '+wordTableName+''' set word = ?, reading = ?, description = ?, sourceId = ?, page = ?, sentence = ? where id is ?;''' commit_db(sql,wordInfo) return jsonify({'status':'success'}),200
def deleteSource(): sourceTableName = request.json['username'] + '_sources' wordTableName = request.json['username'] + '_words' sourceId = request.json['sourceId'] deleteSourceMode = request.json['deleteSourceMode'] if deleteSourceMode != '0' and deleteSourceMode != '1': print('error deleteSourceMode = ' + deleteSourceMode) return jsonify( {'status': 'error deleteSourceMode = ' + deleteSourceMode}), 199 row = query_db('select * from ' + sourceTableName + ' where id = ?', [sourceId], one=True) if row is not None: commit_db('delete from ' + sourceTableName + ' where id = ?', [sourceId]) else: return jsonify({'status': 'cant find source id:' + str(sourceId)}), 199 rowCount = query_db('select count(*) from ' + wordTableName + ' where sourceId = ?', [sourceId], one=True) affectedWordNum = str(rowCount['count(*)']) r = {'status': 'success'} if deleteSourceMode == '0': commit_db('delete from ' + wordTableName + ' where sourceId = ?', [sourceId]) r['detail'] = affectedWordNum + ' words deleted.' return jsonify(r), 200 elif deleteSourceMode == '1': commit_db( 'update ' + wordTableName + ' set sourceId = -1 where sourceId = ?', [sourceId]) r['detail'] = affectedWordNum + ' words updated.' return jsonify(r), 200
def register(): if not stringValid(request.json['username']): return jsonify({'status': 'username invalid'}), 202 if not stringValid(request.json['password']): return jsonify({'status': 'password invalid'}), 202 user = query_db('select * FROM account where USERNAME = ?', [request.json['username']], one=True) if user is not None: r = {'status': 'username existed'} else: r = {'status': 'success'} commit_db('insert into account values(?,?)', [request.json['username'], request.json['password']]) try: sourcesTableName = request.json['username'] + '_sources' wordsTableName = request.json['username'] + '_words' query1 = '''create table ''' query2 = '''( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, source TEXT NOT NULL );''' commit_db(query1 + sourcesTableName + query2) commit_db('''create table ''' + wordsTableName + '''( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, word TEXT NOT NULL, reading TEXT, description TEXT, sourceId INTEGER, sentence TEXT, page TEXT, pick INTEGER DEFAULT 0, correct INTEGER DEFAULT 0 );''') except sqlite3.Error as e: print('er: %s' % e.args[0]) return jsonify(r), 201
def clearOldCookies(): commit_db('delete from cookies where expiredTime < ?', [int(time.time())])