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

PLAN-1562 - Changes origin field from project area to scenario #1685

Merged
merged 3 commits into from
Aug 20, 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
@@ -0,0 +1,33 @@
from django.db import migrations, models


def set_origin(apps, schema_editor):
from planning.models import ScenarioOrigin

Scenario = apps.get_model("planning", "Scenario")
Scenario.objects.all().update(origin=ScenarioOrigin.SYSTEM)


class Migration(migrations.Migration):
dependencies = [
("planning", "0021_planningarea_deleted_at_scenario_deleted_at_and_more"),
]

operations = [
migrations.RemoveField(
model_name="projectarea",
name="origin",
),
migrations.AddField(
model_name="scenario",
name="origin",
field=models.CharField(
choices=[
("SYSTEM", "System"),
("USER", "User"),
],
null=True,
),
),
migrations.RunPython(set_origin),
]
17 changes: 12 additions & 5 deletions src/planscape/planning/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def list_by_user(self, user: Optional[UserType]):
return self.get_queryset().filter(planning_area__id__in=planning_areas)


class ScenarioOrigin(models.TextChoices):
# project comes from optimization algorithm, such as forsys
SYSTEM = "SYSTEM", "System"
# project comes from direct user creation / import
USER = "USER", "User"


class Scenario(CreatedAtMixin, UpdatedAtMixin, DeletedAtMixin, models.Model):
planning_area = models.ForeignKey(
PlanningArea,
Expand All @@ -166,6 +173,11 @@ class Scenario(CreatedAtMixin, UpdatedAtMixin, DeletedAtMixin, models.Model):

name = models.CharField(max_length=120)

origin = models.CharField(
choices=ScenarioOrigin.choices,
null=True,
)

notes = models.TextField(null=True)

configuration = models.JSONField(default=dict)
Expand Down Expand Up @@ -286,11 +298,6 @@ class ProjectArea(

name = models.CharField(max_length=128)

origin = models.CharField(
choices=ProjectAreaOrigin.choices,
default=ProjectAreaOrigin.OPTIMIZATION,
help_text="Determines where this project area came from.",
)
data = models.JSONField(null=True)

geometry = models.MultiPolygonField(
Expand Down
2 changes: 1 addition & 1 deletion src/planscape/planning/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class Meta:
"created_at",
"planning_area",
"name",
"origin",
"notes",
"configuration",
"scenario_result",
Expand Down Expand Up @@ -370,7 +371,6 @@ class Meta:
"id",
"scenario",
"name",
"origin",
"data",
"geometry",
)
4 changes: 4 additions & 0 deletions src/planscape/planning/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
PlanningAreaType,
ProjectArea,
Scenario,
ScenarioOrigin,
ScenarioResult,
ScenarioResultStatus,
ScenarioStatus,
Expand Down Expand Up @@ -90,8 +91,11 @@ def delete_planning_area(

@transaction.atomic()
def create_scenario(user: UserType, **kwargs) -> Scenario:
# precedence here to the `kwargs`. if you supply `origin` here
# your origin will be used instead of this default one.
george-silva marked this conversation as resolved.
Show resolved Hide resolved
data = {
"user": user,
"origin": ScenarioOrigin.SYSTEM,
"result_status": ScenarioResultStatus.PENDING,
**kwargs,
}
Expand Down
7 changes: 3 additions & 4 deletions src/planscape/planning/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ProjectArea,
ProjectAreaOrigin,
Scenario,
ScenarioOrigin,
ScenarioResult,
ScenarioResultStatus,
ScenarioStatus,
Expand Down Expand Up @@ -39,6 +40,8 @@ class Meta:

name = factory.Sequence(lambda x: "scenario %s" % x)

origin = factory.Iterator([ScenarioOrigin.SYSTEM, ScenarioOrigin.USER])

uuid = factory.Faker("uuid4")

status = ScenarioStatus.ACTIVE
Expand All @@ -62,10 +65,6 @@ class ProjectAreaFactory(factory.django.DjangoModelFactory):

name = factory.Sequence(lambda x: "project area %s" % x)

origin = factory.Iterator(
[ProjectAreaOrigin.OPTIMIZATION, ProjectAreaOrigin.USER_CREATED]
)

geometry = MultiPolygon(Polygon(((1, 1), (1, 2), (2, 2), (1, 1))))

class Meta:
Expand Down
Loading