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

DRY common response object database fields #107

Merged
merged 13 commits into from
Aug 22, 2023
16 changes: 9 additions & 7 deletions usaon_vta_survey/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ def conv_String(self, field_args, **extra):
ResponseObservingSystem: model_form(
ResponseObservingSystem,
only=[
'name',
'url',
'author_name',
'author_email',
'short_name',
'long_name',
'website_url',
'contact_name',
'contact_email',
'funding_country',
'funding_agency',
'references_citations',
'notes',
'organization',
'tags',
'version',
'description',
],
),
# TODO: Restrict "rating" values to correct range
Expand Down
30 changes: 21 additions & 9 deletions usaon_vta_survey/models/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ def __io__(cls) -> IORelationship:
return io


class ResponseObjectFieldMixin:
rmarow marked this conversation as resolved.
Show resolved Hide resolved
"""Provide shared fields between all relationshio objects to reduce repition."""

short_name = Column(String(256), nullable=False)
full_name = Column(String(256), nullable=False)
organization = Column(String(256), nullable=False)
# should this be funding agency?
funder = Column(String(256), nullable=False)
rmarow marked this conversation as resolved.
Show resolved Hide resolved
funding_country = Column(String(256), nullable=False)
# clairify this
website_url = Column(String(256), nullable=True)
rmarow marked this conversation as resolved.
Show resolved Hide resolved
description = Column(String(512), nullable=True)
contact_name = Column(String(256), nullable=False)
contact_title = Column(String(256), nullable=True)
contact_email = Column(String(256), nullable=False)
# how do i make it a minimum of 3 tags?
tags = Column(String, nullable=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmarow I'm not sure I understand this. I looked in the glossary and didn't see anything about a minimum number of tags. I think this should be an Array column (sqlalchemy)!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is hard to code, we can just add explanatory text that says "recommend three or more tags." Sandy had suggested this last week, and I added a note in the front-end document, which @rmarow noted. I think the explanatory text is a good way to go. If we do some testing and folks aren't responding to that request, we can adjust.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, let's go with explanatory text and treat the actual validation as icing for later :)

version = Column(String(64), nullable=True)


class User(BaseModel, UserMixin):
__tablename__ = 'user'
id = Column(
Expand Down Expand Up @@ -185,7 +205,7 @@ class Response(BaseModel):
)


class ResponseObservingSystem(BaseModel, IORelationshipMixin):
class ResponseObservingSystem(BaseModel, IORelationshipMixin, ResponseObjectFieldMixin):
__tablename__ = 'response_observing_system'
__table_args__ = (UniqueConstraint('name', 'response_id'),)
id = Column(
Expand Down Expand Up @@ -213,14 +233,6 @@ class ResponseObservingSystem(BaseModel, IORelationshipMixin):
'polymorphic_on': type,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id and response_id are also common fields. Can those go in the mixin as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sqlalchemy did not like when i tried to do this.


url = Column(String(256), nullable=False)
author_name = Column(String(256), nullable=False)
author_email = Column(String(256), nullable=False)
funding_country = Column(String(256), nullable=False)
funding_agency = Column(String(256), nullable=False)
references_citations = Column(String(512), nullable=False)
notes = Column(String(512), nullable=True)

response = relationship(
'Response',
back_populates='observing_systems',
Expand Down
42 changes: 21 additions & 21 deletions usaon_vta_survey/templates/macros/forms/misc.j2
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
{% macro observing_system_fields(form) -%}
rmarow marked this conversation as resolved.
Show resolved Hide resolved
{{form.name.label}} {{form.name(size=50)}}
{% if form.name.errors %}
{{form.short_name.label}} {{form.short_name(size=50)}}
{% if form.short_name.errors %}
<ul class="errors">
{% for error in form.name.errors %}
{% for error in form.short_name.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<br />
rmarow marked this conversation as resolved.
Show resolved Hide resolved

{{form.url.label}} {{form.url(size=50)}}
{% if form.url.errors %}
{{form.website_url.label}} {{form.website_url(size=50)}}
{% if form.website_url.errors %}
<ul class="errors">
{% for error in form.url.errors %}
{% for error in form.website_url.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<br />

{{form.author_name.label}} {{form.author_name(size=50)}}
{% if form.author_name.errors %}
{{form.contact_name.label}} {{form.contact_name(size=50)}}
{% if form.contact_name.errors %}
<ul class="errors">
{% for error in form.author_name.errors %}
{% for error in form.contact_name.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<br />

{{form.author_email.label}} {{form.author_email(size=50)}}
{% if form.author_email.errors %}
{{form.contact_email.label}} {{form.contact_email(size=50)}}
{% if form.contact_email.errors %}
<ul class="errors">
{% for error in form.author_email.errors %}
{% for error in form.contact_email.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
Expand All @@ -49,30 +49,30 @@
{% endif %}
<br />

{{form.funding_agency.label}} {{form.funding_agency(size=50)}}
{% if form.funding_agency.errors %}
{{form.organization.label}} {{form.organization(size=50)}}
{% if form.organization.errors %}
<ul class="errors">
{% for error in form.funding_agency.errors %}
{% for error in form.organization.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<br />

{{form.references_citations.label}} {{form.references_citations(size=50)}}
{% if form.references_citations.errors %}
{{form.tags.label}} {{form.tags(size=50)}}
{% if form.tags.errors %}
<ul class="errors">
{% for error in form.references_citations.errors %}
{% for error in form.tags.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<br />

{{form.notes.label}} {{form.notes(size=50)}}
{% if form.notes.errors %}
{{form.description.label}} {{form.description(size=50)}}
{% if form.description.errors %}
<ul class="errors">
{% for error in form.notes.errors %}
{% for error in form.description.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
Expand Down