Skip to content

Commit f3ee9e7

Browse files
committed
remove unnecessary try block, add default registerd_on value
1 parent c5cf081 commit f3ee9e7

File tree

4 files changed

+18
-38
lines changed

4 files changed

+18
-38
lines changed

lib/pbench/server/api/resources/users_api.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import jwt
2-
import datetime
32
from flask import request, jsonify, make_response
43
from flask_restful import Resource, abort
54
from flask_bcrypt import check_password_hash
@@ -127,7 +126,6 @@ def post(self):
127126
first_name=first_name,
128127
last_name=last_name,
129128
email=email_id,
130-
registered_on=datetime.datetime.now(),
131129
)
132130

133131
# insert the user
@@ -472,15 +470,7 @@ def put(self, username):
472470
)
473471
abort(400, message="Invalid fields in update request payload")
474472
try:
475-
# We will update the user object with the keys and values provided in the request payload.
476-
# THe keys need to match with the column names in the user model.
477473
user.update(**post_data)
478-
except ValueError:
479-
self.logger.warning(
480-
"Either provided values to update the user does not adhere to the user model "
481-
"datatype or user attempted to update the protected properties."
482-
)
483-
abort(400, message="Invalid fields in update request payload")
484474
except Exception:
485475
self.logger.exception("Exception occurred during updating user object")
486476
abort(500, message="INTERNAL ERROR")

lib/pbench/server/database/alembic/env.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from alembic import context
1313

14-
from pbench.common.exceptions import BadConfig, ConfigFileNotSpecified
1514
from pbench.server.database.database import Database
1615
from pbench.common.logger import get_pbench_logger
1716
from pbench.server.api import get_server_config
@@ -57,7 +56,7 @@ def run_migrations_offline():
5756
try:
5857
server_config = get_server_config()
5958
logger = get_pbench_logger(__name__, server_config)
60-
except (ConfigFileNotSpecified, BadConfig) as e:
59+
except Exception as e:
6160
print(e)
6261
sys.exit(1)
6362

@@ -86,7 +85,7 @@ def run_migrations_online():
8685
try:
8786
server_config = get_server_config()
8887
logger = get_pbench_logger(__name__, server_config)
89-
except (ConfigFileNotSpecified, BadConfig) as e:
88+
except Exception as e:
9089
print(e)
9190
sys.exit(1)
9291

lib/pbench/server/database/models/users.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import datetime
12
from flask_bcrypt import generate_password_hash
23
from email_validator import validate_email
34
from sqlalchemy import Column, Integer, String, DateTime, LargeBinary
45
from pbench.server.database.database import Database
5-
from sqlalchemy.orm import relationship
6-
from sqlalchemy.orm import validates
6+
from sqlalchemy.orm import relationship, validates
77

88

99
class User(Database.Base):
@@ -16,7 +16,7 @@ class User(Database.Base):
1616
first_name = Column(String(255), unique=False, nullable=False)
1717
last_name = Column(String(255), unique=False, nullable=False)
1818
password = Column(LargeBinary(128), nullable=False)
19-
registered_on = Column(DateTime, nullable=False)
19+
registered_on = Column(DateTime, nullable=False, default=datetime.datetime.now())
2020
email = Column(String(255), unique=True, nullable=False)
2121
auth_tokens = relationship("ActiveTokens", backref="users")
2222

@@ -39,21 +39,16 @@ def get_protected():
3939
@staticmethod
4040
def query(id=None, username=None, email=None):
4141
# Currently we would only query with single argument. Argument need to be either username/id/email
42-
try:
43-
if username:
44-
user = (
45-
Database.db_session.query(User).filter_by(username=username).first()
46-
)
47-
elif id:
48-
user = Database.db_session.query(User).filter_by(id=id).first()
49-
elif email:
50-
user = Database.db_session.query(User).filter_by(email=email).first()
51-
else:
52-
user = None
53-
except Exception as e:
54-
raise e
42+
if username:
43+
user = Database.db_session.query(User).filter_by(username=username).first()
44+
elif id:
45+
user = Database.db_session.query(User).filter_by(id=id).first()
46+
elif email:
47+
user = Database.db_session.query(User).filter_by(email=email).first()
5548
else:
56-
return user
49+
user = None
50+
51+
return user
5752

5853
def add(self):
5954
"""
@@ -73,13 +68,10 @@ def evaluate_password(self, key, value):
7368
# validate the email field
7469
@validates("email")
7570
def evaluate_email(self, key, value):
76-
try:
77-
valid = validate_email(value)
78-
email = valid.email
79-
except Exception as e:
80-
raise e
81-
else:
82-
return email
71+
valid = validate_email(value)
72+
email = valid.email
73+
74+
return email
8375

8476
def update(self, **kwargs):
8577
"""

lib/pbench/test/unit/server/test_user_auth.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def test_registration_with_registered_user(client, server_config):
9292
username="user",
9393
first_name="firstname",
9494
last_name="lastname",
95-
registered_on=datetime.datetime.now(),
9695
)
9796
Database.db_session.add(user)
9897
Database.db_session.commit()

0 commit comments

Comments
 (0)