Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

97 verify json user #114

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 34 additions & 40 deletions views/user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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
from flask_restful import Resource
import re
from passlib.hash import pbkdf2_sha256
import utils
from jsonschema.exceptions import ValidationError


def valid_email(email):
Expand All @@ -21,67 +21,61 @@ 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:
obj = User.create(
json = request.get_json()
try:
User.verify_json(json)
except ValidationError as err:
return {'message': err.message}, BAD_REQUEST

if valid_email(json['email']) and len(json['password']) > 6:
user = 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['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


class UserResource(Resource):
@auth.login_required
def put(self, uuid):
json = request.get_json()
try:
obj = User.get(uuid=uuid)
except User.DoesNotExist:
return None, NOT_FOUND
User.verify_json(json)
except ValidationError as err:
return None, BAD_REQUEST

if obj != g.current_user:
user = User.get(uuid=uuid)
if user != 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'])
obj.save()

return obj.json(), CREATED
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 user.json(), CREATED
else:
return '', BAD_REQUEST

@auth.login_required
def delete(self, uuid):
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