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

fix: move measurement units to right model (d'oh!) #1214

Merged
merged 6 commits into from
Mar 28, 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
23 changes: 12 additions & 11 deletions terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ type ProjectNode implements Node {
name: String!
description: String!
membershipList: ProjectMembershipListNode!
measurementUnits: ProjectManagementProjectMeasurementUnitsChoices!
privacy: ProjectManagementProjectPrivacyChoices!
archived: Boolean!
siteInstructions: String
Expand Down Expand Up @@ -706,6 +707,15 @@ enum ProjectMembershipProjectRoleChoices {
MANAGER
}

"""An enumeration."""
enum ProjectManagementProjectMeasurementUnitsChoices {
"""English"""
ENGLISH

"""Metric"""
METRIC
}

"""An enumeration."""
enum ProjectManagementProjectPrivacyChoices {
"""Private"""
Expand Down Expand Up @@ -1407,7 +1417,6 @@ enum SoilIdDepthDependentSoilDataCarbonatesChoices {

type ProjectSoilSettingsNode {
project: ProjectNode!
measurementUnits: SoilIdProjectSoilSettingsMeasurementUnitsChoices
depthIntervalPreset: SoilIdProjectSoilSettingsDepthIntervalPresetChoices!
soilPitRequired: Boolean!
slopeRequired: Boolean!
Expand All @@ -1427,15 +1436,6 @@ type ProjectSoilSettingsNode {
depthIntervals: [ProjectDepthIntervalNode!]!
}

"""An enumeration."""
enum SoilIdProjectSoilSettingsMeasurementUnitsChoices {
"""Imperial"""
IMPERIAL

"""Metric"""
METRIC
}

"""An enumeration."""
enum SoilIdProjectSoilSettingsDepthIntervalPresetChoices {
"""Landpks"""
Expand Down Expand Up @@ -2030,6 +2030,7 @@ type ProjectAddMutationPayload {

input ProjectAddMutationInput {
name: String!
measurementUnits: ProjectManagementProjectMeasurementUnitsChoices
privacy: ProjectManagementProjectPrivacyChoices
description: String
siteInstructions: String
Expand All @@ -2046,6 +2047,7 @@ type ProjectUpdateMutationPayload {
input ProjectUpdateMutationInput {
id: ID!
name: String
measurementUnits: ProjectManagementProjectMeasurementUnitsChoices
privacy: ProjectManagementProjectPrivacyChoices
description: String
siteInstructions: String
Expand Down Expand Up @@ -2238,7 +2240,6 @@ type ProjectSoilSettingsUpdateMutationPayload {

input ProjectSoilSettingsUpdateMutationInput {
projectId: ID!
measurementUnits: SoilIdProjectSoilSettingsMeasurementUnitsChoices
depthIntervalPreset: SoilIdProjectSoilSettingsDepthIntervalPresetChoices
soilPitRequired: Boolean
slopeRequired: Boolean
Expand Down
16 changes: 5 additions & 11 deletions terraso_backend/apps/graphql/schema/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ def mutate_and_get_payload(cls, root, info, create_soil_data=True, **kwargs):
log = cls.get_logger()
user = info.context.user

if "privacy" in kwargs:
kwargs["privacy"] = kwargs["privacy"].value

client_time = kwargs.pop("client_time", None)
if not client_time:
client_time = datetime.now()
Expand All @@ -141,9 +138,9 @@ def mutate_and_get_payload(cls, root, info, create_soil_data=True, **kwargs):
site = result.site
site.mark_seen_by(user)
metadata = {
"latitude": kwargs["latitude"],
"longitude": kwargs["longitude"],
"name": kwargs["name"],
"latitude": site.latitude,
"longitude": site.longitude,
"name": site.name,
}
if kwargs.get("project_id", None):
metadata["project_id"] = kwargs["project_id"]
Expand Down Expand Up @@ -194,9 +191,6 @@ def mutate_and_get_payload(cls, root, info, **kwargs):
raise cls.not_allowed(MutationTypes.UPDATE)
project_id = kwargs.pop("project_id", False)

if "privacy" in kwargs:
kwargs["privacy"] = kwargs["privacy"].value

result = super().mutate_and_get_payload(root, info, **kwargs)
if project_id is False:
# no project id included
Expand All @@ -215,10 +209,10 @@ def mutate_and_get_payload(cls, root, info, **kwargs):
client_time = datetime.now()

metadata = {}
for key, value in kwargs.items():
for key in kwargs.keys():
if key == "id":
continue
metadata[key] = value
metadata[key] = getattr(site, key)
if project_id:
if hasattr(project, "soil_settings") and hasattr(site, "soil_data"):
if project_id is not None:
Expand Down
14 changes: 10 additions & 4 deletions terraso_backend/apps/project_management/graphql/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Meta:
filterset_class = ProjectFilterSet
fields = (
"name",
"measurement_units",
"privacy",
"description",
"updated_at",
Expand All @@ -136,6 +137,10 @@ def resolve_seen(self, info):
return True
return self.seen_by.filter(id=user.id).exists()

@classmethod
def measurement_units_enum(cls):
return cls._meta.fields["measurement_units"].type.of_type()

@classmethod
def privacy_enum(cls):
return cls._meta.fields["privacy"].type.of_type()
Expand All @@ -158,6 +163,7 @@ class ProjectAddMutation(BaseWriteMutation):

class Input:
name = graphene.String(required=True)
measurement_units = ProjectNode.measurement_units_enum()
privacy = ProjectNode.privacy_enum()
description = graphene.String()
site_instructions = graphene.String()
Expand All @@ -168,7 +174,6 @@ def mutate_and_get_payload(cls, root, info, create_soil_settings=True, **kwargs)
logger = cls.get_logger()
user = info.context.user
with transaction.atomic():
kwargs["privacy"] = kwargs["privacy"].value
result = super().mutate_and_get_payload(root, info, **kwargs)
result.project.add_manager(user)

Expand All @@ -181,9 +186,9 @@ def mutate_and_get_payload(cls, root, info, create_soil_settings=True, **kwargs)
client_time = datetime.now()
action = log_api.CREATE
metadata = {
"name": kwargs["name"],
"privacy": kwargs["privacy"],
"description": kwargs.get("description"),
"name": result.project.name,
"privacy": result.project.privacy,
"description": result.project.description,
}
logger.log(
user=user,
Expand Down Expand Up @@ -270,6 +275,7 @@ class ProjectUpdateMutation(BaseWriteMutation):
class Input:
id = graphene.ID(required=True)
name = graphene.String()
measurement_units = ProjectNode.measurement_units_enum()
privacy = ProjectNode.privacy_enum()
description = graphene.String()
site_instructions = graphene.String()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright © 2024 Technology Matters
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.

# Generated by Django 5.0.2 on 2024-03-27 21:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("project_management", "0026_alter_project_privacy"),
]

operations = [
migrations.AddField(
model_name="project",
name="measurement_units",
field=models.CharField(
choices=[("ENGLISH", "English"), ("METRIC", "Metric")], default="METRIC"
),
),
]
12 changes: 11 additions & 1 deletion terraso_backend/apps/project_management/models/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,21 @@ class Meta(BaseModel.Meta):
description = models.CharField(max_length=512, default="", blank=True)
membership_list = models.OneToOneField(ProjectMembershipList, on_delete=models.CASCADE)

class MeasurementUnit(models.TextChoices):
ENGLISH = "ENGLISH"
METRIC = "METRIC"

measurement_units = models.CharField(
choices=MeasurementUnit.choices, default=MeasurementUnit.METRIC.value
)

class Privacy(models.TextChoices):
PRIVATE = "PRIVATE"
PUBLIC = "PUBLIC"

privacy = models.CharField(max_length=32, choices=Privacy.choices, default=Privacy.PRIVATE)
privacy = models.CharField(
max_length=32, choices=Privacy.choices, default=Privacy.PRIVATE.value
)

seen_by = models.ManyToManyField(User, related_name="+")
archived = models.BooleanField(
Expand Down
5 changes: 0 additions & 5 deletions terraso_backend/apps/soil_id/graphql/soil_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ class Meta:
"updated_at",
]

@classmethod
def measurement_units_enum(cls):
return cls._meta.fields["measurement_units"].type()

@classmethod
def depth_interval_preset_enum(cls):
return cls._meta.fields["depth_interval_preset"].type.of_type()
Expand Down Expand Up @@ -432,7 +428,6 @@ class ProjectSoilSettingsUpdateMutation(BaseWriteMutation):

class Input:
project_id = graphene.ID(required=True)
measurement_units = ProjectSoilSettingsNode.measurement_units_enum()
depth_interval_preset = ProjectSoilSettingsNode.depth_interval_preset_enum()
soil_pit_required = graphene.Boolean()
slope_required = graphene.Boolean()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright © 2024 Technology Matters
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.

# Generated by Django 5.0.2 on 2024-03-27 21:33

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("soil_id", "0012_remove_depthdependentsoildata_color_hue_substep_and_more"),
]

operations = [
migrations.RemoveField(
model_name="projectsoilsettings",
name="measurement_units",
),
]
6 changes: 0 additions & 6 deletions terraso_backend/apps/soil_id/models/project_soil_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ class Meta(BaseModel.Meta):

project = models.OneToOneField(Project, on_delete=models.CASCADE, related_name="soil_settings")

class MeasurementUnit(models.TextChoices):
IMPERIAL = "IMPERIAL"
METRIC = "METRIC"

measurement_units = models.CharField(blank=True, null=True, choices=MeasurementUnit.choices)

depth_interval_preset = models.CharField(
default=DepthIntervalPreset.LANDPKS.value,
choices=DepthIntervalPreset.choices,
Expand Down
Loading
Loading