Add __repr__ to models

This commit is contained in:
Patrick Smith 2018-09-26 16:50:10 -04:00 committed by Montana
parent 74a5a59dad
commit 8bb9c1ab8a
19 changed files with 123 additions and 0 deletions

View File

@ -30,3 +30,6 @@ class Attachment(Base, mixins.TimestampsMixin):
db.session.commit()
return attachment
def __repr__(self):
return "<Attachment(name='{}', id='{}')>".format(self.filename, self.id)

View File

@ -29,3 +29,8 @@ class AuditEvent(Base, TimestampsMixin):
attrs = inspect(self).dict
connection.execute(self.__table__.insert(), **attrs)
def __repr__(self):
return "<AuditEvent(name='{}', action='{}', id='{}')>".format(
self.display_name, self.action, self.id
)

View File

@ -29,3 +29,12 @@ class Environment(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
def auditable_workspace_id(self):
return self.project.workspace_id
def __repr__(self):
return "<Environment(name='{}', num_users='{}', project='{}', workspace='{}', id='{}')>".format(
self.name,
self.num_users,
self.project.name,
self.project.workspace.name,
self.id,
)

View File

@ -24,6 +24,11 @@ class EnvironmentRole(Base, mixins.TimestampsMixin):
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
user = relationship("User", backref="environment_roles")
def __repr__(self):
return "<EnvironmentRole(role='{}', user='{}', environment='{}', id='{}')>".format(
self.role, self.user.full_name, self.environment.name, self.id
)
Index(
"environments_role_user_environment",

View File

@ -20,3 +20,8 @@ class Project(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
@property
def displayname(self):
return self.name
def __repr__(self):
return "<Project(name='{}', description='{}', workspace='{}', id='{}')>".format(
self.name, self.description, self.workspace.name, self.id
)

View File

@ -223,3 +223,13 @@ class Request(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
@property
def contracting_officer_email(self):
return self.latest_revision.email_co
def __repr__(self):
return "<Request(status='{}', name='{}', creator='{}', is_approved='{}', time_created='{}', id='{}')>".format(
self.status_displayname,
self.displayname,
self.creator.full_name,
self.is_approved,
self.time_created,
self.id,
)

View File

@ -15,3 +15,8 @@ class RequestInternalComment(Base, mixins.TimestampsMixin):
request_id = Column(ForeignKey("requests.id", ondelete="CASCADE"), nullable=False)
request = relationship("Request")
def __repr__(self):
return "<RequestInternalComment(text='{}', user='{}', request='{}', id='{}')>".format(
self.text, self.user.full_name, self.request_id, self.id
)

View File

@ -32,3 +32,8 @@ class RequestReview(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
@property
def full_name_ccpo(self):
return "{} {}".format(self.fname_ccpo, self.lname_ccpo)
def __repr__(self):
return "<RequestReview(status='{}', comment='{}', reviewer='{}', id='{}')>".format(
self.status.log_name, self.comment, self.full_name_reviewer, self.id
)

View File

@ -77,3 +77,8 @@ class RequestRevision(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
uii_ids = Column(ARRAY(String))
treasury_code = Column(String)
ba_code = Column(String)
def __repr__(self):
return "<RequestRevision(request='{}', id='{}')>".format(
self.request_id, self.id
)

View File

@ -55,3 +55,8 @@ class RequestStatusEvent(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
return "Accepted"
else:
return self.displayname
def __repr__(self):
return "<RequestStatusEvent(log_name='{}', request='{}', id='{}')>".format(
self.log_name, self.request_id, self.id
)

View File

@ -25,3 +25,8 @@ class Role(Base, mixins.TimestampsMixin):
perms_set.discard(permission)
self.permissions = list(perms_set)
flag_modified(self, "permissions")
def __repr__(self):
return "<Role(name='{}', description='{}', permissions='{}', id='{}')>".format(
self.name, self.description, self.permissions, self.id
)

View File

@ -63,3 +63,13 @@ class TaskOrder(Base, mixins.TimestampsMixin):
],
)
)
def __repr__(self):
return "<TaskOrder(number='{}', verified='{}', budget='{}', expiration_date='{}', pdf='{}', id='{}')>".format(
self.number,
self.verified,
self.budget,
self.expiration_date,
self.pdf,
self.id,
)

View File

@ -42,3 +42,13 @@ class User(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
@property
def displayname(self):
return self.full_name
def __repr__(self):
return "<User(name='{}', dod_id='{}', email='{}', role='{}', has_workspaces='{}', id='{}')>".format(
self.full_name,
self.dod_id,
self.email,
self.atat_role_name,
self.has_workspaces,
self.id,
)

View File

@ -47,3 +47,8 @@ class Workspace(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
def auditable_workspace_id(self):
return self.id
def __repr__(self):
return "<Workspace(name='{}', request='{}', task_order='{}', user_count='{}', id='{}')>".format(
self.name, self.request_id, self.task_order.number, self.user_count, self.id
)

View File

@ -22,6 +22,11 @@ class WorkspaceRole(Base, mixins.TimestampsMixin, mixins.AuditableMixin):
UUID(as_uuid=True), ForeignKey("users.id"), index=True, nullable=False
)
def __repr__(self):
return "<WorkspaceRole(role='{}', workspace='{}', user_id='{}', id='{}')>".format(
self.role.name, self.workspace.name, self.user_id, self.id
)
Index(
"workspace_role_user_workspace",

View File

@ -59,3 +59,11 @@ class WorkspaceUser(object):
@property
def has_environment_roles(self):
return self.num_environment_roles > 0
def __repr__(self):
return "<WorkspaceUser(user='{}', role='{}', workspace='{}', num_environment_roles='{}')>".format(
self.user_name,
self.role.name,
self.workspace.name,
self.num_environment_roles,
)

View File

@ -16,3 +16,9 @@ def test_attach_raises():
fs = FileStorage(fp, content_type="something/else")
with pytest.raises(AttachmentError):
Attachment.attach(fs)
def test_repr(pdf_upload):
attachment = Attachment.attach(pdf_upload)
assert attachment.filename in str(attachment)
assert str(attachment.id) in str(attachment)

View File

@ -16,3 +16,7 @@ def test_add_user_to_environment():
dev_environment = Environments.add_member(dev_environment, developer, "developer")
assert developer in dev_environment.users
def test_repr():
pass

View File

@ -116,6 +116,19 @@ def test_unprotected_routes_set_user_if_logged_in(client, app, user_session):
assert user.full_name in resp.data.decode()
def test_unprotected_routes_set_user_if_logged_in(client, app, user_session):
user = UserFactory.create()
resp = client.get(url_for("atst.helpdocs"))
assert resp.status_code == 200
assert user.full_name not in resp.data.decode()
user_session(user)
resp = client.get(url_for("atst.helpdocs"))
assert resp.status_code == 200
assert user.full_name in resp.data.decode()
# this implicitly relies on the test config and test CRL in tests/fixtures/crl