"""Register a custom URL map converters, available application wide. References ---------- https://coderwall.com/p/gnafxa/adding-custom-url-map-converters-to-flask-blueprint-objects """ def register_converter(state): state.app.url_map.converters[name or func.__name__] = func self.record_once(register_converter) Blueprint.add_app_url_map_converter = add_app_url_map_converter main = Blueprint('app', __name__) main.add_app_url_map_converter(RegexConverter, 'regex') # Wikidata item identifier matcher l_pattern = r'<regex(r"L[1-9]\d*"):lexeme>' L_PATTERN = re.compile(r'L[1-9]\d*') q_pattern = r'<regex(r"Q[1-9]\d*"):q>' q1_pattern = r'<regex(r"Q[1-9]\d*"):q1>' q2_pattern = r'<regex(r"Q[1-9]\d*"):q2>' Q_PATTERN = re.compile(r'Q[1-9]\d*') p_pattern = r'<regex(r"P[1-9]\d*"):p>' P_PATTERN = re.compile(r'P[1-9]\d*') # Wikidata item identifiers matcher qs_pattern = r'<regex(r"Q[1-9]\d*(?:[^0-9]+Q[1-9]\d*)*"):qs>'
Register a custom URL map converters, available application wide. :param name: the optional name of the filter, otherwise the function name will be used. """ def register_converter(state): state.app.url_map.converters[name or func.__name__] = func self.record_once(register_converter) # monkey-patch the Blueprint object to allow addition of URL map converters Blueprint.add_app_url_map_converter = add_app_url_map_converter # create the eyesopen Flask blueprint main = Blueprint('main', __name__) main.add_app_url_map_converter(ProjectHashConverter, 'slug') def is_project(string): project = Project.query.filter_by(slug=string).first() return project is not None @app.errorhandler(404) def handle_404(e): from flask import request route = request.path route = route.lstrip('/') if (current_user.current_project is None or route.startswith('_') or route.find('/') and is_project(route[:route.find('/')])):
from flask import Blueprint import converters # module containing the custom converter classes def add_app_url_map_converter(self, func, name=None): """ Register a custom URL map converters, available application wide. :param name: the optional name of the filter, otherwise the function name will be used. """ def register_converter(state): state.app.url_map.converters[name or func.__name__] = func self.record_once(register_converter) # monkey-patch the Blueprint object to allow addition of URL map converters Blueprint.add_app_url_map_converter = add_app_url_map_converter # create the eyesopen Flask blueprint bp = Blueprint('myblueprint', __name__) # register the URL map converters that are required bp.add_app_url_map_converter(converters.FooConverter, 'foo') bp.add_app_url_map_converter(converters.BarConverter, 'bar')
''' to map int list like 1,2 in URL ''' def register_converter(state): state.app.url_map.converters[name or func.__name__] = func self.record_once(register_converter) # create int_list converter for blueprint Blueprint.add_app_url_map_converter = add_app_url_map_converter mod = Blueprint('v1', __name__, url_prefix='/api/v1') # add converter to blueprint module, so it can be used in URL mod.add_app_url_map_converter(IntListConverter, 'int_list') @mod.route('/companies', methods=['GET']) @requires_auth def get_companies(): ''' Query companies, e.g. http://localhost:5000/api/v1/companies ''' cur = mysql.connection.cursor() cur.execute("SELECT `index`, company FROM companies;") row_headers = [x[0] for x in cur.description] rows = cur.fetchall() cur.close() json_data = [] for row in rows:
:param name: the optional name of the filter, otherwise the function name will be used. """ def register_converter(state): state.app.url_map.converters[name or func.__name__] = func self.record_once(register_converter) # monkey-patch the Blueprint object to allow addition of URL map converters Blueprint.add_app_url_map_converter = add_app_url_map_converter # create the eyesopen Flask blueprint main = Blueprint('main', __name__) main.add_app_url_map_converter(ProjectHashConverter, 'slug') def is_project(string): project = Project.query.filter_by(slug=string).first() return project is not None @app.errorhandler(404) def handle_404(e): from flask import request route = request.path route = route.lstrip('/') if (current_user.current_project is None or route.startswith('_') or route.find('/') and is_project(route[:route.find('/')])): return "404"