You can now print Items, w/o explicitly defining Labels

master
radex 2020-05-20 23:23:23 +02:00 committed by Piotr Dobrowolski
parent 2e0c031fab
commit 5160052dfd
3 changed files with 26 additions and 16 deletions

View File

@ -72,6 +72,15 @@ class ItemViewSet(viewsets.ModelViewSet):
except Label.DoesNotExist:
raise Http404()
@detail_route(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
@detail_route()
def children(self, request, pk):
item = self.get_object()

View File

@ -89,6 +89,12 @@ class Item(models.Model, TreeModelMixin):
def primary_category(self):
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()
class Meta:
ordering = ('path',)

View File

@ -18,21 +18,16 @@
<div class="col-md-4">
<iframe name="printframe" style="display: none"></iframe>
{% for label in labels %}
<div class="label-item">
<form action="/api/1/labels/{{ label.id }}/print/?quantity=2" method="POST" target="printframe" onsubmit="return confirm('Want to print 2 labels?')" style="display:inline-block">
{% csrf_token %}
<button class="btn btn-default btn-lg btn-primary"><i class="glyphicon glyphicon-print"></i>&nbsp;&nbsp;PRINT&nbsp;x&nbsp;2</button>
</form>
<form action="/api/1/labels/{{ label.id }}/print/" method="POST" target="printframe" onsubmit="return confirm('Want to print this labels?')" style="display:inline-block">
{% csrf_token %}
<button class="btn btn-default btn-lg"><i class="glyphicon glyphicon-print"></i></button>
{% if not has_one_label %}
<code>{{ label.id }}</code>
{% endif %}
</form>
</div>
{% endfor %}
<div class="label-item">
<form action="/api/1/items/{{ item.short_id }}/print/?quantity=2" method="POST" target="printframe" onsubmit="return confirm('Want to print 2 labels?')" style="display:inline-block">
{% csrf_token %}
<button class="btn btn-default btn-lg btn-primary"><i class="glyphicon glyphicon-print"></i>&nbsp;&nbsp;PRINT&nbsp;x&nbsp;2</button>
</form>
<form action="/api/1/items/{{ item.short_id }}/print/" method="POST" target="printframe" onsubmit="return confirm('Want to print this labels?')" style="display:inline-block">
{% csrf_token %}
<button class="btn btn-default btn-lg"><i class="glyphicon glyphicon-print"></i></button>
</form>
</div>
<table class="table table-hover table-striped">
{% if item.owner %}
@ -75,7 +70,7 @@
{% endfor %}
{% if labels %}
<tr><td>a.k.a.</td><td>
<tr><td>legacy labels</td><td>
{% for label in labels %}
<code>{{ label.id }}</code>,
{% endfor %}