| |
@@ -11,6 +11,7 @@
|
| |
from flask.ext.openid import OpenID
|
| |
|
| |
import hubs.models
|
| |
+ import hubs.widgets
|
| |
|
| |
import datanommer.models
|
| |
|
| |
@@ -131,35 +132,70 @@
|
| |
return flask.render_template(
|
| |
'hubs.html', hub=hub, session=session, edit=True)
|
| |
|
| |
+
|
| |
def hub_edit_post(name):
|
| |
hub = get_hub(session, name)
|
| |
is_js = flask.request.form.get('js', False)
|
| |
|
| |
error = False
|
| |
+
|
| |
# Check the input submitted
|
| |
- widget_ids = [
|
| |
- w.strip().replace('widget-', '') for w in flask.request.form.getlist('widgets[]')
|
| |
+ # Right side
|
| |
+ r_widget_ids = [
|
| |
+ w.strip().replace('widget-', '')
|
| |
+ for w in flask.request.form.getlist('right_widgets[]')
|
| |
+ if w.strip()
|
| |
+ ]
|
| |
+ try:
|
| |
+ r_widget_ids = [int(w) for w in r_widget_ids]
|
| |
+ except:
|
| |
+ flask.flash('Invalid widget identifiers submitted', 'error')
|
| |
+ error = True
|
| |
+
|
| |
+ r_indexes = [
|
| |
+ i.strip() for i in flask.request.form.getlist('right_indexes[]')
|
| |
+ if i.strip()
|
| |
+ ]
|
| |
+
|
| |
+ try:
|
| |
+ r_indexes = [int(i) for i in r_indexes]
|
| |
+ except:
|
| |
+ if not is_js:
|
| |
+ flask.flash('Invalid indexes submitted', 'error')
|
| |
+ error = True
|
| |
+
|
| |
+ if len(r_widget_ids) != len(r_indexes):
|
| |
+ if not is_js:
|
| |
+ flask.flash(
|
| |
+ 'The number of indexes and the number of widgets are not of '
|
| |
+ 'the same length', 'error')
|
| |
+ error = True
|
| |
+
|
| |
+ # Left side
|
| |
+ l_widget_ids = [
|
| |
+ w.strip().replace('widget-', '')
|
| |
+ for w in flask.request.form.getlist('left_widgets[]')
|
| |
if w.strip()
|
| |
]
|
| |
try:
|
| |
- widget_ids = [int(w) for w in widget_ids]
|
| |
+ l_widget_ids = [int(w) for w in l_widget_ids]
|
| |
except:
|
| |
flask.flash('Invalid widget identifiers submitted', 'error')
|
| |
error = True
|
| |
|
| |
- indexes = [
|
| |
- i.strip() for i in flask.request.form.getlist('indexes[]')
|
| |
+ l_indexes = [
|
| |
+ i.strip() for i in flask.request.form.getlist('left_indexes[]')
|
| |
if i.strip()
|
| |
]
|
| |
|
| |
try:
|
| |
- indexes = [int(i) for i in indexes]
|
| |
+ l_indexes = [int(i) for i in l_indexes]
|
| |
except:
|
| |
if not is_js:
|
| |
flask.flash('Invalid indexes submitted', 'error')
|
| |
error = True
|
| |
|
| |
- if len(widget_ids) != len(indexes):
|
| |
+ if len(l_widget_ids) != len(l_indexes):
|
| |
if not is_js:
|
| |
flask.flash(
|
| |
'The number of indexes and the number of widgets are not of '
|
| |
@@ -168,10 +204,15 @@
|
| |
|
| |
# If all good, update the database
|
| |
if not error:
|
| |
- for cnt, wid in enumerate(widget_ids):
|
| |
+ for cnt, wid in enumerate(r_widget_ids):
|
| |
+ widget = hubs.models.Widget.get(session, wid)
|
| |
+ if widget.index != r_indexes[cnt]:
|
| |
+ widget.index = r_indexes[cnt]
|
| |
+ session.add(widget)
|
| |
+ for cnt, wid in enumerate(l_widget_ids):
|
| |
widget = hubs.models.Widget.get(session, wid)
|
| |
- if widget.index != indexes[cnt]:
|
| |
- widget.index = indexes[cnt]
|
| |
+ if widget.index != l_indexes[cnt]:
|
| |
+ widget.index = l_indexes[cnt]
|
| |
session.add(widget)
|
| |
hub.last_edited = datetime.datetime.utcnow()
|
| |
session.add(hub)
|
| |
@@ -193,6 +234,106 @@
|
| |
return flask.redirect(flask.url_for('hub', name=name))
|
| |
|
| |
|
| |
+ @app.route('/<name>/add/', methods=['GET', 'POST'])
|
| |
+ @app.route('/<name>/add', methods=['GET', 'POST'])
|
| |
+ def hub_add_widgets(name):
|
| |
+ if flask.request.method == 'POST':
|
| |
+ return hub_add_widget_post(name)
|
| |
+ else:
|
| |
+ return hub_add_widget_get(name)
|
| |
+
|
| |
+
|
| |
+ @app.route('/<name>/add/<widget_name>/', methods=['GET'])
|
| |
+ @app.route('/<name>/add/<widget_name>', methods=['GET'])
|
| |
+ def hub_add_widget(name, widget_name):
|
| |
+ hub = get_hub(session, name)
|
| |
+ side = str(flask.request.args.get('position')).lower()
|
| |
+ if side not in ['right', 'left']:
|
| |
+ flask.abort(400, 'Invalid position provided')
|
| |
+
|
| |
+ widget = None
|
| |
+ w_name = None
|
| |
+ for widgt in hubs.widgets.registry:
|
| |
+ if hubs.widgets.registry[widgt].position in ['both', side] \
|
| |
+ and widgt == widget_name:
|
| |
+ w_name = widgt
|
| |
+ widget = hubs.widgets.registry[widgt]
|
| |
+
|
| |
+ return flask.render_template(
|
| |
+ 'add_widget.html',
|
| |
+ hub=hub,
|
| |
+ widget=widget,
|
| |
+ widget_name=w_name,
|
| |
+ side=side,
|
| |
+ url_to=flask.url_for('hub_add_widgets', name=name),
|
| |
+ )
|
| |
+
|
| |
+
|
| |
+ def hub_add_widget_get(name):
|
| |
+ hub = get_hub(session, name)
|
| |
+ side = str(flask.request.args.get('position')).lower()
|
| |
+ if side not in ['right', 'left']:
|
| |
+ flask.abort(400, 'Invalid position provided')
|
| |
+
|
| |
+ widgets = [
|
| |
+ widget
|
| |
+ for widget in hubs.widgets.registry
|
| |
+ if hubs.widgets.registry[widget].position in ['both', side]
|
| |
+ ]
|
| |
+ return flask.render_template(
|
| |
+ 'add_widget.html',
|
| |
+ hub=hub,
|
| |
+ widgets=widgets,
|
| |
+ side=side,
|
| |
+ )
|
| |
+
|
| |
+
|
| |
+ def hub_add_widget_post(name):
|
| |
+ print flask.request.form
|
| |
+ widget_name = flask.request.form.get('widget_name')
|
| |
+ position = flask.request.form.get('position', '').lower()
|
| |
+ if not widget_name:
|
| |
+ flask.abort(400, 'Invalid request sent')
|
| |
+ if widget_name not in hubs.widgets.registry:
|
| |
+ flask.abort(404, 'Unknown widget called')
|
| |
+
|
| |
+ hub = get_hub(session, name)
|
| |
+
|
| |
+ widget = widget = hubs.models.Widget(
|
| |
+ plugin=widget_name, index=-1, left=position=='left')
|
| |
+ error = False
|
| |
+ config = {}
|
| |
+ for arg in widget.module.data.widget_arguments:
|
| |
+ val = flask.request.form.get(arg.name)
|
| |
+ if not val:
|
| |
+ flask.flash(
|
| |
+ 'You must provide a value for: %s' % arg.name, 'error')
|
| |
+ error = True
|
| |
+ break
|
| |
+ try:
|
| |
+ arg.validator(session, val)
|
| |
+ config[arg.name] = val
|
| |
+ except Exception as err:
|
| |
+ flask.flash('Invalid data provided, error: %s' % err, 'error')
|
| |
+ error = True
|
| |
+ if not error:
|
| |
+ widget.hub = hub
|
| |
+ widget.config = config
|
| |
+ try:
|
| |
+ session.add(widget)
|
| |
+ session.flush()
|
| |
+ widget.hub.last_edited = datetime.datetime.utcnow()
|
| |
+ session.commit()
|
| |
+ except Exception as err:
|
| |
+ print err
|
| |
+ flask.flash(
|
| |
+ 'Could not save the configuration to the database '\
|
| |
+ 'if the error persists, please warn an admin',
|
| |
+ 'error')
|
| |
+
|
| |
+ return flask.render_template(
|
| |
+ 'hubs.html', hub=hub, session=session, edit=True)
|
| |
+
|
| |
|
| |
@app.route('/<hub>/<idx>/')
|
| |
@app.route('/<hub>/<idx>')
|
| |
@@ -211,20 +352,25 @@
|
| |
return response
|
| |
|
| |
|
| |
- @app.route('/<hub>/<idx>/edit/', methods=['GET'])
|
| |
- @app.route('/<hub>/<idx>/edit', methods=['GET'])
|
| |
+ @app.route('/<hub>/<idx>/edit/', methods=['GET', 'POST'])
|
| |
+ @app.route('/<hub>/<idx>/edit', methods=['GET', 'POST'])
|
| |
+ def widget_edit(hub, idx):
|
| |
+ if flask.request.method == 'POST':
|
| |
+ return widget_edit_post(hub, idx)
|
| |
+ else:
|
| |
+ return widget_edit_get(hub, idx)
|
| |
+
|
| |
+
|
| |
def widget_edit_get(hub, idx):
|
| |
widget = get_widget(session, hub, idx)
|
| |
return flask.render_template(
|
| |
'edit.html',
|
| |
hub=hub,
|
| |
widget=widget,
|
| |
- url_to=flask.url_for('widget_edit_post', hub=hub, idx=idx)
|
| |
+ url_to=flask.url_for('widget_edit', hub=hub, idx=idx)
|
| |
)
|
| |
|
| |
|
| |
- @app.route('/<hub>/<idx>/edit/', methods=['POST'])
|
| |
- @app.route('/<hub>/<idx>/edit', methods=['POST'])
|
| |
def widget_edit_post(hub, idx):
|
| |
widget = get_widget(session, hub, idx)
|
| |
error = False
|
| |
@@ -271,7 +417,7 @@
|
| |
'Could not delete this widget from this hub in the database '\
|
| |
'if the error persists, please warn an admin',
|
| |
'error')
|
| |
- return flask.redirect(flask.url_for('hub', name=hub))
|
| |
+ return flask.redirect(flask.url_for('hub_edit', name=hub))
|
| |
|
| |
|
| |
@app.route('/source/<name>/')
|
| |