Esempio n. 1
0
def respond_internal(action, **kw):
    exchange_error = 0
    exchange_message = ''
    error_code = ''
    error_description = ''
    response = {}
    dom = None
    data = ''
    currency = ''
    order_number = ''
    order_date = ''

    print_action(action, 'Respond.Internal')

    op = request.form.get('op') or kw.get('op')

    if IsDebug:
        print '--> op: [%s]' % op

    if not op:
        pass

    elif op == 'get':
        # --------------------------------------------------------------
        # Generate and Send XML to Service (from WEB-form to JavaScript)
        # --------------------------------------------------------------
        data = getXml(action, request, session)
        errors = []

    elif op == 'set':
        # -----------------------------------------------
        # Receive XML from Service (loaded by JavaScript)
        # -----------------------------------------------
        dom, data = receive(action, request, session, **kw)

        if demo():
            exchange_error, exchange_message, total, currency = _demo_price(action)
            order_number, order_date = _demo_order(action)
        else:
            total = float(getDOMItemValue(dom, 'total'))
            currency = CURRENCIES.get(getDOMItemValue(dom, 'currency'), gettext('undefined'))
            error_code = getDOMItemValue(dom, 'errorCode').strip()
            error_description = getDOMItemValue(dom, 'errorDescription')

        errors = getDOMErrors(dom) or ''

        if IsDebug:
            print '--> Total: %s %s' % (total, currency)

        total = '%.2f' % total or ''
        price = '%s %s' % (total, currency) or ''

    return { \
        'action'            : action,
        'op'                : op,
        # --------------
        # Service Errors
        # --------------
        'exchange_error'    : exchange_error, 
        'exchange_message'  : exchange_message,
        'error_code'        : error_code,
        'error_description' : error_description,
        'errors'            : errors,
        # ---
        # IDs
        # ---
        'countryID'         : getDOMItemValue(dom, 'countryID') or '',
        'regionID'          : getDOMItemValue(dom, 'regionID') or '',
        'userID'            : getUserID(getDOMItemValue(dom, 'userID')),
        # --------------
        # Client Details
        # --------------
        'country_name'      : getDOMItemValue(dom, 'countryName') or getCountry(getDOMItemValue(dom, 'countryID')),
        'region_name'       : getDOMItemValue(dom, 'regionName') or getRegion(getDOMItemValue(dom, 'regionID')),
        'client_name'       : getClient(getDOMItemValue(dom, 'userName')),
        # ------------------------------
        # Results (Price & XML-Response)
        # ------------------------------
        'price'             : price,
        'data'              : data,
    }
Esempio n. 2
0
def respond_log(action):
    exchange_error = 0
    exchange_message = ''

    page = None

    current_page, pages, per_page, has_prev, has_next, total, id = (0, 0, 0, False, False, 0, None)
    iter_pages = []
    order = None
    data = None
    dom = None
    items = None
    title = ''

    print_action(action, 'Respond.Log')

    locale = request.form.get('currentUsedLocalization') or ''

    try:
        # ----------------
        # Get Data from DB
        # ----------------
        if not action:
            pass
        elif action == '301':
            exchange_error, exchange_message, page, data = getLogPage(action, request, session)
        elif action == '305':
            exchange_error, exchange_message, id, total, data = getStatictics(action, request, session)
        elif action == '308':
            exchange_error, exchange_message, page, data = removeLogItem(action, request, session)
        else: #if action in ('302','303','304','307',):
            exchange_error, exchange_message, id, order = getLogItem(action, request, session)
    except:
        msg = '--> Database error!'
        # -------------
        # DB Log failed
        # --------------
        print_to(errorlog, msg, request=request)
        raise

    if not (action and action in valid_action_types):
        pass

    elif action in ('301','308',):
        # --------------------
        # Get Log-page content
        # --------------------
        if page:
            current_page, pages, per_page, has_prev, has_next, total = page.get_page_params()
            iter_pages = page.iter_pages()

        if IsDebug:
            print '--> LogPage[%s]: items:[%s] %s-%s-%s-%s iter:%s' % ( \
                current_page, len(data), pages, per_page, has_prev, has_next, iter_pages)
    
    elif action in ('302',):
        # ------------------------------
        # DOM and XML-Items (Dictionary)
        # ------------------------------
        dom, data = receive(action, request, session, data=order.data) #, products_sorted=True

        if IsDebug:
            print '--> LogPageItem[%s]: Data %s, Products %s, Parameters %s' % ( \
                id, len(order.data), len(data['products']), len(data['parameters']))
    
    elif action in ('303','304','307',):
        # ---------
        # Clean XML
        # ---------
        data = order.data #_data(coding='encode')

        # ------------------------------
        # DOM and XML-Items (Dictionary)
        # ------------------------------
        dom, items = receive(action, request, session, data=data)

        title = order.title

        if IsDebug:
            print '--> Loaded LogPageItem[%s]: Data %s, Products %s, Parameters %s' % ( \
                id, len(order.data), len(items['products']), len(items['parameters']))

    elif action in ('305',):

        if IsDebug:
            print '--> LogStatictics[%s]: Data %s' % ( \
                id, data and len(data), )

    if exchange_error:
        if IsDebug:
            print '--> ExchangeError[%s]: %s' % (exchange_error, exchange_message)

    return { \
        'action'            : action,
        'op'                : '',
        # --------------
        # Service Errors
        # --------------
        'exchange_error'    : exchange_error, 
        'exchange_message'  : exchange_message,
        # -----------------------------
        # Results (Log page parameters)
        # -----------------------------
        'id'                : id,
        'rows_on_page'      : len(data),
        'total'             : total,
        'page'              : current_page,
        'pages'             : pages,
        'per_page'          : per_page,
        'has_prev'          : has_prev, 
        'has_next'          : has_next,
        'iter_pages'        : iter_pages,
        # --------------------------
        # Results (Log page content)
        # --------------------------
        'custom_code'       : order and order.code or '',
        'title'             : title,
        'price'             : '',
        'data'              : data,
        'dom'               : dom,
        'items'             : items,
    }
Esempio n. 3
0
def loader():
    if not request.form.get('wizardID'):
        return ''

    host = request.form.get('host')
    debug = request.args.get('debug') == '1' and True or False
    demo(request.form.get('demo'), request=request)

    setReference(host, debug)

    if IsDebug:
        start = datetime.datetime.now()
        print '--> Started at %s' % start.strftime('%H:%M:%S:%f')

    wizard = request.form.get('wizardID') or session.get('wizard')
    action = request.form.get('action') or '204'
    check = request.form.get('check') or ''

    communication = request.form.get('helperCommunicationType') or default_communication
    locale = request.form.get('currentUsedLocalization') or session.get('locale')

    exchange_error = 0

    if IsDebug:
        print '--> action: [%s]' % action
        print '--> communication: [%s]' % communication
        print '--> demo: [%s]' % demo()
        print '--> host: [%s]' % host
        print '--> wizard: [%s]' % wizard

    response = {}

    if not (action and action in valid_action_types):
        return ''

    IsChecked = True

    if not isAuthorized(request.form.get('userID')):
        exchange_error = -4
        exchange_message = gettext('Sorry, you are not authorized.')

        IsChecked = False

    if IsChecked and action in ('207','307') and demo() and not _check(wizard, check):
        exchange_error = -5
        exchange_message = gettext('Sorry, the confirmation code is invalid.')+'<br>'+gettext('Please, try more.')

        IsChecked = False

    if action in ('301','302','303','304','305','307','308',):

        if IsChecked:
            # -------------------------------------------------------------------------------------
            # Get Data from DB (XML:data, DOM:dom, parsed XML-Items:items or Log-page content:data)
            # -------------------------------------------------------------------------------------
            response = respond_log(action)

        if not (response and response.get('data')):
            pass

        elif action in ('303','304','307',):
            requested_action = str(int(action)-100)

            # --------------------------------------------------
            # Get XML (update tags for a new request to Service)
            # --------------------------------------------------
            check, data = getRequestXML(requested_action, request, session, data=response.get('items'), dom=response.get('dom'), 
                title=response.get('title'), page='log',
                url=host)

            # ----------------------------------------
            # Send request to Service and get response
            # ----------------------------------------
            response = respond_external(requested_action, data=data, id=response.get('id'))
            response['action'] = action

            # -------------------------------------------------------------
            # Get Data Dictionary (!!!) from response to send to Controller
            # -------------------------------------------------------------
            if action == '303':
                dom, data = receive(action, request, session, data=response.get('data'))
                response['data'] = data

            response['dom'] = None
            response['items'] = None

        else:
            # --------------------------------------
            # Remove DOM from response to Controller
            # --------------------------------------
            response['dom'] = None
            response['items'] = None

    elif communication == COMMUNICATIONS['external'] or not communication:
        if IsDebug:
            print '--> check: [%s] = %s' % (check, IsChecked)

        if action == '100':
            # -------------------
            # Validate the action
            # -------------------
            if not demo():
                response = {'x1' : None, 'x2' : None, 'op' : None}
            else:
                response = _set_checker(wizard)
        
        elif action in ('203','204','205',):
            # ---------------------------------------------------------------
            # Send External request to Service and get response (calculation)
            # ---------------------------------------------------------------
            response = respond_external(action)
        
        elif action in ('207',) and IsChecked:
            # ---------------------------------------------------------
            # Send External request to Service and get response (order)
            # ---------------------------------------------------------
            response = respond_external(action)

        if response:
            response['data'] = ''

    elif communication == COMMUNICATIONS['internal']:
        if action == '100':
            # -------------------
            # Validate the action
            # -------------------
            response = {'x1' : None, 'x2' : None, 'op' : None}

        elif action in ('203','204','205','206',):
            # -------------------------------------------------
            # Send Internal request to Service and get response
            # -------------------------------------------------
            response = respond_internal(action)

    if not response:
        response = { \
            'action'            : action,
            'op'                : '',
            # -------------------------------
            # Not authorized or check invalid
            # -------------------------------
            'exchange_error'    : exchange_error, 
            'exchange_message'  : exchange_message,
            'error_code'        : '',
            'error_description' : '',
            'errors'            : '',
            # -----------------
            # Results (no data)
            # -----------------
            'price'             : '',
            'data'              : '',
        }            

    if IsDebug:
        finish = datetime.datetime.now()
        t = finish - start
        print '--> Finished at %s' % finish.strftime('%H:%M:%S:%f')
        print '--> Spent time: %s sec' % ((t.seconds*1000000+t.microseconds)/1000000)

    return jsonify(response)