def update_swap():

    id = int(get_jwt()['sub'])

    # get sender user
    sender = Profiles.query.get(id)
    if not sender:
        raise APIException('User not found', 404)

    body = request.get_json()
    check_params(body, 'tournament_id', 'recipient_id')

    # get recipient user
    recipient = Profiles.query.get(body['recipient_id'])
    if not recipient:
        raise APIException('Recipient user not found', 404)

    # get swap
    swap = Swaps.query.get((id, recipient.id, body['tournament_id']))
    counter_swap = Swaps.query.get((recipient.id, id, body['tournament_id']))
    if not swap or not counter_swap:
        raise APIException('Swap not found', 404)

    if 'percentage' in body:

        percentage = abs(body['percentage'])
        counter = abs(body['counter_percentage']
                      ) if 'counter_percentage' in body else percentage

        sender_availability = sender.available_percentage(
            body['tournament_id'])
        if percentage > sender_availability:
            raise APIException((
                'Swap percentage too large. You can not exceed 50% per tournament. '
                f'You have available: {sender_availability}%'), 400)

        recipient_availability = recipient.available_percentage(
            body['tournament_id'])
        if counter > recipient_availability:
            raise APIException(
                ('Swap percentage too large for recipient. '
                 f'He has available to swap: {recipient_availability}%'), 400)

        # So it can be updated correctly with the update_table funcion
        body['percentage'] = swap.percentage + percentage
        update_table(counter_swap,
                     {'percentage': counter_swap.percentage + counter})

    update_table(
        swap,
        body,
        ignore=['tournament_id', 'recipient_id', 'paid', 'counter_percentage'])

    db.session.commit()

    return jsonify(swap.serialize())
Exemple #2
0
    def update_profile(user_id):

        prof = Profiles.query.get(user_id)

        req = request.get_json()
        utils.check_params(req)

        utils.update_table(prof, req, ignore=['profile_pic_url'])

        db.session.commit()

        return jsonify(prof.serialize())
Exemple #3
0
    def update_profile(user_id):

        prof = Profiles.query.get(user_id)

        body = request.get_json()
        check_params(body)

        update_table(prof, body, ignore=['profile_pic_url'])

        db.session.commit()

        return jsonify(prof.serialize())
def update_buy_in(id):

    body = request.get_json()
    check_params(body)

    user_id = int(get_jwt()['sub'])

    buyin = Buy_ins.query.get(id)

    update_table(buyin,
                 body,
                 ignore=['user_id', 'flight_id', 'receipt_img_url'])

    db.session.commit()

    return jsonify(Buy_ins.query.get(id).serialize())
Exemple #5
0
    def update_buy_in(user_id, id):

        body = request.get_json()
        check_params(body)

        buyin = Buy_ins.query.filter_by(id=id, user_id=user_id).first()

        if buyin is None:
            raise APIException('Buy_in not found', 404)

        update_table(buyin,
                     body,
                     ignore=['user_id', 'flight_id', 'receipt_img_url'])

        db.session.commit()

        return jsonify(Buy_ins.query.get(id).serialize())
Exemple #6
0
def update_db(cursor,ip,sql_id):
	cur = cursor
	while True:
		try:
			cur.execute(update_table(ip,sql_id))
		except MySQLdb.OperationalError:
			db = MySQLdb.connect(host = DB_HOST,db = DB_NAME,user = USER,passwd = PASSWORD)
			cur = db.cursor()
			logger.info("Retrying to update ID: %s",sql_id)
			continue
		break
Exemple #7
0
def get_update_user(id):

    if id.isnumeric():
        user = Users.query.get(int(id))
    else:  # email
        user = Users.query.filter_by(email=id).first()

    if user is None:
        raise APIException('User not found with id: ' + id, 404)

    if request.method == 'GET':
        return jsonify(user.serialize())

    req = request.get_json()
    utils.check_params(req)

    utils.update_table(user, req, ignore=['email', 'password'])
    db.session.commit()

    return jsonify(user.serialize())
Exemple #8
0
def update_db(cursor, impressum, sql_id):
    cur = cursor
    while True:
        try:
            cur.execute(update_table(impressum, sql_id))
        except MySQLdb.OperationalError:
            db = MySQLdb.connect(host=DB_HOST,
                                 db=DB_NAME,
                                 user=USER,
                                 passwd=PASSWORD)
            cur = db.cursor()
            logger.info("Retrying to update ID: %s", sql_id)
            continue
        break
def update_profile(id):

    if id == 'me':
        id = str(get_jwt())['sub']

    if not id.isnumeric():
        raise APIException('Invalid id: ' + id, 400)

    prof = Profiles.query.get(int(id))
    if not prof:
        raise APIException('User not found', 404)

    body = request.get_json()
    check_params(body)

    update_table(prof,
                 body,
                 ignore=['profile_pic_url'],
                 action=actions.update_user)

    db.session.commit()

    return jsonify(prof.serialize())
Exemple #10
0
    def update_swap(user_id):

        # get sender user
        sender = Profiles.query.get(user_id)

        body = request.get_json()
        check_params(body, 'tournament_id', 'recipient_id')

        # get recipient user
        recipient = Profiles.query.get(body['recipient_id'])
        if recipient is None:
            raise APIException('Recipient user not found', 404)

        # get swap
        swap = Swaps.query.get((user_id, recipient.id, body['tournament_id']))
        counter_swap = Swaps.query.get(
            (recipient.id, user_id, body['tournament_id']))
        if swap is None or counter_swap is None:
            raise APIException('Swap not found', 404)

        if 'percentage' in body:

            percentage = abs(body['percentage'])
            counter = abs(body['counter_percentage']
                          ) if 'counter_percentage' in body else percentage

            sender_availability = sender.available_percentage(
                body['tournament_id'])
            if percentage > sender_availability:
                raise APIException((
                    'Swap percentage too large. You can not exceed 50% per tournament. '
                    f'You have available: {sender_availability}%'), 400)

            recipient_availability = recipient.available_percentage(
                body['tournament_id'])
            if counter > recipient_availability:
                raise APIException(
                    ('Swap percentage too large for recipient. '
                     f'He has available to swap: {recipient_availability}%'),
                    400)

            new_percentage = swap.percentage + percentage
            new_counter_percentage = counter_swap.percentage + counter

            # So it can be updated correctly with the update_table funcion
            body['percentage'] = new_percentage
            update_table(counter_swap, {'percentage': new_counter_percentage})

            send_email(type='swap_created',
                       to=sender.user.email,
                       data={
                           'percentage': new_percentage,
                           'counter_percentage': new_counter_percentage,
                           'recipient_firstname': recipient.first_name,
                           'recipient_lastname': recipient.last_name,
                           'recipient_email': recipient.user.email
                       })
            send_email(type='swap_created',
                       to=recipient.user.email,
                       data={
                           'percentage': new_counter_percentage,
                           'counter_percentage': new_percentage,
                           'recipient_firstname': sender.first_name,
                           'recipient_lastname': sender.last_name,
                           'recipient_email': sender.user.email
                       })

        update_table(swap,
                     body,
                     ignore=[
                         'tournament_id', 'recipient_id', 'paid',
                         'counter_percentage'
                     ])

        db.session.commit()

        return jsonify([swap.serialize(), counter_swap.serialize()])