def show_asset(asset_id): errors = [] asset = AssetFacade.get(id=ObjectId(asset_id)) if asset_id else None if request.method == 'POST': if request.form.get('delete'): AssetFacade.delete(id=ObjectId(asset_id)) return redirect('/admin/assets') if request.form.get('submit'): name = request.form.get('asset-name') asset_file = request.files.get('asset-file') filename = None kwargs = {'name': name} if asset_file: try: filename = upload_file(asset_file) kwargs['filename'] = filename except Exception as e: errors.append(str(e)) if not len(errors): if not asset: asset = AssetFacade.create(**kwargs) return redirect('/admin/asset/{}'.format(str(asset.id))) else: asset.update(**kwargs) asset = AssetFacade.get(id=ObjectId(asset_id)) return render_template('admin/asset.html', asset=asset, errors=errors)
def show_post(post_id): post = PostFacade.get(id=ObjectId(post_id)) if post_id else None templates = get_theme_templates() assets = AssetFacade.get_all() theme_db = get_theme_db() kinds = theme_db['post_kinds'] if 'post_kinds' in theme_db else [] if request.method == 'POST': if request.form.get('delete'): post.delete() return redirect('/admin/posts') if request.form.get('submit'): name = request.form.get('post-name') content = request.form.get('post-content') template = request.form.get('post-template') post_kind = request.form.get('post-kind') is_published = request.form.get('post-is_published') is not None new_asset_files = request.files.getlist('new-assets') new_assets = [] post_kind = None if not post_kind else post_kind for new_asset_file in new_asset_files: filename = upload_file(new_asset_file) asset = AssetFacade.create(name=new_asset_file.filename, filename=filename) new_assets.append(asset) if post: new_assets = post.assets + new_assets else: pass if not post: post = PostFacade.create(name=name, content=content, template=template, kind=post_kind, is_published=is_published, created_at=datetime.datetime.now(), assets=new_assets) return redirect('/admin/post/{}'.format(str(post.id))) else: post.update(name=name, content=content, template=template, kind=post_kind, is_published=is_published, assets=new_assets) post = PostFacade.get(id=ObjectId(post_id)) return render_template('admin/post.html', templates=templates, assets=assets, kinds=kinds, post=post)
def show_page(page_id): page = PageFacade.get(id=ObjectId(page_id)) if page_id else None templates = get_theme_templates() template_config = None if page: template_config = get_template_config(page.template) if request.method == 'POST': if request.form.get('delete'): page.delete() return redirect('/admin/pages') if page: if request.form.get('delete-data'): page.update(data={}) page = PageFacade.get(id=ObjectId(page_id)) if request.form.get('submit'): name = request.form.get('page-name') template = request.form.get('page-template') is_startpage = request.form.get('page-is_startpage') is not None template_fields = {} if not page else page.fields for k, v in request.files.items(): if 'template-field' in k: field_file = request.files.get(k) assetname = k filename = upload_file(field_file) asset = AssetFacade.create(name=assetname, filename=filename) template_fields[k] = asset.to_dbref() for k, v in request.form.items(): if 'template-field' in k: template_fields[k] = v if not page: page = PageFacade.create(name=name, template=template, is_startpage=is_startpage, fields=template_fields) return redirect('/admin/page/{}'.format(str(page.id))) else: page.update(name=name, template=template, is_startpage=is_startpage, fields=template_fields) page = PageFacade.get(id=ObjectId(page_id)) return render_template('admin/page.html', template_config=template_config, templates=templates, page=page)
def show_account_settings(): user = get_current_user() form = AccountSettingsForm(request.form) if request.method == 'POST' and form.validate(): kwargs = {'name': form.name.data} if 'avatar' in request.files: filename = upload_file(request.files['avatar']) avatar_asset = AssetFacade.create(name=form.name.data + '-avatar', filename=filename) kwargs['avatar'] = avatar_asset user.update(**kwargs) user = UserFacade.get(id=user.id) form.name.data = user.name return render_template('admin/account_settings.html', form=form, user=user)