From f5fc606c0ca46ea644a3998ac1f8b1f4cfca8182 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 21 Jul 2019 10:28:25 -0500 Subject: [PATCH] Added number_of_days_since_restocked property to ItemListing + selling command now displays how many days since the last restock --- GeoffreyApp/api/commands.py | 8 +++----- GeoffreyApp/models.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/GeoffreyApp/api/commands.py b/GeoffreyApp/api/commands.py index 833e789..a4fc82c 100644 --- a/GeoffreyApp/api/commands.py +++ b/GeoffreyApp/api/commands.py @@ -437,16 +437,14 @@ def get_selling(item_name, sort): for shop_id in shop_ids: shop = Shop.objects.get(pk=shop_id['shop_id']).json - item_query = ItemListing.objects.annotate(normalized_price=F('price') / F('amount')) \ - .filter(item_name__icontains=item_name, shop_id=shop_id['shop_id']) \ - .order_by(sort) \ - .values("item_name", "price", "amount") + item_query = ItemListing.objects.filter( + item_name__icontains=item_name, shop_id=shop_id['shop_id']).order_by(sort) item_list = [] for item in item_query: item_list.append(item) - shop["items"] = item_list + shop["items"] = objects_list_to_json(item_list) items.append(shop) return items diff --git a/GeoffreyApp/models.py b/GeoffreyApp/models.py index b80fff9..a51581e 100644 --- a/GeoffreyApp/models.py +++ b/GeoffreyApp/models.py @@ -2,6 +2,7 @@ from django.db import models from django.conf import settings from django.urls import reverse from sys import maxsize +import datetime from GeoffreyApp.util import create_token, objects_list_to_json @@ -413,6 +414,15 @@ class ItemListing(models.Model): else: return self.price / self.amount + @property + def number_of_days_since_restocked(self): + """ + days since this item was restocked + """ + days = (datetime.datetime.now().astimezone() - self.date_restocked.astimezone()).days + + return days + @property def json(self): """ @@ -425,6 +435,7 @@ class ItemListing(models.Model): "price": 1, "amount": 1, "date_restocked": 1553264508, + "days_since_restock": 10, "normalized_price": 1, "shop": {} } @@ -434,6 +445,7 @@ class ItemListing(models.Model): "price": self.price, "amount": self.amount, "date_restocked": self.date_restocked.timestamp(), + "days_since_restock": self.number_of_days_since_restocked, "normalized_price": self.normalized_price, "shop": self.shop.json, }