From 95b4840c32f167b384528bf2410b23bf2322ed72 Mon Sep 17 00:00:00 2001 From: Vincenzo Volpe Date: Fri, 21 Jul 2017 11:51:21 +0200 Subject: [PATCH 1/6] Updated class UsersResource(Resource). --- views/user.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/views/user.py b/views/user.py index e22199e..4a89c2a 100644 --- a/views/user.py +++ b/views/user.py @@ -1,12 +1,13 @@ import uuid from models import User import auth -from flask import g +from flask import g, request from http.client import CREATED, NOT_FOUND, NO_CONTENT, BAD_REQUEST, UNAUTHORIZED from flask_restful import Resource, reqparse import re from passlib.hash import pbkdf2_sha256 import utils +from marshmallow import ValidationError def valid_email(email): @@ -21,20 +22,19 @@ def crypt_password(password): class UsersResource(Resource): def post(self): - parser = reqparse.RequestParser() - parser.add_argument('first_name', type=utils.non_empty_str, required=True) - parser.add_argument('last_name', type=utils.non_empty_str, required=True) - parser.add_argument('email', type=utils.non_empty_str, required=True) - parser.add_argument('password', type=utils.non_empty_str, required=True) - args = parser.parse_args(strict=True) - - if valid_email(args['email']) and len(args['password']) > 6: + json_data = request.get_json() + try: + User.verify_json(json_data) + except ValidationError as err: + return {'message': err.message}, NOT_FOUND + + if valid_email(json_data['email']) and len(json_data['password']) > 6: obj = User.create( uuid=uuid.uuid4(), - first_name=args['first_name'], - last_name=args['last_name'], - email=args['email'], - password=crypt_password(args['password']) + first_name=json_data['first_name'], + last_name=json_data['last_name'], + email=json_data['email'], + password=crypt_password(json_data['password']) ) return obj.json(), CREATED @@ -53,12 +53,12 @@ def put(self, uuid): if obj != g.current_user: return '', UNAUTHORIZED - parser = reqparse.RequestParser() - parser.add_argument('first_name', type=utils.non_empty_str, required=True) - parser.add_argument('last_name', type=utils.non_empty_str, required=True) - parser.add_argument('email', type=utils.non_empty_str, required=True) - parser.add_argument('password', type=utils.non_empty_str, required=True) - args = parser.parse_args(strict=True) + #parser = reqparse.RequestParser() + #parser.add_argument('first_name', type=utils.non_empty_str, required=True) + #parser.add_argument('last_name', type=utils.non_empty_str, required=True) + #parser.add_argument('email', type=utils.non_empty_str, required=True) + #parser.add_argument('password', type=utils.non_empty_str, required=True) + #args = parser.parse_args(strict=True) if valid_email(args['email']) is not None and len(args['password']) > 6: obj.first_name = args['first_name'] From 3783427c7beab5da7e2c2116cc19e0e926f8c8d8 Mon Sep 17 00:00:00 2001 From: Vincenzo Volpe Date: Tue, 25 Jul 2017 09:20:01 +0200 Subject: [PATCH 2/6] Updated class UsersResource(Resource). --- views/user.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/views/user.py b/views/user.py index 4a89c2a..54c42d0 100644 --- a/views/user.py +++ b/views/user.py @@ -10,6 +10,7 @@ from marshmallow import ValidationError + def valid_email(email): return re.match('[a-z]{3,}(?P@)[a-z]{3,}(?P\.)[a-z]{2,}', email) @@ -45,26 +46,20 @@ def post(self): class UserResource(Resource): @auth.login_required def put(self, uuid): + json_data = request.get_json() try: - obj = User.get(uuid=uuid) - except User.DoesNotExist: + obj = User.verify_json(json_data) + except ValidationError as err: return None, NOT_FOUND if obj != g.current_user: return '', UNAUTHORIZED - #parser = reqparse.RequestParser() - #parser.add_argument('first_name', type=utils.non_empty_str, required=True) - #parser.add_argument('last_name', type=utils.non_empty_str, required=True) - #parser.add_argument('email', type=utils.non_empty_str, required=True) - #parser.add_argument('password', type=utils.non_empty_str, required=True) - #args = parser.parse_args(strict=True) - - if valid_email(args['email']) is not None and len(args['password']) > 6: - obj.first_name = args['first_name'] - obj.last_name = args['last_name'] - obj.email = args['email'] - obj.password = crypt_password(args['password']) + if valid_email(json_data['email']) is not None and len(json_data['password']) > 6: + obj.first_name = json_data['first_name'] + obj.last_name = json_data['last_name'] + obj.email = json_data['email'] + obj.password = crypt_password(json_data['password']) obj.save() return obj.json(), CREATED From cc80ca7242c83a655f938da3b57f76a29cfab8df Mon Sep 17 00:00:00 2001 From: Vincenzo Volpe Date: Tue, 25 Jul 2017 10:00:50 +0200 Subject: [PATCH 3/6] Added variable json_data in def delete. --- views/user.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/views/user.py b/views/user.py index 54c42d0..b23581b 100644 --- a/views/user.py +++ b/views/user.py @@ -6,11 +6,9 @@ from flask_restful import Resource, reqparse import re from passlib.hash import pbkdf2_sha256 -import utils from marshmallow import ValidationError - def valid_email(email): return re.match('[a-z]{3,}(?P@)[a-z]{3,}(?P\.)[a-z]{2,}', email) @@ -68,9 +66,10 @@ def put(self, uuid): @auth.login_required def delete(self, uuid): + json_data = request.get_json() try: - obj = User.get(uuid=uuid) - except User.DoesNotExist: + obj = User.verify_json(json_data) + except ValidationError: return None, NOT_FOUND if obj != g.current_user: From 0080192b63ff69e2bd14da10d21d055a06cbf84c Mon Sep 17 00:00:00 2001 From: Vincenzo Volpe Date: Tue, 25 Jul 2017 15:14:56 +0200 Subject: [PATCH 4/6] fixed user views --- views/user.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/views/user.py b/views/user.py index b23581b..028f40e 100644 --- a/views/user.py +++ b/views/user.py @@ -6,7 +6,7 @@ from flask_restful import Resource, reqparse import re from passlib.hash import pbkdf2_sha256 -from marshmallow import ValidationError +from jsonschema.exceptions import ValidationError def valid_email(email): @@ -25,7 +25,7 @@ def post(self): try: User.verify_json(json_data) except ValidationError as err: - return {'message': err.message}, NOT_FOUND + return {'message': err.message}, BAD_REQUEST if valid_email(json_data['email']) and len(json_data['password']) > 6: obj = User.create( @@ -46,10 +46,11 @@ class UserResource(Resource): def put(self, uuid): json_data = request.get_json() try: - obj = User.verify_json(json_data) + User.verify_json(json_data) except ValidationError as err: - return None, NOT_FOUND + return None, BAD_REQUEST + obj = User.get(uuid=uuid) if obj != g.current_user: return '', UNAUTHORIZED @@ -68,8 +69,8 @@ def put(self, uuid): def delete(self, uuid): json_data = request.get_json() try: - obj = User.verify_json(json_data) - except ValidationError: + obj = User.get(uuid=uuid) + except User.DoesNotExist: return None, NOT_FOUND if obj != g.current_user: From fe919b8cd45f24a24fd8c77d8a339f06e3b5ebdb Mon Sep 17 00:00:00 2001 From: Vincenzo Volpe Date: Tue, 25 Jul 2017 15:23:35 +0200 Subject: [PATCH 5/6] Changed variable name in user views. --- views/user.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/views/user.py b/views/user.py index 028f40e..2081a96 100644 --- a/views/user.py +++ b/views/user.py @@ -21,22 +21,22 @@ def crypt_password(password): class UsersResource(Resource): def post(self): - json_data = request.get_json() + json = request.get_json() try: - User.verify_json(json_data) + User.verify_json(json) except ValidationError as err: return {'message': err.message}, BAD_REQUEST - if valid_email(json_data['email']) and len(json_data['password']) > 6: - obj = User.create( + if valid_email(json['email']) and len(json['password']) > 6: + user = User.create( uuid=uuid.uuid4(), - first_name=json_data['first_name'], - last_name=json_data['last_name'], - email=json_data['email'], - password=crypt_password(json_data['password']) + first_name=json['first_name'], + last_name=json['last_name'], + email=json['email'], + password=crypt_password(json['password']) ) - return obj.json(), CREATED + return user.json(), CREATED else: return '', BAD_REQUEST @@ -44,39 +44,39 @@ def post(self): class UserResource(Resource): @auth.login_required def put(self, uuid): - json_data = request.get_json() + json = request.get_json() try: - User.verify_json(json_data) + User.verify_json(json) except ValidationError as err: return None, BAD_REQUEST - obj = User.get(uuid=uuid) - if obj != g.current_user: + user = User.get(uuid=uuid) + if user != g.current_user: return '', UNAUTHORIZED - if valid_email(json_data['email']) is not None and len(json_data['password']) > 6: - obj.first_name = json_data['first_name'] - obj.last_name = json_data['last_name'] - obj.email = json_data['email'] - obj.password = crypt_password(json_data['password']) - obj.save() + if valid_email(json['email']) is not None and len(json['password']) > 6: + user.first_name = json['first_name'] + user.last_name = json['last_name'] + user.email = json['email'] + user.password = crypt_password(json['password']) + user.save() - return obj.json(), CREATED + return user.json(), CREATED else: return '', BAD_REQUEST @auth.login_required def delete(self, uuid): - json_data = request.get_json() + json = request.get_json() try: - obj = User.get(uuid=uuid) + user = User.get(uuid=uuid) except User.DoesNotExist: return None, NOT_FOUND - if obj != g.current_user: + if user != g.current_user: return '', UNAUTHORIZED - obj.status = 'deleted' - obj.save() + user.status = 'deleted' + user.save() return None, NO_CONTENT From f14c937a96ba0ba2e98a1be9ca0afe2c92b9b923 Mon Sep 17 00:00:00 2001 From: Vincenzo Volpe Date: Tue, 25 Jul 2017 15:28:45 +0200 Subject: [PATCH 6/6] Fixed flake8. --- views/user.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/views/user.py b/views/user.py index 2081a96..e453bf1 100644 --- a/views/user.py +++ b/views/user.py @@ -3,7 +3,7 @@ import auth from flask import g, request from http.client import CREATED, NOT_FOUND, NO_CONTENT, BAD_REQUEST, UNAUTHORIZED -from flask_restful import Resource, reqparse +from flask_restful import Resource import re from passlib.hash import pbkdf2_sha256 from jsonschema.exceptions import ValidationError @@ -67,7 +67,6 @@ def put(self, uuid): @auth.login_required def delete(self, uuid): - json = request.get_json() try: user = User.get(uuid=uuid) except User.DoesNotExist: