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