1 |
# -*- coding utf-8 -*- |
2 |
# classes/views/apps.py |
3 |
|
4 |
from flask import Module, jsonify, request, render_template, redirect,\ |
5 |
url_for, flash, abort |
6 |
|
7 |
from hashlib import md5 |
8 |
from werkzeug.security import gen_salt |
9 |
|
10 |
from swtstore.classes.models import Client |
11 |
from swtstore.classes.models.um import User |
12 |
from swtstore.classes.utils import urlnorm |
13 |
|
14 |
|
15 |
app = Module(__name__) |
16 |
|
17 |
|
18 |
@app.route('/register', methods=['GET', 'POST']) |
19 |
def register(): |
20 |
current_user = User.getCurrentUser() |
21 |
if current_user is None: |
22 |
return redirect(url_for('frontend.index')) |
23 |
|
24 |
if request.method == 'GET': |
25 |
return render_template('app/register.html') |
26 |
|
27 |
elif request.method == 'POST': |
28 |
req_fields = ['name', 'host_url', 'redirect_uris', 'scopes'] |
29 |
for field in req_fields: |
30 |
if not request.form.get(field): |
31 |
abort(404) |
32 |
|
33 |
new_app = Client( |
34 |
id = gen_salt(40), |
35 |
client_secret = gen_salt(50), |
36 |
name = request.form.get('name'), |
37 |
description = request.form.get('desc'), |
38 |
user_id = current_user.id, |
39 |
_host_url = request.form.get('host_url'), |
40 |
_redirect_uris = urlnorm(request.form.get('redirect_uris')), |
41 |
_default_scopes = ' '.join(request.form.get('scopes').split(',')), |
42 |
_is_private = False |
43 |
) |
44 |
new_app.persist() |
45 |
|
46 |
return redirect(url_for('user.myApps')) |