class WardData(object): def __init__(self): self.ward = Ward() def fetch_ward(self, code): if not isinstance(code, int): return {"error": "Invalid ward code found" + str(code)} return return_ward(self.ward.get_one(code)) def fetch_all_wards(self): return [return_ward(ward) for ward in self.ward.get_all()] def add_new_ward(self, name, constituency_code): if not isinstance(name, str): return {"error": "Invalid ward name found: " + str(name)} elif not isinstance(constituency_code, int): return { "error": "Invalid constituency code found: " + str(constituency_code) } try: newWard = Ward(name=name, constituency_code=constituency_code).save() return return_ward(newWard) except: raise return {"error": "Creating new ward failed"} def update_ward(self, code, name, constituency_code): if not isinstance(code, int): return {"error": "Invalid ward code found: " + str(code)} elif not isinstance(name, str): return {"error": "Invalid ward name found: " + str(name)} elif not isinstance(constituency_code, int): return { "error": "Invalid constituency code found: " + str(constituency_code) } ward = self.ward.get_one(code) ward.name = name if name != "" else ward.name ward.constituency_code = name if name != "" else ward.constituency_code try: newWard = ward.update() return return_ward(newWard) except: return {"error": "Updating ward failed"} def delete_ward(self, code): if not isinstance(code, int): return {"error": "Invalid ward code found: " + str(code)} ward = self.ward.get_one(code) ward.delete()
def import_ward(ward_file): option_type_file = get_csv_file("wards") with open(option_type_file, 'r') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: datarow = Ward(name=row[0], contact=row[1]) db.session.add(datarow) db.session.commit
def setUp(self): db.create_all(app=self.app_t) self.http = self.create_app().test_client() # Create a new County county = County(name="Nakuru").save() county = County(name="Turkana").save() # create constituencies associated with Nakuru constituency1 = Constituency(name="Kongowea", county_code=county.code) constituency2 = Constituency(name="Nairobi", county_code=county.code) # create wards associated with Kongowea and Nairobi Ward(name="xxxxx", constituency_code=constituency1.code) Ward(name="yyyyyy", constituency_code=constituency1.code) Ward(name="Hurama", constituency_code=constituency2.code) Ward(name="Pangani", constituency_code=constituency2.code)
def return_constituency(constituency): if isinstance(constituency, Constituency) and constituency is not None: wards = Ward().get_by_constituency_code(constituency.code) return { "code": constituency.code, "name": constituency.name, "wards": [return_ward(ward) for ward in wards] } else: return {"code": "", "name": "", "wards": "[]"}
def create(): ''' create a new ward ''' form = CreateWard() if form.validate_on_submit: ward = Ward.query.filter_by(name=form.name.data).first() if ward is None: ward = Ward(name=form.name.data) ward.save() flash('Successfully created a new ward') return redirect(url_for('home.dashboard')) else: flash('Ward already exists') return redirect(url_for('ward.create')) else: return redirect(url_for('ward.create')) return render_template('ward/create.html', form=form)
def create_ward(): ''' creates a new Ward ''' form = CreateWardForm() if request.method == 'POST': if form.validate_on_submit: ward = Ward.query.filter_by(name = form.name.data).first() if ward is None: ward = Ward( name = form.name.data, beds = form.beds.data, nurse_id = form.nurse_id.data.id ) ward.save() flash('Successfully created a new ward') return redirect(url_for('admin.wards')) else: flash('Ward already exists') return render_template('admin/create-ward.html', title='Wards', form=form)
def add_new_ward(self, name, constituency_code): if not isinstance(name, str): return {"error": "Invalid ward name found: " + str(name)} elif not isinstance(constituency_code, int): return { "error": "Invalid constituency code found: " + str(constituency_code) } try: newWard = Ward(name=name, constituency_code=constituency_code).save() return return_ward(newWard) except: raise return {"error": "Creating new ward failed"}
def __init__(self): self.ward = Ward()