d120774 by Anon Ray at 2014-03-08 |
1 |
|
|
2 |
# -*- coding utf-8 -*- |
|
3 |
# classes/views/apps.py |
|
4 |
|
|
5 |
|
f867edb by Anon Ray at 2014-04-10 |
6 |
from flask import Module, jsonify, request, render_template, redirect,\ |
|
7 |
url_for, flash, abort |
|
8 |
|
d120774 by Anon Ray at 2014-03-08 |
9 |
from hashlib import md5 |
|
10 |
from werkzeug.security import gen_salt |
|
11 |
|
|
12 |
from swtstore.classes.models import Client |
|
13 |
from swtstore.classes.models.um import User |
f867edb by Anon Ray at 2014-04-10 |
14 |
from swtstore.classes.utils import urlnorm |
d120774 by Anon Ray at 2014-03-08 |
15 |
|
|
16 |
|
|
17 |
app = Module(__name__) |
|
18 |
|
|
19 |
@app.route('/', methods=['GET']) |
|
20 |
# list apps owned by current user |
|
21 |
def list(): |
|
22 |
current_user = User.getCurrentUser() |
|
23 |
if current_user is None: |
f867edb by Anon Ray at 2014-04-10 |
24 |
return redirect(url_for('frontend.index')) |
d120774 by Anon Ray at 2014-03-08 |
25 |
|
|
26 |
her_apps = Client.getClientsByCreator(current_user.id) |
|
27 |
print 'her_apps' |
|
28 |
print her_apps |
|
29 |
return render_template('list_apps.html', apps=her_apps, |
|
30 |
user=current_user.to_dict()) |
|
31 |
|
|
32 |
|
|
33 |
@app.route('/register', methods=['GET', 'POST']) |
|
34 |
def register(): |
|
35 |
current_user = User.getCurrentUser() |
|
36 |
if current_user is None: |
f867edb by Anon Ray at 2014-04-10 |
37 |
return redirect(url_for('frontend.index')) |
d120774 by Anon Ray at 2014-03-08 |
38 |
|
|
39 |
user = current_user.to_dict() |
|
40 |
|
|
41 |
if request.method == 'GET': |
|
42 |
return render_template('register_app.html', user=user) |
|
43 |
|
|
44 |
elif request.method == 'POST': |
f867edb by Anon Ray at 2014-04-10 |
45 |
req_fields = ['name', 'host_url', 'redirect_uris', 'scopes'] |
|
46 |
for field in req_fields: |
|
47 |
if not request.form.get(field): |
|
48 |
abort(404) |
d120774 by Anon Ray at 2014-03-08 |
49 |
|
|
50 |
new_app = Client( |
|
51 |
id = gen_salt(40), |
|
52 |
client_secret = gen_salt(50), |
|
53 |
name = request.form.get('name'), |
|
54 |
description = request.form.get('desc'), |
|
55 |
user_id = current_user.id, |
f867edb by Anon Ray at 2014-04-10 |
56 |
_host_url = request.form.get('host_url'), |
|
57 |
_redirect_uris = urlnorm(request.form.get('redirect_uris')), |
|
58 |
_default_scopes = ' '.join(request.form.get('scopes').split(',')), |
d120774 by Anon Ray at 2014-03-08 |
59 |
_is_private = False |
|
60 |
) |
|
61 |
new_app.persist() |
|
62 |
|
|
63 |
return redirect(url_for('list')) |
|
64 |
|