admin.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from os import path
  2. from flask_admin_s3_upload import S3ImageUploadField
  3. from project import admin, app, db
  4. from foo.models import Thingy
  5. from library.admin_utils import ProtectedModelView
  6. from library.prefix_file_utcnow import prefix_file_utcnow
  7. class ThingyView(ProtectedModelView):
  8. column_list = ('image',)
  9. form_excluded_columns = ('image_storage_type',
  10. 'image_storage_bucket_name')
  11. form_overrides = dict(
  12. image=S3ImageUploadField)
  13. form_args = dict(
  14. image=dict(
  15. base_path=app.config['UPLOADS_FOLDER'],
  16. relative_path=app.config['THINGY_IMAGE_RELATIVE_PATH'],
  17. url_relative_path=app.config['UPLOADS_RELATIVE_PATH'],
  18. namegen=prefix_file_utcnow,
  19. storage_type_field='image_storage_type',
  20. bucket_name_field='image_storage_bucket_name',
  21. ))
  22. def scaffold_form(self):
  23. form_class = super(ThingyView, self).scaffold_form()
  24. static_root_parent = path.abspath(
  25. path.join(app.config['PROJECT_ROOT'], '..'))
  26. if app.config['USE_S3']:
  27. form_class.image.kwargs['storage_type'] = 's3'
  28. form_class.image.kwargs['bucket_name'] = \
  29. app.config['S3_BUCKET_NAME']
  30. form_class.image.kwargs['access_key_id'] = \
  31. app.config['AWS_ACCESS_KEY_ID']
  32. form_class.image.kwargs['access_key_secret'] = \
  33. app.config['AWS_SECRET_ACCESS_KEY']
  34. form_class.image.kwargs['static_root_parent'] = \
  35. static_root_parent
  36. return form_class
  37. admin.add_view(ThingyView(Thingy, db.session, name='Thingies'))