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

(FEAT): Add reason_for_order field to Order model #502

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions hackathon_site/event/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def test_cannot_leave_with_order(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)

OrderItem.objects.create(order=order, hardware=hardware)
Expand Down Expand Up @@ -272,6 +273,7 @@ def test_leave_with_order(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
OrderItem.objects.create(order=order, hardware=hardware)

Expand Down Expand Up @@ -681,7 +683,10 @@ def setUp(self):
super().setUp()
self.team = Team.objects.create()
self.order = Order.objects.create(
status="Cart", team=self.team, request={"hardware": []}
status="Cart",
team=self.team,
request={"hardware": []},
reason_for_order="Creating a Robot",
)
self.hardware = Hardware.objects.create(
name="name",
Expand Down Expand Up @@ -712,7 +717,10 @@ def setUp(self):
# making extra data to test if team data is being filtered
self.team2 = Team.objects.create(team_code="ABCDE")
self.order_2 = Order.objects.create(
status="Submitted", team=self.team2, request={"hardware": []}
status="Submitted",
team=self.team2,
request={"hardware": []},
reason_for_order="Creating a Robot",
)
OrderItem.objects.create(
order=self.order_2, hardware=self.hardware,
Expand Down Expand Up @@ -755,6 +763,7 @@ def setUp(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)

self.hardware = Hardware.objects.create(
Expand Down Expand Up @@ -804,6 +813,7 @@ def test_post_another_team_incident(self):
status="Cart",
team=self.team2,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
self.order_item2 = OrderItem.objects.create(
order=self.order2, hardware=self.other_hardware,
Expand Down Expand Up @@ -1006,6 +1016,7 @@ def setUp(self):
status="Submitted",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
OrderItem.objects.create(order=order, hardware=hardware)
self.pk = order.id
Expand Down Expand Up @@ -1046,6 +1057,7 @@ def test_failed_beginning_status(self):
status="Picked Up",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
response = self.client.patch(self._build_view(order.id), self.request_data)
self.assertEqual(
Expand All @@ -1062,6 +1074,7 @@ def test_cannot_change_other_team_order(self):
status="Submitted",
team=self.team2,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
response = self.client.patch(self._build_view(order.id), self.request_data)
self.assertEqual(
Expand Down
2 changes: 1 addition & 1 deletion hackathon_site/hardware/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class OrderAdmin(admin.ModelAdmin):
"id",
"get_team_code",
)
fields = ("team", "status")
fields = ("team", "status", "reason_for_order")
search_fields = ("id", "team__team_code")
inlines = (
OrderItemInline,
Expand Down
18 changes: 18 additions & 0 deletions hackathon_site/hardware/migrations/0011_order_reason_for_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.15 on 2023-06-18 15:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("hardware", "0010_alter_order_status"),
]

operations = [
migrations.AddField(
model_name="order",
name="reason_for_order",
field=models.CharField(default="Hackathon Project", max_length=350),
),
]
18 changes: 18 additions & 0 deletions hackathon_site/hardware/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.db.models import Count, F, Q
from django.core.exceptions import ValidationError

from event.models import Team as TeamEvent

Expand Down Expand Up @@ -115,10 +116,27 @@ class Order(models.Model):
max_length=64, choices=STATUS_CHOICES, default="Submitted"
)
request = models.JSONField(null=False)
reason_for_order = models.CharField(
max_length=350, null=False, blank=False, default=None
)

created_at = models.DateTimeField(auto_now_add=True, null=False)
updated_at = models.DateTimeField(auto_now=True, null=False)

def validate_reason_for_order_length(self):
# If field is none, DB will raise IntegrityError automatically
if self.reason_for_order is None:
return
max_length = self._meta.get_field("reason_for_order").max_length
if len(self.reason_for_order) > max_length:
raise ValidationError(
f"Reason for order must be {max_length} characters or less."
)

def save(self, *args, **kwargs):
self.validate_reason_for_order_length()
super().save(*args, **kwargs)

def __str__(self):
return f"{self.id}"

Expand Down
2 changes: 2 additions & 0 deletions hackathon_site/hardware/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class Meta:
"team_id",
"team_code",
"status",
"reason_for_order",
"created_at",
"updated_at",
"request",
Expand Down Expand Up @@ -334,6 +335,7 @@ def create(self, validated_data):
team=self.context["request"].user.profile.team,
status="Submitted",
request=serialized_requested_hardware,
reason_for_order="Hackathon Project",
)
response_data["order_id"] = new_order.id
order_items += [
Expand Down
28 changes: 28 additions & 0 deletions hackathon_site/hardware/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def setUp(self):
{"id": 3, "quantity": 2},
]
},
reason_for_order="Creating a Robot",
)

def _build_filter_url(self, **kwargs):
Expand Down Expand Up @@ -349,6 +350,7 @@ def setUp(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
self.hardware = Hardware.objects.create(
name="name",
Expand Down Expand Up @@ -471,6 +473,7 @@ def setUp(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
self.hardware = Hardware.objects.create(
name="name",
Expand Down Expand Up @@ -501,6 +504,7 @@ def setUp(self):
status="Submitted",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
OrderItem.objects.create(
order=self.order_2, hardware=self.hardware,
Expand All @@ -512,6 +516,7 @@ def setUp(self):
status="Cancelled",
team=self.team2,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
OrderItem.objects.create(
order=self.order_3, hardware=self.hardware,
Expand All @@ -520,6 +525,7 @@ def setUp(self):
status="Submitted",
team=self.team2,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
OrderItem.objects.create(
order=self.order_4, hardware=self.hardware,
Expand Down Expand Up @@ -662,6 +668,7 @@ def setUp(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
self.hardware = Hardware.objects.create(
name="name",
Expand Down Expand Up @@ -692,6 +699,7 @@ def setUp(self):
status="Submitted",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
self.order_item_3 = OrderItem.objects.create(
order=self.order_2, hardware=self.hardware,
Expand All @@ -703,6 +711,7 @@ def setUp(self):
status="Cancelled",
team=self.team2,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
self.order_item_5 = OrderItem.objects.create(
order=self.order_3, hardware=self.hardware,
Expand All @@ -711,6 +720,7 @@ def setUp(self):
status="Submitted",
team=self.team2,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
self.order_item_6 = OrderItem.objects.create(
order=self.order_4, hardware=self.hardware,
Expand Down Expand Up @@ -813,6 +823,7 @@ def setUp(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
self.permissions = Permission.objects.filter(
content_type__app_label="hardware", codename="add_incident"
Expand Down Expand Up @@ -875,6 +886,7 @@ def setUp(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 1}]},
reason_for_order="Creating a Robot",
)
self.hardware = Hardware.objects.create(
name="name",
Expand Down Expand Up @@ -1084,6 +1096,7 @@ def create_order(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2},]},
reason_for_order="Creating a Robot",
)

self.category1 = Category.objects.create(name="category1", max_per_team=4)
Expand Down Expand Up @@ -1269,6 +1282,7 @@ def test_invalid_input_hardware_limit_past_orders(self):
team=self.user.profile.team,
status="Submitted",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
submitted_order_item = OrderItem.objects.create(
order=submitted_order, hardware=hardware
Expand All @@ -1278,6 +1292,7 @@ def test_invalid_input_hardware_limit_past_orders(self):
team=self.user.profile.team,
status="Ready for Pickup",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
ready_order_item = OrderItem.objects.create(
order=ready_order, hardware=hardware
Expand All @@ -1287,6 +1302,7 @@ def test_invalid_input_hardware_limit_past_orders(self):
team=self.user.profile.team,
status="Picked Up",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
picked_up_order_item = OrderItem.objects.create(
order=picked_up_order, hardware=hardware
Expand Down Expand Up @@ -1327,6 +1343,7 @@ def test_hardware_limit_returned_orders(self):
team=self.user.profile.team,
status="Picked Up",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
healthy_order_item = OrderItem.objects.create(
order=order, hardware=hardware, part_returned_health="Healthy"
Expand Down Expand Up @@ -1379,6 +1396,7 @@ def test_hardware_limit_cancelled_orders(self):
team=self.user.profile.team,
status="Cancelled",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
order_item = OrderItem.objects.create(order=order, hardware=hardware)

Expand Down Expand Up @@ -1451,6 +1469,7 @@ def test_invalid_input_category_limit_past_orders(self):
team=self.user.profile.team,
status="Submitted",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
submitted_order_item = OrderItem.objects.create(
order=submitted_order, hardware=hardware
Expand All @@ -1460,6 +1479,7 @@ def test_invalid_input_category_limit_past_orders(self):
team=self.user.profile.team,
status="Ready for Pickup",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
ready_order_item = OrderItem.objects.create(
order=ready_order, hardware=hardware
Expand All @@ -1469,6 +1489,7 @@ def test_invalid_input_category_limit_past_orders(self):
team=self.user.profile.team,
status="Picked Up",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
picked_up_order_item = OrderItem.objects.create(
order=picked_up_order, hardware=hardware
Expand Down Expand Up @@ -1509,6 +1530,7 @@ def test_category_limit_returned_orders(self):
team=self.user.profile.team,
status="Picked Up",
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
reason_for_order="Creating a Robot",
)
healthy_order_item = OrderItem.objects.create(
order=order, hardware=hardware, part_returned_health="Healthy"
Expand Down Expand Up @@ -1561,6 +1583,7 @@ def test_category_limit_cancelled_orders(self):
team=self.user.profile.team,
status="Cancelled",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
order_item = OrderItem.objects.create(order=order, hardware=hardware)

Expand Down Expand Up @@ -1778,6 +1801,7 @@ def test_limited_by_remaining_quantities(self):
team=self.user.profile.team,
status="Submitted",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
OrderItem.objects.bulk_create(
[
Expand Down Expand Up @@ -1849,6 +1873,7 @@ def test_no_remaining_quantities(self):
team=self.user.profile.team,
status="Submitted",
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
order_item = OrderItem.objects.create(order=order, hardware=hardware)

Expand Down Expand Up @@ -1915,6 +1940,7 @@ def setUp(self):
status="Submitted",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
OrderItem.objects.create(order=order, hardware=hardware)
self.pk = order.id
Expand Down Expand Up @@ -1964,6 +1990,7 @@ def test_failed_beginning_status(self):
status="Picked Up",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
response = self.client.patch(self._build_view(order.id), request_data)
self.assertEqual(
Expand All @@ -1985,6 +2012,7 @@ def setUp(self):
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
reason_for_order="Creating a Robot",
)
self.permissions = Permission.objects.filter(
content_type__app_label="hardware", codename="change_order"
Expand Down
Loading