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

BDRSPS-883 Added specific implementation for the occurrence field tablers to include vocab links to the threatStatus table #251

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ must come from one of five options in this table.</font></ins>

{{tables.vocabularies}}

<a name="threatStatus-vocabularies"></a>
<a name="threatStatus-vocabularies"></a><a name="conservationJurisdiction-vocabularies"></a>
<ins>Table 2b: Suggested values for conditionally mandatory values for the threatStatus and
conservationJurisdiction in the template. State and Territory conservationJurisdictions spelt out
in words are also valid. For some threatStatus terms, alternative labels are provided that are also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ must come from one of five options in this table.</font></ins>

{{tables.vocabularies}}

<a name="threatStatus-vocabularies"></a>
<a name="threatStatus-vocabularies"></a><a name="conservationAuthority-vocabularies"></a>
<ins>Table 2b: Suggested values for conditionally mandatory values for the threatStatus and
conservationAuthority in the template. State and Territory Conservation Authorities spelt out
in words are also valid. For some threatStatus terms, alternative labels are provided that are also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ labels with similar semantics are provided. Note: <font color="red">`geodeticDat

{{tables.vocabularies}}

<a name="threatStatus-vocabularies"></a>
<a name="threatStatus-vocabularies"></a><a name="conservationJurisdiction-vocabularies"></a>
<ins>Table 2b: Suggested values for conditionally mandatory values for the `threatStatus` and
`conservationJurisdiction` fields in the template. State and Territory `conservationJurisdictions`
spelt out as words are also valid. For some `threatStatus` terms, alternative labels are provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ labels with similar semantics are provided. Note: <font color="red">`geodeticDat

{{tables.vocabularies}}

<a name="threatStatus-vocabularies"></a>
<a name="threatStatus-vocabularies"></a><a name="conservationAuthority-vocabularies"></a>
<ins>Table 2b: Suggested values for conditionally mandatory values for the `threatStatus` and
`conservationAuthority` fields in the template. State and Territory `conservationAuthority`
spelt out as words are also valid. For some `threatStatus` terms, alternative labels are provided
Expand Down
2 changes: 1 addition & 1 deletion docs/contexts/incidental_occurrence_data_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Create context
_ctx = {
"tables": {
"fields": tables.fields.FieldTabler(template_id=mapper_id, format="markdown").generate_table(),
"fields": tables.fields.OccurrenceFieldTabler(template_id=mapper_id, format="markdown").generate_table(),
"vocabularies": tables.vocabs.VocabTabler(template_id=mapper_id, format="markdown").generate_table(),
"threat_status": tables.threat_status.ThreatStatusTabler(
template_id=mapper_id, format="markdown"
Expand Down
2 changes: 1 addition & 1 deletion docs/contexts/incidental_occurrence_data_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _ctx() -> dict[str, Any]:
# Return
return {
"tables": {
"fields": tables.fields.FieldTabler(template_id=mapper_id, format="markdown").generate_table(),
"fields": tables.fields.OccurrenceFieldTabler(template_id=mapper_id, format="markdown").generate_table(),
"vocabularies": tables.vocabs.VocabTabler(template_id=mapper_id, format="markdown").generate_table(),
"threat_status": tables.threat_status.ThreatStatusTabler(
template_id=mapper_id, format="markdown"
Expand Down
2 changes: 1 addition & 1 deletion docs/contexts/survey_occurrence_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Create context
_ctx = {
"tables": {
"fields": tables.fields.FieldTabler(template_id=mapper_id, format="markdown").generate_table(),
"fields": tables.fields.OccurrenceFieldTabler(template_id=mapper_id, format="markdown").generate_table(),
"vocabularies": tables.vocabs.VocabTabler(
template_id=mapper_id,
format="markdown",
Expand Down
2 changes: 1 addition & 1 deletion docs/contexts/survey_occurrence_data_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _ctx() -> dict[str, Any]:
# Return
return {
"tables": {
"fields": tables.fields.FieldTabler(template_id=mapper_id, format="markdown").generate_table(),
"fields": tables.fields.OccurrenceFieldTabler(template_id=mapper_id, format="markdown").generate_table(),
"vocabularies": tables.vocabs.VocabTabler(template_id=mapper_id, format="markdown").generate_table(),
"threat_status": tables.threat_status.ThreatStatusTabler(
template_id=mapper_id, format="markdown"
Expand Down
58 changes: 53 additions & 5 deletions docs/tables/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ def header(self) -> list[str]:
raw_hdr = (hdr.serialization_alias or hdr.title for hdr in FieldTableRow.model_fields.values())
return [hdr for hdr in raw_hdr if hdr is not None]

@property
def fields(self) -> list[types.schema.Field]:
"""Getter for the fields.

Returns:
list[types.schema.Field]: List of all fields from schema.
"""
# Get fields from schema and return
dict_fields = self.mapper.schema()["fields"]
return [types.schema.Field.model_validate(f) for f in dict_fields]

def generate_table(
self,
dest: IO | None = None,
Expand All @@ -60,12 +71,8 @@ def generate_table(
# Write header
self.writer.writeheader()

# Localize fields
dict_fields = self.mapper.schema()["fields"]
fields: list[types.schema.Field] = [types.schema.Field.model_validate(f) for f in dict_fields]

# Iterate through fields and add to csv
for field in fields:
for field in self.fields:
# Perform basic mapping initially
field_table_row = self.generate_row(field)

Expand Down Expand Up @@ -210,6 +217,47 @@ def mutual_inclusivity(field_name: str, checklist: frictionless.Checklist) -> se
return fields


class OccurrenceField(types.schema.Field):
"""Specific implementation of the field model.

To be used only in the creation of survey and incidental occurrence field tables.
"""

@property
def publishable_vocabularies(self) -> list[str]:
"""Returns a list of only those vocabularies that are publishable.

Returns:
list[str]: The publishable vocabularies.
"""
# Filter and return
vocabs = []
for v_id in self.vocabularies:
v = self.get_vocab(v_id)
# Modified from normal case threatStatus and conservationAuthority fields to make them
# publishable so links are created for their corresponding vocab which is
# currently its own table in the instructions.
if v.publish or v.vocab_id in ["THREAT_STATUS", "CONSERVATION_AUTHORITY"]:
vocabs.append(v_id)
return vocabs


class OccurrenceFieldTabler(FieldTabler):
"""Specific implementation of the field tabler for survey and incidental occurrence templates."""

@property
def fields(self) -> list[types.schema.Field]:
"""Getter for the fields.

Returns:
list[OccurrenceField]: List of all fields from schema. Note: this specifically
returns OccurrenceField object list
"""
# Get fields from schema and return
dict_fields = self.mapper.schema()["fields"]
return [OccurrenceField.model_validate(f) for f in dict_fields]


if __name__ == "__main__":
"""Main entry point."""
# Create argument parser
Expand Down