def fetch_create_card_response(request):
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ("card_number", "chip_number", "money_amount")
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields)
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_number = data.get("card_number")
    chip_number = data.get("chip_number")
    money_amount = data.get("money_amount")

    error_response = get_error_response(card_number, chip_number, money_amount)
    if error_response:
        error_response['success'] = False
        return error_response

    new_card = create_new_card(card_number, chip_number, money_amount)
    if not new_card._state.db:
        return {
            "success": False,
            "error": "Карта не была добавлена в базу данных"
        }

    return {"success": True}
Example #2
0
def fetch_exclude_deposit_money_response(request):
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ('card_type', 'card', 'exclude_amount')
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields)
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_type = data.get("card_type")
    card = data.get("card")
    exclude_amount = data.get("exclude_amount")

    card_error_response = check_card.get_card_error_response(card_type, card)
    if card_error_response.get('error'):
        card_error_response['success'] = False
        return card_error_response

    operator = take_credit_helpers.get_operator(request)
    team = check_card.get_team_by_card(card_type, card)
    team_card = check_card.get_team_card(team)
    team_deposit = get_team_deposit(team)

    error_response = get_error_response(team, team_deposit, exclude_amount,
                                        operator)
    if error_response:
        error_response['success'] = False
        return error_response

    increase_card_money_amount_to_exclude_amount(team_card, exclude_amount)
    decrease_deposit_invest_amount_to_exclude_amount(team_deposit,
                                                     exclude_amount)
    return {"success": True}
Example #3
0
def fetch_create_team_response(request):
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ("name", "owner", "faculty", "group", "bank", "card",
                       "card_type")
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields)
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    name = data.get("name")
    owner = data.get("owner")
    faculty = data.get("faculty")
    group = data.get("group")
    bank = data.get("bank")
    card = data.get("card")
    card_type = data.get("card_type")

    error_response = get_error_response(name, owner, faculty, group, bank,
                                        card_type, card)
    if error_response:
        error_response['success'] = False
        return error_response

    new_team = create_new_team(name, owner, faculty, group, bank, card_type,
                               card)
    if not new_team._state.db:
        return {
            "success": False,
            "error": "Команда не была добавлена в базу данных"
        }

    return {"success": True}
Example #4
0
def fetch_transfer_money_response(request, give_money):
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ("card_type", "card", "money_amount")
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields
    )
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_type = data.get('card_type')
    card = data.get('card')
    money_amount = data.get('money_amount')

    card_error_response = check_card.get_card_error_response(card_type, card)
    if card_error_response:
        card_error_response['success'] = False
        return card_error_response

    error_response = get_error_response(money_amount)
    if error_response:
        error_response['success'] = False
        return error_response

    team = check_card.get_team_by_card(card_type, card)
    team_card = check_card.get_team_card(team)

    if give_money:
        team_card.money_amount += money_amount
    else:
        team_card.money_amount -= money_amount

    team_card.save()
    return {"success": True}
Example #5
0
def fetch_buy_share_response(request):
    current_half_year = Timing.objects.all()[0].current_half_year
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ('card_type', 'card', 'share_type', 'amount')
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields)
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_type = data.get('card_type')
    card = data.get('card')

    card_error_response = check_card.get_card_error_response(card_type, card)
    if card_error_response:
        card_error_response['success'] = False
        return card_error_response

    share_type_to_buy = data.get('share_type')
    amount_to_buy = data.get('amount')

    team = check_card.get_team_by_card(card_type, card)
    team_card = check_card.get_team_card(team)
    share_type = ShareType.objects.filter(name=share_type_to_buy)
    if not share_type:
        return {'success': False, 'error': 'Такого типа акций нет'}
    share_type = share_type[0]

    stock_price = share_type.stock_price.filter(half_year=current_half_year)
    if not stock_price:
        return {'success': False, 'error': 'У акций не заполнены расценки'}
    current_price = stock_price[0].buy_price

    error_response = get_error_response(share_type, amount_to_buy,
                                        current_price, team_card)
    if error_response:
        error_response['success'] = False
        return error_response

    decrease_card_money_amount_to_buyed_shares_price(team_card, current_price,
                                                     amount_to_buy)

    deal = ShareDeal.objects.filter(team=team, share_type=share_type)
    if deal:
        deal[0].amount += amount_to_buy
        deal[0].save()
    else:
        deal = create_new_deal(team, share_type, amount_to_buy)

    return {'success': True}
def fetch_sell_share_response(request):
    current_half_year = Timing.objects.all()[0].current_half_year
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ('card_type', 'card', 'share_type', 'amount')
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields)
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_type = data.get('card_type')
    card = data.get('card')

    card_error_response = check_card.get_card_error_response(card_type, card)
    if card_error_response:
        card_error_response['success'] = False
        return card_error_response

    share_type_to_sell = data.get("share_type")
    amount_to_sell = data.get('amount')

    team = check_card.get_team_by_card(card_type, card)
    team_card = check_card.get_team_card(team)

    share_type = ShareType.objects.filter(name=share_type_to_sell)
    if not share_type:
        return {'success': False, 'error': 'Такого типа акций нет'}
    share_type = share_type[0]

    stock_price = share_type.stock_price.filter(half_year=current_half_year)
    if not stock_price:
        return {'success': False, 'error': 'У акций не заполнены расценки'}
    current_price = stock_price[0].sell_price

    deal = ShareDeal.objects.filter(team=team, share_type=share_type)
    if not deal:
        return {'success': False, 'error': 'Команда не покупала таких акций'}
    deal = deal[0]

    error_response = get_error_response(amount_to_sell, deal)
    if error_response:
        error_response['success'] = False
        return error_response

    increase_card_money_amount_to_sold_shares_price(team_card, current_price,
                                                    amount_to_sell)
    decrease_deal_share_type_amount(deal, amount_to_sell)

    return {'success': True}
Example #7
0
def fetch_invest_money_response(request):
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ('card_type', 'card', 'invest_amount')
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields
    )
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_type = data.get("card_type")
    card = data.get("card")
    invest_amount = data.get("invest_amount")

    card_error_response = check_card.get_card_error_response(card_type, card)
    if card_error_response.get('error'):
        card_error_response['success'] = False
        return card_error_response

    operator = take_credit.get_operator(request)
    team = check_card.get_team_by_card(card_type, card)
    team_card = check_card.get_team_card(team)

    error_response = get_error_response(team, invest_amount, operator)
    if error_response:
        error_response['success'] = False
        return error_response

    decrease_team_card_money_amount_to_invest_amount(team_card, invest_amount)

    team_deposit = get_team_deposit(team)
    if not team_deposit:
        team_deposit = create_new_deposit(team, invest_amount)
    else:
        team_deposit.invest_amount += invest_amount
        team_deposit.save()

    if not team_deposit._state.db:
        return {
            "success": False,
            "error": "Депозит не был добавлен в базу данных"
        }

    return {"success": True}
def fetch_credit_info_response(request):
    data = json.loads(request.body.decode("utf-8"))
    card_type = data.get("card_type")
    card = data.get("card")

    expected_fields = ("card_type", "card")
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields)
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_error_response = check_card.get_card_error_response(card_type, card)
    if card_error_response.get('error'):
        card_error_response['success'] = False
        return card_error_response

    credit_info = get_credit_info(card_type, card)
    return credit_info
Example #9
0
def fetch_check_card_response(request):
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ("card_type", "card")
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields)
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_type = data.get("card_type")
    card = data.get("card")

    error_response = get_card_error_response(card_type, card)
    if error_response:
        error_response['success'] = False
        return error_response

    team = get_team_by_card(card_type, card)
    response = {'success': True, 'team_name': team.name}
    return response
def fetch_take_credit_response(request):
    data = json.loads(request.body.decode("utf-8"))

    expected_fields = ('card_type', 'card', 'credit_amount', 'term')
    not_received_fields = helpers.get_not_recieved_fields(
        data, expected_fields)
    if not_received_fields:
        return helpers.get_not_received_all_expected_fields_error_response(
            not_received_fields)

    card_type = data.get("card_type")
    card = data.get("card")
    credit_amount = data.get("credit_amount")
    term = data.get("term")

    card_error_response = check_card.get_card_error_response(card_type, card)
    if card_error_response.get('error'):
        card_error_response['success'] = False
        return card_error_response

    operator = get_operator(request)
    team = check_card.get_team_by_card(card_type, card)
    team_card = check_card.get_team_card(team)

    error_response = get_error_response(team, team_card, credit_amount, term,
                                        operator)
    if error_response:
        error_response['success'] = False
        return error_response

    transfer_credit_amount_to_team_card(team_card, credit_amount)
    credit = create_new_credit(team, credit_amount, term)
    if not credit._state.db:
        return {
            "success": False,
            "error": "Кредит не был добавлен в базу данных"
        }

    return {"success": True}