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