def update_tokens(request): """ We need to record these updates to token states and provide a way to view this in the Gateway Interface. """ session = DBSession() data = simplejson.loads(request.body) if not 'device_id' in data: return Response('You must provide an device_id') device = session.query(Device)\ .filter_by(device_id=data['device_id']).first() if device: for i in data['tokens']: token = session.query(Token)\ .filter_by(token=i['token_id']).first() if token: circuit = session.query(Circuit)\ .filter_by(pin=i['account_id']).first() if circuit: job = AddCredit(token.value, circuit, token) session.add(job) token.state = 5 session.merge(token) session.flush() return Response('Ok') else: return Response('You must provide a valid device_id')
def check_meters(env): """ Function to check the state of a meter. Run via the command line python process_check_meters.py config.ini or via a cron job. """ sql_settings = env["registry"].settings["sqlalchemy.url"] # initialze the database and bind the models to the connection. # bad things happen if we don't call this. initialize_sql(sql_settings) session = DBSession() meters = session.query(Meter).all() for meter in meters: time_difference = datetime.now() - timedelta(hours=1) log.info("Check meter %s" % meter) for c in meter.get_circuits(): log.info("--> Check for circuit %s " % c) last_log = c.get_last_log() # if we have a log # often circuits are configured and we never hear from them. if last_log is not None: if time_difference > last_log.gateway_time: alert = UnresponsiveCircuit(datetime.now(), meter, c, last_log.gateway_time) session.add(alert) else: log.info("We have not heard from circuit %s" % c.id)
def make_tokens(request): """ A view function that allows the vendor application to request to new tokens. """ session = DBSession() batch = TokenBatch() session.add(batch) session.flush() data = simplejson.loads(request.body) if not 'device_id' in data: return Response('You must provide an device_id') else: device = session.query( Device).filter_by(device_id=data['device_id']).first() if device: if not 'tokens' in data: return Response('You must provide an amount of tokens') for group in data['tokens']: for i in range(0, group['count']): token = Token(Token.get_random(), batch=batch, value=group['denomination']) session.add(token) session.flush() return Response( [{'token_id': int(token.token), 'denomination': float(token.value)} for token in batch.getTokens()]) else: return Response('Not a valid device')
def new_user(request): """ Function to add a new user to the Gateway's auth system. Requires the user to provide a user name, password and email address. The group for the new user is give a list to select from. TODO, add support for org's once they are add to the models. """ session = DBSession() form = AddUserForm(request.POST) form.group.query = session.query(Group).all() if request.method == 'POST' and form.validate(): user = User(form.name.data, form.password.data, form.email.data, form.group.data) session.add(user) return HTTPFound(location=request.route_url('admin-users')) else: return {'form': form}