예제 #1
0
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()
예제 #2
0
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')
예제 #3
0
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})
예제 #4
0
def sm_add():
    """
    Add new stream message
    """
    message = post_get('message')
    new_message = StreamMessage(message=message)
    new_message.save()
    return 'Ok'
예제 #5
0
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')
예제 #6
0
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))
예제 #7
0
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')
예제 #8
0
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))