Skip to content
Snippets Groups Projects
Commit c0647a0d authored by Julian Rother's avatar Julian Rother
Browse files

Wrote a common create endpoint to replace all new_* ones

parent 9c24a5ba
No related branches found
No related tags found
No related merge requests found
......@@ -248,10 +248,6 @@ def logout():
session.pop('user')
return redirect(request.values.get('ref', url_for('index')))
@app.route('/edit', methods=['GET', 'POST'])
@mod_required
def edit(prefix="", ignore=[]):
# All editable tables are expected to have a 'time_updated' field
tabs = {
'courses': ('courses_data', 'id', ['visible', 'listed', 'title', 'short',
'handle', 'organizer', 'subject', 'semester', 'downloadable',
......@@ -262,11 +258,18 @@ def edit(prefix="", ignore=[]):
'chapters': ('chapters', 'id', ['time', 'text', 'visible', 'deleted']),
'announcements': ('announcements', 'id', ['text', 'internal', 'level', 'visible', 'deleted'])
}
@app.route('/edit', methods=['GET', 'POST'])
@mod_required
def edit(prefix="", ignore=[]):
# All editable tables are expected to have a 'time_updated' field
ignore.append('ref')
modify('BEGIN')
if request.is_json:
changes = request.get_json().items()
else:
changes = request.args.items()
created = {}
for key, val in changes:
if key in ignore:
continue
......@@ -277,8 +280,21 @@ def edit(prefix="", ignore=[]):
modify('INSERT INTO changelog ("table",id_value,id_key,field,value_new,value_old,"when",who,executed) VALUES (?,?,?,?,?,(SELECT %s FROM %s WHERE %s = ?),?,?,1)'%(column,tabs[table][0],tabs[table][1]),table,id,tabs[table][1],column,val,id,datetime.now(),session['user']['givenName'])
modify('UPDATE %s SET %s = ?, time_updated = ? WHERE %s = ?'%(tabs[table][0], column, tabs[table][1]), val, datetime.now(), id)
modify('COMMIT')
if 'ref' in request.values:
return redirect(request.values['ref'])
return "OK", 200
@app.route('/new/<table>', methods=['GET', 'POST'])
@mod_required
def create(table):
assert table in tabs
id = modify('INSERT INTO %s (created_by, time_created, time_updated) VALUES (?, ?, ?)'%tabs[table][0],
session['user']['dbid'], datetime.now(), datetime.now())
edit(prefix=table+'.'+str(id)+'.')
if 'ref' in request.values:
return redirect(request.values['ref'])
return str(id), 200
@app.route('/newcourse', methods=['GET', 'POST'])
@mod_required
def new_course():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment