Skip to content

Commit

Permalink
new support for image import
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Nov 16, 2015
1 parent b894ea9 commit e01c8ee
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/budy/models/media.py
Expand Up @@ -39,11 +39,13 @@

import os
import zipfile
import tempfile
import mimetypes

import appier

from . import base
from . import product

BASE_URL = "http://localhost:8080/"

Expand Down Expand Up @@ -100,7 +102,7 @@ def import_media_s(cls, file, strict = False):
)
buffer = appier.legacy.BytesIO(data)
file = zipfile.ZipFile(buffer, "r")
target = "c:/tobias"
target = tempfile.mkdtemp()
try: file.extractall(target)
finally: file.close()

Expand All @@ -115,19 +117,18 @@ def import_media_s(cls, file, strict = False):

content_type, _encoding = mimetypes.guess_type(image_name, strict = False)

product_id_s = base
product_id = base
label = "undefined"
order = 0
size = "large"

base_s = base.split("_")
if len(base_s) >= 1: product_id_s = base_s[0]
if len(base_s) >= 1: product_id = base_s[0]
if len(base_s) >= 2: order = int(base_s[1])
if len(base_s) >= 3: label = base_s[2]
if len(base_s) >= 4: size = base_s[3]

product_id = int(product_id_s)
description = "%s_%s_%s" % (product_id_s, label, size)
description = "%s_%s_%s" % (product_id, label, size)

image_path = os.path.join(path, image_name)
image_file = open(image_path, "rb")
Expand All @@ -139,10 +140,16 @@ def import_media_s(cls, file, strict = False):
label = label,
order = order,
size = size,
file = appier.File((product_id_s, content_type, image_data))
file = appier.File((product_id, content_type, image_data))
)
media.save()

_product = product.Product.get(product_id = product_id, raise_e = False)
if not _product: continue

_product.images.append(media)
_product.save()

@classmethod
def _build(cls, model, map):
id = model.get("id", None)
Expand Down

0 comments on commit e01c8ee

Please sign in to comment.