mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 05:28:49 -04:00
Add rest of user model fields to api.
First step to moving user interactions to api.
This commit is contained in:
@@ -8,7 +8,8 @@ from app.models import User
|
||||
|
||||
def save_model_user(usr, update_dict={}):
|
||||
if update_dict:
|
||||
del update_dict['id']
|
||||
if update_dict.get('id'):
|
||||
del update_dict['id']
|
||||
db.session.query(User).filter_by(id=usr.id).update(update_dict)
|
||||
else:
|
||||
db.session.add(usr)
|
||||
|
||||
10
app/encryption.py
Normal file
10
app/encryption.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from flask.ext.bcrypt import generate_password_hash, check_password_hash
|
||||
|
||||
|
||||
def hashpw(password):
|
||||
return generate_password_hash(password.encode('UTF-8'), 10)
|
||||
|
||||
|
||||
def check_hash(password, hashed_password):
|
||||
# If salt is invalid throws a 500 should add try/catch here
|
||||
return check_password_hash(hashed_password, password)
|
||||
@@ -2,6 +2,10 @@ from . import db
|
||||
import datetime
|
||||
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from app.encryption import (
|
||||
hashpw,
|
||||
check_hash
|
||||
)
|
||||
|
||||
|
||||
def filter_null_value_fields(obj):
|
||||
@@ -14,6 +18,7 @@ class User(db.Model):
|
||||
__tablename__ = 'users'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String, nullable=False, index=True, unique=False)
|
||||
email_address = db.Column(db.String(255), nullable=False, index=True, unique=True)
|
||||
created_at = db.Column(
|
||||
db.DateTime,
|
||||
@@ -27,6 +32,23 @@ class User(db.Model):
|
||||
unique=False,
|
||||
nullable=True,
|
||||
onupdate=datetime.datetime.now)
|
||||
_password = db.Column(db.String, index=False, unique=False, nullable=False)
|
||||
mobile_number = db.Column(db.String, index=False, unique=False, nullable=False)
|
||||
password_changed_at = db.Column(db.DateTime, index=False, unique=False, nullable=True)
|
||||
logged_in_at = db.Column(db.DateTime, nullable=True)
|
||||
failed_login_count = db.Column(db.Integer, nullable=False, default=0)
|
||||
state = db.Column(db.String, nullable=False, default='pending')
|
||||
|
||||
@property
|
||||
def password(self):
|
||||
raise AttributeError("Password not readable")
|
||||
|
||||
@password.setter
|
||||
def password(self, password):
|
||||
self._password = hashpw(password)
|
||||
|
||||
def check_password(self, password):
|
||||
return check_hash(password, self._password)
|
||||
|
||||
|
||||
user_to_service = db.Table(
|
||||
|
||||
@@ -11,7 +11,7 @@ from marshmallow import post_load
|
||||
class UserSchema(ma.ModelSchema):
|
||||
class Meta:
|
||||
model = models.User
|
||||
exclude = ("updated_at", "created_at", "user_to_service")
|
||||
exclude = ("updated_at", "created_at", "user_to_service", "_password")
|
||||
|
||||
|
||||
# TODO process users list, to return a list of user.id
|
||||
|
||||
@@ -17,8 +17,13 @@ user = Blueprint('user', __name__)
|
||||
@user.route('', methods=['POST'])
|
||||
def create_user():
|
||||
user, errors = user_schema.load(request.get_json())
|
||||
req_json = request.get_json()
|
||||
if not req_json.get('password'):
|
||||
return jsonify(result="error", message={'error': 'password missing'}), 400
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
user.password = req_json.get('password')
|
||||
save_model_user(user)
|
||||
return jsonify(data=user_schema.dump(user).data), 201
|
||||
|
||||
@@ -37,15 +42,8 @@ def update_user(user_id):
|
||||
delete_model_user(user)
|
||||
else:
|
||||
status_code = 200
|
||||
# TODO there has got to be a better way to do the next three lines
|
||||
update_user, errors = user_schema.load(request.get_json())
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
update_dict, errors = user_schema.dump(update_user)
|
||||
# TODO FIX ME
|
||||
# Remove update_service model which is added to db.session
|
||||
db.session.rollback()
|
||||
save_model_user(user, update_dict=update_dict)
|
||||
save_model_user(user, update_dict=request.get_json())
|
||||
return jsonify(data=user_schema.dump(user).data), status_code
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user