path = path[0] else: path = path.split('|') if len(path) > 1: next_url = url_for('.edit', path='|'.join(path[1:])) path = path[0] base_path, full_path, path = self._normalize_path(path) dir_url = self._get_dir_url('.index', os.path.dirname(path)) next_url = next_url or dir_url form = EditForm() if request.method == 'POST': form.process(request.form, content='') if form.validate(): try: with open(full_path, 'w') as f: f.write(request.form['content']) except IOError: flash("Error saving changes to file!", 'error') else: flash("Changes saved successfully!") return redirect(next_url) else: with open(full_path, 'r') as f: form.content.data = f.read() return self.render('edit_docs.html', dir_url=dir_url, form=form, path=path) admin.add_view(DocsFilesView(name='Docs', endpoint='docs'))
path = os.path.join(app.root_path, 'static', 'screenshots') thumb_path = os.path.join(app.root_path, 'static', 'screenshots', 'thumb') filename = secure_filename(upload.filename) filepath = os.path.join(path, filename) thumb_filepath = os.path.join(thumb_path, filename) from PIL import Image upload.save(filepath) img = Image.open(filepath) thumb = img.resize((100, 100)) thumb.save(thumb_filepath) form.filename.data = filename return super(ScreenshotView, self).create_model(form) class ScreenshotFilesView(AuthFileAdmin): def __init__(self, **kwargs): path = os.path.join(app.root_path, 'static', 'screenshots') super(ScreenshotFilesView, self).__init__(path, '/static/screenshots/', **kwargs) admin.add_view(ScreenshotView(db.session, name='Manage', endpoint='screenshots', category='Screenshots')) admin.add_view(ScreenshotFilesView(name='Files', endpoint='screenshot-files', category='Screenshots'))
# Slug (https://gist.github.com/1428479) # https://bitbucket.org/r0sk/flaskblog/src/2f57d47347fd/blog.py#cl-46 def make_slug(text, encoding=None, permitted_chars='abcdefghijklmnopqrstuvwxyz0123456789-'): if isinstance(text, str): text = text.decode(encoding or 'utf-8') clean_text = text.strip().replace(' ', '-').lower() while '--' in clean_text: clean_text = clean_text.replace('--', '-') ascii_text = normalize('NFKD', clean_text).encode('ascii', 'ignore') strict_text = map(lambda x: x if x in permitted_chars else '', ascii_text) return unicode(''.join(strict_text)) class NewsView(AuthModelView): can_create = True list_columns = ('title', 'slug', 'date_created') form_columns = ('slug', 'title', 'summary') def __init__(self, session, **kwargs): super(NewsView, self).__init__(NewsArticle, session, **kwargs) def create_model(self, form): if form.data['slug'] == '': form.slug.data = make_slug(form.data['title']) return super(NewsView, self).create_model(form) admin.add_view(NewsView(db.session, name='News', endpoint='news'))
can_create = True list_columns = ('username', 'admin', 'active') form_columns = ('username', 'password', 'status') form_overrides = dict(password=PasswordField, status=SelectField) form_args = dict( password=dict( default='' ), status=dict( choices=[ (str(0), 'Not Active'), (str(User.ACTIVE), 'Active Not Admin'), (str(User.ADMIN), 'Admin Not Active'), (str(User.ADMIN | User.ACTIVE), 'Admin And Active') ] ) ) def __init__(self, session, **kwargs): super(UsersView, self).__init__(User, session, **kwargs) def create_model(self, form): if form.data['password'] != '': form.password.data = User.encode(form.data['username'], form.data['password']) return super(UsersView, self).create_model(form) admin.add_view(UsersView(db.session, name='Users', endpoint='users'))