Esempio n. 1
0
def bundle_app(config={}):
    ''' Create a Flask app, set settings, load extensions, blueprints and create database. '''
    app = create_app(config)

    # NOTE: avoid creating or interacting with the database during migration
    if not app.config.get('MIGRATION', False):
        create_db(app, testing=app.config.get('TESTING', False))
        start_tasks(app)

    if os.name != 'nt':
        # !!! it did not work creates no back-end available error !!!
        # !!! strange bug , do not remove !!!
        if get_printers_usb():
            pass

    @app.route('/language_switch/<language>')
    def language_switch(language):
        ''' Endpoint to switch the default language.

        Parameters
        ----------
            language: str
                language short form to switch to.
        '''
        session['lang'] = language

        if current_user.is_authenticated:
            return redirect(str(request.referrer))

        return redirect(url_for('core.root'))

    @app.before_first_request
    def default_language():
        ''' Set the default language before the first request. '''
        if session.get('lang') not in list(SUPPORTED_LANGUAGES.keys()):
            session['lang'] = 'en'

    @app.errorhandler(404)
    @app.errorhandler(500)
    @app.errorhandler(413)
    def page_not_found(error):
        ''' Adding error handlers on main app instance. '''
        if getattr(error, 'code', None) == 413:
            flash('Error: file uploaded is too large ', 'danger')
            if current_user.is_authenticated:
                return redirect(url_for('cust_app.multimedia', nn=1))
            return redirect(url_for('core.root'))

        getattr(error, 'code', None) != 404 and log_error(error)
        flash('Error: something wrong , or the page is non-existing', 'danger')
        return redirect(url_for('core.root'))

    @app.route('/nojs/<int:enabled_js>')
    def nojs(enabled_js):
        ''' Handle JavaScript disabled or not supported. '''
        if enabled_js == 1:
            next_url = session.get('next_url', '/')

            if next_url != '/':
                return redirect(next_url)

            return redirect(url_for('core.root'))
        return render_template('nojs.html', page_title='Javascript is disabled')

    def moment_wrapper(moment):
        ''' Temproray moment wrapper to add `.toTime()` and `.toNow()`
            TODO: Remove and update Flask-Moment when PR's mereged.
        '''
        def toTime(*args, **kwargs):
            content = str(moment.fromTime(*args, **kwargs))

            return Markup(content.replace('from(', 'to('))

        def toNow(*args, **kwargs):
            content = str(moment.fromNow(*args, **kwargs))

            return Markup(content.replace('fromNow(', 'toNow('))

        setattr(moment, 'toTime', toTime)
        setattr(moment, 'toNow', toNow)
        return moment

    @app.context_processor
    def inject_vars():
        ''' Injecting default variables to all templates. '''
        ar = session.get('lang') == 'AR'  # adding language support var

        # modifying side bar spacing for specific paths
        path = request.path
        admin_routes = ['/users', '/user_a', '/admin_u', '/user_u', '/csv', '/settings']
        admin_route = any([path in admin_routes, path[:7] in admin_routes, path[:5] in admin_routes])

        return dict(path=path, adme=admin_route, brp=Markup('<br>'), ar=ar, version=VERSION, str=str,
                    defLang=session.get('lang'), getattr=getattr, settings=Settings.get(), Serial=Serial,
                    checkId=lambda id, records: id in [i.id for i in records], offices=Office.query.all(),
                    moment_wrapper=moment_wrapper, current_path=quote(request.path, safe=''),
                    windows=os.name == 'nt', unix=os.name != 'nt')

    return app
Esempio n. 2
0
def ticket():
    ''' view of ticket customization '''
    windows = os.name == 'nt'
    settings = data.Settings.get()
    printer = data.Printer.get()
    touch_screen_settings = data.Touch_store.get()
    printers = get_printers_cli(windows=windows, unix=not windows)\
        if windows or settings.lp_printing else get_printers_usb()
    form = TicketForm(printers, settings.lp_printing)

    if form.validate_on_submit():
        printer = data.Printer.get()  # NOTE: sessions's lost

        if form.kind.data == 1:  # Rigestered
            printer.value = form.value.data
            printer.active = False
            touch_screen_settings.n = True
        else:  # Printed
            printer_id = form.printers.data

            if printer_id == '00':  # Not found
                flash('Error: you must have available printer, to use printed',
                      'danger')
                return redirect(url_for('cust_app.ticket'))

            if windows or settings.lp_printing:
                printer.name = printer_id
            else:
                id_chunks = printer_id.split('_')
                printer.vendor = convert_to_int_or_hex(id_chunks[0])
                printer.product = convert_to_int_or_hex(id_chunks[1])

                if len(id_chunks) == 4:
                    printer.in_ep = convert_to_int_or_hex(id_chunks[2])
                    printer.out_ep = convert_to_int_or_hex(id_chunks[3])
                else:
                    printer.in_ep = None
                    printer.out_ep = None

            printer.active = True
            printer.langu = form.langu.data
            printer.value = form.value.data
            printer.scale = form.scale.data
            touch_screen_settings.n = False

        db.session.commit()
        flash('Notice: settings have been updated .', 'info')
        return redirect(url_for('cust_app.ticket'))

    if not form.errors:
        form.kind.data = 1 if touch_screen_settings.n else 2
        form.langu.data = printer.langu
        form.value.data = printer.value
        form.scale.data = printer.scale

        if windows or settings.lp_printing:
            form.printers.data = printer.name or ''
        else:
            form.printers.data = f'{printer.vendor}_{printer.product}'
            form.printers.data += f'_{printer.in_ep}_{printer.out_ep}'

    return render_template('ticket.html',
                           navbar='#snb2',
                           page_title='Tickets',
                           vtrue=data.Vid.query.first().enable,
                           strue=data.Slides_c.query.first().status,
                           form=form,
                           hash='#da7')