-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
61 lines (52 loc) · 2.14 KB
/
Copy pathdebug.py
File metadata and controls
61 lines (52 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
"""
Debug module for lpm
This module allows to modify the raw database document in JSON format.
:copyright: (c) 2016 Hannes Friederich.
:license: BSD, see LICENSE for more details.
"""
from flask import Blueprint, current_app, request, flash, render_template, abort
from flask.ext.pymongo import ObjectId
from flask_wtf import Form
from wtforms import TextAreaField, SubmitField
from wtforms.validators import InputRequired
from bson.json_util import dumps, loads
from lpm.login import role_required
from lpm.utils import extract_errors
bp = Blueprint('debug', __name__)
class DebugForm(Form):
# TODO: JSON validator?
document = TextAreaField(label='Document', validators=[InputRequired()])
@bp.route('/<collection>/<id>', methods=['GET', 'POST'])
@role_required('db_debug')
def debug(collection, id):
try:
obj = current_app.mongo.db[collection].find_one(id)
if obj is None:
try:
id = ObjectId(id)
obj = current_app.mongo.db[collection].find_one_or_404(id)
except:
abort(404)
obj = dumps(obj, indent=2) # transform to pretty JSON
form = DebugForm(request.form, data=dict(document=obj))
if request.method == 'POST' and form.validate_on_submit():
try:
new_obj = loads(form.document.data)
result = current_app.mongo.db[collection].find_one_and_replace(
filter={'_id': id},
replacement=new_obj
)
if result:
flash('data successfully updated', 'success')
obj = current_app.mongo.db[collection].find_one_or_404(id)
form.document.data = dumps(obj, indent=2)
else:
flash('data update failed', 'error')
except Exception as e:
flash(e, 'error')
# show the form page
extract_errors(form)
return render_template('debug/debug_form.html', collection=collection, id=id, form=form, obj=obj)
except Exception as e:
return str(e) # always return something meaningful