def addUpdateCarriers(): gwid = request.form['gwid'] name = request.form['name'] ip_addr = request.form['ip_addr'] strip = request.form['strip'] prefix = request.form['prefix'] # Adding if len(gwid) <= 0: print(name) Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_CARRIER) Addr = Address(name, ip_addr, 32, settings.FLT_CARRIER) try: db.add(Gateway) db.add(Addr) db.commit() return displayCarriers() except: db.rollback() return render_template('error.html', type="db") # Updating else: db.query(Gateways).filter(Gateways.gwid == gwid).update( {'description': "name:" + name, 'address': ip_addr, 'strip': strip, 'pri_prefix': prefix}) # TODO: You will end up with multiple Address records -will fix Addr = Address(name, ip_addr, 32, settings.FLT_CARRIER) try: db.add(Addr) db.commit() return displayCarriers() except: db.rollback() return render_template('error.html', type="db")
def add_patient_answers(answers): """ Inserts new answers from a patient. :param answers: patient answers :param session: current session :return: patient with anonymous name """ session = init() new_answers = [] for key in answers.keys(): new_answers.append(answers[key]) new_entity = Entity(name=random_string(get_all_entities()), type="patient") new_patient = Patient(date_of_birth="11.11.11", entity=new_entity) new_address = Address(post_code="1337", entity=new_entity) session.add(new_address) session.add(new_patient) session.add(new_entity) for answer in answers: q = session.query(Question).filter(Question.id == answer).first() if q is not None: try: new_score = Score(entity=new_entity, question=q, score=answers[answer]) session.add(new_score) session.commit() except: session.rollback() print("Error: Could not save patient scores to database") return new_entity.name
def add_new_center(json_data): """ Inserts a new center to the database. All the questions answered is also added to the database with a default score of 50. :param json_data: json_data from the frontend application :return: None """ session = init() questions = get_all_questions()["questions"] new_entity = Entity(type="center", name=json_data["i"]) #new_entity = Entity(type="center", name="test1") new_center = Center(contact_person =json_data["l"] , phone_number=json_data["k"], entity=new_entity) #new_center = Center(contact_person="kontakt1", phone_number=12345678,entity=new_entity) new_address = Address(street_name="testname", street_number=51, post_code=json_data["postnummer"], entity=new_entity) #new_address = Address(street_name="testname", street_number=51, post_code=1234, entity=new_entity) session.add(new_entity) session.add(new_center) session.add(new_address) try: session.commit() except: session.rollback() print("Error submitting treatment center info") raise ValueError("Error submitting treatment center info") keys = json_data.keys() for key in keys: #Iterate all keys for question in questions: #Iterate all questions from database if isinstance(json_data[key], list): # if the current element is a list for data in json_data[key]: #Iterate list if there is multiple layers if data == question["id"]: try: q = session.query(Question).filter(Question.id == data).first() new_score = Score(entity=new_entity, question=q, score=50.0) session.add(new_score) session.commit() except: session.rollback() print("Error: Could not add center answer to database") elif key == ("id"+str(question["id"])) and not json_data[key] == 'false': #if leaf try: q = session.query(Question).filter(Question.id == question["id"]).first() new_score = Score(entity=new_entity, question=q, score=50.0) session.add(new_score) session.commit() except: session.rollback() print("Error: Could not add center answer to database") session.close()
def delete(aid): """ /api/addresses/{aid} Delete addresses identified by aid :param aid: new address information :return: the deleted address """ try: address = Address.get(Address.id == aid) except: abort(404, f"Email with id {aid} not found") address.delete_instance() return address.serialize()
def update(aid, address): """ /api/addresses/{aid} Update address identified by aid :param aid: id of the address to retrieve :param address: new address information :return: the new address object """ try: theAddress = Address.get(Address.id == aid) except DoesNotExist: abort(404, f"Address with id {aid} for contact {cid} not found") theAddress.full_address = address.get("address") theAddress.save() return theAddress.serialize()
def create(cid, address): """ /api/contacts/{cid}/addresses Create new contact address :param address: address information :return: matching address """ theAddress = address.get("address", None) if theAddress is None: abort(406, "Address field must not be empty") model = Address.create(**{'full_address': theAddress, 'contact_id': cid}) return make_response( f"{theAddress} successfully created for contact {cid}", 201)
def update_address(address: userAddress, update: bool = False): obj3 = Submit_data() user = obj3.get_userdata() for row in user: user_exist = True if userAddress.username == row[0] else False if user_exist: if update: up = Submit_data().update_address(address) return {"Message": "Address updates successfully"} else: up = Submit_data().address( Address( username=address.username, street=address.street, pincode=address.pincode, country=address.country, state=address.state, phone_no=address.phone_no, )) return {"Message": "Address added successfully"} else: return {"Message": "No user found"}
def addUpdatePBX(): gwid = request.form['gwid'] name = request.form['name'] ip_addr = request.form['ip_addr'] strip = request.form['strip'] prefix = request.form['prefix'] fusionpbx_db_enabled = request.form.get('fusionpbx_db_enabled', "0") fusionpbx_db_server = request.form['fusionpbx_db_server'] fusionpbx_db_username = request.form['fusionpbx_db_username'] fusionpbx_db_password = request.form['fusionpbx_db_password'] print("fusionpbx_db_enabled: %s", fusionpbx_db_enabled) # Adding if len(gwid) <= 0: print(name) Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_PBX) Addr = Address(name, ip_addr, 32, settings.FLT_PBX) db.add(Gateway) db.add(Addr) db.commit() db.refresh(Gateway) if fusionpbx_db_enabled == "1": print('*****This fusionpbx_db_server:' + fusionpbx_db_server) FusionPBXDB = dSIPFusionPBXDB(Gateway.gwid, fusionpbx_db_server, fusionpbx_db_username, fusionpbx_db_password, int(fusionpbx_db_enabled)) db.add(FusionPBXDB) db.commit() return displayPBX() # Updating else: db.query(Gateways).filter(Gateways.gwid == gwid).update({ 'description': "name:" + name, 'address': ip_addr, 'strip': strip, 'pri_prefix': prefix }) exists = db.query(dSIPFusionPBXDB).filter( dSIPFusionPBXDB.pbx_id == gwid).scalar() if exists: db.query(dSIPFusionPBXDB).filter( dSIPFusionPBXDB.pbx_id == gwid).update({ 'pbx_id': gwid, 'db_ip': fusionpbx_db_server, 'db_username': fusionpbx_db_username, 'db_password': fusionpbx_db_password, 'enabled': fusionpbx_db_enabled }) else: FusionPBXDB = dSIPFusionPBXDB(gwid, fusionpbx_db_server, fusionpbx_db_username, fusionpbx_db_password, int(fusionpbx_db_enabled)) db.add(FusionPBXDB) db.query(Address).filter(Address.tag == "name:" + name).update( {'ip_addr': ip_addr}) db.commit() return displayPBX()
domain = Domain(name=domain_name) db.session.add(domain) db.session.commit() # Get the address try: address = db.session.query(Address).\ filter(Address.local == local_part).\ filter(Address.domain_id == domain.id).\ first() except ValueError, e: return app.response_class(response='{"error": "%s"}' % e, mimetype='application/json', status=400) # Add the address if it doesn't exist if address is None: address = Address(local=local_part, domain_id=domain.id) db.session.add(address) db.session.commit() # Set the last received date address.date_last_received = datetime.utcnow() # Add one to the total number of received emails address.total_received += 1 # Add the current spam score to the total address.total_spam_score += float(request.form.get('X-Mailgun-Sscore')) db.session.add(address) db.session.commit()
def addDomain(domain, authtype, pbxs, notes, db): # Create the domain because we need the domain id PBXDomain = Domain(domain=domain, did=domain) db.add(PBXDomain) db.flush() # Check if list of PBX's if pbxs: pbx_list = re.split(' |,', pbxs) else: pbx_list = [] # If list is found if len(pbx_list) > 1 and authtype == "passthru": pbx_id = pbx_list[0] else: #Else Single value was submitted pbx_id = pbxs # Implement Passthru authentication to the first PBX on the list. # because Passthru Registration only works against one PBX if authtype == "passthru": PBXDomainAttr1 = DomainAttrs(did=domain, name='pbx_list', value=pbx_id) PBXDomainAttr2 = DomainAttrs(did=domain, name='pbx_type', value="0") PBXDomainAttr3 = DomainAttrs(did=domain, name='created_by', value="0") PBXDomainAttr4 = DomainAttrs(did=domain, name='domain_auth', value=authtype) PBXDomainAttr5 = DomainAttrs(did=domain, name='description', value="notes:{}".format(notes)) PBXDomainAttr6 = DomainAttrs(did=domain, name='pbx_ip', value=gatewayIdToIP(pbx_id, db)) db.add(PBXDomainAttr1) db.add(PBXDomainAttr2) db.add(PBXDomainAttr3) db.add(PBXDomainAttr4) db.add(PBXDomainAttr5) db.add(PBXDomainAttr6) # MSTeams Support elif authtype == "msteams": # Set of MS Teams Proxies msteams_dns_endpoints = settings.MSTEAMS_DNS_ENDPOINTS msteams_ip_endpoints = settings.MSTEAMS_IP_ENDPOINTS # Attributes to specify that the domain was created manually PBXDomainAttr1 = DomainAttrs(did=domain, name='pbx_list', value="{}".format( ",".join(msteams_dns_endpoints))) PBXDomainAttr2 = DomainAttrs(did=domain, name='pbx_type', value="0") PBXDomainAttr3 = DomainAttrs(did=domain, name='created_by', value="0") PBXDomainAttr4 = DomainAttrs(did=domain, name='domain_auth', value=authtype) PBXDomainAttr5 = DomainAttrs(did=domain, name='description', value="notes:{}".format(notes)) # Serial folking will be used to forward registration info to multiple PBX's PBXDomainAttr6 = DomainAttrs(did=domain, name='dispatcher_alg_reg', value="4") PBXDomainAttr7 = DomainAttrs(did=domain, name='dispatcher_alg_in', value="4") # Create entry in dispatcher and set dispatcher_set_id in domain_attrs PBXDomainAttr8 = DomainAttrs(did=domain, name='dispatcher_set_id', value=PBXDomain.id) # Use the default MS Teams SIP Proxy List if one isn't defined print("pbx list {}".format(pbx_list)) if len(pbx_list) == 0 or pbx_list[0] == '': for endpoint in msteams_dns_endpoints: dispatcher = Dispatcher( setid=PBXDomain.id, destination=endpoint, attrs="socket=tls:{}:5061;ping_from=sip:{}".format( settings.EXTERNAL_IP_ADDR, domain), description='msteam_endpoint:{}'.format(endpoint)) db.add(dispatcher) db.add(PBXDomainAttr1) db.add(PBXDomainAttr2) db.add(PBXDomainAttr3) db.add(PBXDomainAttr4) db.add(PBXDomainAttr5) db.add(PBXDomainAttr6) db.add(PBXDomainAttr7) db.add(PBXDomainAttr8) # Check if the MSTeams IP(s) that send us OPTION messages is in the the address table for endpoint_ip in msteams_ip_endpoints: address_query = db.query(Address).filter( Address.ip_addr == endpoint_ip).first() if address_query is None: Addr = Address("msteams-sbc", endpoint_ip, 32, settings.FLT_MSTEAMS, gwgroup=0) db.add(Addr) # Add Endpoint group to enable Inbound Mapping endpointGroup = {"name": domain, "endpoints": None} endpoints = [] for hostname in msteams_dns_endpoints: endpoints.append({ "hostname": hostname, "description": "msteams_endpoint", "maintmode": False }) endpointGroup['endpoints'] = endpoints addEndpointGroups(endpointGroup, "msteams", domain) # Implement external authentiction to either Realtime DB or Local Subscriber table else: # Attributes to specify that the domain was created manually PBXDomainAttr1 = DomainAttrs(did=domain, name='pbx_list', value=str(pbx_list)) PBXDomainAttr2 = DomainAttrs(did=domain, name='pbx_type', value="0") PBXDomainAttr3 = DomainAttrs(did=domain, name='created_by', value="0") PBXDomainAttr4 = DomainAttrs(did=domain, name='domain_auth', value=authtype) PBXDomainAttr5 = DomainAttrs(did=domain, name='description', value="notes:{}".format(notes)) # Serial folking will be used to forward registration info to multiple PBX's PBXDomainAttr6 = DomainAttrs(did=domain, name='dispatcher_alg_reg', value="8") PBXDomainAttr7 = DomainAttrs(did=domain, name='dispatcher_alg_in', value="4") # Create entry in dispatcher and set dispatcher_set_id in domain_attrs PBXDomainAttr8 = DomainAttrs(did=domain, name='dispatcher_set_id', value=PBXDomain.id) for pbx_id in pbx_list: dispatcher = Dispatcher(setid=PBXDomain.id, destination=gatewayIdToIP(pbx_id, db), description='pbx_id:{}'.format(pbx_id)) db.add(dispatcher) db.add(PBXDomainAttr1) db.add(PBXDomainAttr2) db.add(PBXDomainAttr3) db.add(PBXDomainAttr4) db.add(PBXDomainAttr5) db.add(PBXDomainAttr6) db.add(PBXDomainAttr7) db.add(PBXDomainAttr8)
def addUpdatePBX(): gwid = request.form['gwid'] name = request.form['name'] ip_addr = request.form['ip_addr'] strip = request.form['strip'] prefix = request.form['prefix'] fusionpbx_db_enabled = request.form.get('fusionpbx_db_enabled', "0") fusionpbx_db_server = request.form['fusionpbx_db_server'] fusionpbx_db_username = request.form['fusionpbx_db_username'] fusionpbx_db_password = request.form['fusionpbx_db_password'] print("fusionpbx_db_enabled: %s", fusionpbx_db_enabled) # Adding if len(gwid) <= 0: print(name) Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_PBX) Addr = Address(name, ip_addr, 32, settings.FLT_PBX) db.add(Gateway) db.add(Addr) db.commit() db.refresh(Gateway) if fusionpbx_db_enabled == "1": print('*****This fusionpbx_db_server:' + fusionpbx_db_server) FusionPBXDB = dSIPFusionPBXDB(Gateway.gwid, fusionpbx_db_server, fusionpbx_db_username, fusionpbx_db_password, int(fusionpbx_db_enabled)) #Add another Gateway that represents the External interface, which is 5080 by default name = name + " external" #Test ip address to see if it contains a port number index = ip_addr.find(":") if index > 0: ip_addr = ip_addr[:index - 1] ip_addr = ip_addr + ":5080" Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_PBX) else: ip_addr = ip_addr + ":5080" Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_PBX) db.add(Gateway) db.add(FusionPBXDB) db.commit() return displayPBX() # Updating else: db.query(Gateways).filter(Gateways.gwid == gwid).update({ 'description': "name:" + name, 'address': ip_addr, 'strip': strip, 'pri_prefix': prefix }) exists = db.query(dSIPFusionPBXDB).filter( dSIPFusionPBXDB.pbx_id == gwid).scalar() if exists: db.query(dSIPFusionPBXDB).filter( dSIPFusionPBXDB.pbx_id == gwid).update({ 'pbx_id': gwid, 'db_ip': fusionpbx_db_server, 'db_username': fusionpbx_db_username, 'db_password': fusionpbx_db_password, 'enabled': fusionpbx_db_enabled }) else: FusionPBXDB = dSIPFusionPBXDB(gwid, fusionpbx_db_server, fusionpbx_db_username, fusionpbx_db_password, int(fusionpbx_db_enabled)) db.add(FusionPBXDB) db.query(Address).filter(Address.tag == "name:" + name).update( {'ip_addr': ip_addr}) db.commit() return displayPBX()
def addUpdatePBX(): gwid = request.form['gwid'] name = request.form['name'] ip_addr = request.form.get("ip_addr", "") strip = request.form.get('strip', "0") prefix = request.form['prefix'] authtype = request.form['authtype'] fusionpbx_db_enabled = request.form.get('fusionpbx_db_enabled', "0") fusionpbx_db_server = request.form['fusionpbx_db_server'] fusionpbx_db_username = request.form['fusionpbx_db_username'] fusionpbx_db_password = request.form['fusionpbx_db_password'] pbx_username = request.form['pbx_username'] pbx_password = request.form['pbx_password'] print("fusionpbx_db_enabled: %s", fusionpbx_db_enabled) # Adding if len(gwid) <= 0: print(name) Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_PBX) db.add(Gateway) db.flush() if authtype == "ip": Addr = Address(name, ip_addr, 32, settings.FLT_PBX) db.add(Addr) else: Subscriber = Subscribers(pbx_username, pbx_password, settings.DOMAIN, Gateway.gwid) db.add(Subscriber) db.commit() db.refresh(Gateway) if fusionpbx_db_enabled == "1": print('*****This fusionpbx_db_server:' + fusionpbx_db_server) FusionPBXDB = dSIPFusionPBXDB(Gateway.gwid, fusionpbx_db_server, fusionpbx_db_username, fusionpbx_db_password, int(fusionpbx_db_enabled)) # Add another Gateway that represents the External interface, which is 5080 by default name = name + " external" # Test ip address to see if it contains a port number index = ip_addr.find(":") if index > 0: ip_addr = ip_addr[:index - 1] ip_addr = ip_addr + ":5080" Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_PBX) else: ip_addr = ip_addr + ":5080" Gateway = Gateways(name, ip_addr, strip, prefix, settings.FLT_PBX) db.add(Gateway) db.add(FusionPBXDB) db.commit() return displayPBX() # Updating else: # Update the Gateway table db.query(Gateways).filter(Gateways.gwid == gwid).update({ 'description': "name:" + name, 'address': ip_addr, 'strip': strip, 'pri_prefix': prefix }) # Update FusionPBX tables exists = db.query(dSIPFusionPBXDB).filter( dSIPFusionPBXDB.pbx_id == gwid).scalar() if exists: db.query(dSIPFusionPBXDB).filter( dSIPFusionPBXDB.pbx_id == gwid).update({ 'pbx_id': gwid, 'db_ip': fusionpbx_db_server, 'db_username': fusionpbx_db_username, 'db_password': fusionpbx_db_password, 'enabled': fusionpbx_db_enabled }) else: FusionPBXDB = dSIPFusionPBXDB(gwid, fusionpbx_db_server, fusionpbx_db_username, fusionpbx_db_password, int(fusionpbx_db_enabled)) db.add(FusionPBXDB) # Update Subscribers table auth credentials are being used if authtype == "userpwd": # Remove ip address from address table address = db.query(Address).filter(Address.tag == 'name:' + name) address.delete(synchronize_session=False) # Add the username and password that will be used for authentication # Check if the entry in the subscriber table already exists exists = db.query(Subscribers).filter( Subscribers.rpid == gwid).scalar() if exists: db.query(Subscribers).filter(Subscribers.rpid == gwid).update({ 'username': pbx_username, 'password': pbx_password, 'rpid': gwid }) else: Subscriber = Subscribers(pbx_username, pbx_password, settings.DOMAIN, gwid) db.add(Subscriber) else: #Update the Address table with the new ip address db.query(Address).filter(Address.tag == "name:" + name).update( {'ip_addr': ip_addr}) db.commit() return displayPBX()