Beispiel #1
0
    def test_delete_item__success(self):
        user = self.create_user(superuser=True)
        item1 = self.create_item()

        resp = self.open_with_auth(
            'items/{}'.format(item1.uuid), 'delete', user.email, 'p4ssw0rd', data='')
        assert resp.status_code == NO_CONTENT
        assert Item.count() == 0
        resp = self.open('item/{}'.format(item1.uuid), 'get', data='')
        assert resp.status_code == NOT_FOUND
Beispiel #2
0
    def test_create_item__failure_field_wrong_type(self):
        user = self.create_user(superuser=True)

        new_item_data = {
            'name': 'Item one',
            'price': 'Ten',
            'description': 'Description one',
            'category': 'Category one'
        }
        resp = self.open_with_auth(
            '/items/', 'post', user.email, 'p4ssw0rd', data=new_item_data)
        assert resp.status_code == BAD_REQUEST
        assert Item.count() == 0
Beispiel #3
0
    def test_create_item__failure_empty_field_only_spaces(self):
        user = self.create_user(superuser=True)

        new_item_data = {
            'name': '    ',
            'price': 5,
            'description': 'desc1',
            'category': 'varie',
            'availability': 11
        }
        resp = self.open_with_auth(
            '/items/', 'post', user.email, 'p4ssw0rd', data=new_item_data)
        assert resp.status_code == BAD_REQUEST
        assert Item.count() == 0
Beispiel #4
0
def item_edit(id=None):
    if id:
        item = Item.query.get_or_404(id)
        related = ', '.join(i.id for i in item.related)
        if not related == '':
            related += ', '

    else:
        item = Item()
        # The following attributes are needed to show this dummy-item
        item.count = 1
        item.name = ''
        related = ''

    itemlist = Item.query.all()
    itemlist = ' '.join(i.id for i in itemlist)

    # Require form
    if request.method == 'GET':
        return pjax('create_item.html', item=item, itemlist=itemlist, related=related)
Beispiel #5
0
def item_edit(id=None):
    if id:
        item = Item.query.get_or_404(id)
        related = ', '.join(i.id for i in item.related)
        if not related == '':
            related += ', '

    else:
        item = Item()
        # The following attributes are needed to show this dummy-item
        item.count = 1
        item.name = ''
        related = ''

    itemlist = Item.query.all()
    itemlist = ' '.join(i.id for i in itemlist)

    # Require form
    if request.method == 'GET':
        return pjax('create_item.html',
                    item=item,
                    itemlist=itemlist,
                    related=related)
Beispiel #6
0
def item_post(id=None):
    db.session.rollback() # See comment in create_transaction()
    if id:
        itm = Item.query.get_or_404(id)
    else:
        id = make_url_safe(request.form.get('name'))
        itm = Item(id=id)
        db.session.add(itm)
        
    itm.name = request.form.get('name')
    itm.description = request.form.get('description')
    itm.count = int(request.form.get('count')) if request.form.get('count') else 1

    itm.tax_base_int = request_or_none('tax_base_int')
    itm.tax_base_edu = request_or_none('tax_base_edu')
    itm.tax_base_ext = request_or_none('tax_base_ext')
    itm.tax_int = request_or_none('tax_int')
    itm.tax_edu = request_or_none('tax_edu')
    itm.tax_ext = request_or_none('tax_ext')


    itm.related = []
    for iid in request.form.get('related').split(', '):
        if iid == '':
            continue
        i = Item.query.get(iid)
        if i is None:
            flash(u'Artikel "%s" ist nicht bekannt!' % ii )
            continue
        itm.related.append(i)

    itm.tax_period = request.form.get('tax_period')

    itm.price_buy = request_or_none('price_buy')

    itm.category = request.form.get('category')

    db.session.commit()

    # Update image if necessary
    file = request.files['image']
    if file:
        import os
        from PIL import Image as i
        filename = secure_filename(id).lower() + '.jpg'

        image = i.open(file)
        if image.mode != "RGB":
            image = image.convert("RGB")

        image.save(os.path.join(app.config['UPLOAD_FOLDER'], 'full', filename), "jpeg")

        w  = image.size[0]
        h = image.size[1]

        aspect = w / float(h)
        ideal_aspect = 1.0

        if aspect > ideal_aspect:  # Then crop the left and right edges:
            w_ = int(ideal_aspect * h)
            offset = (w - w_)/2
            resize = (offset, 0, w - offset, h)
        else:  # ... crop the top and bottom:
            h_ = int(w/ideal_aspect)
            offset = (h - h_)/2
            resize = (0, offset, w, h - offset)

        image = image.crop(resize).resize((140, 140), i.ANTIALIAS)
        image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename), "jpeg")

    return redirect( url_for('item', id=id) )
Beispiel #7
0
def item_post(id=None):
    db.session.rollback()  # See comment in create_transaction()
    if id:
        itm = Item.query.get_or_404(id)
    else:
        id = make_url_safe(request.form.get('name'))
        itm = Item(id=id)
        db.session.add(itm)

    itm.name = request.form.get('name')
    itm.description = request.form.get('description')
    itm.count = int(
        request.form.get('count')) if request.form.get('count') else 1

    itm.tax_base_int = request_or_none('tax_base_int')
    itm.tax_base_edu = request_or_none('tax_base_edu')
    itm.tax_base_ext = request_or_none('tax_base_ext')
    itm.tax_int = request_or_none('tax_int')
    itm.tax_edu = request_or_none('tax_edu')
    itm.tax_ext = request_or_none('tax_ext')

    itm.related = []
    for iid in request.form.get('related').split(', '):
        if iid == '':
            continue
        i = Item.query.get(iid)
        if i is None:
            flash(u'Artikel "%s" ist nicht bekannt!' % ii)
            continue
        itm.related.append(i)

    itm.tax_period = request.form.get('tax_period')

    itm.price_buy = request_or_none('price_buy')

    itm.category = request.form.get('category')

    db.session.commit()

    # Update image if necessary
    file = request.files['image']
    if file:
        import os
        from PIL import Image as i
        filename = secure_filename(id).lower() + '.jpg'

        image = i.open(file)
        if image.mode != "RGB":
            image = image.convert("RGB")

        image.save(os.path.join(app.config['UPLOAD_FOLDER'], 'full', filename),
                   "jpeg")

        w = image.size[0]
        h = image.size[1]

        aspect = w / float(h)
        ideal_aspect = 1.0

        if aspect > ideal_aspect:  # Then crop the left and right edges:
            w_ = int(ideal_aspect * h)
            offset = (w - w_) / 2
            resize = (offset, 0, w - offset, h)
        else:  # ... crop the top and bottom:
            h_ = int(w / ideal_aspect)
            offset = (h - h_) / 2
            resize = (0, offset, w, h - offset)

        image = image.crop(resize).resize((140, 140), i.ANTIALIAS)
        image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename), "jpeg")

    return redirect(url_for('item', id=id))