d120774 by Anon Ray at 2014-03-08 1
# -*- coding utf-8 -*-
2
# classes/views/context.py
3
cffc26d by Anon Ray at 2014-06-10 4
from flask import Module, request, render_template, redirect,\
5
    url_for, json, current_app, abort
d120774 by Anon Ray at 2014-03-08 6
1e7645a by Anon Ray at 2014-06-03 7
from swtstore.classes.models import Context, User
d120774 by Anon Ray at 2014-03-08 8
9
10
context = Module(__name__)
11
cffc26d by Anon Ray at 2014-06-10 12
d120774 by Anon Ray at 2014-03-08 13
@context.route('/register', methods=['GET', 'POST'])
14
def register():
15
    current_user = User.getCurrentUser()
16
    if current_user is None:
8fe0bbc by Anon Ray at 2014-05-10 17
        return redirect(url_for('frontend.index'))
d120774 by Anon Ray at 2014-03-08 18
19
    if request.method == 'GET':
427df6d by Anon Ray at 2014-05-11 20
        return render_template('context/register.html')
d120774 by Anon Ray at 2014-03-08 21
22
    if request.method == 'POST':
cffc26d by Anon Ray at 2014-06-10 23
        if not request.form.get('name') or not request.form.get('defn'):
24
            abort(400)
d120774 by Anon Ray at 2014-03-08 25
8fe0bbc by Anon Ray at 2014-05-10 26
    current_app.logger.debug('New Context: defn: %s ',
27
                             request.form.get('defn'))
d120774 by Anon Ray at 2014-03-08 28
    json_ld = json.loads(request.form.get('defn'))
8fe0bbc by Anon Ray at 2014-05-10 29
    current_app.logger.debug('Resulting json_ld %s', json_ld)
d120774 by Anon Ray at 2014-03-08 30
31
    new_context = Context(
cffc26d by Anon Ray at 2014-06-10 32
        name=request.form.get('name'),
33
        definition=json_ld,
34
        user_id=current_user.id
d120774 by Anon Ray at 2014-03-08 35
    )
8fe0bbc by Anon Ray at 2014-05-10 36
    current_app.logger.debug('New Context created: %s', new_context)
d120774 by Anon Ray at 2014-03-08 37
    new_context.persist()
38
8fe0bbc by Anon Ray at 2014-05-10 39
    return redirect(url_for('user.myContexts'))