Make POST and PUT return created roles/accounts

develop
esensar 2018-10-24 22:20:50 +02:00
parent bb8d747de5
commit 42c3566ab0
6 changed files with 38 additions and 16 deletions

View File

@ -14,7 +14,7 @@ def create_account(username, email, password):
:type username: string :type username: string
:type email: string :type email: string
:type password: string :type password: string
:returns: Email confirmation token if creation was successful :returns: Account and Email confirmation token if creation was successful
:rtype: string :rtype: string
:raises: ValueError if account already exists :raises: ValueError if account already exists
""" """
@ -24,7 +24,7 @@ def create_account(username, email, password):
account.save() account.save()
emailtoken = generate_confirmation_token(account.email) emailtoken = generate_confirmation_token(account.email)
return emailtoken return account, emailtoken
raise ValueError("Account with given parameters already exists") raise ValueError("Account with given parameters already exists")
@ -58,6 +58,7 @@ def update_account_role(account_id, role_id):
acc = Account.get(id=account_id) acc = Account.get(id=account_id)
acc.role_id = role_id acc.role_id = role_id
acc.save() acc.save()
return acc
def create_role(display_name, permissions): def create_role(display_name, permissions):
@ -74,6 +75,7 @@ def create_role(display_name, permissions):
""" """
role = Role(display_name, permissions) role = Role(display_name, permissions)
role.save() role.save()
return role
def get_role(role_id): def get_role(role_id):

View File

@ -7,7 +7,7 @@ from calendar import timegm
class Account(db.Model): class Account(db.Model):
__tablename__ = 'accounts' __tablename__ = 'accounts'
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String, index=True, unique=True) username = db.Column(db.String, index=True, unique=True)
password = db.Column(db.String) password = db.Column(db.String)
email = db.Column(db.String, index=True, unique=True) email = db.Column(db.String, index=True, unique=True)
@ -123,7 +123,7 @@ class Account(db.Model):
class Role(db.Model): class Role(db.Model):
__tablename__ = 'roles' __tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
display_name = db.Column(db.String, unique=True) display_name = db.Column(db.String, unique=True)
permissions = db.Column(db.ARRAY(db.String)) permissions = db.Column(db.ARRAY(db.String))

View File

@ -60,10 +60,9 @@ class RolesResource(ProtectedResource):
@use_args(RoleCreationSchema(), locations=('json',)) @use_args(RoleCreationSchema(), locations=('json',))
@swag_from('swagger/create_role_spec.yaml') @swag_from('swagger/create_role_spec.yaml')
def post(self, args): def post(self, args):
success = accounts.create_role(args['display_name'], created_role = accounts.create_role(args['display_name'],
args['permissions']) args['permissions'])
if success: return RoleSchema().dump(created_role), 201
return '', 201
@swag_from('swagger/get_roles_spec.yaml') @swag_from('swagger/get_roles_spec.yaml')
def get(self): def get(self):
@ -77,9 +76,9 @@ class AccountRoleResource(ProtectedResource):
if g.current_account.id == account_id: if g.current_account.id == account_id:
abort(403, message='You may not change your own roles', abort(403, message='You may not change your own roles',
status='error') status='error')
success = accounts.update_account_role(account_id, args['role_id']) updated_account = accounts.update_account_role(
if success: account_id, args['role_id'])
return '', 204 return UserSchema().dump(updated_account), 200
class AccountListResource(Resource): class AccountListResource(Resource):
@ -87,7 +86,7 @@ class AccountListResource(Resource):
@swag_from('swagger/create_account_spec.yaml') @swag_from('swagger/create_account_spec.yaml')
def post(self, args): def post(self, args):
try: try:
emailtoken = accounts.create_account( created_account, emailtoken = accounts.create_account(
args['username'], args['username'],
args['email'], args['email'],
args['password']) args['password'])
@ -101,7 +100,7 @@ class AccountListResource(Resource):
args['email'], args['email'],
'Please confirm your email', 'Please confirm your email',
html) html)
return '', 201 return UserSchema().dump(created_account), 201
except ValueError: except ValueError:
abort(422, message='Account already exists', status='error') abort(422, message='Account already exists', status='error')

View File

@ -14,7 +14,14 @@ parameters:
security: [] security: []
responses: responses:
201: 201:
description: Successful creation description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/User'
422: 422:
description: Account already exists description: Account already exists
schema: schema:

View File

@ -12,4 +12,11 @@ parameters:
$ref: '#/definitions/Role' $ref: '#/definitions/Role'
responses: responses:
201: 201:
description: Successful creation description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/Role'

View File

@ -20,5 +20,12 @@ parameters:
role_id: role_id:
type: integer type: integer
responses: responses:
204: 200:
description: Success description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/User'