def show_product():
    productId = int(request.args.get('productId'))
    form = ProductForm(request.form)

    product = Product.query.get(productId)
    dto = ProductDetailDTO(product)

    if request.method == 'POST':
        if not get_current_user().isAdmin:
            return Response('', status=401)

        if form.validate():
            product = Product.query.get(productId)
            product.price = form.price.data
            product.name = form.name.data
            if form.hidden.data == "1":
                photo = request.files['photo']
                photo.save(os.getenv('photo_dir') + fr'\{product.code}.jpg')
            db.session.commit()
            return flask.redirect(url_for('products'))
        return render_template('product.html', product=dto, form=form)

    form.price.default = product.price
    form.name.default = product.name
    form.process()
    return render_template('product.html', product=dto, form=form)
def order(service: OrderService):
    orderId = int(request.args.get('orderId') or -1)
    userOrder = service.get_order_for_user(orderId)

    form = OrderForm(request.form)
    if request.method == 'POST':
        lock.acquire()
        if form.validate():
            success, invoice = service.finalize(form.invoice.data,
                                                form.message.data,
                                                form.address.data,
                                                form.clientName.data)
            lock.release()
            if not success:
                return Response('', status=400)
            if invoice is not None:
                return send_from_directory(directory=os.getenv('invoices'),
                                           filename=invoice)
        lock.release()

    if userOrder is None:
        return Response('', status=400)

    dtos = ([ProductOrderDTO(x) for x in userOrder.get_products()])
    dtos.sort(key=lambda x: x.code)

    return render_template('order.html',
                           form=form,
                           dtos=dtos,
                           totalPrice=userOrder.calculate_total_price(),
                           status=userOrder.status.name,
                           id=userOrder.id)
def sign_up(service: AccountService):
    form = RegistrationForm(request.form)
    if request.method == 'POST':
        if form.validate(extra_validators={'email' : [service.validate_email()] }):
            service.register(form.email.data, form.password.data)
            return redirect(url_for('hello_world'))

    return render_template('sign_up.html', form = form )
def login(service: AccountService):
    form = LoginForm(request.form)
    user = User.query.filter_by(email = form.email.data).first()
    if form.validate(extra_validators={ 'password' : [service.good_password(user)]}):
        login_user(user, remember = True)
        next = flask.request.args.get('next')
        return flask.redirect(next or flask.url_for('products'))

    return render_template('login_form.html', form=form)
def orders(service: OrderService):
    page = int(request.args.get('page') or 1)
    dtos, hasPrev, hasNext = service.get_orders(page, pageSize=20)

    return render_template('order_list.html',
                           dtos=dtos,
                           hasPrev=hasPrev,
                           hasNext=hasNext,
                           pageNumber=page)
def get_synchronization(service: SynchronizationService):
    synchId = int(request.args.get('synchId') or -1)
    synchronization, actions = service.get_synchronization(synchId)
    if synchronization is None:
        return Response('', status=405)

    return render_template(
        'synchronization.html',
        date=synchronization.date,
        actions=actions,
        id=synchId,
        modified=synchronization.get_number_of_modifications())
def synchronizations(service: ScheduleService):
    page = int(request.args.get('page') or 1)
    form = ScheduleForm(request.form)
    message = _("You have entered invalid cron")
    schedule = service.get_schedule(form.schedule.data)

    if form.validate(extra_validators={
            'schedule': [lambda form, field: validate(schedule, message)]
    }):
        service.start_job(schedule)
        return Response('', 200)

    entites = Synchronization.query.paginate(page, 20, False)
    dtos = [SynchronizationDTO(s) for s in entites.items]
    return render_template('synchronization_list.html',
                           dtos=dtos,
                           form=form,
                           hasNext=entites.has_next,
                           hasPrev=entites.has_prev,
                           pageNumber=page)
def products(service: ProductService):
    filter = request.args.get('filter')
    page = int(request.args.get('page') or 1)
    minPrice = int(request.args.get('minPrice') or 0)
    maxPrice = int(request.args.get('maxPrice') or 9999999)
    categories = request.args.get('categories')
    if categories is None:
        categories = []
    else:
        categories = categories.split(',')[:-1]

    dtos, hasPrev, hasNext = service.get_product_list(filter,
                                                      page,
                                                      minPrice,
                                                      maxPrice,
                                                      categories,
                                                      numberOnPage=8)
    return render_template('product_list.html',
                           dtos=dtos,
                           pageNumber=page,
                           hasNextPage=hasNext,
                           hasPrevPage=hasPrev)