diff options
author | luxagraf <sng@luxagraf.net> | 2014-05-26 12:14:35 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2014-05-26 12:14:35 -0400 |
commit | ddbffaf30e8cb960e7baa288c6ada2d948593f74 (patch) | |
tree | e026bba16e2112b93e06f22625bd65a0ff23c9c9 /app/stuff/temp.py | |
parent | 8aaad31d807066d7340c094968d435a724ab32c8 (diff) |
Added "stuff" to apps
Diffstat (limited to 'app/stuff/temp.py')
-rw-r--r-- | app/stuff/temp.py | 115 |
1 files changed, 115 insertions, 0 deletions
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) + |