2021-03-02 20:27:14 +00:00
|
|
|
from uuid import UUID, uuid4
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2018-05-11 16:58:48 +00:00
|
|
|
from flask import g, request
|
2021-03-02 20:27:14 +00:00
|
|
|
from flask.json import jsonify
|
2018-09-07 10:38:02 +00:00
|
|
|
from teal.resource import View
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2021-03-02 20:27:14 +00:00
|
|
|
from ereuse_devicehub.db import db
|
2018-04-27 17:16:43 +00:00
|
|
|
from ereuse_devicehub.resources.user.exceptions import WrongCredentials
|
|
|
|
from ereuse_devicehub.resources.user.models import User
|
|
|
|
|
|
|
|
|
|
|
|
class UserView(View):
|
|
|
|
def one(self, id: UUID):
|
|
|
|
return self.schema.jsonify(User.query.filter_by(id=id).one())
|
|
|
|
|
|
|
|
|
|
|
|
def login():
|
2018-05-11 16:58:48 +00:00
|
|
|
# We use custom schema as we only want to parse a subset of user
|
2021-02-10 12:05:43 +00:00
|
|
|
user_s = g.resource_def.SCHEMA(only=('email', 'password')) # type: UserS
|
2018-05-11 16:58:48 +00:00
|
|
|
# noinspection PyArgumentList
|
|
|
|
u = request.get_json(schema=user_s)
|
2018-04-27 17:16:43 +00:00
|
|
|
user = User.query.filter_by(email=u['email']).one_or_none()
|
|
|
|
if user and user.password == u['password']:
|
2018-05-11 16:58:48 +00:00
|
|
|
schema_with_token = g.resource_def.SCHEMA(exclude=set())
|
|
|
|
return schema_with_token.jsonify(user)
|
2018-04-27 17:16:43 +00:00
|
|
|
else:
|
|
|
|
raise WrongCredentials()
|
2021-03-02 20:27:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def logout():
|
|
|
|
# We use custom schema as we only want to parse a subset of user
|
|
|
|
g.user.token = uuid4()
|
|
|
|
db.session.add(g.user)
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify('Ok')
|