diff --git a/src/wuttafarm/db/alembic/versions/8898184c5c75_convert_active_to_archived.py b/src/wuttafarm/db/alembic/versions/8898184c5c75_convert_active_to_archived.py new file mode 100644 index 0000000..70bbe2c --- /dev/null +++ b/src/wuttafarm/db/alembic/versions/8898184c5c75_convert_active_to_archived.py @@ -0,0 +1,250 @@ +"""convert active to archived + +Revision ID: 8898184c5c75 +Revises: 3e2ef02bf264 +Create Date: 2026-02-14 18:41:23.042951 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +import wuttjamaican.db.util + + +# revision identifiers, used by Alembic. +revision: str = "8898184c5c75" +down_revision: Union[str, None] = "3e2ef02bf264" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + + # animal + op.alter_column("animal", "active", new_column_name="archived") + animal = sa.sql.table( + "animal", + sa.sql.column("uuid"), + sa.sql.column("archived"), + ) + cursor = op.get_bind().execute(animal.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + animal.update() + .where(animal.c.uuid == row.uuid) + .values({"archived": not row.archived}) + ) + op.alter_column("animal_version", "active", new_column_name="archived") + animal_version = sa.sql.table( + "animal_version", + sa.sql.column("uuid"), + sa.sql.column("archived"), + ) + cursor = op.get_bind().execute(animal_version.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + animal_version.update() + .where(animal_version.c.uuid == row.uuid) + .values({"archived": not row.archived}) + ) + + # group + op.alter_column("group", "active", new_column_name="archived") + group = sa.sql.table( + "group", + sa.sql.column("uuid"), + sa.sql.column("archived"), + ) + cursor = op.get_bind().execute(group.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + group.update() + .where(group.c.uuid == row.uuid) + .values({"archived": not row.archived}) + ) + op.alter_column("group_version", "active", new_column_name="archived") + group_version = sa.sql.table( + "group_version", + sa.sql.column("uuid"), + sa.sql.column("archived"), + ) + cursor = op.get_bind().execute(group_version.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + group_version.update() + .where(group_version.c.uuid == row.uuid) + .values({"archived": not row.archived}) + ) + + # land_asset + op.alter_column("land_asset", "active", new_column_name="archived") + land_asset = sa.sql.table( + "land_asset", + sa.sql.column("uuid"), + sa.sql.column("archived"), + ) + cursor = op.get_bind().execute(land_asset.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + land_asset.update() + .where(land_asset.c.uuid == row.uuid) + .values({"archived": not row.archived}) + ) + op.alter_column("land_asset_version", "active", new_column_name="archived") + land_asset_version = sa.sql.table( + "land_asset_version", + sa.sql.column("uuid"), + sa.sql.column("archived"), + ) + cursor = op.get_bind().execute(land_asset_version.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + land_asset_version.update() + .where(land_asset_version.c.uuid == row.uuid) + .values({"archived": not row.archived}) + ) + + # structure + op.alter_column("structure", "active", new_column_name="archived") + structure = sa.sql.table( + "structure", + sa.sql.column("uuid"), + sa.sql.column("archived"), + ) + cursor = op.get_bind().execute(structure.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + structure.update() + .where(structure.c.uuid == row.uuid) + .values({"archived": not row.archived}) + ) + op.alter_column("structure_version", "active", new_column_name="archived") + structure_version = sa.sql.table( + "structure_version", + sa.sql.column("uuid"), + sa.sql.column("archived"), + ) + cursor = op.get_bind().execute(structure_version.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + structure_version.update() + .where(structure_version.c.uuid == row.uuid) + .values({"archived": not row.archived}) + ) + + +def downgrade() -> None: + + # structure + op.alter_column("structure", "archived", new_column_name="active") + structure = sa.sql.table( + "structure", + sa.sql.column("uuid"), + sa.sql.column("active"), + ) + cursor = op.get_bind().execute(structure.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + structure.update() + .where(structure.c.uuid == row.uuid) + .values({"active": not row.active}) + ) + op.alter_column("structure_version", "archived", new_column_name="active") + structure_version = sa.sql.table( + "structure_version", + sa.sql.column("uuid"), + sa.sql.column("active"), + ) + cursor = op.get_bind().execute(structure_version.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + structure_version.update() + .where(structure_version.c.uuid == row.uuid) + .values({"active": not row.active}) + ) + + # land_asset + op.alter_column("land_asset", "archived", new_column_name="active") + land_asset = sa.sql.table( + "land_asset", + sa.sql.column("uuid"), + sa.sql.column("active"), + ) + cursor = op.get_bind().execute(land_asset.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + land_asset.update() + .where(land_asset.c.uuid == row.uuid) + .values({"active": not row.active}) + ) + op.alter_column("land_asset_version", "archived", new_column_name="active") + land_asset_version = sa.sql.table( + "land_asset_version", + sa.sql.column("uuid"), + sa.sql.column("active"), + ) + cursor = op.get_bind().execute(land_asset_version.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + land_asset_version.update() + .where(land_asset_version.c.uuid == row.uuid) + .values({"active": not row.active}) + ) + + # group + op.alter_column("group", "archived", new_column_name="active") + group = sa.sql.table( + "group", + sa.sql.column("uuid"), + sa.sql.column("active"), + ) + cursor = op.get_bind().execute(group.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + group.update() + .where(group.c.uuid == row.uuid) + .values({"active": not row.active}) + ) + op.alter_column("group_version", "archived", new_column_name="active") + group_version = sa.sql.table( + "group_version", + sa.sql.column("uuid"), + sa.sql.column("active"), + ) + cursor = op.get_bind().execute(group_version.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + group_version.update() + .where(group_version.c.uuid == row.uuid) + .values({"active": not row.active}) + ) + + # animal + op.alter_column("animal", "archived", new_column_name="active") + animal = sa.sql.table( + "animal", + sa.sql.column("uuid"), + sa.sql.column("active"), + ) + cursor = op.get_bind().execute(animal.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + animal.update() + .where(animal.c.uuid == row.uuid) + .values({"active": not row.active}) + ) + op.alter_column("animal_version", "archived", new_column_name="active") + animal_version = sa.sql.table( + "animal_version", + sa.sql.column("uuid"), + sa.sql.column("active"), + ) + cursor = op.get_bind().execute(animal_version.select()) + for row in cursor.fetchall(): + op.get_bind().execute( + animal_version.update() + .where(animal_version.c.uuid == row.uuid) + .values({"active": not row.active}) + ) diff --git a/src/wuttafarm/db/model/animals.py b/src/wuttafarm/db/model/animals.py index e23f0c5..4bc1f8a 100644 --- a/src/wuttafarm/db/model/animals.py +++ b/src/wuttafarm/db/model/animals.py @@ -148,11 +148,12 @@ class Animal(model.Base): """, ) - active = sa.Column( + archived = sa.Column( sa.Boolean(), nullable=False, + default=False, doc=""" - Whether the animal is currently active. + Whether the animal is archived. """, ) diff --git a/src/wuttafarm/db/model/groups.py b/src/wuttafarm/db/model/groups.py index eae034f..2cec3eb 100644 --- a/src/wuttafarm/db/model/groups.py +++ b/src/wuttafarm/db/model/groups.py @@ -68,11 +68,12 @@ class Group(model.Base): """, ) - active = sa.Column( + archived = sa.Column( sa.Boolean(), nullable=False, + default=False, doc=""" - Whether the group is active. + Whether the group is archived. """, ) diff --git a/src/wuttafarm/db/model/land.py b/src/wuttafarm/db/model/land.py index 53c93cf..17e22c1 100644 --- a/src/wuttafarm/db/model/land.py +++ b/src/wuttafarm/db/model/land.py @@ -126,11 +126,12 @@ class LandAsset(model.Base): """, ) - active = sa.Column( + archived = sa.Column( sa.Boolean(), nullable=False, + default=False, doc=""" - Whether the land asset is currently active. + Whether the land asset is archived. """, ) diff --git a/src/wuttafarm/db/model/structures.py b/src/wuttafarm/db/model/structures.py index d9fccdb..546c9a1 100644 --- a/src/wuttafarm/db/model/structures.py +++ b/src/wuttafarm/db/model/structures.py @@ -97,11 +97,12 @@ class Structure(model.Base): """, ) - active = sa.Column( + archived = sa.Column( sa.Boolean(), nullable=False, + default=False, doc=""" - Whether the structure is currently active. + Whether the structure is archived. """, ) diff --git a/src/wuttafarm/importing/farmos.py b/src/wuttafarm/importing/farmos.py index 4acbe24..f337226 100644 --- a/src/wuttafarm/importing/farmos.py +++ b/src/wuttafarm/importing/farmos.py @@ -192,7 +192,7 @@ class AnimalImporter(FromFarmOS, ToWutta): "is_sterile", "birthdate", "notes", - "active", + "archived", "image_url", ] @@ -252,9 +252,9 @@ class AnimalImporter(FromFarmOS, ToWutta): notes = notes["value"] if self.farmos_4x: - active = not animal["attributes"]["archived"] + archived = animal["attributes"]["archived"] else: - active = animal["attributes"]["status"] == "active" + archived = animal["attributes"]["status"] == "archived" return { "farmos_uuid": UUID(animal["id"]), @@ -264,7 +264,7 @@ class AnimalImporter(FromFarmOS, ToWutta): "sex": animal["attributes"]["sex"], "is_sterile": sterile, "birthdate": birthdate, - "active": active, + "archived": archived, "notes": notes, "image_url": image_url, } @@ -344,7 +344,7 @@ class GroupImporter(FromFarmOS, ToWutta): "is_location", "is_fixed", "notes", - "active", + "archived", ] def get_source_objects(self): @@ -358,9 +358,9 @@ class GroupImporter(FromFarmOS, ToWutta): notes = notes["value"] if self.farmos_4x: - active = not group["attributes"]["archived"] + archived = group["attributes"]["archived"] else: - active = group["attributes"]["status"] == "active" + archived = group["attributes"]["status"] == "archived" return { "farmos_uuid": UUID(group["id"]), @@ -368,7 +368,7 @@ class GroupImporter(FromFarmOS, ToWutta): "name": group["attributes"]["name"], "is_location": group["attributes"]["is_location"], "is_fixed": group["attributes"]["is_fixed"], - "active": active, + "archived": archived, "notes": notes, } @@ -388,7 +388,7 @@ class LandAssetImporter(FromFarmOS, ToWutta): "is_location", "is_fixed", "notes", - "active", + "archived", ] def setup(self): @@ -418,9 +418,9 @@ class LandAssetImporter(FromFarmOS, ToWutta): notes = notes["value"] if self.farmos_4x: - active = not land["attributes"]["archived"] + archived = land["attributes"]["archived"] else: - active = land["attributes"]["status"] == "active" + archived = land["attributes"]["status"] == "archived" return { "farmos_uuid": UUID(land["id"]), @@ -429,7 +429,7 @@ class LandAssetImporter(FromFarmOS, ToWutta): "land_type_uuid": land_type.uuid, "is_location": land["attributes"]["is_location"], "is_fixed": land["attributes"]["is_fixed"], - "active": active, + "archived": archived, "notes": notes, } @@ -505,7 +505,7 @@ class StructureImporter(FromFarmOS, ToWutta): "is_location", "is_fixed", "notes", - "active", + "archived", "image_url", ] @@ -550,9 +550,9 @@ class StructureImporter(FromFarmOS, ToWutta): image_url = image_style["large"] if self.farmos_4x: - active = not structure["attributes"]["archived"] + archived = structure["attributes"]["archived"] else: - active = structure["attributes"]["status"] == "active" + archived = structure["attributes"]["status"] == "archived" return { "farmos_uuid": UUID(structure["id"]), @@ -561,7 +561,7 @@ class StructureImporter(FromFarmOS, ToWutta): "structure_type_uuid": structure_type.uuid, "is_location": structure["attributes"]["is_location"], "is_fixed": structure["attributes"]["is_fixed"], - "active": active, + "archived": archived, "notes": notes, "image_url": image_url, } diff --git a/src/wuttafarm/web/views/animal_types.py b/src/wuttafarm/web/views/animal_types.py index 09d1e25..acb1bd9 100644 --- a/src/wuttafarm/web/views/animal_types.py +++ b/src/wuttafarm/web/views/animal_types.py @@ -67,7 +67,7 @@ class AnimalTypeView(WuttaFarmMasterView): "sex", "is_sterile", "birthdate", - "active", + "archived", ] rows_sort_defaults = "name" diff --git a/src/wuttafarm/web/views/animals.py b/src/wuttafarm/web/views/animals.py index e22095e..adddf2a 100644 --- a/src/wuttafarm/web/views/animals.py +++ b/src/wuttafarm/web/views/animals.py @@ -46,7 +46,7 @@ class AnimalView(WuttaFarmMasterView): "sex", "is_sterile", "birthdate", - "active", + "archived", ] sort_defaults = "name" @@ -61,7 +61,7 @@ class AnimalView(WuttaFarmMasterView): "birthdate", "sex", "is_sterile", - "active", + "archived", "notes", "farmos_uuid", "drupal_id", @@ -94,7 +94,9 @@ class AnimalView(WuttaFarmMasterView): f.set_widget("notes", "notes") # image - if animal.image_url: + if self.creating: + f.remove("image") + elif animal.image_url: f.set_widget("image", ImageWidget("animal image")) f.set_default("image", animal.image_url) diff --git a/src/wuttafarm/web/views/groups.py b/src/wuttafarm/web/views/groups.py index 5f2746b..2524bff 100644 --- a/src/wuttafarm/web/views/groups.py +++ b/src/wuttafarm/web/views/groups.py @@ -42,7 +42,7 @@ class GroupView(WuttaFarmMasterView): "name", "is_location", "is_fixed", - "active", + "archived", ] sort_defaults = "name" @@ -55,7 +55,7 @@ class GroupView(WuttaFarmMasterView): "name", "is_location", "is_fixed", - "active", + "archived", "notes", "farmos_uuid", "drupal_id", diff --git a/src/wuttafarm/web/views/land_assets.py b/src/wuttafarm/web/views/land_assets.py index 18f7a3d..1cad870 100644 --- a/src/wuttafarm/web/views/land_assets.py +++ b/src/wuttafarm/web/views/land_assets.py @@ -45,7 +45,7 @@ class LandAssetView(WuttaFarmMasterView): "is_location", "is_fixed", "notes", - "active", + "archived", ] sort_defaults = "name" @@ -60,7 +60,7 @@ class LandAssetView(WuttaFarmMasterView): "is_location", "is_fixed", "notes", - "active", + "archived", "farmos_uuid", "drupal_id", ] diff --git a/src/wuttafarm/web/views/land_types.py b/src/wuttafarm/web/views/land_types.py index 21bfabc..20d8a21 100644 --- a/src/wuttafarm/web/views/land_types.py +++ b/src/wuttafarm/web/views/land_types.py @@ -60,7 +60,7 @@ class LandTypeView(WuttaFarmMasterView): "name", "is_location", "is_fixed", - "active", + "archived", ] rows_sort_defaults = "name" diff --git a/src/wuttafarm/web/views/structure_types.py b/src/wuttafarm/web/views/structure_types.py index ca85fb9..f9d9bf0 100644 --- a/src/wuttafarm/web/views/structure_types.py +++ b/src/wuttafarm/web/views/structure_types.py @@ -60,7 +60,7 @@ class StructureTypeView(WuttaFarmMasterView): "name", "is_location", "is_fixed", - "active", + "archived", ] rows_sort_defaults = "name" diff --git a/src/wuttafarm/web/views/structures.py b/src/wuttafarm/web/views/structures.py index df58fda..b9da13e 100644 --- a/src/wuttafarm/web/views/structures.py +++ b/src/wuttafarm/web/views/structures.py @@ -45,7 +45,7 @@ class StructureView(WuttaFarmMasterView): "structure_type", "is_location", "is_fixed", - "active", + "archived", ] sort_defaults = "name" @@ -60,7 +60,7 @@ class StructureView(WuttaFarmMasterView): "is_location", "is_fixed", "notes", - "active", + "archived", "farmos_uuid", "drupal_id", "image_url",