summaryrefslogtreecommitdiff
path: root/bak/unused_apps/stuff/temp.py
blob: 2150563e1c4f91e902ab7c2f143c6206551c4446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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)