def get_selection(session_id): session = Session.objects(id=session_id)[0] selection = session.selection if selection is None: selection = RVDSelection() return utility.update_selection(session, selection) return selection.get_safe()
def add_item_to_selection(session_id: str, part: dict, job_type: str): session: QuerySet = Session.objects(id=session_id) session: Session = session[0] selection = session.selection if selection is None: selection = RVDSelection() selection.items.append(utility.get_cart_item(job_type, part)) return utility.update_selection(session, selection)
def get_suggestion(session_id, only_present, part_params: dict, part_type: str): only_present = True if (only_present == 'true' or only_present is True) else False session = Session.objects(id=session_id)[0] if session.selection is None: session.selection = utility.create_selection() session.save() candidate = utility.get_candidate_by_params(part_params, part_type, only_present) parameters = utility.get_candidate_params(candidate) return {'suggestion': candidate, 'parameters': parameters}
def del_item_from_selection(session_id: str, item_index: int): session: QuerySet = Session.objects(id=session_id) session: Session = session[0] selection = session.selection if selection is None: selection = RVDSelection() if item_index >= len(selection.items): return selection.get_safe() del selection.items[item_index] return utility.update_selection(session, selection)
def get_filtered_params(session_id, only_present): only_present = True if only_present == 'true' else False session = Session.objects(id=session_id)[0] if session.selection is None: session.selection = utility.create_selection() session.save() selection = session.selection candidates = utility.get_candidates_by_params(selection, only_present) parameters = utility.get_parameters_list(candidates) return {'candidates': candidates, 'parameters': parameters, 'selection': selection.get_safe()}
def set_part(session_id: str, collection: str, part_id: str, amount: float): if amount == '': amount = 0 session = Session.objects(id=session_id)[0] collection = collections[collection] part = collection.objects(id=part_id)[0] price = round(part.price * utility.get_price_coef(part.price, amount), 2) item = CartItem(name=part.name, item=part, amount=amount, price=price, total_price=round(price * amount, 2)) selection = utility.save_selection(session, [], {}, item) res_part = selection['part'] res = {'current_part': res_part} return res
def get_user_sessions(username: str, limit=None, offset=None, sorting=None) -> QuerySet: if username is not None: sessions = Session.objects(user=username) else: sessions = Session.objects if offset: sessions = sessions.skip(offset) if limit: sessions = sessions.limit(limit) if sorting: sessions = sessions.order_by(sorting) return sessions
def update_selection(session_id: str, selection: dict): session: QuerySet = Session.objects(id=session_id) session: Session = session[0] if selection['items'] is not None: selection['items'] = set_linked_params(selection['items']) return utility.save_selection(session, selection['items'], selection['subtotal'])
def clear_part(session_id: str): session = Session.objects(id=session_id)[0] if session.selection and session.selection.part: del session.selection.part session.save()
def update_amount(session_id: str, amount: float): session: QuerySet = Session.objects(id=session_id) session: Session = session[0] selection = session.selection selection.subtotal['amount'] = amount return utility.update_selection(session, selection)
def set_job_type(session_id: str, type: str): session: QuerySet = Session.objects(id=session_id) session: Session = session[0] selection = session.selection selection.subtotal['job_type'] = type return utility.update_selection(session, selection)
def get_session(sid: str) -> Session: return Session.objects( id=sid)[0] if len(Session.objects(id=sid)) > 0 else None
def count_user_sessions(username: str = None): if username is not None: sessions = Session.objects(user=username) else: sessions = Session.objects return sessions.count()