def test_none(): app, db, admin = setup() GeoModel = create_models(db) db.create_all() GeoModel.query.delete() db.session.commit() view = ModelView(GeoModel, db.session) admin.add_view(view) # Make some test clients client = app.test_client() rv = client.post('/admin/geomodel/new/', data={ "name": "test1", }) assert rv.status_code == 302 model = db.session.query(GeoModel).first() url = '/admin/geomodel/edit/?id=%s' % model.id rv = client.get(url) assert rv.status_code == 200 data = rv.data.decode('utf-8') assert (r' name="point"></textarea>' in data or ' name="point">\n</textarea>' in data or ' name="point">\r\n</textarea>' in data)
def test_mapbox_fix_point_coordinates(): app, db, admin = setup() app.config['MAPBOX_FIX_COORDINATES_ORDER'] = True GeoModel = create_models(db) db.create_all() GeoModel.query.delete() db.session.commit() view = ModelView(GeoModel, db.session) admin.add_view(view) # Make some test clients client = app.test_client() rv = client.post( '/admin/geomodel/new/', data={ "name": "test1", "point": '{"type": "Point", "coordinates": [125.8, 10.0]}', "line": '{"type": "LineString", "coordinates": [[50.2345, 94.2], [50.21, 94.87]]}', "polygon": '{"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]}', "multi": '{"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}', }) model = db.session.query(GeoModel).first() # Notice how the coordinates are reversed here, i.e. longitude first which # is the way it's stored in PostGIS columns. eq_(list(to_shape(model.point).coords), [(10.0, 125.8)]) eq_(list(to_shape(model.line).coords), [(94.2, 50.2345), (94.87, 50.21)]) eq_(list(to_shape(model.polygon).exterior.coords), [(0.0, 100.0), (0.0, 101.0), (1.0, 101.0), (1.0, 100.0), (0.0, 100.0)]) eq_(list(to_shape(model.multi).geoms[0].coords), [(0.0, 100.0)]) eq_(list(to_shape(model.multi).geoms[1].coords), [(1.0, 101.0)]) rv = client.get('/admin/geomodel/') eq_(rv.status_code, 200) html = rv.data.decode('utf-8') pattern = r'(.|\n)+({.*"type": ?"Point".*})</textarea>(.|\n)+' group = re.match(pattern, html).group(2) p = json.loads(group) # Reversed order again, so that it's parsed correctly by leaflet eq_(p['coordinates'][0], 10.0) eq_(p['coordinates'][1], 125.8)
def register_routes(app): """ 在 flask 中,模块化路由的功能由 蓝图(Blueprints)提供 蓝图可以拥有自己的静态资源路径、模板路径(现在还没涉及) 用法如下 """ # 注册蓝图 # 有一个 url_prefix 可以用来给蓝图中的每个路由加一个前缀 app.register_blueprint(index_routes) app.register_blueprint(topic_routes, url_prefix='/topic') app.register_blueprint(reply_routes, url_prefix='/reply') app.register_blueprint(board_routes, url_prefix='/board') app.register_blueprint(mail_routes, url_prefix='/mail') app.register_blueprint(chat_routes, url_prefix='/chatroom') app.template_filter()(count) app.template_filter()(format_time) app.errorhandler(404)(not_found) admin = Admin(app, name='BBS', template_mode='bootstrap3') admin.add_view(ModelView(User, db.session)) admin.add_view(ModelView(Topic, db.session)) admin.add_view(ModelView(Reply, db.session))
def test_none(): app, db, admin = setup() GeoModel = create_models(db) db.create_all() GeoModel.query.delete() db.session.commit() view = ModelView(GeoModel, db.session) admin.add_view(view) # Make some test clients client = app.test_client() rv = client.post("/admin/geomodel/new/", data={"name": "test1"}) eq_(rv.status_code, 302) model = db.session.query(GeoModel).first() url = "/admin/geomodel/edit/?id=%s" % model.id rv = client.get(url) eq_(rv.status_code, 200) data = rv.data.decode("utf-8") ok_(r' name="point"></textarea>' in data)
"""Flask admin.py file.""" from os import environ from flask_admin import Admin from flask_admin.contrib.geoa import ModelView from .models import db, ATM, User, ATMUpdateLog admin = Admin(name='atmfinda', url=environ.get('ADMIN_URL', '/admin'), template_mode='bootstrap3') admin.add_view(ModelView(ATM, db.session)) admin.add_view(ModelView(User, db.session)) admin.add_view(ModelView(ATMUpdateLog, db.session))
def test_model(): app, db, admin = setup() GeoModel = create_models(db) db.create_all() GeoModel.query.delete() db.session.commit() view = ModelView(GeoModel, db.session) admin.add_view(view) eq_(view.model, GeoModel) eq_(view._primary_key, 'id') # Verify form eq_(view._create_form_class.point.field_class, GeoJSONField) eq_(view._create_form_class.point.kwargs['geometry_type'], "POINT") eq_(view._create_form_class.line.field_class, GeoJSONField) eq_(view._create_form_class.line.kwargs['geometry_type'], "LINESTRING") eq_(view._create_form_class.polygon.field_class, GeoJSONField) eq_(view._create_form_class.polygon.kwargs['geometry_type'], "POLYGON") eq_(view._create_form_class.multi.field_class, GeoJSONField) eq_(view._create_form_class.multi.kwargs['geometry_type'], "MULTIPOINT") # Make some test clients client = app.test_client() rv = client.get('/admin/geomodel/') eq_(rv.status_code, 200) rv = client.get('/admin/geomodel/new/') eq_(rv.status_code, 200) rv = client.post( '/admin/geomodel/new/', data={ "name": "test1", "point": '{"type": "Point", "coordinates": [125.8, 10.0]}', "line": '{"type": "LineString", "coordinates": [[50.2345, 94.2], [50.21, 94.87]]}', "polygon": '{"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]}', "multi": '{"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}', }) eq_(rv.status_code, 302) model = db.session.query(GeoModel).first() eq_(model.name, "test1") eq_(to_shape(model.point).geom_type, "Point") eq_(list(to_shape(model.point).coords), [(125.8, 10.0)]) eq_(to_shape(model.line).geom_type, "LineString") eq_(list(to_shape(model.line).coords), [(50.2345, 94.2), (50.21, 94.87)]) eq_(to_shape(model.polygon).geom_type, "Polygon") eq_(list(to_shape(model.polygon).exterior.coords), [(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)]) eq_(to_shape(model.multi).geom_type, "MultiPoint") eq_(len(to_shape(model.multi).geoms), 2) eq_(list(to_shape(model.multi).geoms[0].coords), [(100.0, 0.0)]) eq_(list(to_shape(model.multi).geoms[1].coords), [(101.0, 1.0)]) rv = client.get('/admin/geomodel/') eq_(rv.status_code, 200) html = rv.data.decode('utf-8') pattern = r'(.|\n)+({.*"type": ?"Point".*})</textarea>(.|\n)+' group = re.match(pattern, html).group(2) p = json.loads(group) eq_(p['coordinates'][0], 125.8) eq_(p['coordinates'][1], 10.0) url = '/admin/geomodel/edit/?id=%s' % model.id rv = client.get(url) eq_(rv.status_code, 200) # rv = client.post(url, data={ # "name": "edited", # "point": '{"type": "Point", "coordinates": [99.9, 10.5]}', # "line": '', # set to NULL in the database # }) # eq_(rv.status_code, 302) # # model = db.session.query(GeoModel).first() # eq_(model.name, "edited") # eq_(to_shape(model.point).geom_type, "Point") # eq_(list(to_shape(model.point).coords), [(99.9, 10.5)]) # eq_(to_shape(model.line), None) # eq_(to_shape(model.polygon).geom_type, "Polygon") # eq_(list(to_shape(model.polygon).exterior.coords), # [(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)]) # eq_(to_shape(model.multi).geom_type, "MultiPoint") # eq_(len(to_shape(model.multi).geoms), 2) # eq_(list(to_shape(model.multi).geoms[0].coords), [(100.0, 0.0)]) # eq_(list(to_shape(model.multi).geoms[1].coords), [(101.0, 1.0)]) url = '/admin/geomodel/delete/?id=%s' % model.id rv = client.post(url) eq_(rv.status_code, 302) eq_(db.session.query(GeoModel).count(), 0)
def test_model(): app, db, admin = setup() GeoModel = create_models(db) db.create_all() GeoModel.query.delete() db.session.commit() view = ModelView(GeoModel, db.session) admin.add_view(view) assert view.model == GeoModel assert view._primary_key == 'id' # Verify form assert view._create_form_class.point.field_class == GeoJSONField assert view._create_form_class.point.kwargs['geometry_type'] == "POINT" assert view._create_form_class.line.field_class == GeoJSONField assert view._create_form_class.line.kwargs['geometry_type'] == "LINESTRING" assert view._create_form_class.polygon.field_class == GeoJSONField assert view._create_form_class.polygon.kwargs['geometry_type'] == "POLYGON" assert view._create_form_class.multi.field_class == GeoJSONField assert view._create_form_class.multi.kwargs['geometry_type'] == "MULTIPOINT" # Make some test clients client = app.test_client() rv = client.get('/admin/geomodel/') assert rv.status_code == 200 rv = client.get('/admin/geomodel/new/') assert rv.status_code == 200 rv = client.post('/admin/geomodel/new/', data={ "name": "test1", "point": '{"type": "Point", "coordinates": [125.8, 10.0]}', "line": '{"type": "LineString", "coordinates": [[50.2345, 94.2], [50.21, 94.87]]}', "polygon": ('{"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0],' ' [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]}'), "multi": '{"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}', }) assert rv.status_code == 302 model = db.session.query(GeoModel).first() assert model.name == "test1" assert to_shape(model.point).geom_type == "Point" assert list(to_shape(model.point).coords) == [(125.8, 10.0,)] assert to_shape(model.line).geom_type == "LineString" assert list(to_shape(model.line).coords) == [(50.2345, 94.2, (50.21, 94.87)]) assert to_shape(model.polygon).geom_type == "Polygon" eq_(list(to_shape(model.polygon).exterior.coords), [(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)]) assert to_shape(model.multi).geom_type == "MultiPoint" assert len(to_shape(model.multi).geoms) == 2 assert list(to_shape(model.multi).geoms[0].coords) == [(100.0, 0.0]) assert list(to_shape(model.multi).geoms[1].coords) == [(101.0, 1.0]) rv = client.get('/admin/geomodel/') assert rv.status_code == 200 html = rv.data.decode('utf-8') pattern = r'(.|\n)+({.*"type": ?"Point".*})</textarea>(.|\n)+' group = re.match(pattern, html).group(2) p = json.loads(group) assert p['coordinates'][0] == 125.8 assert p['coordinates'][1] == 10.0 url = '/admin/geomodel/edit/?id=%s' % model.id rv = client.get(url) assert rv.status_code == 200 data = rv.data.decode('utf-8') assert (r'{"type":"MultiPoint","coordinates":[[100,0],[101,1]]}</textarea>' in data or r'{"type":"MultiPoint","coordinates":[[100,0],[101,1]]}' in data) # rv = client.post(url, data={ # "name": "edited", # "point": '{"type": "Point", "coordinates": [99.9, 10.5]}', # "line": '', # set to NULL in the database # }) # assert rv.status_code == 302 # # model = db.session.query(GeoModel).first() # assert model.name == "edited" # assert to_shape(model.point).geom_type == "Point" # assert list(to_shape(model.point).coords) == [(99.9, 10.5]) # assert to_shape(model.line) == None # assert to_shape(model.polygon).geom_type == "Polygon" # eq_(list(to_shape(model.polygon).exterior.coords), # [(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)]) # assert to_shape(model.multi).geom_type == "MultiPoint" # assert len(to_shape(model.multi).geoms) == 2 # assert list(to_shape(model.multi).geoms[0].coords) == [(100.0, 0.0]) # assert list(to_shape(model.multi).geoms[1].coords) == [(101.0, 1.0]) url = '/admin/geomodel/delete/?id=%s' % model.id rv = client.post(url) assert rv.status_code == 302 assert db.session.query(GeoModel).count() == 0
id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) point = db.Column(Geometry("MULTILINESTRING")) # Flask views @app.route('/') def index(): return '<a href="/admin/">Click me to get to Admin!</a>' # Create admin admin = admin.Admin(app, name='Example: GeoAlchemy', template_mode='bootstrap3') # Add views admin.add_view(ModelView(Point, db.session, category='Points')) admin.add_view(ModelView(MultiPoint, db.session, category='Points')) admin.add_view(ModelView(Polygon, db.session, category='Polygons')) admin.add_view(ModelView(MultiPolygon, db.session, category='Polygons')) admin.add_view(ModelView(LineString, db.session, category='Lines')) admin.add_view(ModelView(MultiLineString, db.session, category='Lines')) if __name__ == '__main__': db.create_all() # Start app app.run(debug=True)
from application import app, admin, db from application.models import Count, Router, Campus, RouterAlias from flask_admin.contrib.geoa import ModelView admin.add_view(ModelView(Count, db.session)) admin.add_view(ModelView(Router, db.session)) admin.add_view(ModelView(Campus, db.session)) admin.add_view(ModelView(RouterAlias, db.session))
class OrdersModelView(ModelView): column_list = [ 'ship_name', 'order_date', 'ship_address', 'notes', 'employees.first_name' ] can_view_details = True class ProductsModelView(ModelView): column_list = [ 'product_code', 'product_name', 'quantity_per_unit', 'standard_cost' ] can_view_details = True class PurchaseOrdersModelView(ModelView): column_list = ['id', 'submitted_date', 'status_id', 'notes', 'approved_by'] can_view_details = True # Administration Site admin = Admin(app, name='Web Office', template_mode='bootstrap3') admin.add_view(CustomersModelView(Customers, session, 'Clientes')) admin.add_view(EmployeesModelView(Employees, session, 'Empleados')) admin.add_view(OrdersModelView(Orders, session, 'Ordenes')) admin.add_view(ProductsModelView(Products, session, 'Productos')) admin.add_view(ModelView(Privileges, session, 'Provilegios')) admin.add_view( PurchaseOrdersModelView(PurchaseOrders, session, 'Ordenes Realizadas'))
id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) point = db.Column(Geometry("MULTILINESTRING")) # Flask views @app.route("/") def index(): return '<a href="/admin/">Click me to get to Admin!</a>' # Create admin admin = admin.Admin(app, name="Example: GeoAlchemy", template_mode="bootstrap3") # Add views admin.add_view(ModelView(Point, db.session, category="Points")) admin.add_view(ModelView(MultiPoint, db.session, category="Points")) admin.add_view(ModelView(Polygon, db.session, category="Polygons")) admin.add_view(ModelView(MultiPolygon, db.session, category="Polygons")) admin.add_view(ModelView(LineString, db.session, category="Lines")) admin.add_view(ModelView(MultiLineString, db.session, category="Lines")) if __name__ == "__main__": db.create_all() # Start app app.run(debug=True)