def add_form(session, name, max_score, order=0): """ Adds a new Form to the database. """ order = _get_unique_order(Form, order) new_form = Form(name, max_score, order) session.add(new_form) return new_form
def store_registration(mailer, session, data): """ Stores a registration to the database. The *data* dictionary contains the following items (all strings): * group_name * contact_name * email * tel * time: The time of day that the group begins the "run". * comments * num_vegetarians * num_participants * user_id: The ID of the user that made the registration """ qry = Group.query.filter_by(name=data['group_name']) check = qry.first() if check: raise ValueError('Group {} already registered'.format( data['group_name'])) else: key = os.urandom(50).encode('base64').replace('/', '')[0:20] # Regenerate keys if needed (just in case to avoid duplicates). qry = Group.query.filter_by(confirmation_key=key) check_key = qry.first() while check_key: key = os.urandom(50).encode('base64').replace('/', '')[0:20] qry = Group.query.filter_by(confirmation_key=key) check_key = qry.first() new_grp = Group(data['group_name'], data['contact_name'], data['tel'], None, data['time'], data['comments'], key, data['user_id']) order = start_time_to_order(data['time']) new_grp.order = _get_unique_order(Group, order) new_grp.num_vegetarians = int(data['num_vegetarians']) new_grp.num_participants = int(data['num_participants']) new_grp.email = data['email'] session.add(new_grp) try: session.flush() except IntegrityError: session.rollback() LOG.exception('Error while adding the new group {0}'.format( data['group_name'])) raise ValueError('Error while adding the new group {0}'.format( data['group_name'])) return key
def add_station(stat_name, contact, phone, order, session): """ Creates a new :py:class:`Station` in the database. """ order = _get_unique_order(Station, order) new_station = Station(stat_name, contact, phone) new_station.order = order session.add(new_station) return "Station {0} added. Contact: {1} / {2}".format( stat_name, contact, phone)