Skip to content

Commit

Permalink
basic shopping view in MealPlanEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Dec 28, 2024
1 parent 4312b5a commit bf4fc9a
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 84 deletions.
23 changes: 19 additions & 4 deletions cookbook/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import redis
import requests
from PIL import UnidentifiedImageError
from PIL.ImImagePlugin import number
from PIL.features import check
from django.contrib import messages
from django.contrib.auth.models import Group, User
Expand Down Expand Up @@ -1361,6 +1362,9 @@ def get_queryset(self):
return self.queryset.filter(space=self.request.space)


@extend_schema_view(list=extend_schema(parameters=[
OpenApiParameter(name='mealplan', description=_('Returns only entries associated with the given mealplan id'), type=int)
]))
class ShoppingListRecipeViewSet(LoggingMixin, viewsets.ModelViewSet):
queryset = ShoppingListRecipe.objects
serializer_class = ShoppingListRecipeSerializer
Expand All @@ -1369,6 +1373,14 @@ class ShoppingListRecipeViewSet(LoggingMixin, viewsets.ModelViewSet):

def get_queryset(self):
self.queryset = self.queryset.filter(Q(entries__space=self.request.space) | Q(recipe__space=self.request.space))

# TODO implement test for this
if not self.detail:
mealplan = self.request.query_params.get('mealplan', None)

if mealplan is not None:
self.queryset = self.queryset.filter(mealplan_id=mealplan)

return self.queryset.filter(Q(entries__isnull=True)
| Q(entries__created_by=self.request.user)
| Q(entries__created_by__in=list(self.request.user.get_shopping_share()))).distinct().all()
Expand Down Expand Up @@ -1405,6 +1417,7 @@ def bulk_create_entries(self, request, pk):
OpenApiParameter(name='updated_after',
description=_('Returns only elements updated after the given timestamp in ISO 8601 format.'),
type=datetime.datetime),
OpenApiParameter(name='mealplan', description=_('Returns only entries associated with the given mealplan id'), type=int)
]))
class ShoppingListEntryViewSet(LoggingMixin, viewsets.ModelViewSet):
"""
Expand Down Expand Up @@ -1435,17 +1448,20 @@ def get_queryset(self):
).distinct().all()

updated_after = self.request.query_params.get('updated_after', None)
mealplan = self.request.query_params.get('mealplan', None)

if not self.detail:
# to keep the endpoint small, only return entries as old as user preference recent days
today_start = timezone.now().replace(hour=0, minute=0, second=0)
week_ago = today_start - datetime.timedelta(days=min(self.request.user.userpreference.shopping_recent_days, 14))
self.queryset = self.queryset.filter((Q(checked=False) | Q(completed_at__gte=week_ago)))

if mealplan is not None:
self.queryset = self.queryset.filter(list_recipe__mealplan_id=mealplan)

try:
if updated_after:
updated_after = parse_datetime(updated_after)
print('adding filter updated_after', updated_after)
self.queryset = self.queryset.filter(updated_at__gte=updated_after)
except Exception:
traceback.print_exc()
Expand Down Expand Up @@ -1588,9 +1604,8 @@ def get_queryset(self):
return self.queryset.filter(space=self.request.space).all()


# TODO explain what internal_note is for
@extend_schema_view(list=extend_schema(parameters=[
OpenApiParameter(name='internal_note', description=_('I have no idea what internal_note is for.'), type=str)
OpenApiParameter(name='internal_note', description=_('Text field to store data that gets carried over to the UserSpace created from the InviteLink'), type=str)
]))
class InviteLinkViewSet(LoggingMixin, StandardFilterModelViewSet):
queryset = InviteLink.objects
Expand Down Expand Up @@ -1806,7 +1821,7 @@ def post(self, request, *args, **kwargs):
"""
serializer = ImportImageSerializer(data=request.data, partial=True)
if serializer.is_valid():
#generativeai.configure(api_key=GOOGLE_AI_API_KEY)
# generativeai.configure(api_key=GOOGLE_AI_API_KEY)

# model = generativeai.GenerativeModel('gemini-1.5-flash-latest')
# img = PIL.Image.open('')
Expand Down
36 changes: 23 additions & 13 deletions vue3/src/components/display/ShoppingLineItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
<!-- </div>-->



<div class="flex-grow-1 p-2">
<div class="d-flex">
<div class="d-flex flex-column pr-2">
<span v-for="[i, a] in amounts" v-bind:key="a.key">
<span>
<i class="fas fa-check text-success fa-fw" v-if="a.checked"></i>
<i class="fas fa-clock-rotate-left text-info fa-fw" v-if="a.delayed"></i> <b>
<i class="fas fa-clock-rotate-left text-info fa-fw" v-if="a.delayed"></i> <b>
<span :class="{'text-disabled': a.checked || a.delayed}">
{{ $n(a.amount) }}
<span v-if="a.unit">{{ a.unit.name }}</span>
Expand All @@ -25,7 +24,7 @@
<br/>
</span>
</div>
<div class="d-flex flex-column flex-grow-1 align-self-center" >
<div class="d-flex flex-column flex-grow-1 align-self-center">
{{ shoppingListFood.food.name }} <br/>
<span v-if="infoRow"><small class="text-disabled">{{ infoRow }}</small></span>
</div>
Expand Down Expand Up @@ -62,16 +61,20 @@ import {isDelayed, isShoppingListFoodDelayed} from "@/utils/logic_utils";
const emit = defineEmits(['clicked'])
const props = defineProps({
entries: {type: Array as PropType<Array<ShoppingListEntry>>, required: true},
shoppingListFood: {type: {} as PropType<IShoppingListFood>, required: true},
hideInfoRow: {type: Boolean, default: false}
})
const entries = computed(() => {
return Array.from(props.shoppingListFood.entries.values())
})
/**
* ID of outer container, used by swipe system
*/
const itemContainerId = computed(() => {
let id = 'id_sli_'
for (let i in props.entries) {
for (let i in entries.value) {
id += i + '_'
}
return id
Expand All @@ -82,8 +85,8 @@ const itemContainerId = computed(() => {
* tests if all entries of the given food are checked
*/
const isChecked = computed(() => {
for (let i in props.entries) {
if (!props.entries[i].checked) {
for (let i in entries.value) {
if (!entries.value[i].checked) {
return false
}
}
Expand All @@ -101,7 +104,7 @@ const isShoppingLineDelayed = computed(() => {
* style action button depending on if all items are checked or not
*/
const actionButtonIcon = computed(() => {
if (isChecked.value){
if (isChecked.value) {
return 'fa-solid fa-plus'
}
return 'fa-solid fa-check'
Expand All @@ -116,8 +119,8 @@ const actionButtonIcon = computed(() => {
const amounts = computed((): Map<number, ShoppingLineAmount> => {
let unitAmounts = new Map<number, ShoppingLineAmount>()
for (let i in props.entries) {
let e = props.entries[i]
for (let i in entries.value) {
let e = entries.value[i]
if (!e.checked && !isDelayed(e)
|| (e.checked && useUserPreferenceStore().deviceSettings.shopping_show_checked_entries)
Expand Down Expand Up @@ -147,15 +150,22 @@ const amounts = computed((): Map<number, ShoppingLineAmount> => {
return unitAmounts
})
/**
* compute the second (info) row of the line item based on the entries and the device settings
*/
const infoRow = computed(() => {
if(props.hideInfoRow){
return ''
}
let info_row = []
let authors = []
let recipes = []
let meal_pans = []
for (let i in props.entries) {
let e = props.entries[i]
for (let i in entries.value) {
let e = entries.value[i]
if (authors.indexOf(e.createdBy.displayName) === -1) {
authors.push(e.createdBy.displayName)
Expand Down Expand Up @@ -203,7 +213,7 @@ function setFoodIgnoredAndChecked(food: Food) {
useMessageStore().addError(ErrorMessageType.UPDATE_ERROR, err)
})
useShoppingStore().setEntriesCheckedState(props.entries, true, false)
useShoppingStore().setEntriesCheckedState(entries.value, true, false)
}
/**
Expand Down
2 changes: 1 addition & 1 deletion vue3/src/components/display/ShoppingListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
<v-divider></v-divider>

<template v-for="[i, value] in category.foods" :key="value.food.id">
<shopping-line-item :shopping-list-food="value" :entries="Array.from(value.entries.values())"
<shopping-line-item :shopping-list-food="value"
@clicked="() => {shoppingLineItemDialog = true; shoppingLineItemDialogFood = value;}"></shopping-line-item>
</template>

Expand Down
Loading

0 comments on commit bf4fc9a

Please sign in to comment.