blob: 77346bb8107551f92ca9a6c732744a322ac79c36 (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
"""The views/routes for the resources package"""
import uuid
from flask import request, jsonify, Blueprint, current_app as app
from .models import (
resource_categories, resource_category_by_id,
create_resource as _create_resource)
from ... import db
from ...dictify import dictify
from ...authentication.oauth2.resource_server import require_oauth
resources = Blueprint("resources", __name__)
@resources.route("/categories", methods=["GET"])
@require_oauth("profile group resource")
def list_resource_categories():
"""Retrieve all resource categories"""
db_uri = app.config["AUTH_DB"]
with db.connection(db_uri) as conn:
return jsonify(tuple(
dictify(category) for category in resource_categories(conn)))
@resources.route("/create", methods=["POST"])
@require_oauth("profile group resource")
def create_resource():
"""Create a new resource"""
with require_oauth.acquire("profile group resource") as the_token:
form = request.form
resource_name = form.get("resource_name")
resource_category_id = uuid.UUID(form.get("resource_category"))
db_uri = app.config["AUTH_DB"]
with db.connection(db_uri) as conn:
resource = _create_resource(
conn, resource_name, resource_category_by_id(
conn, resource_category_id).maybe(False, lambda rcat: rcat),
the_token.user)
return jsonify(dictify(resource))
|