Exemple #1
0
def save_selection(session: Session, items: list, subtotal: dict, part: CartItem = None) -> dict:
    selection = RVDSelection(items=items, subtotal=subtotal, part=part)
    selection = calc_subtotal(selection)
    selection = copy_selection_parameters(selection)
    session.selection = selection
    session.save()
    return selection.get_safe()
Exemple #2
0
def start_session(user=''):
    sid = get_random_string(15)
    while check_session(sid):
        sid = get_random_string(15)
    session = Session(id=sid)
    session.user = user
    session.save()
    return session
Exemple #3
0
def get_session(sid) -> Session:
    query = {"_id": sid}
    res = db.find_one(session_collection, query)
    if res is None:
        return None
    session = Session()
    session.create_from_struct(res)
    return session
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
Exemple #10
0
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
Exemple #11
0
def delete_session(sid: str):
    session = get_session(sid)
    Session.delete(session)
Exemple #12
0
def update_selection(session: Session, selection: RVDSelection) -> dict:
    session.selection = calc_subtotal(selection)
    session.save()
    return selection.get_safe()
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)
Exemple #17
0
def count_user_sessions(username: str = None):
    if username is not None:
        sessions = Session.objects(user=username)
    else:
        sessions = Session.objects
    return sessions.count()
Exemple #18
0
def get_session(sid: str) -> Session:
    return Session.objects(
        id=sid)[0] if len(Session.objects(id=sid)) > 0 else None
Exemple #19
0
def create_session(sid):
    session = Session()
    session.set_id(sid)
    db.insert(session_collection, session.to_dict())
    return session