Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
PleatherStarfish committed Jun 27, 2024
1 parent 00e9439 commit 4fcae94
Show file tree
Hide file tree
Showing 53 changed files with 14,316 additions and 51,245 deletions.
6 changes: 6 additions & 0 deletions backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
get_user_shopping_list_total_quantity,
archive_shopping_list,
get_user_anonymous_shopping_list_quantity,
get_all_unique_component_ids,
)


Expand Down Expand Up @@ -151,6 +152,11 @@
ArchivedShoppingListsView.as_view(),
name="delete-shopping-list",
),
path(
"shopping-list/unique-components/",
get_all_unique_component_ids,
name="unique-components",
),
path("components/", ComponentView.as_view(), name="component-list"),
path("components/<str:pks>/", get_components_by_ids, name="component-list-by-ids"),
path("module/<slug:slug>/", ModuleDetailView.as_view(), name="module-detail"),
Expand Down
3 changes: 0 additions & 3 deletions backend/inventory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ def get_components_locations(request):
along with the quantity of each component in each location.
"""
component_pks = request.GET.getlist("component_pks")
print("component_pks", component_pks)

if not component_pks:
# Return an empty JSON object if no component_pks provided
Expand Down Expand Up @@ -216,9 +215,7 @@ def get_components_locations(request):
}
for item in locations_and_quantities
]
print("TEST", locations_with_quantity)

components_locations[component_pk] = locations_with_quantity
print("TEST2", components_locations)

return Response(components_locations, status=status.HTTP_200_OK)
95 changes: 58 additions & 37 deletions backend/shopping_list/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,50 +616,62 @@ def add_all_user_shopping_list_to_inventory(request):
user = request.user
shopping_list = UserShoppingList.objects.filter(user=user)

# Extract location_list from the request; it's expected to be a dictionary
# Ensure location_list is a dictionary
if not isinstance(request.data, dict):
return Response(
{"detail": "Invalid data format; expected a dictionary of locations."},
status=status.HTTP_400_BAD_REQUEST,
)

location_list = request.data

with transaction.atomic():
for shopping_list_item in shopping_list:
# Retrieve the location for this specific shopping list item, if provided
item_location = location_list.get(shopping_list_item.id)
for shopping_list_item in shopping_list:
component_id = str(shopping_list_item.component.id)
shopping_list_item_location = location_list.get(component_id, None)

# Determine if a specific location is provided
specific_location_provided = item_location is not None
if shopping_list_item_location is not None:
user_inventory_items = UserInventory.objects.filter(
user=user,
component_id=shopping_list_item.component.id,
location__exact=shopping_list_item_location,
)
else:
user_inventory_items = UserInventory.objects.filter(
user=user,
component__id=shopping_list_item.component.id,
location__isnull=True,
)

# Find or create the inventory item based on location availability
if specific_location_provided:
item_location_list = item_location.split(",") if item_location else None
inventory_item = UserInventory.objects.filter(
user=user,
component=shopping_list_item.component,
location__in=item_location_list,
).first()
else:
inventory_item = UserInventory.objects.filter(
user=user,
component=shopping_list_item.component,
location__isnull=True,
).first()
# Get the total quantity of a component from user's shopping list
shopping_list_quantity = UserShoppingList.objects.filter(
user=user, component__id=shopping_list_item.component.id
)

if inventory_item:
# Update the existing inventory item
inventory_item.quantity += shopping_list_item.quantity
inventory_item.old_quantity = inventory_item.quantity
inventory_item.old_location = inventory_item.location
inventory_item.save()
else:
# Create a new inventory item with the specified location or null location
UserInventory.objects.create(
user=user,
component=shopping_list_item.component,
quantity=shopping_list_item.quantity,
location=item_location.split(",") if item_location else None,
)
quantity = sum(item.quantity for item in shopping_list_quantity)

shopping_list_item.delete()
# If the user has that item in their inventory
if user_inventory_items.exists():
inventory_item = user_inventory_items.first()
inventory_item.quantity += quantity
inventory_item.save()
else:
# Create a new inventory item with the specified location and quantity
UserInventory.objects.create(
user=user,
component_id=shopping_list_item.component.id,
quantity=quantity,
location=shopping_list_item_location,
)

return Response(status=status.HTTP_204_NO_CONTENT)
UserShoppingList.objects.filter(user=user).delete()

return Response(
{
"detail": "Inventory updated successfully.",
"processed_items": len(shopping_list),
},
status=status.HTTP_200_OK,
)


@api_view(["POST"])
Expand Down Expand Up @@ -715,3 +727,12 @@ def archive_shopping_list(request):
return Response(
{"message": "Shopping List saved successfully"}, status=status.HTTP_200_OK
)


@api_view(["GET"])
@permission_classes([IsAuthenticated])
def get_all_unique_component_ids(request):
user = request.user
shopping_list = UserShoppingList.objects.filter(user=user)
component_ids = shopping_list.values_list("component_id", flat=True).distinct()
return Response(component_ids, status=status.HTTP_200_OK)
2 changes: 1 addition & 1 deletion backend/static/css/styles.css

Large diffs are not rendered by default.

14,199 changes: 2 additions & 14,197 deletions backend/static/js/main.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion backend/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
href="{% static 'images/logo.png' %}"
/>
<link rel="stylesheet" href="{% static 'css/styles.css' %}" />

{% comment %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orestbida/[email protected]/dist/cookieconsent.css" />
{% endcomment %}

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300&family=Roboto:wght@400&display=swap" rel="stylesheet">
Expand Down Expand Up @@ -74,13 +78,18 @@

<!-- Project JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/cash/8.1.5/cash.min.js"></script>

{% comment %}
<script src="https://cdn.jsdelivr.net/gh/orestbida/[email protected]/dist/cookieconsent.js" defer></script>
{% endcomment %}

<script data-cookiecategory="analytics" type=\"text/javascript\" defer>\n(function(f,b){if(!b.__SV){var e,g,i,h;window.mixpanel=b;b._i=[];b.init=function(e,f,c){function g(a,d){var b=d.split(\".\");2==b.length&&(a=a[b[0]],d=b[1]);a[d]=function(){a.push([d].concat(Array.prototype.slice.call(arguments,0)))}}var a=b;\"undefined\"!==typeof c?a=b[c]=[]:c=\"mixpanel\";a.people=a.people||[];a.toString=function(a){var d=\"mixpanel\";\"mixpanel\"!==c&&(d+=\".\"+c);a||(d+=\" (stub)\");return d};a.people.toString=function(){return a.toString(1)+\".people (stub)\"};i=\"disable time_event track track_pageview track_links track_forms track_with_groups add_group set_group remove_group register register_once alias unregister identify name_tag set_config reset opt_in_tracking opt_out_tracking has_opted_in_tracking has_opted_out_tracking clear_opt_in_out_tracking start_batch_senders people.set people.set_once people.unset people.increment people.append people.union people.track_charge people.clear_charges people.delete_user people.remove\".split(\" \");\nfor(h=0;h<i.length;h++)g(a,i[h]);var j=\"set set_once union unset remove delete\".split(\" \");a.get_group=function(){function b(c){d[c]=function(){call2_args=arguments;call2=[c].concat(Array.prototype.slice.call(call2_args,0));a.push([e,call2])}}for(var d={},e=[\"get_group\"].concat(Array.prototype.slice.call(arguments,0)),c=0;c<j.length;c++)b(j[c]);return d};b._i.push([e,f,c])};b.__SV=1.2;e=f.createElement(\"script\");e.type=\"text/javascript\";e.async=!0;e.src=\"undefined\"!==typeof MIXPANEL_CUSTOM_LIB_URL?\nMIXPANEL_CUSTOM_LIB_URL:\"file:\"===f.location.protocol&&\"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js\".match(/^\\/\\//)?\"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js\":\"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js\";g=f.getElementsByTagName(\"script\")[0];g.parentNode.insertBefore(e,g)}})(document,window.mixpanel||[]);\n</script>

<script src="{% static 'js/base.js' %}"></script>
<script src="{% static 'js/cc.js' %}"></script>
<script src="{% static 'js/mobile_menu.js' %}"></script>


{% comment %}
<script data-cookiecategory="analytics" defer>
window.addEventListener("load", function () {
if (mixpanel) {
Expand All @@ -93,6 +102,7 @@
}
});
</script>
{% endcomment %}

<script>
window.addEventListener('load', function() {
Expand Down
Loading

0 comments on commit 4fcae94

Please sign in to comment.