Deduplicate print function
This commit is contained in:
parent
c15f1bb840
commit
30c3c3eb7a
|
@ -12,6 +12,13 @@ from django.shortcuts import get_object_or_404
|
||||||
from storage.views import apply_smart_search
|
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):
|
class SmartSearchFilterBackend(filters.BaseFilterBackend):
|
||||||
"""
|
"""
|
||||||
Filters query using smartsearch filter
|
Filters query using smartsearch filter
|
||||||
|
@ -35,11 +42,7 @@ class LabelViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
@action(detail=True, methods=["post"], permission_classes=[AllowAny])
|
@action(detail=True, methods=["post"], permission_classes=[AllowAny])
|
||||||
def print(self, request, pk):
|
def print(self, request, pk):
|
||||||
quantity = min(int(request.query_params.get("quantity", 1)), 5)
|
return api_print(request.query_params.get("quantity", 1), self.get_object())
|
||||||
obj = self.get_object()
|
|
||||||
for _ in range(quantity):
|
|
||||||
obj.print()
|
|
||||||
return obj
|
|
||||||
|
|
||||||
|
|
||||||
class ItemViewSet(viewsets.ModelViewSet):
|
class ItemViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -76,12 +79,7 @@ class ItemViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
@action(detail=True, methods=["post"], permission_classes=[AllowAny])
|
@action(detail=True, methods=["post"], permission_classes=[AllowAny])
|
||||||
def print(self, request, pk):
|
def print(self, request, pk):
|
||||||
# todo: deduplicate
|
return api_print(request.query_params.get("quantity", 1), self.get_object())
|
||||||
quantity = min(int(request.query_params.get("quantity", 1)), 5)
|
|
||||||
obj = self.get_object()
|
|
||||||
for _ in range(quantity):
|
|
||||||
obj.print()
|
|
||||||
return obj
|
|
||||||
|
|
||||||
@action(
|
@action(
|
||||||
detail=True,
|
detail=True,
|
||||||
|
|
|
@ -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):
|
class Category(models.Model):
|
||||||
name = models.CharField(max_length=127)
|
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)
|
return next((c for c in self.categories.all() if c.icon_id), None)
|
||||||
|
|
||||||
def print(self):
|
def print(self):
|
||||||
# todo: deduplicate
|
api_print(self.short_id())
|
||||||
resp = requests.post(
|
|
||||||
"{}/api/1/print/{}".format(settings.LABEL_API, self.short_id())
|
|
||||||
)
|
|
||||||
resp.raise_for_status()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ("path",)
|
ordering = ("path",)
|
||||||
|
@ -135,5 +136,4 @@ class Label(models.Model):
|
||||||
return "{}".format(self.id)
|
return "{}".format(self.id)
|
||||||
|
|
||||||
def print(self):
|
def print(self):
|
||||||
resp = requests.post("{}/api/1/print/{}".format(settings.LABEL_API, self.id))
|
api_print(self.id)
|
||||||
resp.raise_for_status()
|
|
||||||
|
|
Loading…
Reference in New Issue