def test_relationships(self): house_title1 = "title_house1" house_title2 = "title_house1" district1 = "test_relation_district1" district2 = "test_relation_district2" area1 = "test_relation_area1" area2 = "test_relation_area2" community1 = "test_relation_c1" community2 = "test_relation_c2" d1 = District(name=district1) d2 = District(name=district2) db.session.add(d1) db.session.add(d2) db.session.commit() a1 = Area(name=area1, district=d1) a2 = Area(name=area2, district=d2) db.session.add(a1) db.session.add(a2) db.session.commit() c1 = Community(name=community1, area=a1) c2 = Community(name=community2, area=a2) db.session.add(c1) db.session.add(c2) db.session.commit() h1 = House(title=house_title1, community=c1) h2 = House(title=house_title2, community=c2) db.session.add(h1) db.session.add(h2) db.session.commit() self.assertTrue(District.query.filter_by(name=district1).first().areas.first()\ .communities.first().houses.first().title==house_title1) self.assertTrue(District.query.filter_by(name=district2).first().areas.first() \ .communities.first().houses.first().title == house_title2)
def test_add_community(self): area_name = "area_for_test" community_name = "test_community" area = Area(name=area_name) db.session.add(area) db.session.commit() community = Community(name=community_name, area=area) db.session.add(community) db.session.commit() aarea = Area().query.filter_by(name=area_name).first() self.assertTrue(aarea.communities.first().name == community_name)
def test_area_assign(self): u = User(username='******', email='*****@*****.**') a = Area(name='Talladega') db.session.add(u) db.session.add(a) db.session.commit() self.assertEqual(u.areas.all(), []) u.add_area(a) db.session.commit() self.assertTrue(u.is_assigned_area(a)) self.assertEqual(u.areas.count(), 1) self.assertEqual(u.areas.first().name, 'Talladega') u.rm_area(a) db.session.commit() self.assertFalse(u.is_assigned_area(a)) self.assertEqual(u.areas.count(), 0) a.add_user(u) db.session.commit() self.assertTrue(a.is_assigned_user(u)) self.assertEqual(a.users.count(), 1) self.assertEqual(a.users.first().username, 'john') a.rm_user(u) db.session.commit() self.assertFalse(a.is_assigned_user(u)) self.assertEqual(a.users.count(), 0)
def get_areas(): # get all the areas areas = Area.get_all() allSmallCells = SmallCell.get_all() results = [] for area in areas: smallcellInArea = [] for smallcell in allSmallCells: smallcellInArea.append(smallcell.serialise()) obj = { 'id': area.id, 'name': area.name, 'date_created': area.date_created, 'date_modified': area.date_modified, 'center_lat' : area.center_lat, 'center_lng' : area.center_lng, 'zoom' : area.zoom, 'geodata': area.geodata, 'smallcells' : smallcellInArea } results.append(obj) return make_response(jsonify({ 'list' : results })), 200
def create_area(): area_list = [ '金牛区', '温江区', '青羊区', '郫都区', '龙泉驿区', '双流县', '成华区', '武侯区', '高新区', ] for i in area_list: a = Area() a.name = i a.add_update() return '成功'
def get_detail_area_info(area_info_id): """获取地区全称""" area_list = [] area = Area.get_by_id(area_info_id) area_list.append(area.name) if area.pid: query = Area.select().where(Area.id == area.pid) area_1 = None if query.count() < 1 else query[0] if area_1: area_list.append(area_1.name) if area_1.pid: query = Area.select().where(Area.id == area_1.pid) area_2 = None if query.count() < 1 else query[0] if area_2: area_list.append(area_2.name) area_str = ''.join(reversed(area_list)) return area_str
def create_area(): form = CreateAreaForm(original_name=None) all_shifts = Shift.query.all() if form.validate_on_submit(): area = Area(name=form.name.data.capitalize()) shifts = conv(form.shifts.data) db.session.add(area) for shift in shifts: if shift.capitalize() in [s.name for s in all_shifts]: area.add_shift( Shift.query.filter_by(name=shift.capitalize()).first()) db.session.commit() flash('Area {} added'.format(area.name)) return redirect(url_for('main.create_area')) elif request.method == 'GET': form.shifts.data = ', '.join([s.name for s in all_shifts]) return render_template(tempdir + 'create_area.html', title='Create New Area', form=form)
def create_area(): form = AreaForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): area = Area( address=form.data["address"], city=form.data['city'], state=form.data['state'], zipcode=form.data['zipcode'], description=form.data['description'], clean=False, latitude=form.data['latitude'], longitude=form.data['longitude'], created_at=datetime.datetime.now(), user_id=current_user.id, ) db.session.add(area) db.session.commit() return {"area": area.to_dict()} return {"errors": "error with area form / post route"}
def test_add_aera_setter(self): district_name = "test_district" area_name = "testarea" district = District(name=district_name) db.session.add(district) db.session.commit() d = District.query.filter_by(name=district_name).first() area = Area(name=area_name, district=d) db.session.add(area) db.session.commit() dd = District.query.filter_by(name=district_name).first() self.assertTrue(dd.areas.first().name == area_name)
def insert_new_area(): data = request.json roles = [ 'area_code', 'area_name', 'area_size', 'longitude', 'latitude', 'city' ] if not validate(roles, data): abort(422) code = data['area_code'] name = data['area_name'] size = data['area_size'] longitude = data['longitude'] latitude = data['latitude'] city = data['city'] area = Area(code=code, name=name, size=size, longitude=longitude, latitude=latitude, city=city) area.save(True) return jsonify({"success": True, "area": area.format()})
class RequestForm(FlaskForm): id = HiddenField() title = StringField('Title', validators=[DataRequired()]) description = TextAreaField('Description', validators=[DataRequired()]) client = SelectField('Client', choices = Client.choices(), coerce = Client.coerce, validators=[DataRequired()]) priority = IntegerField('Priority', validators=[DataRequired()]) target_date = DateField('Target Date', format='%Y-%m-%d', validators=[DataRequired()]) product_area = SelectField('Product Area', choices = Area.choices(), coerce = Area.coerce, validators=[DataRequired()]) submit = SubmitField('Submit')
def add(): if request.method == 'POST': name = request.form['name'] if not name: flash('请输入区域名称') return redirect(request.referrer) area = Area(name=name) db.session.add(area) db.session.commit() flash('新增成功') return redirect('/admin/area') return render_template('admin/area/add.html')
def create_areas(): # get the access token name = request.data.get('name', '') geodata = request.data.get('geodata', '') center_lat = request.data.get('center_lat') center_lng = request.data.get('center_lng') zoom = request.data.get('zoom') area = Area(name=name, geodata=geodata, center_lat=center_lat, center_lng=center_lng, zoom=zoom) area.save() response = jsonify({ 'id': area.id, 'name': area.name, 'geodata': area.geodata, 'center_lat' : area.center_lat, 'center_lng' : area.center_lng, 'zoom' : area.zoom, 'date_created': area.date_created, 'date_modified': area.date_modified }) return make_response(response), 201
def area_add(): form = AreaForm() if form.validate_on_submit(): data = form.data area = Area.query.filter_by(name=data["name"]).count() if area == 1: flash("地区已存在", "error") return redirect(url_for("admin.area_add")) area = Area(name=data["name"], is_recommended=data["is_recommended"], introduction=data["introduction"]) db.session.add(area) db.session.commit() addOplog("添加地区" + data["name"]) flash("地区添加成功", "ok") return redirect(url_for("admin.area_add")) return render_template("admin/area_add.html", form=form)
def import_country(): global areas areas_import = ImportAreasForm() form = AddCountryForm() countries = Country.query.all() if areas_import.file.data: stream = areas_import.file.data.read().decode('utf-8-sig').splitlines() csv_import = csv.reader(stream, delimiter=';') areas = [(str(area[0]), str(area[1])) for area in csv_import] return render_template('import_country.html', areas_import=areas_import, form=form, areas=areas, countries=countries) if form.submit.data and form.validate(): areas_count = len(areas) name = form.name.data.strip() iso = form.iso.data.strip() existing_country = Country.query.filter_by(iso=iso).first() if existing_country: Area.query.filter_by(country_id=existing_country.id).delete() db.session.delete(existing_country) country = Country(name=name, iso=iso, areas_count=areas_count, imported_by=current_user) db.session.add(country) db.session.commit() for (name, iso) in areas: area = Area(name=name, iso=iso, country_id=country.id) db.session.add(area) db.session.commit() return redirect(url_for('import_country')) return render_template('import_country.html', areas_import=areas_import, form=form, countries=countries)
def insert_data(path): with open(path, 'r', encoding='utf-8') as json_file: json_file_text = json_file.read() json_text = json.loads(json_file_text) for keys in json_text.keys(): print(keys) province = Province(name=keys) db.session.add(province) db.session.commit() for key2 in json_text[keys].keys(): print('\t' + key2, end='\n\t\t') city = City(name=key2, province_id=province.id) db.session.add(city) db.session.commit() for index in json_text[keys][key2]: print(index, end=', ') area = Area(name=index, city_id=city.id) db.session.add(area) db.session.commit() print('\n')
def area_add(): """ 添加Chapter """ form = AreaForm() if form.validate_on_submit(): data = form.data # 接收数据 area = Area.query.filter_by(name=data["name"]).count() # 说明已经有这个Chapter了 if area == 1: flash("Chapters already exist", "err") return redirect(url_for("admin.area_add")) area = Area(name=data["name"], is_recommended=data['is_recommended'], introduction=data['introduction']) db.session.add(area) db.session.commit() addOplog("Add chapter " + data["name"]) # 添加日志 flash("Successful addition of chapter", "ok") return redirect(url_for("admin.area_add")) return render_template("admin/chapter_add.html", form=form)
def seed_areas(): area_1 = Area(address="White Plains", city="Ewa Beach", state="HI", zipcode="96706", description="White Plains Beach", clean=False, latitude=21.3035, longitude=-158.0452, created_at="2021-03-03 04:05:06", user_id="1") area_2 = Area(address="91-1450 Renton Rd", city="Ewa Beach", state="HI", zipcode="96706", description="Asing Community Park", clean=False, latitude=21.3508, longitude=-158.0253, created_at="2021-03-01 10:15:04", user_id="2") area_3 = Area(address="2199 Kalia Rd", city="Honolulu", state="HI", zipcode="96815", description="The stretch of Kalia road by Lewers Lounge", clean=False, latitude=21.2779, longitude=-157.8323, created_at="2021-02-26 12:00:00", user_id="4") area_4 = Area(address="Lower Saki Mana Rd", city="Waimea", state="HI", zipcode="96796", description="Polihale State Park", clean=False, latitude=22.0795, longitude=-159.7648, created_at="2021-04-01 07:03:12", user_id="1") area_5 = Area(address="200 W. Kawili St", city="Hilo", state="HI", zipcode="96720", description="Campus of University of Hawaii at Hilo", clean=False, latitude=19.7018462, longitude=-155.0791607, created_at="2021-04-01 07:27:44", user_id="3") area_6 = Area(address="Kahekili Hwy", city="Wailuku", state="HI", zipcode="96793", description="Waihee Ridge Trail", clean=False, latitude=20.9529, longitude=-156.5316, created_at="2021-04-12 13:45:29", user_id="4") area_7 = Area(address="6600 Makena Alanui", city="Kihei", state="HI", zipcode="96753", description="Makena Beach", clean=False, latitude=20.6316, longitude=-156.4448, created_at="2021-04-25 10:31:00", user_id="1") area_8 = Area(address="Kaahumanu Pl", city="Kailua-Kona", state="HI", zipcode="96740", description="Kailua Pier", clean=False, latitude=19.6400, longitude=-155.9969, created_at="2021-04-25 14:12:01", user_id="2") area_9 = Area(address="Mauna Kea Summit Rd", city="Hilo", state="HI", zipcode="96720", description="Lake Waiau Parking Lot", clean=False, latitude=19.810409, longitude=-155.46768, created_at="2021-04-27 08:28:55", user_id="5") db.session.add(area_1) db.session.add(area_2) db.session.add(area_3) db.session.add(area_4) db.session.add(area_5) db.session.add(area_6) db.session.add(area_7) db.session.add(area_8) db.session.add(area_9) db.session.commit()
f = Facility(name='Foinaven', risk_cut_off=302500, deferred_prod_cost=18) db.session.add(f) vessels_file = open('inputs/vessels.csv') vessels = csv.reader(vessels_file) for vessel in vessels: v = Vessel(name=vessel[0], abbr=vessel[1], day_rate=vessel[2], mob_time=vessel[3], facility=f) db.session.add(v) a = Area(name='DC1', equity_share=0.72, facility=f) db.session.add(a) c = Component(ident='P11', category='Tree', service_type='Production', area=a) db.session.add(a) consequences_file = open('inputs/consequences.csv') consequences = csv.reader(consequences_file) for consequence in consequences: cons = Consequence(name=consequence[0], mean_time_to_repair=consequence[10], replacement_cost=consequence[11], deferred_prod_rate=consequence[14], component=c, facility=f)
import os import csv from random import * from flask_api import FlaskAPI, status from flask_sqlalchemy import SQLAlchemy from app import db, create_app from app.models import Area, Site, SmallCell, LTESighting, Network create_app(config_name=os.getenv('APP_SETTINGS')).app_context().push() #delete everything. LTESighting.delete_all() SmallCell.delete_all() Site.delete_all() Area.delete_all() Network.delete_all() site1 = Site('Tesco Oxford Street') site1.save() site2 = Site('Tesco Regent Street') site2.save() if True: with open('data/networks.csv', 'r') as csvfile: reader = csv.reader(csvfile) for row in list(reader): network = (Network(row[0], row[5], row[2], row[7])) network.save() with open(