2023-01-10 17:24:26 -05:00
|
|
|
"""
|
|
|
|
HeatPump Service
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
2023-01-17 19:00:22 -05:00
|
|
|
Copyright © 2023 Project Author Peter Yefi peteryefi@gmail.com
|
2023-01-10 17:24:26 -05:00
|
|
|
"""
|
|
|
|
import json
|
2023-01-16 15:31:02 -05:00
|
|
|
from flask import Response, request
|
2023-01-10 17:24:26 -05:00
|
|
|
from flask_restful import Resource
|
|
|
|
from imports.user_factory import UserFactory
|
2023-01-11 19:56:51 -05:00
|
|
|
from exports.user_factory import UserFactory as ExUserFactory
|
2023-01-10 17:24:26 -05:00
|
|
|
import os
|
|
|
|
from hub_logger import logger
|
2023-01-11 19:56:51 -05:00
|
|
|
from hub_api.helpers.auth import generate_auth_token, role_required
|
|
|
|
from persistence.models import UserRoles
|
2023-01-10 17:24:26 -05:00
|
|
|
|
|
|
|
|
2023-01-16 15:31:02 -05:00
|
|
|
class User(Resource):
|
2023-01-10 17:24:26 -05:00
|
|
|
def __init__(self):
|
|
|
|
self.user_factory = UserFactory(db_name='hub_prod', app_env='PROD',
|
|
|
|
dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
|
|
|
|
2023-01-16 15:31:02 -05:00
|
|
|
@role_required([UserRoles.Admin.value])
|
|
|
|
def post(self):
|
2023-01-10 17:24:26 -05:00
|
|
|
try:
|
2023-01-16 15:31:02 -05:00
|
|
|
payload = request.get_json()
|
|
|
|
user = self.user_factory.create_user(name=payload["name"], email=payload["email"], password=payload["password"],
|
|
|
|
role=payload["role"])
|
2023-01-10 17:24:26 -05:00
|
|
|
if type(user) is dict:
|
2023-01-11 19:56:51 -05:00
|
|
|
return Response(response=json.dumps(user), status=400)
|
2023-01-10 17:24:26 -05:00
|
|
|
return Response(response=json.dumps({'user': {'id': user.id, 'name': user.name, 'email': user.email,
|
|
|
|
'password': user.password, 'role': user.role.value}}), status=201)
|
|
|
|
except Exception as err:
|
|
|
|
logger.error(err)
|
|
|
|
return Response(response=json.dumps({'err_msg': 'Sorry an error occurred while creating user'}), status=400)
|
|
|
|
|
2023-01-16 15:31:02 -05:00
|
|
|
@role_required([UserRoles.Admin.value])
|
|
|
|
def put(self):
|
2023-01-10 17:24:26 -05:00
|
|
|
try:
|
2023-01-16 15:31:02 -05:00
|
|
|
payload = request.get_json()
|
|
|
|
res = self.user_factory.update_user(user_id=payload['id'], name=payload['name'], password=payload['password'],
|
|
|
|
role=payload['role'], email=payload['email'])
|
2023-01-10 17:24:26 -05:00
|
|
|
if res:
|
|
|
|
return Response(response=json.dumps(res), status=400)
|
|
|
|
return Response(response=json.dumps({'success': 'user updated successfully'}), status=200)
|
|
|
|
except Exception as err:
|
|
|
|
logger.error(err)
|
|
|
|
return Response(response=json.dumps({'err_msg': 'Sorry, an error occurred while updating user'}),
|
|
|
|
status=400)
|
2023-01-11 19:56:51 -05:00
|
|
|
|
|
|
|
|
2023-01-16 15:31:02 -05:00
|
|
|
class UserLogin(Resource):
|
2023-01-11 19:56:51 -05:00
|
|
|
def __init__(self):
|
|
|
|
self.user_factory = ExUserFactory(db_name='hub_prod', app_env='PROD',
|
|
|
|
dotenv_path="{}/.env".format(os.path.expanduser('~')))
|
|
|
|
|
2023-01-16 15:31:02 -05:00
|
|
|
def post(self):
|
2023-01-11 19:56:51 -05:00
|
|
|
try:
|
2023-01-16 15:31:02 -05:00
|
|
|
payload = request.get_json()
|
|
|
|
user = self.user_factory.login_user(email=payload["email"], password=payload["password"])
|
2023-01-11 19:56:51 -05:00
|
|
|
if type(user) is dict:
|
|
|
|
return Response(response=json.dumps(user), status=400)
|
|
|
|
user = user[0]
|
|
|
|
user_dict = {
|
|
|
|
'user': {
|
|
|
|
'id': user.id,
|
|
|
|
'name': user.name,
|
|
|
|
'email': user.email,
|
|
|
|
'password': user.password,
|
|
|
|
'role': user.role.value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user_dict['token'] = generate_auth_token(user_dict)
|
2023-01-16 15:31:02 -05:00
|
|
|
return Response(response=json.dumps(user_dict), status=200)
|
2023-01-11 19:56:51 -05:00
|
|
|
except Exception as err:
|
|
|
|
logger.error(err)
|
2023-01-16 15:31:02 -05:00
|
|
|
return Response(response=json.dumps({'err_msg': 'An error occurred while authenticating user'}), status=400)
|