Skip to content
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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN pip install -r requirements.txt && \
mkdir /staticfiles && \
chown -R django-user:django-user /staticfiles && \
chown -R django-user:django-user recipes && \
chown -R django-user:django-user inventory && \
chmod -R 755 /staticfiles

ENV PATH="/scripts:/py/bin:$PATH"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ To avoid wiping your local db every time you rebuild, make sure the migrate line

### Nice to have

- recipes have uuids why
- if quantity 0, remove expiration and sort last
- recipes by ingredient
- fix test connection
- separate recipe app
- separate user app
- recipe
- rds
- i don't like ingredients as their own model but maybe i need it
- why "ingredientSet"
- recipes by tag
- elastic beanstalk
- want ids to be numbers instead of strings
Expand Down
5 changes: 3 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ python manage.py wait_for_db
python manage.py collectstatic --noinput
# The next line wipes the database, so... be careful.
# python manage.py flush --no-input
yes | python manage.py makemigrations --noinput
python manage.py migrate --fake
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --fake contenttypes
python manage.py spectacular
gunicorn --config gunicorn_config.py cms.wsgi:application

Expand Down
2 changes: 1 addition & 1 deletion inventory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __str__(self):
class InventoryItem(Model):
product = ForeignKey(Product, on_delete=CASCADE)
expiration_date = DateField(null=True, blank=True, auto_now_add=False)
person = ManyToManyField(User)
person = ForeignKey(User, on_delete=CASCADE, default=1)
quantity = IntegerField(default=0)
unit = ForeignKey(QuantitativeUnit, on_delete=CASCADE, blank=True, default='')
ordering_fields = ['expiration_date']
Expand Down
50 changes: 45 additions & 5 deletions inventory/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import graphene
from graphene import Node
from graphene_django import DjangoObjectType
from django_filters import OrderingFilter
from graphene_django.filter import DjangoFilterConnectionField
from .models import Product, InventoryItem, QuantitativeUnit
from core.models import User

Expand All @@ -19,6 +21,9 @@ class InventoryItemType(DjangoObjectType):
class Meta:
model = InventoryItem
fields = ('id', 'product', 'expiration_date', 'person', 'quantity', 'unit')
filter_fields = {'product__name': ['icontains'], 'person__id': ['exact']}
interfaces = (Node, )
interfaces = (graphene.Node,)


class UserType(DjangoObjectType):
Expand All @@ -32,7 +37,10 @@ class Query(graphene.ObjectType):

products = graphene.List(ProductType)

inventory_items = graphene.List(InventoryItemType)
units = graphene.List(UnitType)

# inventory_items = graphene.List(InventoryItemType)
inventory_items = DjangoFilterConnectionField(InventoryItemType)

viewer = graphene.Field(lambda: Query)

Expand All @@ -54,13 +62,16 @@ def resolve_product(self, info, **kwargs):
def resolve_products(self, info, **kwargs):
return Product.objects.all().order_by('name')

def resolve_units(self, info, **kwargs):
return QuantitativeUnit.objects.all().order_by('name')

def resolve_inventory_items(self, info, **kwargs):
return InventoryItem.objects.all().order_by('expiration_date')


class UpdateItemQuantity(graphene.Mutation):
class Arguments:
id = graphene.Int(required=True)
id = graphene.String(required=True)
quantity = graphene.Int(required=True)

inventory_item = graphene.Field(InventoryItemType)
Expand All @@ -79,7 +90,7 @@ class QuantitativeUnitInput(graphene.InputObjectType):
class InventoryItemInput(graphene.InputObjectType):
id = graphene.Int(required=True)
quantity = graphene.Int(required=True)
unit = graphene.Field(QuantitativeUnitInput)
unit_id = graphene.Int(required=False)
expiration_date = graphene.String(required=True)


Expand All @@ -89,14 +100,43 @@ class CreateInventoryItem(graphene.Mutation):
class Arguments:
new_inventory_item = InventoryItemInput(required=True)

def mutate(self, info, item_id, quantity):
inventory_item = InventoryItem.objects.create(**new_inventory_item)
def mutate(self, info, new_inventory_item):
user = info.context.user

if user.is_anonymous:
raise Exception("Authentication credentials were not provided.")

user_id = user.id

# TODO: inventory item needs to have person
inventory_item = InventoryItem.objects.create(product_id=new_inventory_item.id, person_id=user_id, quantity=new_inventory_item.quantity, expiration_date=new_inventory_item.expiration_date, unit_id=new_inventory_item.unit_id)
return CreateInventoryItem(inventory_item=inventory_item)


class DeleteInventoryItem(graphene.Mutation):
success = graphene.Boolean()
message = graphene.String()

class Arguments:
id = graphene.ID(required=True)

def mutate(self, info, id):
try:
# Retrieve the item by ID
item = InventoryItem.objects.get(id=id)
# Delete the item
item.delete()
return DeleteInventoryItem(success=True, message="Item deleted successfully.")
except InventoryItem.DoesNotExist:
return DeleteInventoryItem(success=False, message="Item not found.")
except Exception as e:
return DeleteInventoryItem(success=False, message=f"An error occurred: {str(e)}")


class Mutation(graphene.ObjectType):
create_inventory_item = CreateInventoryItem.Field()
update_item_quantity = UpdateItemQuantity.Field()
delete_inventory_item = DeleteInventoryItem.Field()


schema = graphene.Schema(query=Query, mutation=Mutation)
3 changes: 1 addition & 2 deletions recipes/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.contrib import admin
from .models import Recipe, Ingredient, Tag
from .models import Recipe, Ingredient

admin.site.register(Recipe)
admin.site.register(Ingredient)
admin.site.register(Tag)
34 changes: 12 additions & 22 deletions recipes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,9 @@
from inventory.models import QuantitativeUnit, Product


class Tag(Model):
value = CharField(max_length=100, blank=True, null=True, default=None)

def __str__(self):
return self.value


class Ingredient(Model):
quantity = IntegerField
unit = ForeignKey(QuantitativeUnit, on_delete=CASCADE)
product = ForeignKey(Product, on_delete=CASCADE)

def __str__(self):
return self.product.name


class Recipe(Model):
instructions = TextField(blank=True)
ingredients = ManyToManyField(
'Ingredient',
related_name='recipes'
)
ingredients = ManyToManyField(Product, through='Ingredient')
name = CharField(max_length=255)
user = ForeignKey(
settings.AUTH_USER_MODEL,
Expand All @@ -38,8 +19,17 @@ class Recipe(Model):
estimated_cost = DecimalField(max_digits=5, decimal_places=2, blank=True,
null=True)
url = CharField(max_length=255, blank=True)
# tag = ForeignKey(Tag, related_name='recipes', on_delete=CASCADE,
# blank=True, null=True)

def __str__(self):
return self.name


class Ingredient(Model):
product = ForeignKey(Product, on_delete=CASCADE)
recipe = ForeignKey(Recipe, on_delete=CASCADE, blank=True, null=True)
quantity = CharField(max_length=100, blank=True, null=True)
unit = ForeignKey(QuantitativeUnit, on_delete=CASCADE)

def __str__(self):
return f"{self.quantity} {self.unit} of {self.product.name}"

30 changes: 3 additions & 27 deletions recipes/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
from graphene import Node
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Recipe, Tag, Ingredient


class TagType(DjangoObjectType):
class Meta:
model = Tag
from .models import Recipe, Ingredient


class IngredientType(DjangoObjectType):
Expand All @@ -19,16 +14,12 @@ class RecipeType(DjangoObjectType):
class Meta:
model = Recipe
fields = "__all__"
filter_fields = ('name', )
filter_fields = {'name': ['icontains'], 'ingredient__product__id': ['exact']}
interfaces = (Node, )
interfaces = (graphene.Node,)


class Query(graphene.ObjectType):
tag = graphene.Field(TagType, id=graphene.Int(), name=graphene.String())

tags = graphene.List(TagType)

recipe = graphene.Field(RecipeType, id=graphene.Int(),
name=graphene.String())

Expand All @@ -37,21 +28,6 @@ class Query(graphene.ObjectType):
class Arguments:
ingredient_id = graphene.ID()

def resolve_tag(self, info, **kwargs):
id = kwargs.get('id')
name = kwargs.get('name')

if id is not None:
return Tag.objects.get(pk=id)

if name is not None:
return Tag.objects.get(name=name)

return None

def resolve_tags(self, info, **kwargs):
return Tag.objects.all()

def resolve_recipe(self, info, **kwargs):
id = kwargs.get('id')
name = kwargs.get('name')
Expand All @@ -65,7 +41,7 @@ def resolve_recipe(self, info, **kwargs):
return None

def resolve_recipes(self, info, **kwargs):
return Recipe.objects.select_related('tag').all()
return Recipe.objects.all()


schema = graphene.Schema(query=Query)
Loading