def register(action, dom, attrs): """ Register XML-response in DB. Get items form DOM and clear XML. Valid actions: 203|204|205|207. Arguments: dom - DOM object attrs - Dictionary (requested items: action, data (XML), selected_item, custom_code and another...) Returns: data - Dictionary (next_custom_code) """ print_db_state() IsError = False print_action(action, 'DBase.Register') # ---- # User # ---- userID = get_item(dom, attrs, 'userID') user = userID and User.query.filter_by(userID=userID).first() if not user: user = User(userID, title=get_item(dom, attrs, 'userName'), email=getDOMItemValue(dom, 'orderEmail')) _add(user) # ------ # Module # ------ wizardID = get_item(dom, attrs, 'wizardID') module = wizardID and Module.query.filter_by(wizardID=wizardID).first() if not module: module = Module(wizardID, title=re.sub(r'\s+', ' ', re.sub(r'<.*?>', '', get_item(dom, attrs, 'wizardName')))) m = re.search(r'.*(DUS-[\d-]*)', module.wizardName) if m: module.DUS = m.group(1) _add(module) # ------------------ # Parameters & Items # ------------------ for n, x in enumerate(getDOMParameters(dom, False)): parameter = Parameter.query.filter_by(CIS=x['id']).first() if not parameter: parameter = Parameter(x['id'], valid_parameter_names.get(x['type'].upper()), unit=x['unit']) _add(parameter) item = Item.query.filter_by(parameter=parameter, module=module).first() if not item: item = Item(parameter, module) _add(item) # --------------------- # Order & Prices & Data # --------------------- if action not in valid_action_types: IsError = True otype = -1 id = get_item(dom, attrs, 'selected_item') code = get_item(dom, attrs, 'custom_code') IsAddOrder = False IsUpdateOrder = False custom_code = '' order = None if IsError: pass # ---------------------------------------------- # Get Order item from DB and update or add a new # ---------------------------------------------- elif action == '203': if not id: otype = ORDER_STANDARD order = Order.query.filter_by(user=user, module=module, otype=otype).first() else: order = get_order(id, None) custom_code = order and order.code otype = -1 elif action == '204': otype = ORDER_CUSTOM order = get_order(id, code, user=user, module=module) elif action == '205': otype = ORDER_CUSTOM #order = get_order(None, code, user=user, module=module) elif action == '207': otype = ORDER_DONE else: otype = -1 if IsDebug: print '>>> Order: %s %s %s' % (order, str(code), str(id)) if otype in valid_order_types: price_columns = get_columns(dom, attrs, model_price_columns) if not attrs.get('title'): attrs['title'] = gettext(valid_order_names[otype]) if not order: order = Order(user, module, otype, get_columns(dom, attrs, model_order_columns)) #if otype == ORDER_DONE: # order.code = Order.generate_custom_code(user, module, otype=ORDER_DONE) #elif otype == ORDER_CUSTOM: # order.code = get_item(dom, attrs, 'custom_code') or \ # Order.generate_custom_code(user, module, otype=otype) IsAddOrder = True else: if action in ('203','207'): pass elif action == '204': if order.option_update: order.data = order._data(get_item(dom, attrs, 'data')) IsUpdateOrder = True elif action == '205': order.set_attrs(get_columns(dom, attrs, model_order_columns)) IsUpdateOrder = True current_price = Order.generate_current_price(int(float(price_columns['total']) * 100), \ price_columns['currency']) if IsAddOrder: last_price = '' order.current_price = current_price else: last_price = order.current_price if order.otype in (ORDER_STANDARD, ORDER_CUSTOM,): order.current_price = current_price order.rating = order.current_price < last_price and RATING_DOWN or \ order.current_price > last_price and RATING_UP or \ RATING_EQUAL else: order.rating = current_price > last_price and RATING_DOWN or \ current_price < last_price and RATING_UP or \ RATING_EQUAL if IsDebug: print '>>> Current price: %s %s %s' % (last_price, order.current_price, order.rating) if order and IsAddOrder: _add(order) if order and (IsAddOrder or (order.option_cost and last_price != current_price)): _add(Price(order, price_columns)) # ---------- # Save to DB # ---------- _commit(IsUpdateOrder and True or False) # --------------------- # Get data for response # --------------------- data = {} next_custom_code = Order.generate_custom_code(user, module) if action in ('203','204','205',): if order: data['title'] = order.title data['custom_code'] = order.code or custom_code data['next_custom_code'] = order.option_update and order.code or next_custom_code data['option_update'] = order.option_update data['option_cost'] = order.option_cost else: data['next_custom_code'] = next_custom_code data['custom_code'] = custom_code return data