コード例 #1
0
ファイル: views.py プロジェクト: shristigautam/diver
def upload_item(request):
    selected_category1 = 0
    selected_category2 = Item.CATEGORIES[0][1][0][0]
    specified_lower = None
    specified_upper = None
    search_result = []
    if request.method == 'POST':
        category = request.POST['category']
        for i in range(len(Item.CATEGORIES)):
            for c, n in Item.CATEGORIES[i][1]:
                if c == category:
                    selected_category1 = i
                    selected_category2 = c
                    break

        # default image
        filename = "no_image.gif"

        if 'file' in request.FILES:
            file = request.FILES['file']
            if file != '':

                filename = file._name
                filename.replace(" ", "_")

                image_dir = os.path.join(IMAGE_DIR, filename)
                print(image_dir)
                # for python3
                fp = open('%s' % (image_dir), 'wb')
                for chunk in file.chunks():
                    fp.write(chunk)

                fp.close()

        patterns = request.POST['patterns']
        materials = request.POST['materials']
        price = request.POST['price']
        comment = request.POST['comment']

        item = Item(pattern=patterns, material=materials, comment=comment,
                    category=category, price=price, images="http://localhost:8000/static/images/" + filename)

        size = Size()

        if (selected_category1 == 0):
            item.type = 0
            item.sleeve_level = request.POST['sleeve_level']
            item.neck = request.POST['neck_types']
            item.zipper = request.POST['zipper']
            item.button = request.POST['button_top']

            size.length_cm = request.POST['length_cm']
            size.shoulder_cm = request.POST['shoulder_cm']
            size.chest_cm = request.POST['chest_cm']
            size.sleeve_cm = request.POST['sleeve_cm']
            size.letter = request.POST['letter']

        elif (selected_category1 == 1):
            item.type = 1
            item.sleeve_level = request.POST['sleeve_level']
            item.button_outer = request.POST['button_outer']
            item.length_level = request.POST['length_level_outer']
            item.collar = request.POST['collar']
            item.hat = request.POST['hat']
            item.zipper = request.POST['zipper']
            item.padding = request.POST['padding']

            size.length_cm = request.POST['length_cm']
            size.shoulder_cm = request.POST['shoulder_cm']
            size.chest_cm = request.POST['chest_cm']
            size.sleeve_cm = request.POST['sleeve_cm']
            size.letter = request.POST['letter']

        elif (selected_category1 == 2):
            item.type = 2
            item.length_level = request.POST['length_level_bottom']
            item.fit = request.POST['fit_types']

            size.waist_cm = request.POST['waist_cm']
            size.thigh_cm = request.POST['thigh_cm']
            size.crotch_cm = request.POST['crotch_cm']

        item.save()

        # color1 = request.POST['color1']
        # color2 = request.POST['color2']
        # color3 = request.POST['color3']
        # color = Color(item = item, color_id1 = color1, color_id2 = color2,  color_id3 = color3)
        # color.save()

        size.item = item
        size.save()

    return render(request, 'upload_item.html',
                  {'categories': Item.CATEGORIES,
                   'materials': Item.MATERIALS,
                   'neck_types': Item.NECK_TYPES,
                   'patterns': Item.PATTERNS,
                   'fit_types': Item.FIT_TYPES,
                   'selected_category1': selected_category1,
                   'selected_category2': selected_category2,
                   'hanger': request.session.get('hanger', None)})