Exemplo n.º 1
0
def upload_categ_BG(id):
    if (current_user.role_id != 2):
        return json.dumps({'success': False})

    categ = Categ.query.get_or_404(id)

    if 'file' not in request.files:
        return json.dumps({'success1': False})

    file = request.files['file']

    img = Image(hex_id=id)
    img.ext = file.mimetype.replace('image/', '')

    if (not img.ext in ['png', 'jpg', 'jpeg', 'gif']):
        return json.dumps({'success2': False})

    db.session.add(img)
    db.session.commit()

    try:
        file.save(os.getcwd() + f"/app/static/uploadedImgs/{img.id}.{img.ext}")
    except BaseException:
        db.session.delete(img)
        db.session.commit()
        return json.dumps({'success3': False})

    categ.BG_img = f'{img.id}.{img.ext}'
    db.session.add(categ)
    db.session.commit()

    return json.dumps({'success': True})
Exemplo n.º 2
0
def upload_hex_img(id):
    hex = Hexagon.query.get(id)
    if hex.user_id != current_user.id:
        return json.dumps({'success': False})

    if 'file' not in request.files:
        return json.dumps({'success': False})

    file = request.files['file']

    img = Image(hex_id=id)
    img.ext = file.mimetype.replace('image/', '')

    if (not img.ext in ['png', 'jpg', 'jpeg', 'gif'
                        ]):  # TODO: создать таблицу с расширениями файлов
        return json.dumps({'success': False})
    db.session.add(img)
    db.session.commit()

    categ_name = hex.categ.name

    create_dir("/app/static/uploadedImgs/" + categ_name)
    create_dir(f"/app/static/uploadedImgs/{categ_name}/{hex.chain.id}")
    create_dir(
        f"/app/static/uploadedImgs/{categ_name}/{hex.chain.id}/{hex.id}")

    filename = f"static/uploadedImgs/{categ_name}/{hex.chain.id}/{hex.id}/{img.id}.{img.ext}"
    if 'BG' in file.filename:
        hex.BG_img = filename
        for img in hex.imgs:
            img.is_BG = False
            db.session.add(img)

        img.is_BG = True
        db.session.add_all([hex, img])
        db.session.commit()

    try:
        file.save(os.getcwd() + "/app/" + filename)
    except BaseException:
        db.session.delete(img)
        db.session.commit()
        return json.dumps({'success': False})

    subs = current_user.subscriptions_to_me
    for sub in subs:
        create_notification(
            sub.subscriber_id,
            _('User %(username)s changed his/her hexagon in category %(categname)s',
              categname=hex.categ.name,
              username=current_user.username), 'change',
            url_for('get_hex', id=hex.id))

    return json.dumps({
        'success': True,
        'url': filename,
        'uuid': img.id,
        "isBG": img.is_BG
    })