diff --git a/chapters.py b/chapters.py index bbb60eff63a6f67e5973bdbaa50620c2c3742eb0..c5573fb9ac623ac343485ca7de239b2bdd715678 100644 --- a/chapters.py +++ b/chapters.py @@ -40,7 +40,8 @@ def suggest_chapter(lectureid): course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0] id = modify( 'INSERT INTO chapters (lecture_id, time, text, time_created, time_updated, created_by, submitted_by) VALUES (?, ?, ?, ?, ?, ?, ?)', - lectureid, chapter_start, text, datetime.now(), datetime.now(), session.get('user', {'dbid':None})['dbid'], submitter + lectureid, chapter_start, text, datetime.now(), datetime.now(), session.get('user', {'dbid':None})['dbid'], submitter, + get_id=True ) chapter = query('SELECT * FROM chapters WHERE id = ?', id)[0] if not ismod(): diff --git a/db.py b/db.py index d12252c9915bfb59744456448cd53a16eabc5c51..583035c9336b4f33e2406395c8fddd833da3987d 100644 --- a/db.py +++ b/db.py @@ -100,8 +100,8 @@ def query(operation, *params, delim="sep", nlfix=True): try: cur = get_dbcursor() cur.execute(operation, params) - except mysql.connector.errors.InternalError as e: - if e.msg == 'Deadlock found when trying to get lock; try restarting transaction': + except Exception as e: + if str(e) == 'Deadlock found when trying to get lock; try restarting transaction': tries += 1 retry = True else: @@ -109,8 +109,8 @@ def query(operation, *params, delim="sep", nlfix=True): rows = [] try: rows = cur.fetchall() - except mysql.connector.errors.InterfaceError as e: - if e.msg == 'No result set to fetch from.': + except Exception as e: + if str(e) == 'No result set to fetch from.' or str(e) == "the last operation didn't produce a result": # no problem, we were just at the end of the result set pass else: @@ -129,10 +129,12 @@ def query(operation, *params, delim="sep", nlfix=True): ptr[name] = col return res -def modify(operation, *params): +def modify(operation, *params, get_id=False): operation, params = fix_query(operation, params) cur = get_dbcursor() cur.execute(operation, params) + if not get_id: + return None return cur.lastrowid @app.teardown_request diff --git a/edit.py b/edit.py index 711e8a4704051b68c30c081d8fb8483e299b2f5d..6da7d39ff231798cd1b5512a8ec173020984dbc4 100644 --- a/edit.py +++ b/edit.py @@ -227,8 +227,10 @@ def create(table): assert column not in defaults columns.append('"'+column+'"') values.append(val) + assert editable_tables[table]['idcolumn'] == 'id' id = modify('INSERT INTO %s (%s) VALUES (%s)'%(editable_tables[table]['table'], - ','.join(columns), ','.join(['?']*len(values))), *values) + ','.join(columns), ','.join(['?']*len(values))), *values, + get_id=True) if table == 'courses': set_responsible(id, session['user']['dbid'], 1) if 'ref' in request.values: diff --git a/jobmanagement.py b/jobmanagement.py index 3378ef9400d26c7fc351278a4feab472a55ea669..8f84ce5ec42f6962fdd78be01cd2a57e9458d36b 100644 --- a/jobmanagement.py +++ b/jobmanagement.py @@ -50,7 +50,8 @@ def schedule_job(jobtype, data=None, priority=0, queue="default"): if not data: data = {} return modify('INSERT INTO jobs (type, priority, queue, data, time_created) VALUES (?, ?, ?, ?, ?)', - jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now()) + jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now(), + get_id=True) def cancel_job(job_id): query('UPDATE jobs SET state = \'deleted\' WHERE id = ? AND state = \'ready\'', job_id) diff --git a/sorter.py b/sorter.py index a0d24c11e031eb11bfd4758ce61982a5cc4e2a3f..258b242d8b2c0d72be0451ea0ea2b29d18ece8ff 100644 --- a/sorter.py +++ b/sorter.py @@ -61,7 +61,8 @@ def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1, dura (lecture_id, visible, path, video_format, title, comment, internal, file_modified, time_created, time_updated, created_by, hash, file_size, duration, source) VALUES (?, ?, ?, ?, '', '', '', ?, ?, ?, ?, ?, ?, ?, ?)''', - lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize, duration, sourceid) + lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize, duration, sourceid, + get_id=True) if not sourceid: query('INSERT INTO sortlog (lecture_id,video_id,path,"when") VALUES (?,?,?,?)', lectureid, video_id, dbfilepath, datetime.now()) schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True})