Esempio n. 1
0
def catalog_routes(index):
    catalog = get_catalog()
    user_processes = list(
        filter(lambda x: x.catalog_id == index, current_user.processes))[::-1]
    form_class = get_catalog_form(catalog[index]['form_value'])
    form = form_class()
    if form.validate_on_submit():
        photo_count = len(
            list(
                filter(lambda x: not x.startswith('_'),
                       form_class.__dict__.keys()))) - 1
        data = []
        for i in range(photo_count):
            f = getattr(form, f'file{i}').data
            filename = secure_filename(f.filename)
            f.save(
                os.path.join(app.instance_path, app.config['UPLOAD_PATH'],
                             filename))
            data.append(f"{app.config['UPLOAD_PATH']}/{filename}")
        db_sess = db_session.create_session()
        process = Process()
        process.catalog_id = index
        process.request_data = json.dumps(data)
        current_user.processes.append(process)
        db_sess.merge(current_user)
        db_sess.commit()
        return redirect(url_for('catalog_routes', index=index))
    return render_template('catalog_page.html',
                           data=catalog[index],
                           form=form,
                           processes=user_processes)
Esempio n. 2
0
def profile():
    user_processes = current_user.processes[::-1]
    catalog = get_catalog()
    return render_template('profile.html',
                           processes=user_processes,
                           type_needed=True,
                           catalog=catalog)
Esempio n. 3
0
def catalog_delete(index):
    catalog = get_catalog(True)
    if current_user.id == catalog[index].get('owner_id', ''):
        db_sess = db_session.create_session()
        page = db_sess.query(CatalogPage).get(catalog[index]['id'])
        page.is_delete = True
        db_sess.commit()
        catalog.pop(index)
Esempio n. 4
0
def main():
    db_session.global_init(DB_FILE)
    conn = sqlite3.connect(DB_FILE, isolation_level=None)
    conn.execute('pragma journal_mode=wal')
    c = conn.cursor()
    while True:
        try:
            catalog = get_catalog()
            orders = c.execute(
                "SELECT id, catalog_id, request_data FROM processes WHERE is_finished == 0"
            ).fetchall()
            for i, catalog_id, request_data in orders:
                try:
                    if 'url' in catalog[catalog_id]:
                        catalog_obj = catalog[catalog_id]
                        content = process_by_url(
                            catalog_obj['url'], catalog_obj['request_data'],
                            catalog_obj['request_photo_type'],
                            catalog_obj['result_key'],
                            json.loads(request_data))
                    else:
                        func = conventers[catalog_id]
                        content = func(json.loads(request_data))
                    filename = f"{RESULT_PHOTOS_FOLDER}/{secure_filename(f'{datetime.datetime.now()}.jpg')}"
                    with open('static/' + filename, "wb") as f:
                        f.write(content)
                    c.execute(
                        f"UPDATE processes SET is_finished = ?, result_code = ?, result = ? WHERE id = ?",
                        (True, 200, filename, i))
                except KeyError:
                    c.execute(
                        f"UPDATE processes SET is_finished = ?, result_code = ?, result = ? WHERE id = ?",
                        (True, 400, "Неправильный ключ для результата запроса",
                         i))
                except Exception as ex:
                    c.execute(
                        f"UPDATE processes SET is_finished = ?, result_code = ?, result = ? WHERE id = ?",
                        (True, 400, str(ex), i))
            time.sleep(1)
        except Exception as ex:
            print(ex)
Esempio n. 5
0
def default():
    return render_template('main.html', catalog=get_catalog())