def send_subscription_email(subscription, gallery): description = u'{}...'.format( gallery.description()[:400]) if gallery.description() else '' category = gallery.latest_category() template = env.get_template('subscription_email.txt') body_text = template.render(user_name=subscription.name, blog_url=gallery.url(), cancel_url=subscription.cancel_url(), published_at=format_date(gallery.published_at), category=category, unsubscribe_url=subscription.url()) template = env.get_template('subscription_email.html') body_html = template.render(user_name=subscription.name, blog_url=gallery.url(), gallery_title=gallery.name, subtitle=gallery.subtitle, published_at=format_date(gallery.published_at), author=gallery.author, description=description, category=category, cancel_url=subscription.cancel_url(), unsubscribe_url=subscription.url()) send_email(subscription.email, gallery.name, body_text=body_text, body_html=body_html)
def post_edit(post_id): if request.method == 'GET': try: post = Post.get(Post.post_id == post_id) # todo: get not deleted except Post.DoesNotExist: abort(404) form = PostForm(obj=post) all_categories = Category.select() template = env.get_template('post/edit.html') return template.render( item=post, form=form, categories=all_categories, ) elif request.method == 'POST': post = Post.get(Post.post_id == post_id) post.category = post_get('category-id') post.post_text = post_get('text') post.slug = post_get('slug') post.title = post_get('title') post.draft = bool(int(post_get('draft'))) # zero int is False post.language = post_get('language') post.show_on_index = post_get_checkbox('show_on_index') post.date_updated = datetime.now() new_tags = post_get('tags') old_tags = Tag.select().join(Tag_to_Post)\ .where(Tag_to_Post.post_id == post_id) remove_tags(old_tags, new_tags, post_id) add_new_tags(new_tags, post_id) post.save() app.flash('Article updated') return redirect('/post/' + str(post_id))
def post_edit(post_id): if request.method == 'GET': try: post = Post.get(Post.post_id == post_id) # todo: get not deleted except Post.DoesNotExist: abort(404) form = PostForm(obj=post) all_categories = Category.select() template = env.get_template('post/edit.html') return template.render( item=post, form=form, categories=all_categories, ) elif request.method == 'POST': post = Post.get(Post.post_id == post_id) post.category = post_get('category-id') post.post_text = post_get('text') post.slug = post_get('slug') post.title = post_get('title') post.draft = bool(int(post_get('draft'))) # zero int is False post.language = post_get('language') post.show_on_index = bool(post_get('show-on-index')) post.date_updated = datetime.now() new_tags = post_get('tags') old_tags = Tag.select().join(Tag_to_Post)\ .where(Tag_to_Post.post_id == post_id) remove_tags(old_tags, new_tags, post_id) add_new_tags(new_tags, post_id) post.save() app.flash('Article updated') redirect('/post/' + str(post_id))
def login(): if 'back' in request.query: back = request.query['back'] else: back = '/' if app.current_user is not None: app.flash( 'Log out from current session in order to use different account', category='info', ) redirect(back) if request.method == 'POST': try: user = User.get(User.mail == post_get('email')) except DoesNotExist: app.flash('No such user') else: if user.user_password == User.encode_password( post_get('password')): app.flash('Successfully logged in') app.login(user) redirect(back) else: app.flash('Wrong password', category='info') template = env.get_template('user/login.html') return template.render()
def wrapper(*args, **kwargs): tplvars = func(*args, **kwargs) template = env.get_template(tpl_name) if tplvars is not None: return template.render(**tplvars) else: return template.render()
def post_edit(post_id): if request.method == 'GET': try: post = Post.get(Post.post_id == post_id) # todo: get not deleted except Post.DoesNotExist: abort(404) all_categories = Category.select() template = env.get_template('post/edit.html') return template.render(item=post, categories=all_categories, submit_button=u'Оновити') elif request.method == 'POST': post = Post.get(Post.post_id == post_id) post.category = post_get('category_id') post.post_text = post_get('text') post.title = post_get('title') post.draft = bool(int(post_get('draft'))) # zero int is False post.language = post_get('language') new_tags = post_get('tags') old_tags = Tag.select().join(Tag_to_Post)\ .where(Tag_to_Post.post_id == post_id) remove_tags(old_tags, new_tags, post_id) add_new_tags(new_tags, post_id) post.save() app.flash(u'Статтю успішно оновлено') redirect('/post/' + str(post_id))
def login(): if 'back' in request.query: back = request.query['back'] else: back = '/' if app.current_user is not None: app.flash(u'Вийдіть з поточної сесії, щоб увійти під іншим акаунтом') redirect(back) if request.method == 'POST': try: user = User.get(User.mail == post_get('email')) except DoesNotExist: app.flash(u'Немає такого користувача') else: if user.user_password == User.encode_password( post_get('password')): app.flash(u'Ви успішно увійшли') app.login(user) redirect(back) else: app.flash(u'Невірний пароль') template = env.get_template('user/login.html') return template.render()
def upload(): """ Uploads a file to the site storage on the server """ # todo: add auto-rename # todo: add format checking and size for pictures form = SimpleUploadForm(request.POST) template = env.get_template('upload.html') if request.method == 'POST' and form.validate(): up_file = form.upload_file.data folder = os.path.join(config.ROOT_FOLDER, form.file_folder.data) if not os.path.exists(folder): os.makedirs(folder) new_filename = up_file.filename file_path = os.path.join(folder, new_filename) # Generate unique filename if one already exists if os.path.exists(file_path): new_filename = distort_filename(up_file.filename) file_path = os.path.join(folder, new_filename) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(up_file.file.read()) uploaded_file = join_all_path(['/', form.file_folder.data, new_filename]).replace('\\', '/') app.flash('File uploaded') return template.render(form=form, uploaded_file=uploaded_file) return template.render(form=form)
def post_add(): form = PostForm() if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/add.html') return template.render( form=form, categories=all_categories, ) if request.method == 'POST': post = Post.create( category=post_get('category-id'), post_text=post_get('text'), title=post_get('title'), slug=post_get('slug'), user=app.current_user.user_id, date_posted=datetime.now(), draft=bool(int(post_get('draft'))), show_on_index=bool(post_get('show-on-index')), language=post_get('language'), ) post_id = post.post_id post.save() add_new_tags(post_get('tags'), post_id) redirect('/post/' + str(post_id))
def banners(): template = env.get_template('banners.html') if request.method == 'GET': all_banners = Banner.select() return template.render({'banners': all_banners}) elif request.method == 'POST': banner_img = request.files.get('banner_img') banners_folder = static_path('img/banners/') file_path = os.path.join(banners_folder, banner_img.filename) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(banner_img.file.read()) link = post_get('link') parsed_link = urlparse(link) if parsed_link.scheme == '': link = 'http://{0}'.format(link) if not parsed_link.path or parsed_link.path == '#': link = '#' banner = Banner.create(desc=post_get('desc'), link=link, img=banner_img.filename) app.flash(u'+1 новий банер') redirect('/banners')
def banners(): template = env.get_template('banners.html') if request.method == 'GET': all_banners = Banner.select() return template.render({'banners': all_banners}) elif request.method == 'POST': banner_img = request.files.get('banner_img') banners_folder = static_path('img/banners/') file_path = os.path.join(banners_folder, banner_img.filename) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(banner_img.file.read()) link = post_get('link') parsed_link = urlparse(link) if parsed_link.scheme == '': link = 'http://{0}'.format(link) if not parsed_link.path or parsed_link.path == '#': link = '#' banner = Banner.create(desc=post_get('desc'), link=link, img=banner_img.filename) app.flash('+1 banner') redirect('/banners')
def post_add(): form = PostForm() if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/add.html') return template.render( form=form, categories=all_categories, ) if request.method == 'POST': post = Post.create( category=post_get('category-id'), post_text=post_get('text'), title=post_get('title'), slug=post_get('slug'), user=app.current_user.user_id, date_posted=datetime.now(), draft=bool(int(post_get('draft'))), show_on_index=post_get_checkbox('show_on_index'), language=post_get('language'), ) post_id = post.post_id post.save() add_new_tags(post_get('tags'), post_id) redirect('/post/' + str(post_id))
def upload(): """ Uploads a file to the site storage on the server """ # todo: add auto-rename # todo: add format checking and size for pictures form = SimpleUploadForm(request.POST) template = env.get_template('upload.html') if request.method == 'POST' and form.validate(): up_file = form.upload_file.data folder = os.path.join(config.ROOT_FOLDER, form.file_folder.data) if not os.path.exists(folder): os.makedirs(folder) new_filename = up_file.filename file_path = os.path.join(folder, new_filename) # Generate unique filename if one already exists if os.path.exists(file_path): new_filename = distort_filename(up_file.filename) file_path = os.path.join(folder, new_filename) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(up_file.file.read()) uploaded_file = join_all_path(['/', form.file_folder.data, new_filename]).replace('\\', '/') app.flash(u'Файл завантажено') return template.render(form=form, uploaded_file=uploaded_file) return template.render(form=form)
def quote_add(): template = env.get_template('quote_add.html') all_quotes = Quote.select() if request.method == 'POST': quote = Quote.create(text=post_get('text'), author=post_get('author')) app.flash(u'Цитата додана', 'success') return template.render({'quotes': all_quotes})
def quote_add(): template = env.get_template('quote_add.html') all_quotes = Quote.select() if request.method == 'POST': quote = Quote.create(text=post_get('text'), author=post_get('author')) app.flash('Quote added', 'success') return template.render({'quotes': all_quotes})
def category_add(): if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/category_add.html') return template.render(categories=all_categories) if request.method == 'POST': new_category = Category.create(category_name=post_get('category_name')) app.flash('New category was added') redirect('/category/add')
def category_add(): if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/category_add.html') return template.render(categories=all_categories) if request.method == 'POST': new_category = Category.create(category_name=post_get('category_name')) app.flash(u'Нова категорія була успішно додана') redirect('/category/add')
def sp_add(): form = StaticPageForm(request.POST) template = env.get_template('static_page_admin.html') if form.validate_on_post(): app.log(form.page_url.data) new_page = StaticPage.create(title=form.title.data, url=form.page_url.data, text=form.text.data) app.flash('New page!') pages = StaticPage.select() return template.render(form=form, pages=pages)
def _render_post_page(post): cu = app.current_user if post.deleted or post.draft: if cu is not None and \ (cu.user_id == post.user.user_id or cu.is_admin()): app.log('Showing post to its creator or to admin') else: abort(404) template = env.get_template('post/view.html') post.views += 1 post.save() return template.render(item=post)
def send_subscription_email(subscription, gallery): description = u'{}...'.format(gallery.description()[:400]) if gallery.description() else '' category = gallery.latest_category() template = env.get_template('subscription_email.txt') body_text = template.render(user_name=subscription.name, blog_url=gallery.url(), cancel_url=subscription.cancel_url(), published_at=format_date(gallery.published_at), category=category, unsubscribe_url=subscription.url()) template = env.get_template('subscription_email.html') body_html = template.render(user_name=subscription.name, blog_url=gallery.url(), gallery_title=gallery.name, subtitle=gallery.subtitle, published_at=format_date(gallery.published_at), author=gallery.author, description=description, category=category, cancel_url=subscription.cancel_url(), unsubscribe_url=subscription.url()) send_email(subscription.email, gallery.name, body_text=body_text, body_html=body_html)
def joke_add(): template = env.get_template('item_add.html') all_jokes = SiteJoke.select() form = ItemForm(model_class=SiteJoke, url_prefix='joke') if request.method == 'POST': SiteJoke.create( text=post_get('text'), ) app.flash('New joke is here', 'success') return template.render({ 'form': form, 'items': all_jokes, })
def category_list(category_id): """ List all posts in current category """ try: category = Category.get(Category.category_id == category_id) except DoesNotExist: abort(404) all_posts = Post.get_posts()\ .where(Post.category == category_id).order_by(Post.date_posted.desc()) template = env.get_template('post/list.html') return template.render(posts=all_posts, info='Articles for category "%s"' % category.category_name)
def category_list(category_id): """ List all posts in current category """ try: category = Category.get(Category.category_id == category_id) except DoesNotExist: abort(404) all_posts = Post.get_posts()\ .where(Post.category == category_id).order_by(Post.date_posted.desc()) template = env.get_template('post/list.html') return template.render( posts=all_posts, info=u'Статті у категорії "%s"' % category.category_name )
def posts_for_tag(tag_id): try: tag = Tag.get(Tag.tag_id == tag_id) except DoesNotExist: # todo: enable logging for all these exceptions abort(404) posts = Post.get_posts().join(Tag_to_Post).\ where(Tag_to_Post.tag_id == tag_id) how = posts.count() if how: info = u'Статті, відмічені тегом "%s" (%s)' % (tag.text, how) else: info = u'Для рідкісного тега "%s" немає статтей' % tag.text template = env.get_template('post/list.html') return template.render(posts=posts, info=info)
def posts_for_tag(tag_id): try: tag = Tag.get(Tag.tag_id == tag_id) except DoesNotExist: # todo: enable logging for all these exceptions abort(404) posts = Post.get_posts().join(Tag_to_Post).\ where(Tag_to_Post.tag_id == tag_id) how = posts.count() if how: info = 'Tagged with [%s] x%d' % (tag.text, how) else: info = 'No posts for [%s] tag' % tag.text template = env.get_template('post/list.html') return template.render(posts=posts, info=info)
def post_index(): all_posts = Post.get_posts().order_by(Post.date_posted.desc()).\ limit(config.POSTS_PER_PAGE) for item in all_posts: item.post_text = shorten_text(item.post_text) random_banner = Banner.select().\ where(Banner.disabled == False).order_by(fn.Rand()).first() quote = Quote.select().order_by(fn.Rand()).first() messages = StreamMessage.select() template = env.get_template('post/index.html') return template.render(posts=all_posts, banner=random_banner, stream_messages=messages, quote=quote, link_what='pstlink')
def sp_add(): form = StaticPageForm( request.POST, model_class=StaticPage, url_prefix='sp', ) template = env.get_template('item_add.html') if form.validate_on_post(): app.log(form.page_url.data) StaticPage.create( title=form.title.data, url=form.page_url.data, text=form.text.data, ) app.flash('New page!') pages = StaticPage.select() return template.render(form=form, items=pages)
def post_add(): if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/add.html') return template.render(categories=all_categories) if request.method == 'POST': post = Post.create( category=post_get('category_id'), post_text=post_get('text'), title=post_get('title'), user=app.current_user.user_id, date_posted=datetime.now(), draft=int(post_get('draft')) == 1, language=post_get('language') ) post_id = post.post_id post.save() add_new_tags(post_get('tags'), post_id) redirect('/post/' + str(post_id))
def gallery(): template = env.get_template('gallery_add.html') if request.method == 'GET': photos = Photo.select() return template.render(photos=photos) elif request.method == 'POST': photo_file = request.files.get('photo') file_ext = os.path.splitext(photo_file.filename)[1] gallery_folder = static_path('img/gallery/') f_name = generate_filename(prefix='photo', suffix=file_ext) file_path = os.path.join(gallery_folder, f_name) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(photo_file.file.read()) photo = Photo.create(desc=post_get('desc'), photo=f_name) app.flash('Picture added to the gallery') redirect('/gallery_add')
def gallery(): template = env.get_template('gallery_add.html') if request.method == 'GET': photos = Photo.select() return template.render(photos=photos) elif request.method == 'POST': photo_file = request.files.get('photo') file_ext = os.path.splitext(photo_file.filename)[1] gallery_folder = static_path('img/gallery/') f_name = generate_filename(prefix='photo', suffix=file_ext) file_path = os.path.join(gallery_folder, f_name) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(photo_file.file.read()) photo = Photo.create(desc=post_get('desc'), photo=f_name) app.flash(u'Фото успішно додане') redirect('/gallery_add')
def post_view(post_id): try: # todo: rewrite with func in Post post = Post.get(Post.post_id == post_id) if post.deleted: # todo: make it visible to its creator raise DoesNotExist cu = app.current_user if post.draft: if cu is not None and cu.user_id != post.user.user_id: raise DoesNotExist if cu is None: raise DoesNotExist except DoesNotExist: abort(404) template = env.get_template('post/view.html') # post.update(views=post.views+1).execute() #classmethod! post.views += 1 post.save() # instance method! return template.render(item=post)
def post_index(): all_posts = Post.get_posts(index_only=True)\ .order_by(Post.date_posted.desc())\ .limit(config.POSTS_PER_PAGE) for item in all_posts: item.post_text = shorten_text(item.post_text) random_banner = Banner.select()\ .where(Banner.disabled == False)\ .order_by(fn.Rand())\ .first() quote = Quote.select().order_by(fn.Rand()).first() messages = StreamMessage.select() template = env.get_template('post/index.html') return template.render(posts=all_posts, banner=random_banner, stream_messages=messages, quote=quote, link_what='pstlink')
def signup(): template = env.get_template('user/signup.html') form = SignupForm(request.POST) if request.method == 'POST': if form.validate(): try: user = User.get(User.mail == form.mail.data) except DoesNotExist: new_user = User.create( mail=form.mail.data, user_password=User.encode_password(form.password.data), first_name=form.first_name.data, last_name=form.last_name.data, nickname=form.nickname.data ) app.flash(u'Успішна реєстрація. Тепер ви можете увійти') redirect('/login') else: app.flash(u'Користувач з такою поштою уже існує') return template.render(form=form)
def signup(): template = env.get_template('user/signup.html') form = SignupForm(request.POST) if request.method == 'POST': if form.validate(): try: user = User.get(User.mail == form.mail.data) except DoesNotExist: new_user = User.create( mail=form.mail.data, user_password=User.encode_password(form.password.data), first_name=form.first_name.data, last_name=form.last_name.data, nickname=form.nickname.data, ) app.flash('Well done! Now you can log in') redirect('/login') else: app.flash( 'User with such an email already exists', category='info', ) return template.render(form=form)
def error404(error): template = env.get_template('errors/404.html') return template.render()
def error500(error): template = env.get_template('errors/500.html') return template.render()
def server_static(page_name): page = models.StaticPage.get_or_404(models.StaticPage.url == page_name) template = env.get_template('static_page.html') return template.render(page=page)
def deleted_posts(): posts = Post.get_deleted() info = 'Deleted articles' template = env.get_template('post/list.html') return template.render(posts=posts, info=info)
def render_template(tpl_name, *args, **kwagrs): """ Render template helper function """ template = env.get_template(tpl_name) return template.render(*args, **kwagrs)
def list_users(): users = User.select().order_by(User.date_registered.desc()) template = env.get_template('user/index.html') return template.render(users=users)
def deleted_posts(): posts = Post.get_deleted() info = u'Видалені статті' template = env.get_template('post/list.html') return template.render(posts=posts, info=info)