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 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 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 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 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 sm_add(): """ Add new stream message """ message = post_get('message') new_message = StreamMessage(message=message) new_message.save() return 'Ok'
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_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 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 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 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')