Esempio n. 1
0
def update(db, person):
    """Update the parameters of a person with id <person['id']>. Anything other than the id can change

    :param db: psycopg2 cursor database handle
    :param person: dict representing an existing person
    :return: dict representing the updated person
    """
    validate(person, expect_id=True)
    try:
        db.execute("""UPDATE people SET name = %(name)s, accommodation = %(accommodation)s, age_group = %(age_group)s,
                      deposit_paid = %(deposit_paid)s, fully_paid = %(fully_paid)s, comped = %(comped)s
                      WHERE id = %(id)s
                      RETURNING id, name, accommodation, age_group, deposit_paid, fully_paid, comped""",
                   person)
    except psycopg2.IntegrityError:
        raise utils.StoneretreatException('person name %(name)s already exists' % person)
    if db.rowcount != 1:
        raise utils.StoneretreatException('invalid person id provided')

    new_person = db.dictfetchone()
    new_person['group'] = None

    gid = person.get('group', None)
    groups.remove_member(db, new_person['id'])
    if gid is not None:
        groups.add_member(db, gid, new_person['id'])
        new_person['group'] = gid

    return new_person
Esempio n. 2
0
def new(db, person):
    """Add a new person to the year

    :param db: psycopg2 cursor database handle
    :param person: dict representing a new person
    :return: dict containing the info about the person, as well as the id generated by the db
    """
    validate(person, expect_id=False)
    try:
        db.execute("""INSERT INTO people (name, accommodation, age_group, comped, deposit_paid, fully_paid)
                      VALUES (%(name)s, %(accommodation)s, %(age_group)s, %(comped)s, %(deposit_paid)s, %(fully_paid)s)
                      RETURNING id, name, accommodation, age_group, comped, deposit_paid, fully_paid""",
                   person)
    except psycopg2.IntegrityError:
        raise utils.StoneretreatException('person %(name)s already exists' % person)

    new_person = db.dictfetchone()
    new_person['group'] = None

    gid = person.get('group', None)
    groups.remove_member(db, new_person['id'])
    if gid is not None:
        groups.add_member(db, gid, new_person['id'])
        new_person['group'] = gid

    return new_person
Esempio n. 3
0
def update(db, group):
    """Update the parameters of a group with id <group['id']>. Anything other than the id can change

    :param db: psycopg2 cursor database handle
    :param group: dict representing an existing group
    :return: dict representing the updated group
    """
    validate(group, expect_id=True)
    try:
        db.execute("""UPDATE groups SET name = %(name)s
                      WHERE id = %(id)s
                      RETURNING id, name""",
                   group)
    except psycopg2.IntegrityError:
        raise utils.StoneretreatException('group name %(name)s already exists' % group)
    if db.rowcount != 1:
        raise utils.StoneretreatException('invalid group id provided')

    updated_group = db.dictfetchone()
    updated_group['members'] = []
    if 'members' in group:
        db.execute("DELETE FROM group_people WHERE gid = %(id)s", group)
        for m in group['members']:
            try:
                db.execute("INSERT INTO group_people (gid, pid) VALUES (%s, %s)", (group['id'], m))
            except psycopg2.IntegrityError:
                raise utils.StoneretreatException('invalid person id %s for group %s' % (m, group['name']))
            updated_group['members'].append(m)

    return updated_group
Esempio n. 4
0
def new(db, group):
    """Add a new group to the year

    :param db: psycopg2 cursor database handle
    :param group: dict representing a new group
    :return: dict containing the info about the group, as well as the id generated by the db
    """
    validate(group, expect_id=False)
    try:
        db.execute("""INSERT INTO groups (name)
                      VALUES (%(name)s)
                      RETURNING id, name""",
                   group)
    except psycopg2.IntegrityError:
        raise utils.StoneretreatException('group %(name)s already exists' % group)

    new_group = db.dictfetchone()
    new_group['members'] = []
    if 'members' in group:
        for m in group['members']:
            try:
                db.execute("INSERT INTO group_people (gid, pid) VALUES (%s, %s)", (new_group['id'], m))
            except psycopg2.IntegrityError:
                raise utils.StoneretreatException('invalid person id %s for group %s' % (m, new_group['name']))
            new_group['members'].append(m)

    return new_group
Esempio n. 5
0
def add_charge():
    """API endpoint to add a charge to the database
    """
    charge = request.get_json(force=True, silent=True)
    try:
        validate(charge, expect_id=False)
        charge = g.tx.charges.new(charge)
    except utils.StoneretreatException as e:
        return make_error(str(e))

    return make_success(charge)
Esempio n. 6
0
def add_accommodation():
    """Add an accommodation to the database
    """
    accommodation = request.get_json(force=True, silent=True)
    try:
        validate(accommodation, expect_id=False)
        accommodation = g.tx.accommodations.new(accommodation)
    except utils.StoneretreatException as e:
        return make_error(str(e))

    return make_success(accommodation)
Esempio n. 7
0
def update_charge(id_):
    """API endpoint to modify a charge's info in the database
    """
    charge = request.get_json(force=True, silent=True)

    try:
        charge['id'] = validate_id(id_)
        validate(charge, expect_id=True)
        g.tx.charges.udpate(charge)
    except utils.StoneretreatException as e:
        return make_error(str(e))

    return make_success(charge)
Esempio n. 8
0
def update_accommodation(id_):
    """Update an accommodation in the database
    """
    accommodation = request.get_json(force=True, silent=True)
    if not accommodation:
        return make_error('No accommodation details provided')

    try:
        accommodation['id'] = validate_id(id_)
        validate(accommodation, expect_id=True)
        accommodation = g.tx.accommodations.update(accommodation)
    except utils.StoneretreatException as e:
        return make_error(str(e))

    return make_success(accommodation)
Esempio n. 9
0
def new(db, accommodation):
    """Add a new accommodation (tent, cabin, house, ...) to the year

    :param db: psycopg2 cursor database handle
    :param accommodation: dict representing a new accommodation
    :return: dict containing name, adult_price, child_price, as well as the id generated by the db
    """
    validate(accommodation, expect_id=False)
    try:
        db.execute("""INSERT INTO accommodations (name, adult_price, child_price)
                      VALUES (%(name)s, %(adult_price)s, %(child_price)s)
                      RETURNING id, name, adult_price, child_price""",
                   accommodation)
    except psycopg2.IntegrityError:
        raise utils.StoneretreatException('accommodation %(name)s already exists' % accommodation)
    return db.dictfetchone()
Esempio n. 10
0
def update(db, accommodation):
    """Update the parameters of the accommodation with id <id_>. Anything other than the id can change

    :param db: psycopg2 cursor database handle
    :param accommodation: Dict representing an existing accommodation
    :return: dict containing name, adult_price, child_price, as well as the id generated by the db
    """
    validate(accommodation, expect_id=True)
    try:
        db.execute("""UPDATE accommodations SET name = %(name)s, adult_price = %(adult_price)s,
                      child_price = %(child_price)s
                      WHERE id = %(id)s RETURNING id, name, adult_price, child_price""",
                   accommodation)
    except psycopg2.IntegrityError:
        raise utils.StoneretreatException('accommodation name %(name)s already taken' % accommodation)
    if db.rowcount != 1:
        raise utils.StoneretreatException('invalid accommodation id provided')
    return db.dictfetchone()
Esempio n. 11
0
from utils.validators import validate, make_string_object, make_number_object, make_array_object, make_boolean_object,\
	make_dict_object


create_base_endpoint_schema = validate(
	make_string_object("endpoint")
)

create_relative_endpoint_schema = validate(
	make_number_object("id"),
	make_string_object("endpoint"),
	make_string_object("method")
)

update_endpoint_schema_schema = validate(
	make_array_object("fields", _type="object", properties=dict(
		**make_string_object("type"),
		**make_string_object("key"),
		**make_string_object("value"),
		**make_boolean_object("is_array")
	)),
	make_number_object("id"),
	make_dict_object("meta_data", properties=dict(
		**make_number_object("num_pages"),
		**make_boolean_object("is_paginated")
	))
)

create_schema_schema = validate(
	make_string_object("name"),
	make_array_object("fields", _type="object", properties=dict(