Make POST and PUT return created roles/accounts
parent
bb8d747de5
commit
42c3566ab0
|
@ -14,7 +14,7 @@ def create_account(username, email, password):
|
|||
:type username: string
|
||||
:type email: string
|
||||
:type password: string
|
||||
:returns: Email confirmation token if creation was successful
|
||||
:returns: Account and Email confirmation token if creation was successful
|
||||
:rtype: string
|
||||
:raises: ValueError if account already exists
|
||||
"""
|
||||
|
@ -24,7 +24,7 @@ def create_account(username, email, password):
|
|||
account.save()
|
||||
|
||||
emailtoken = generate_confirmation_token(account.email)
|
||||
return emailtoken
|
||||
return account, emailtoken
|
||||
|
||||
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.role_id = role_id
|
||||
acc.save()
|
||||
return acc
|
||||
|
||||
|
||||
def create_role(display_name, permissions):
|
||||
|
@ -74,6 +75,7 @@ def create_role(display_name, permissions):
|
|||
"""
|
||||
role = Role(display_name, permissions)
|
||||
role.save()
|
||||
return role
|
||||
|
||||
|
||||
def get_role(role_id):
|
||||
|
|
|
@ -7,7 +7,7 @@ from calendar import timegm
|
|||
class Account(db.Model):
|
||||
__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)
|
||||
password = db.Column(db.String)
|
||||
email = db.Column(db.String, index=True, unique=True)
|
||||
|
@ -123,7 +123,7 @@ class Account(db.Model):
|
|||
class Role(db.Model):
|
||||
__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)
|
||||
permissions = db.Column(db.ARRAY(db.String))
|
||||
|
||||
|
|
|
@ -60,10 +60,9 @@ class RolesResource(ProtectedResource):
|
|||
@use_args(RoleCreationSchema(), locations=('json',))
|
||||
@swag_from('swagger/create_role_spec.yaml')
|
||||
def post(self, args):
|
||||
success = accounts.create_role(args['display_name'],
|
||||
args['permissions'])
|
||||
if success:
|
||||
return '', 201
|
||||
created_role = accounts.create_role(args['display_name'],
|
||||
args['permissions'])
|
||||
return RoleSchema().dump(created_role), 201
|
||||
|
||||
@swag_from('swagger/get_roles_spec.yaml')
|
||||
def get(self):
|
||||
|
@ -77,9 +76,9 @@ class AccountRoleResource(ProtectedResource):
|
|||
if g.current_account.id == account_id:
|
||||
abort(403, message='You may not change your own roles',
|
||||
status='error')
|
||||
success = accounts.update_account_role(account_id, args['role_id'])
|
||||
if success:
|
||||
return '', 204
|
||||
updated_account = accounts.update_account_role(
|
||||
account_id, args['role_id'])
|
||||
return UserSchema().dump(updated_account), 200
|
||||
|
||||
|
||||
class AccountListResource(Resource):
|
||||
|
@ -87,7 +86,7 @@ class AccountListResource(Resource):
|
|||
@swag_from('swagger/create_account_spec.yaml')
|
||||
def post(self, args):
|
||||
try:
|
||||
emailtoken = accounts.create_account(
|
||||
created_account, emailtoken = accounts.create_account(
|
||||
args['username'],
|
||||
args['email'],
|
||||
args['password'])
|
||||
|
@ -101,7 +100,7 @@ class AccountListResource(Resource):
|
|||
args['email'],
|
||||
'Please confirm your email',
|
||||
html)
|
||||
return '', 201
|
||||
return UserSchema().dump(created_account), 201
|
||||
except ValueError:
|
||||
abort(422, message='Account already exists', status='error')
|
||||
|
||||
|
|
|
@ -14,7 +14,14 @@ parameters:
|
|||
security: []
|
||||
responses:
|
||||
201:
|
||||
description: Successful creation
|
||||
description: Success
|
||||
schema:
|
||||
type: object
|
||||
required:
|
||||
- content
|
||||
properties:
|
||||
content:
|
||||
$ref: '#/definitions/User'
|
||||
422:
|
||||
description: Account already exists
|
||||
schema:
|
||||
|
|
|
@ -12,4 +12,11 @@ parameters:
|
|||
$ref: '#/definitions/Role'
|
||||
responses:
|
||||
201:
|
||||
description: Successful creation
|
||||
description: Success
|
||||
schema:
|
||||
type: object
|
||||
required:
|
||||
- content
|
||||
properties:
|
||||
content:
|
||||
$ref: '#/definitions/Role'
|
||||
|
|
|
@ -20,5 +20,12 @@ parameters:
|
|||
role_id:
|
||||
type: integer
|
||||
responses:
|
||||
204:
|
||||
200:
|
||||
description: Success
|
||||
schema:
|
||||
type: object
|
||||
required:
|
||||
- content
|
||||
properties:
|
||||
content:
|
||||
$ref: '#/definitions/User'
|
||||
|
|
Loading…
Reference in New Issue