def add_users(n): for i in range(n): user = User.add(f"FirstName{i}", f"LastName{i}") Address.add_address(user, f"street{i}", f"town{i}", f"postalCode{i}") EMailAddress.add_emailAddress(user, f"mail{i}") PhoneNumber.add_phone(user, f"phone{i}1") PhoneNumber.add_phone(user, f"phone{i}2") print(str(i))
def client_connect(ccname): env = get_env() vpn = DB.Vpn(env['HOSTNAME']) if not vpn in DB.vpns: register_vpn() user = DB.users[env['COMMON_NAME']] if not user or not user.exists(): user = create_user(vpn, env) addr = Address(env['TRUSTED_IP'], env['TRUSTED_PORT']) cluster = DB.Cluster(user, vpn.chal) connection = DB.Connection(addr) cluster.connections.add(connection) connection.update(addr=addr, vpn=vpn, user=user, cluster=cluster, alive=True) if CONNECTION_TTL is not None: connection.expire(alive=CONNECTION_TTL) logging.info("New connection from {cn}@{ip}:{port} on vlan {vlan}".format( cn=env['COMMON_NAME'], vlan=user.vlan, ip=addr.ip, port=addr.port)) with open(ccname, 'w') as ccfile: ccfile.write(CCTEMPLATE.format(vlan=user.vlan)) if env["PUSH_ADDR"] and env["PUSH_MASK"]: ccfile.write( IFCONFIG.format(addr=env["PUSH_ADDR"], mask=env["PUSH_MASK"]))
def client_disconnect(): env = get_env() client = '{TRUSTED_IP}:{TRUSTED_PORT}'.format(**env) connection = DB.Connection(Address(env['TRUSTED_IP'], env['TRUSTED_PORT'])) if connection.exists(): connection.delete('alive') else: logging.warn("Connection {} removed from Redis prior to disconnect".format(client))
def add_new_user(): if request.method == 'POST': first_name = request.form.get('first_name') last_name = request.form.get('last_name') address = request.form.get('address') city = request.form.get('city') new_address = Address(address=address, city=city) new_user = User(first_name=first_name, last_name=last_name, address=new_address) DBUser(new_user).add_entity() return redirect(url_for('/db.manage_data')) return render_template('add_user.html')
def update_user(): u_id = str(request.args.get('user_id')) first_name = str(request.args.get('first_name')) last_name = str(request.args.get('last_name')) address = str(request.args.get('address')) city = str(request.args.get('city')) if request.method == 'POST': iid = str(request.form.get('user_id')) first_name = str(request.form.get('first_name')) last_name = str(request.form.get('last_name')) address = str(request.form.get('address')) city = str(request.form.get('city')) DBUser(User(first_name=first_name, last_name=last_name, address=Address(address=address, city=city))).update_entity(int(iid)) return redirect(url_for('/db.manage_data')) return render_template('update_user.html', first_name=first_name, last_name=last_name, address=address, city=city, user_id=u_id)
if len(request.args) == 0: print("hello") return {"error": "No params supplied"}, 404 # get user id from params user_id = request.args.get('user_id', None) # if user_id params did not come, return error if not user_id: return {"error": "Wrong params"}, 500 # list all addresses stored for this user addrs_in_portfolio = db.list({"user_id": 1}) addrs_in_portfolio = [i.get("address") for i in addrs_in_portfolio] # get fresh data from API # wallet = Wallet() # portfolio_data = wallet.get_wallets(addrs_in_portfolio) ## mock data portfolio_data = json.load(open('data.json')) print(1) return portfolio_data, 200 if __name__ == '__main__': db = Address() app.run()
def generate_test_user(): generated_cpf = "".join([str(randint(0, 9)) for _ in range(11)]) user = User.where(User.document == generated_cpf).first() while user is not None: generated_cpf = "".join([str(randint(0, 9)) for _ in range(11)]) user = User.where(User.document == generated_cpf).first() user = User( document=generated_cpf, first_name="Test User", last_name=generated_cpf, email=f"test_{generated_cpf}@email.com", phone_number=f"55119{generated_cpf[0:8]}") generated_cep = "".join([str(randint(0, 9)) for _ in range(8)]) address = Address.where(Address.postal_code == generated_cep).first() while address is not None: generated_cep = "".join([str(randint(0, 9)) for _ in range(8)]) address = Address.where(Address.postal_code == generated_cep).first() address = Address( postal_code=generated_cep, country="BR", state="SP", city="São Paulo", neighbourhood=f"Bairro {generated_cep}", street=f"Rua {generated_cep}" ) power_supply = PowerSupply( status="up", description=f"Power Supply {generated_cep}") occurrence = Occurrence(power_supply=power_supply, category=("power_outage" if random() < 0.5 else "maintenance"), description=f"Occurance 1", status=("done" if power_supply.is_up( ) else "in_progress" if random() < 0.5 else "cancelled"), start_time=datetime.now()-timedelta(weeks=2), end_time=(datetime.now()-timedelta(weeks=1) if power_supply.is_up() else None), estimated_end_time=(datetime.now()-timedelta(hours=1) if random() < 0.5 else None)) today = date.today() day = randint(1, 15) month = today.month year = today.year due_dates = [] for _ in range(24): due_dates.append(datetime.strptime(f"{day}-{month}-{year}", "%d-%m-%Y").date()) month = month-1 if month > 1 else 12 year = year-1 if month == 12 else year bills = [Bill(user=user, value=round(uniform(0.0, 500.0), 2), paid=random() < 0.5, due_date=due_date) for due_date in due_dates] power_supply.address = address user.address = address session.add(address) session.add(user) session.add(power_supply) session.add(occurrence) session.add_all(bills) session.commit() return { "cpf": generated_cpf, "cep": generated_cep, "oldest_bill": due_dates[-1], "most_recent_bill": due_dates[0] }
def build_address(postal_code): address = Address(country="BR") postal_info = get_postal_info(postal_code) address.from_dict(postal_info) return address