From c4c21e0ec1d6ce72186b1492debca3b75ec2aa0c Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Sun, 28 May 2017 22:00:54 +0200 Subject: [PATCH] Initial label printing integration --- requirements.txt | 4 +++ spejstore/settings.py | 2 ++ storage/migrations/0004_auto_20170528_1945.py | 31 +++++++++++++++++ storage/models.py | 33 ++++++++++++++++--- storage/serializers.py | 2 +- 5 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 storage/migrations/0004_auto_20170528_1945.py diff --git a/requirements.txt b/requirements.txt index dcc4a5c..0eaab6e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ +certifi==2017.4.17 +chardet==3.0.3 Django==1.10.1 git+https://github.com/djangonauts/django-hstore@61427e474cb2f4be8fdfce225d78a5330bc77eb0#egg=django-hstore git+https://github.com/informatic/django-tree@993cec827ed989d3c162698a739da95b9227604b#egg=django-tree @@ -9,3 +11,5 @@ Pillow==3.3.1 psycopg2==2.6.2 djangorestframework-hstore==1.3 pyldap==2.4.28 +requests==2.16.5 +urllib3==1.21.1 diff --git a/spejstore/settings.py b/spejstore/settings.py index 7aa620f..0f1f2c8 100644 --- a/spejstore/settings.py +++ b/spejstore/settings.py @@ -182,3 +182,5 @@ REST_FRAMEWORK = { 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' ] } + +LABEL_API = 'http://label.waw.hackerspace.pl:5678' diff --git a/storage/migrations/0004_auto_20170528_1945.py b/storage/migrations/0004_auto_20170528_1945.py new file mode 100644 index 0000000..4601db0 --- /dev/null +++ b/storage/migrations/0004_auto_20170528_1945.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.1 on 2017-05-28 19:45 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('storage', '0003_auto_20170424_2002'), + ] + + operations = [ + migrations.RemoveField( + model_name='label', + name='revision', + ), + migrations.AddField( + model_name='label', + name='created', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + migrations.AddField( + model_name='label', + name='style', + field=models.CharField(choices=[('basic_99012_v1', 'Basic Dymo 89x36mm label')], default='basic_99012_v1', max_length=32), + ), + ] diff --git a/storage/models.py b/storage/models.py index 5bf5ce0..96fd150 100644 --- a/storage/models.py +++ b/storage/models.py @@ -1,13 +1,17 @@ from __future__ import unicode_literals +import uuid +import re + from django.db import models +from django.conf import settings from django.contrib.auth.models import User from django_hstore import hstore -import uuid - from tree.fields import PathField from tree.models import TreeModelMixin +import requests + STATES = ( ('present', 'Present'), @@ -17,7 +21,6 @@ STATES = ( ) -# Create your models here. class Category(models.Model): name = models.CharField(max_length=127) @@ -53,6 +56,17 @@ class Item(models.Model, TreeModelMixin): from django.urls import reverse return reverse('item-display', kwargs={'pk': str(self.pk)}) + def get_or_create_label(self, **kwargs): + defaults = { + 'id': re.sub('[^A-Z0-9]', '', self.name.upper())[:16], + } + + defaults.update(kwargs) + + obj, created = self.labels.get_or_create(**kwargs, defaults=defaults) + + return obj + class Meta: ordering = ('path',) @@ -65,4 +79,15 @@ class ItemImage(models.Model): class Label(models.Model): id = models.CharField(max_length=64, primary_key=True) item = models.ForeignKey(Item, related_name='labels') - revision = models.IntegerField() + style = models.CharField(max_length=32, choices=( + ('basic_99012_v1', 'Basic Dymo 89x36mm label'), + ), default='basic_99012_v1') + created = models.DateTimeField(auto_now_add=True, blank=True) + + def __str__(self): + return self.id + + def print(self): + resp = requests.post( + '{}/api/1/print/{}'.format(settings.LABEL_API, self.id)) + resp.raise_for_status() diff --git a/storage/serializers.py b/storage/serializers.py index fcd27cc..859b95a 100644 --- a/storage/serializers.py +++ b/storage/serializers.py @@ -12,4 +12,4 @@ class LabelSerializer(serializers.ModelSerializer): item = ItemSerializer() class Meta: model = Label - fields = ('id', 'item', 'revision') + fields = ('id', 'item', 'style')