app.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import os
  2. import os.path as op
  3. from flask import Flask, url_for
  4. from flask_sqlalchemy import SQLAlchemy
  5. from sqlalchemy.event import listens_for
  6. from jinja2 import Markup
  7. from flask_admin import Admin, form
  8. from flask_admin.form import rules
  9. from flask_admin.contrib import sqla
  10. # Create application
  11. app = Flask(__name__, static_folder='files')
  12. # Create dummy secrey key so we can use sessions
  13. app.config['SECRET_KEY'] = '123456790'
  14. # Create in-memory database
  15. app.config['DATABASE_FILE'] = 'sample_db.sqlite'
  16. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE']
  17. app.config['SQLALCHEMY_ECHO'] = True
  18. db = SQLAlchemy(app)
  19. # Create directory for file fields to use
  20. file_path = op.join(op.dirname(__file__), 'files')
  21. try:
  22. os.mkdir(file_path)
  23. except OSError:
  24. pass
  25. # Create models
  26. class File(db.Model):
  27. id = db.Column(db.Integer, primary_key=True)
  28. name = db.Column(db.Unicode(64))
  29. path = db.Column(db.Unicode(128))
  30. def __unicode__(self):
  31. return self.name
  32. class Image(db.Model):
  33. id = db.Column(db.Integer, primary_key=True)
  34. name = db.Column(db.Unicode(64))
  35. path = db.Column(db.Unicode(128))
  36. def __unicode__(self):
  37. return self.name
  38. class User(db.Model):
  39. id = db.Column(db.Integer, primary_key=True)
  40. first_name = db.Column(db.Unicode(64))
  41. last_name = db.Column(db.Unicode(64))
  42. email = db.Column(db.Unicode(128))
  43. phone = db.Column(db.Unicode(32))
  44. city = db.Column(db.Unicode(128))
  45. country = db.Column(db.Unicode(128))
  46. notes = db.Column(db.UnicodeText)
  47. # Delete hooks for models, delete files if models are getting deleted
  48. @listens_for(File, 'after_delete')
  49. def del_file(mapper, connection, target):
  50. if target.path:
  51. try:
  52. os.remove(op.join(file_path, target.path))
  53. except OSError:
  54. # Don't care if was not deleted because it does not exist
  55. pass
  56. @listens_for(Image, 'after_delete')
  57. def del_image(mapper, connection, target):
  58. if target.path:
  59. # Delete image
  60. try:
  61. os.remove(op.join(file_path, target.path))
  62. except OSError:
  63. pass
  64. # Delete thumbnail
  65. try:
  66. os.remove(op.join(file_path,
  67. form.thumbgen_filename(target.path)))
  68. except OSError:
  69. pass
  70. # Administrative views
  71. class FileView(sqla.ModelView):
  72. # Override form field to use Flask-Admin FileUploadField
  73. form_overrides = {
  74. 'path': form.FileUploadField
  75. }
  76. # Pass additional parameters to 'path' to FileUploadField constructor
  77. form_args = {
  78. 'path': {
  79. 'label': 'File',
  80. 'base_path': file_path,
  81. 'allow_overwrite': False
  82. }
  83. }
  84. class ImageView(sqla.ModelView):
  85. def _list_thumbnail(view, context, model, name):
  86. if not model.path:
  87. return ''
  88. return Markup('<img src="%s">' % url_for('static',
  89. filename=form.thumbgen_filename(model.path)))
  90. column_formatters = {
  91. 'path': _list_thumbnail
  92. }
  93. # Alternative way to contribute field is to override it completely.
  94. # In this case, Flask-Admin won't attempt to merge various parameters for the field.
  95. form_extra_fields = {
  96. 'path': form.ImageUploadField('Image',
  97. base_path=file_path,
  98. thumbnail_size=(100, 100, True))
  99. }
  100. class UserView(sqla.ModelView):
  101. """
  102. This class demonstrates the use of 'rules' for controlling the rendering of forms.
  103. """
  104. form_create_rules = [
  105. # Header and four fields. Email field will go above phone field.
  106. rules.FieldSet(('first_name', 'last_name', 'email', 'phone'), 'Personal'),
  107. # Separate header and few fields
  108. rules.Header('Location'),
  109. rules.Field('city'),
  110. # String is resolved to form field, so there's no need to explicitly use `rules.Field`
  111. 'country',
  112. # Show macro from Flask-Admin lib.html (it is included with 'lib' prefix)
  113. rules.Container('rule_demo.wrap', rules.Field('notes'))
  114. ]
  115. # Use same rule set for edit page
  116. form_edit_rules = form_create_rules
  117. create_template = 'rule_create.html'
  118. edit_template = 'rule_edit.html'
  119. # Flask views
  120. @app.route('/')
  121. def index():
  122. return '<a href="/admin/">Click me to get to Admin!</a>'
  123. # Create admin
  124. admin = Admin(app, 'Example: Forms', template_mode='bootstrap3')
  125. # Add views
  126. admin.add_view(FileView(File, db.session))
  127. admin.add_view(ImageView(Image, db.session))
  128. admin.add_view(UserView(User, db.session, name='User'))
  129. def build_sample_db():
  130. """
  131. Populate a small db with some example entries.
  132. """
  133. import random
  134. import string
  135. db.drop_all()
  136. db.create_all()
  137. first_names = [
  138. 'Harry', 'Amelia', 'Oliver', 'Jack', 'Isabella', 'Charlie','Sophie', 'Mia',
  139. 'Jacob', 'Thomas', 'Emily', 'Lily', 'Ava', 'Isla', 'Alfie', 'Olivia', 'Jessica',
  140. 'Riley', 'William', 'James', 'Geoffrey', 'Lisa', 'Benjamin', 'Stacey', 'Lucy'
  141. ]
  142. last_names = [
  143. 'Brown', 'Smith', 'Patel', 'Jones', 'Williams', 'Johnson', 'Taylor', 'Thomas',
  144. 'Roberts', 'Khan', 'Lewis', 'Jackson', 'Clarke', 'James', 'Phillips', 'Wilson',
  145. 'Ali', 'Mason', 'Mitchell', 'Rose', 'Davis', 'Davies', 'Rodriguez', 'Cox', 'Alexander'
  146. ]
  147. locations = [
  148. ("Shanghai", "China"),
  149. ("Istanbul", "Turkey"),
  150. ("Karachi", "Pakistan"),
  151. ("Mumbai", "India"),
  152. ("Moscow", "Russia"),
  153. ("Sao Paulo", "Brazil"),
  154. ("Beijing", "China"),
  155. ("Tianjin", "China"),
  156. ("Guangzhou", "China"),
  157. ("Delhi", "India"),
  158. ("Seoul", "South Korea"),
  159. ("Shenzhen", "China"),
  160. ("Jakarta", "Indonesia"),
  161. ("Tokyo", "Japan"),
  162. ("Mexico City", "Mexico"),
  163. ("Kinshasa", "Democratic Republic of the Congo"),
  164. ("Bangalore", "India"),
  165. ("New York City", "United States"),
  166. ("London", "United Kingdom"),
  167. ("Bangkok", "Thailand"),
  168. ("Tehran", "Iran"),
  169. ("Dongguan", "China"),
  170. ("Lagos", "Nigeria"),
  171. ("Lima", "Peru"),
  172. ("Ho Chi Minh City", "Vietnam"),
  173. ]
  174. for i in range(len(first_names)):
  175. user = User()
  176. user.first_name = first_names[i]
  177. user.last_name = last_names[i]
  178. user.email = user.first_name.lower() + "@example.com"
  179. tmp = ''.join(random.choice(string.digits) for i in range(10))
  180. user.phone = "(" + tmp[0:3] + ") " + tmp[3:6] + " " + tmp[6::]
  181. user.city = locations[i][0]
  182. user.country = locations[i][1]
  183. db.session.add(user)
  184. images = ["Buffalo", "Elephant", "Leopard", "Lion", "Rhino"]
  185. for name in images:
  186. image = Image()
  187. image.name = name
  188. image.path = name.lower() + ".jpg"
  189. db.session.add(image)
  190. for i in [1, 2, 3]:
  191. file = File()
  192. file.name = "Example " + str(i)
  193. file.path = "example_" + str(i) + ".pdf"
  194. db.session.add(file)
  195. db.session.commit()
  196. return
  197. if __name__ == '__main__':
  198. # Build a sample db on the fly, if one does not exist yet.
  199. app_dir = op.realpath(os.path.dirname(__file__))
  200. database_path = op.join(app_dir, app.config['DATABASE_FILE'])
  201. if not os.path.exists(database_path):
  202. build_sample_db()
  203. # Start app
  204. app.run(debug=True)