Skip to content

Commit

Permalink
fix(plant): fixes form to only include zone objects the user has. (#258)
Browse files Browse the repository at this point in the history
* fix(plant): fixes form to only include zone objects the user has.

* fix: tests
  • Loading branch information
ollz272 authored May 6, 2023
1 parent 55dfa6d commit fb00c4b
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions apps/plants/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from plants.models import Plant, Sensor
from rest_framework.exceptions import ValidationError

from zones.models import Zone


class PlantDataFilterForm(forms.Form):
"""
Expand Down Expand Up @@ -48,6 +50,9 @@ class PlantForm(forms.ModelForm):
def __init__(self, user=None, *args, **kwargs):
super().__init__(*args, **kwargs)

if user is not None:
self.fields["zone"].queryset = Zone.objects.filter(user=user)

self.user = user
self.helper = FormHelper()
self.helper.layout = Layout(
Expand Down
2 changes: 1 addition & 1 deletion apps/plants/migrations/0010_plant_zone.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 4.2 on 2023-05-01 08:04

from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
2 changes: 1 addition & 1 deletion apps/plants/migrations/0011_alter_plant_zone.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 4.2 on 2023-05-01 08:10

from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
2 changes: 1 addition & 1 deletion apps/plants/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class PlantFactory(DjangoModelFactory):
user = factory.SubFactory(UserFactory)
name = factory.Sequence(lambda n: f"plant {n}")
zone = factory.SubFactory(ZoneFactory)
zone = factory.SubFactory(ZoneFactory, user=factory.SelfAttribute("..user"))

class Meta:
model = models.Plant
Expand Down
2 changes: 1 addition & 1 deletion apps/plants/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class TestPlantForm(TestCase):
def test_create_plant(self):
user = UserFactory()
zone = ZoneFactory()
zone = ZoneFactory(user=user)
plant_form = PlantForm(user=user, data={"name": "test", "zone": zone.id})
plant_form.save()

Expand Down
3 changes: 2 additions & 1 deletion apps/plants/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_create_plant_200(self):
self.assertEqual(resp.status_code, 200)

def test_create_plant_301_submit_form(self):
zone = ZoneFactory()
zone = ZoneFactory(user=self.user)
resp = self.app.get(reverse(self.url_name))
form = resp.forms[0]
form["name"] = "test"
Expand All @@ -63,6 +63,7 @@ def setUp(self) -> None:
self.app.set_user(self.user)
self.url_name = "plant-update"
self.plant = PlantFactory(user=self.user)
self.zone = ZoneFactory(user=self.user)

def test_update_plant_200(self):
resp = self.app.get(reverse(self.url_name, kwargs={"plant_pk": self.plant.id}))
Expand Down
1 change: 0 additions & 1 deletion apps/zones/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.contrib import admin

from zones.models import Zone


Expand Down
4 changes: 2 additions & 2 deletions apps/zones/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Generated by Django 4.2 on 2023-05-01 08:04

from django.conf import settings
import django.contrib.gis.db.models.fields
from django.db import migrations, models
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
2 changes: 1 addition & 1 deletion apps/zones/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib.auth import get_user_model
from django.db import models
from django.contrib.gis.db import models as gis_models
from django.db import models


class Zone(models.Model):
Expand Down

0 comments on commit fb00c4b

Please sign in to comment.