Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. @app.route('/double/<int:number>')
  2. def double(number):
  3. return '%d' % (number * 2)
  4.  
  5. class UserConverter(BaseConverter):
  6. """Converts Users for flask URLs."""
  7.  
  8. def to_python(self, value):
  9. """Called to convert a `value` to its python equivalent.
  10.  
  11. Here, we convert `value` to a User.
  12. """
  13. # Anytime an invalid value is provided, raise ValidationError. Flask
  14. # will catch this, and continue searching the other routes. First,
  15. # check that value is an integer, if not there is no way it could be
  16. # a user.
  17. if not value.isdigit():
  18. raise ValidationError()
  19. user_id = int(value)
  20.  
  21. # Look up the user in the database.
  22. if user_id not in _database:
  23. raise ValidationError()
  24.  
  25. # Otherwise, return the user.
  26. return _database[user_id]
  27.  
  28. def to_url(self, value):
  29. """Called to convert a `value` to its `url` equivalent.
  30.  
  31. Here we convert `value`, the User object to an integer - the user id.
  32. """
  33. return str(value.id)
  34.  
  35. app.url_map.converters['user'] = UserConverter
  36.  
  37. url_for('find_user', user=user)
  38.  
  39. from flask import Flask, url_for
  40. from werkzeug.routing import BaseConverter, ValidationError
  41.  
  42.  
  43. class User:
  44.  
  45. def __init__(self, id, name):
  46. self.id = id
  47. self.name = name
  48.  
  49.  
  50. class UserConverter(BaseConverter):
  51. """Converts Users for flask URLs."""
  52.  
  53. def to_python(self, value):
  54. """Called to convert a `value` to its python equivalent.
  55.  
  56. Here, we convert `value` to a User.
  57. """
  58. # Anytime an invalid value is provided, raise ValidationError. Flask
  59. # will catch this, and continue searching the other routes. First,
  60. # check that value is an integer, if not there is no way it could be
  61. # a user.
  62. if not value.isdigit():
  63. raise ValidationError()
  64. user_id = int(value)
  65.  
  66. # Look up the user in the database.
  67. if user_id not in _database:
  68. raise ValidationError()
  69.  
  70. # Otherwise, return the user.
  71. return _database[user_id]
  72.  
  73. def to_url(self, value):
  74. """Called to convert a `value` to its `url` equivalent.
  75.  
  76. Here we convert `value`, the User object to an integer - the user id.
  77. """
  78. return str(value.id)
  79.  
  80.  
  81. # Create a `database` of users.
  82. _database = {
  83. 1: User(1, 'Bob'),
  84. 2: User(2, 'Jim'),
  85. 3: User(3, 'Ben')
  86. }
  87.  
  88. app = Flask(__name__)
  89. app.url_map.converters['user'] = UserConverter
  90.  
  91. @app.route('/find/<user:user>')
  92. def find_user(user):
  93. return "User: %s" % user.name
  94.  
  95. @app.route('/find/<user:user>/extrapath')
  96. def find_userextra(user):
  97. return 'User extra: %s' % user.name
  98.  
  99. @app.route('/users')
  100. def list_users():
  101. # Return some broken HTML showing url's to our users.
  102. s = ''
  103. for user in _database.values():
  104. s += url_for('find_user', user=user) + '<br/>'
  105. return s
  106.  
  107. if __name__ == '__main__':
  108. app.run(debug=True)
  109.  
  110. @app.route('/user/<user:user>/something')
  111.  
  112. @app.route('/user/<user:user>/something-else')
  113.  
  114. @app.route('/user/<user:user>/)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement