def get_authors(self): return [ make_model('authors', content_type='fixed') ] + [ make_model(item, content_type='author') for item in app.db.author_set() ]
def get_tags(self): return [ make_model('tags', content_type='fixed') ] + [ make_model(item, content_type='tag') for item in app.db.tag_set() ]
def get_categories(self): return [ make_model('categories', content_type='fixed') ] + [ make_model(item, content_type='category') for item in app.db.category_set() ]
def get_index(self): return [ make_model(app.theme_context.get('INDEX_CATEGORY'), content_type='category') ] + [ make_model(item, content_type='category') for item in app.theme_context.get('LIST_CATEGORIES', []) ]
def build_menu_item(item, app=None): """Return a name for menu item based on its destination""" app = app or current_app dropdown_enabled = app.theme_context.get('MENU_DROPDOWN_ENABLED', False) name = item.get('name') if item.get('index_id'): content = app.db.get('index', {'_id': item['index_id']}) if ( dropdown_enabled and item.get('item_type') == 'dropdown' and content['content_type'] == 'block' ): return ( name or content['title'], [build_menu_item(subitem) for subitem in content['block_items']] ) return (name or content['title'], url_for_content(content)) for ref in ['author', 'category', 'tag']: data = item.get(f"{ref}_id") if not data: continue return (name or data, make_model(data, ref).url) return (name, item['item'])
def format_link(self, request, obj, fieldname, *args, **kwars): """Format a link from the model""" model = make_model(obj) value = getattr(model, fieldname) return html.a(href=value, title=value, target='_blank')(html.i(class_="icon icon-resize-small", style="margin-right: 5px;")())
def get_text_block(title, app=None): app = app or current_app block = app.db.get( 'index', {'content_type': 'block', 'title': title, 'published': True} ) if block: return make_model(block).content
def format_datetime(self, request, obj, fieldname, *args, **kwargs): """Returns the formated datetime in string from object""" model = make_model(obj) return html.div(style='min-width:130px;')( getattr(model, fieldname).strftime( app.config.get('ADMIN_DATE_FORMAT', '%Y-%m-%d') ) )
def format_link(self, request, obj, fieldname, *args, **kwars): """Format a link from the model""" model = make_model(obj) value = getattr(model, fieldname) return html.a(href=value, title=value, target='_blank')( html.i(class_="icon icon-resize-small", style="margin-right: 5px;")() )
def get_block_by_id(_id, app=None): app = app or current_app block = app.db.get( 'index', {'content_type': 'block', '_id': _id, 'published': True} ) if block: return make_model(block)
def format_view_on_site(self, request, obj, fieldname, *args, **kwargs): """Returns button to view or preview depending on content status""" model = make_model(obj) return html.a( href=model.external_url, target='_blank', )(html.i(class_="icon fa fa-globe glyphicon glyphicon-globe", style="margin-right: 5px;")(), 'View' if model.published else 'Preview')
def get_articles_and_pages(self): return [ make_model(item) for item in app.db.content_set( {'published': True, 'content_type': {'$in': ['article', 'page']}}, sort=app.theme_context.get('ARTICLE_ORDER_BY', [('date', -1)]) ) ]
def format_ul(self, request, obj, fieldname, *args, **kwars): """Given a list of data format it is ul/li""" model = make_model(obj) field = getattr(model, fieldname) column_formatters_args = getattr(self, 'column_formatters_args', {}) _args = column_formatters_args.get('ul', {}).get(fieldname, {}) ul = html.ul(style=_args.get('style', "min-width:200px;max-width:300px;")) placeholder = _args.get('placeholder', u"{i}") lis = [html.li(placeholder.format(item=item)) for item in field] return ul(*lis)
def format_status(self, request, obj, fieldname, *args, **kwargs): """Format the status published or not published and other booleans""" model = make_model(obj) status = getattr(model, fieldname) column_formatters_args = getattr(self, 'column_formatters_args', {}) _args = column_formatters_args.get('status', {}).get(fieldname, {}) labels = _args.get('labels', {}) return html.span(class_="label label-{0}".format( labels.get(status, 'default')), style=_args.get('style', 'min-height:18px;'))(status)
def format_status(self, request, obj, fieldname, *args, **kwargs): """Format the status published or not published and other booleans""" model = make_model(obj) status = getattr(model, fieldname) column_formatters_args = getattr(self, 'column_formatters_args', {}) _args = column_formatters_args.get('status', {}).get(fieldname, {}) labels = _args.get('labels', {}) return html.span( class_="label label-{0}".format(labels.get(status, 'default')), style=_args.get('style', 'min-height:18px;') )(status)
def get_articles_and_pages(self): return [ make_model(item) for item in app.db.content_set( { 'published': True, 'content_type': { '$in': ['article', 'page'] } }, sort=app.theme_context.get('ARTICLE_ORDER_BY', [('date', -1)])) ]
def format_url(self, request, obj, fieldname, *args, **kwargs): """Get the url of a content object""" column_formatters_args = getattr(self, 'column_formatters_args', {}) _args = column_formatters_args.get('get_url', {}).get(fieldname, {}) attribute = _args.get('attribute', 'url') method = _args.get('method', 'url') model = make_model(obj) text = getattr(model, fieldname, '') if attribute: target = getattr(model, attribute, None) else: target = model url = getattr(target, method, lambda: '#')() return html.a(href=url)(text if text not in [None, 'None'] else '')
def get_categories(self): return [make_model('categories', content_type='fixed')] + [ make_model(item, content_type='category') for item in app.db.category_set() ]
def get_blocks(app=None, *args, **kwargs): app = app or current_app blocks = app.db.block_set(*args, **kwargs) if blocks: return [make_model(block) for block in blocks]
def get_tags(self): return [make_model('tags', content_type='fixed')] + [ make_model(item, content_type='tag') for item in app.db.tag_set() ]
def get_authors(self): return [make_model('authors', content_type='fixed')] + [ make_model(item, content_type='author') for item in app.db.author_set() ]