def post(self): form = self.form(request.form) if form.validate(): user = get_current_user() avatar_file_path = user.avatar_file_path avatar = request.files.get('avatar') if avatar: filename = secure_filename(avatar.filename) avatar_file_path = os.path.join('avatars', str(user.id), filename) path = os.path.join(lazy_media_path(), avatar_file_path) if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path), 0o777) avatar.save(path) form.populate_obj(user) user.avatar_file_path = avatar_file_path if avatar: user.use_avatar_from = 'upload' user.username = User.generate_username(user.username or user.name, user=user) self.update_user_links(request.form, user) user.save() flash('Profile saved!', 'alert') return redirect( request.args.get('next') or url_for('quokka.modules.accounts.profile_edit')) else: flash('Error ocurred!', 'alert error') # form errors return render_template('accounts/profile_edit.html', form=form)
class ImageAdmin(MediaAdmin): roles_accepted = ('admin', 'editor') column_list = ('title', 'path', 'thumb', 'published') form_columns = ['title', 'slug', 'path', 'channel', 'summary', 'published'] def _list_thumbnail(view, context, model, name): if not model.path: return '' return Markup( '<img src="%s">' % url_for( 'media', filename="{0}".format(form.thumbgen_filename( model.path)) ) ) column_formatters = { 'thumb': _list_thumbnail } form_extra_fields = { 'path': ImageUploadField( 'Image', base_path=lazy_media_path(), thumbnail_size=(100, 100, True), endpoint="media", namegen=dated_path, permission=0o777, allowed_extensions="MEDIA_IMAGE_ALLOWED_EXTENSIONS", ) }
def post(self): form = self.form(request.form) if form.validate(): user = get_current_user() avatar_file_path = user.avatar_file_path avatar = request.files.get('avatar') if avatar: filename = secure_filename(avatar.filename) avatar_file_path = os.path.join( 'avatars', str(user.id), filename ) path = os.path.join(lazy_media_path(), avatar_file_path) if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path), 0o777) avatar.save(path) form.populate_obj(user) user.avatar_file_path = avatar_file_path if avatar: user.use_avatar_from = 'upload' user.username = User.generate_username( user.username or user.name, user=user ) self.update_user_links(request.form, user) user.save() flash('Profile saved!', 'alert') return redirect( request.args.get('next') or url_for('quokka.modules.accounts.profile_edit') ) else: flash('Error ocurred!', 'alert error') # form errors return render_template('accounts/profile_edit.html', form=form)
class ImageAdmin(MediaAdmin): roles_accepted = ('admin', 'editor', 'author') column_list = ('title', 'full_path', 'thumb', 'published') form_columns = ['title', 'slug', 'path', 'channel', 'content_format', 'comments_enabled', 'summary', 'published'] column_formatters = { 'thumb': _list_thumbnail } form_extra_fields = { 'path': ImageUploadField( 'Image', base_path=lazy_media_path(), thumbnail_size=get_setting_value('MEDIA_IMAGE_THUMB_SIZE', default=(200, 200, True)), endpoint="quokka.core.media", namegen=dated_path, permission=0o777, allowed_extensions="MEDIA_IMAGE_ALLOWED_EXTENSIONS", ) } form_ajax_refs = { 'channel': AjaxModelLoader( 'channel', Channel, fields=['title', 'slug', 'long_slug'], filters={"long_slug__startswith": "media/image"} ) }
class FileAdmin(MediaAdmin): form_args = { 'path': { 'label': 'File', 'base_path': lazy_media_path(), 'namegen': dated_path, 'permission': 0o777 } }
class ImageAdmin(MediaAdmin): roles_accepted = ('admin', 'editor', 'author') column_list = ('title', 'path', 'thumb', 'published') form_columns = ['title', 'slug', 'path', 'channel', 'content_format', 'comments_enabled', 'summary', 'published'] def _list_thumbnail(self, context, model, name): if not model.path: return '' return Markup( '<img src="%s" width="100">' % url_for( 'media', filename=form.thumbgen_filename(model.path) ) ) column_formatters = { 'thumb': _list_thumbnail } form_extra_fields = { 'path': ImageUploadField( 'Image', base_path=lazy_media_path(), thumbnail_size=get_setting_value('MEDIA_IMAGE_THUMB_SIZE', default=(200, 200, True)), endpoint="media", namegen=dated_path, permission=0o777, allowed_extensions="MEDIA_IMAGE_ALLOWED_EXTENSIONS", ) } form_ajax_refs = { 'channel': AjaxModelLoader( 'channel', Channel, fields=['title', 'slug', 'long_slug'], filters={"long_slug__startswith": "media/image"} ) }
class FileAdmin(MediaAdmin): form_args = { 'path': { 'label': 'File', 'base_path': lazy_media_path(), 'namegen': dated_path, 'permission': 0o777 }, 'summary': {'widget': TextEditor()}, 'slug': {'widget': PrepopulatedText(master='title')}, } form_ajax_refs = { 'channel': AjaxModelLoader( 'channel', Channel, fields=['title', 'slug', 'long_slug'], filters={"long_slug__startswith": "media/files"} ) }
class BaseContentAdmin(ModelAdmin): """ All attributes added here for example more info in admin source github.com/mrjoes/flask-admin/blob/master/flask_admin/model/base.py or Flask-admin documentation """ roles_accepted = ('admin', 'editor', 'author') can_create = True can_edit = True can_delete = True # list_template = 'admin/model/list.html' # edit_template = 'admin/custom/edit.html' # create_template = 'admin/custom/create.html' column_list = ('title', 'slug', 'channel', 'published', 'created_at', 'available_at', 'view_on_site') column_formatters = { 'view_on_site': ModelAdmin.formatters.get('view_on_site'), 'created_at': ModelAdmin.formatters.get('datetime'), 'available_at': ModelAdmin.formatters.get('datetime') } # column_type_formatters = {} # column_labels = {} # column_descriptions = {} # column_sortable_list = [] / ('name', ('user', 'user.username')) # column_default_sort = 'pk' # column_choices = {'column': ('value', 'display')} # column_display_pk = True column_filters = [ 'published', 'title', 'summary', 'created_at', 'available_at' ] column_searchable_list = ('title', 'summary') form_columns = [ 'title', 'slug', 'channel', 'related_channels', 'summary', 'published', 'add_image', 'contents', 'show_on_channel', 'available_at', 'available_until', 'tags', 'values', 'template_type', 'license', 'authors' ] # form_excluded_columns = [] # form = None # form_overrides = None form_widget_args = { 'summary': { 'style': 'width: 400px; height: 100px;' }, 'title': { 'style': 'width: 400px' }, 'slug': { 'style': 'width: 400px' }, } form_args = { # 'body': {'widget': TextEditor()}, 'slug': { 'widget': PrepopulatedText(master='title') } } form_subdocuments = { 'contents': { 'form_subdocuments': { None: { 'form_columns': ('content', 'caption', 'purpose', 'order', 'thumb'), 'form_ajax_refs': { 'content': { 'fields': ['title', 'long_slug', 'summary'] } }, 'form_extra_fields': { 'thumb': ThumbField('thumb', endpoint="media") } } } }, } # form_extra_fields = {} form_extra_fields = { 'add_image': ContentImageField( 'Add new image', base_path=lazy_media_path(), thumbnail_size=lazy_str_setting('MEDIA_IMAGE_THUMB_SIZE', default=(100, 100, True)), endpoint="media", namegen=dated_path, permission=0o777, allowed_extensions="MEDIA_IMAGE_ALLOWED_EXTENSIONS", ) } # action_disallowed_list # page_size = 20 # form_ajax_refs = { # 'main_image': {"fields": ('title',)} # } def after_model_change(self, form, model, is_created): if not hasattr(form, 'add_image'): return form.add_image.save_contents(model)