1
0
Fork 0

Deduplicate print function

This commit is contained in:
Dariusz Niemczyk 2023-07-17 20:14:06 +02:00
parent c15f1bb840
commit 30c3c3eb7a
No known key found for this signature in database
2 changed files with 16 additions and 18 deletions

View File

@ -12,6 +12,13 @@ from django.shortcuts import get_object_or_404
from storage.views import apply_smart_search
def api_print(quantity, obj):
amount = min(int(quantity), 5)
for _ in range(amount):
obj.print()
return Response({"status": "success"})
class SmartSearchFilterBackend(filters.BaseFilterBackend):
"""
Filters query using smartsearch filter
@ -35,11 +42,7 @@ class LabelViewSet(viewsets.ModelViewSet):
@action(detail=True, methods=["post"], permission_classes=[AllowAny])
def print(self, request, pk):
quantity = min(int(request.query_params.get("quantity", 1)), 5)
obj = self.get_object()
for _ in range(quantity):
obj.print()
return obj
return api_print(request.query_params.get("quantity", 1), self.get_object())
class ItemViewSet(viewsets.ModelViewSet):
@ -76,12 +79,7 @@ class ItemViewSet(viewsets.ModelViewSet):
@action(detail=True, methods=["post"], permission_classes=[AllowAny])
def print(self, request, pk):
# todo: deduplicate
quantity = min(int(request.query_params.get("quantity", 1)), 5)
obj = self.get_object()
for _ in range(quantity):
obj.print()
return obj
return api_print(request.query_params.get("quantity", 1), self.get_object())
@action(
detail=True,

View File

@ -23,6 +23,11 @@ STATES = (
)
def api_print(id):
resp = requests.post("{}/api/1/print/{}".format(settings.LABEL_API, id))
resp.raise_for_status()
class Category(models.Model):
name = models.CharField(max_length=127)
@ -103,11 +108,7 @@ class Item(models.Model, TreeModelMixin):
return next((c for c in self.categories.all() if c.icon_id), None)
def print(self):
# todo: deduplicate
resp = requests.post(
"{}/api/1/print/{}".format(settings.LABEL_API, self.short_id())
)
resp.raise_for_status()
api_print(self.short_id())
class Meta:
ordering = ("path",)
@ -135,5 +136,4 @@ class Label(models.Model):
return "{}".format(self.id)
def print(self):
resp = requests.post("{}/api/1/print/{}".format(settings.LABEL_API, self.id))
resp.raise_for_status()
api_print(self.id)