1 |
|
2 |
# -*- coding utf-8 -*- |
3 |
# classes/views/context.py |
4 |
|
5 |
|
6 |
from flask import Module, jsonify, request, render_template, redirect, url_for |
7 |
import json |
8 |
|
9 |
from swtstore.classes.models import Context |
10 |
from swtstore.classes.models.um import User |
11 |
|
12 |
|
13 |
context = Module(__name__) |
14 |
|
15 |
@context.route('/', methods=['GET']) |
16 |
# list apps owned by current user |
17 |
def list(): |
18 |
current_user = User.getCurrentUser() |
19 |
if current_user is None: |
20 |
return redirect(url_for('index')) |
21 |
|
22 |
her_contexts = Context.getByCreator(current_user.id) |
23 |
print 'her_apps' |
24 |
print her_contexts |
25 |
|
26 |
return render_template('list_contexts.html', contexts=her_contexts, |
27 |
user=current_user.to_dict()) |
28 |
|
29 |
|
30 |
@context.route('/register', methods=['GET', 'POST']) |
31 |
def register(): |
32 |
current_user = User.getCurrentUser() |
33 |
if current_user is None: |
34 |
return redirect(url_for('index')) |
35 |
|
36 |
user = current_user.to_dict() |
37 |
|
38 |
if request.method == 'GET': |
39 |
return render_template('register_context.html', user=user) |
40 |
|
41 |
if request.method == 'POST': |
42 |
if not request.form.get('name') or not request.form.get('defn'): |
43 |
abort(400) |
44 |
|
45 |
print request.form.get('defn') |
46 |
json_ld = json.loads(request.form.get('defn')) |
47 |
print json_ld |
48 |
|
49 |
new_context = Context( |
50 |
name = request.form.get('name'), |
51 |
definition = json_ld, |
52 |
user_id = current_user.id |
53 |
) |
54 |
print new_context |
55 |
new_context.persist() |
56 |
|
57 |
return redirect(url_for('list')) |