university-final-iot-backend/app/api/resources/account.py

45 lines
1.3 KiB
Python
Raw Normal View History

from flask_restful import Resource, abort
2018-05-08 08:52:36 +00:00
from flask import g
from webargs import fields
from webargs.flaskparser import use_args
2018-05-07 14:13:07 +00:00
from flasgger import swag_from
2018-05-06 19:42:21 +00:00
import app.accounts as accounts
2018-05-08 08:52:36 +00:00
from app.api import ProtectedResource, protected
2018-05-06 19:42:21 +00:00
2018-05-08 08:52:36 +00:00
user_args = {
'user': fields.Nested({
'username': fields.Str(required=True),
'email': fields.Email(required=True),
'password': fields.Str(required=True)
}, required=True, location='json')
}
2018-05-06 19:42:21 +00:00
2018-05-08 08:52:36 +00:00
class AccountResource(ProtectedResource):
@swag_from('swagger/get_account_spec.yaml')
def get(self, account_id):
if g.current_account.id == account_id:
return g.current_account, 200
abort(403, message='You can only get your own account', status='error')
class AccountListResource(Resource):
@use_args(user_args)
2018-05-07 14:13:07 +00:00
@swag_from('swagger/create_account_spec.yaml')
def post(self, args):
2018-05-06 19:42:21 +00:00
try:
args = args['user']
2018-05-06 19:42:21 +00:00
success = accounts.create_account(
args['username'],
args['email'],
args['password'])
if success:
return '', 201
except ValueError:
abort(422, message='Account already exists', status='error')
2018-05-07 14:13:07 +00:00
@protected
2018-05-07 14:13:07 +00:00
def get(self):
return '', 200