diff options
-rw-r--r-- | app/stuff/__init__.py | 0 | ||||
-rw-r--r-- | app/stuff/admin.py | 19 | ||||
-rw-r--r-- | app/stuff/models.py | 43 | ||||
-rw-r--r-- | app/stuff/temp.py | 115 |
4 files changed, 177 insertions, 0 deletions
diff --git a/app/stuff/__init__.py b/app/stuff/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/stuff/__init__.py diff --git a/app/stuff/admin.py b/app/stuff/admin.py new file mode 100644 index 0000000..638b40a --- /dev/null +++ b/app/stuff/admin.py @@ -0,0 +1,19 @@ +from django.contrib import admin +from stuff.models import Item + + +class ItemAdmin(admin.ModelAdmin): + list_display = ( + 'title', + 'image_url', + 'admin_thumbnail', + 'creator', + 'item_type', + 'current_price', + 'item_format', + 'isbn' + ) + list_filter = ['item_format', 'item_type'] + + +admin.site.register(Item, ItemAdmin) diff --git a/app/stuff/models.py b/app/stuff/models.py new file mode 100644 index 0000000..ee065a2 --- /dev/null +++ b/app/stuff/models.py @@ -0,0 +1,43 @@ +from django.db import models +from django.utils.encoding import force_text +from django.conf import settings + + +class Item(models.Model): + isbn = models.CharField(max_length=100, blank=True, null=True) + serial_number = models.CharField(max_length=100, blank=True, null=True) + ITEM_TYPES = ( + (0, 'Book'), + (1, 'Apparel'), + (2, 'Gadget'), + (3, 'Furniture'), + ) + item_type = models.IntegerField(choices=ITEM_TYPES, default=0) + title = models.CharField(max_length=200) + creator = models.CharField(max_length=200, blank=True, null=True) + creator_sort_last_name = models.CharField(max_length=200, blank=True, null=True) + creator_sort_first_name = models.CharField(max_length=200, blank=True, null=True) + current_price = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True) + retail_price = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True) + purchase_price = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True) + amazon_url = models.CharField(max_length=400, blank=True, null=True) + image_url = models.CharField(max_length=400, blank=True, null=True) + image = models.FileField(upload_to='stuff/', null=True, blank=True) + item_format = models.CharField(max_length=255, blank=True, null=True) + date_released = models.DateTimeField(blank=True, null=True) + date_purchased = models.DateTimeField(blank=True, null=True) + date_added = models.DateTimeField(blank=True, null=True) + + class Meta: + ordering = ['-date_added'] + + def __str__(self): + return self.title + + def get_image_url(self): + return '%s%s' % (settings.MEDIA_URL, self.image) + + def admin_thumbnail(self): + return force_text('<a href=""><img src="%s" width="100" style="width:100px"></a>' % (self.get_image_url())) + admin_thumbnail.allow_tags = True + admin_thumbnail.short_description = 'Thumbnail' diff --git a/app/stuff/temp.py b/app/stuff/temp.py new file mode 100644 index 0000000..2150563 --- /dev/null +++ b/app/stuff/temp.py @@ -0,0 +1,115 @@ +from stuff.models import Item +from django.conf import settings +from datetime import datetime +from decimal import Decimal +import csv +item_types = { 'Book':0, 'Apparel': 1,'Gadget': 2, 'Furniture': 3} +""" +csv_file = csv.DictReader(open('stuff.csv', 'rb'), delimiter=',') +for line in csv_file: + if line['release date'][:-6] != '': + date_released = datetime.strptime(line['release date'][:-6], "%Y-%m-%d %H:%M:%S") + else: + date_released = None + if line['purchase date'][:-6] != '': + date_purchased = datetime.strptime(line['purchase date'][:-6], "%Y-%m-%d %H:%M:%S") + else: + date_purchased = None + if line['creation date'][:-6] != '': + date_added = datetime.strptime(line['creation date'][:-6], "%Y-%m-%d %H:%M:%S") + else: + date_added = None + item_type = int(item_types[line['item type']]) + if line['current value'][1:] != '': + try: + current_price = Decimal(line['current value'][1:].replace(",", "")) + except: + current_price = None + else: + current_price = None + if line['retail price'][1:] != '': + try: + retail_price = Decimal(line['retail price'][1:].replace(",", "")) + except: + retail_price =None + else: + retail_price =None + if line['purchase price'][1:] != '' and line['purchase price'] != "Gift": + try: + purchase_price = Decimal(line['purchase price'][1:].replace(",", "").replace('\\xa0', ' ')) + except: + purchase_price =None + else: + purchase_price =None + i, created = Item.objects.get_or_create( + isbn = line['ISBN'], + serial_number = line['serial number'], + type = item_type, + title = line['title'], + creator = line['creator'], + current_price = current_price, + retail_price = retail_price, + purchase_price = purchase_price, + amazon_url = line['amazon link'], + image_url = line['coverImageLargeURLString'], + format = line['format'], + date_released = date_released, + date_purchased = date_purchased, + date_added = date_added + ) + print created, i.title +""" +import datetime +import os +import io +import urllib + + +from django.contrib.contenttypes.models import ContentType +from django.template.defaultfilters import slugify +from django.core.exceptions import ObjectDoesNotExist +from django.conf import settings + +# Required PIL classes may or may not be available from the root namespace +# depending on the installation +try: + import Image + import ImageFile + import ImageFilter + import ImageEnhance +except ImportError: + try: + from PIL import Image + from PIL import ImageFile + from PIL import ImageFilter + from PIL import ImageEnhance + except ImportError: + raise ImportError("Could not import the Python Imaging Library.") + +ImageFile.MAXBLOCK = 1000000 + + +def grab_item_image(item): + crop_dir = settings.IMAGES_ROOT + '/stuff/' + if not os.path.isdir(crop_dir): + os.makedirs(crop_dir) + remote = item.image_url + try: + fname = urllib.request.urlopen(remote) + except: + return False + im = io.BytesIO(fname.read()) + img = Image.open(im) + ext = slugify(item.title)[:40] + filename = '%s/%s.jpg' %(crop_dir, ext) + try: + if img.format == 'JPEG': + img.save(filename, 'JPEG', quality=95, optimize=True) + else: + img.save(filename) + except IOError: + if os.path.isfile(filename): + os.unlink(filename) + pass + return 'images/stuff/%s.jpg'%(ext) + |