def checknewscomments(news):
    orm.sql_debug(False)

    if not news:
        with orm.db_session:
            first_newsitem = orm.select(orm.min(x.id) for x in NewsItem).first()
            last_newsitem = orm.select(orm.max(x.id) for x in NewsItem).first()
        newsitem_id = first_newsitem
    else:
        i = 0

    while True:
        with orm.db_session:
            if not news:
                newsitem = NewsItem.select(lambda x: x.id >= newsitem_id and x.id <= last_newsitem).first()
                if not newsitem:
                    break
            else:
                if i >= len(news):
                    break
                if news[i].isdigit():
                    newsitem = NewsItem.get(id=int(news[i]))
                else:
                    newsitem = NewsItem.get(name=news[i])
                i += 1
                if not newsitem:
                    print('News item {} not found'.format(news[i - 1]))
                    continue

            print('News item {} ({})'.format(newsitem.id, newsitem.name))
            comments_list = newsitem.bl.select_comments().order_by('c.date, c.id')
            check_comments_for(newsitem, comments_list)

            newsitem_id = newsitem.id + 1
Beispiel #2
0
def parse_news_id(news_id):
    if not news_id.isdigit():
        newsitem = NewsItem.get(name=news_id)
        if not newsitem:
            abort(404)
        return newsitem.id
    return int(news_id)
Beispiel #3
0
def parse_news_id(news_id):
    if not news_id.isdigit():
        newsitem = NewsItem.get(name=news_id)
        if not newsitem:
            abort(404)
        return newsitem.id
    return int(news_id)
Beispiel #4
0
def shown_newsitem():
    n = NewsItem.get(show=True)
    if not n:
        return
    last_id = request.cookies.get('last_newsitem')
    if last_id and last_id.isdigit() and int(last_id) == n.id:
        return
    return n
Beispiel #5
0
def show(name, comments_page):
    newsitem = NewsItem.get(name=name)
    if not newsitem:
        abort(404)

    if newsitem.is_template:
        template = current_app.jinja_env.from_string(newsitem.content)
        template.name = 'db/news/{}.html'.format(name)
        content = render_template(template,
                                  newsitem_name=newsitem.name,
                                  newsitem_title=newsitem.title)
    else:
        content = newsitem.content

    per_page = current_user.comments_per_page or current_app.config[
        'COMMENTS_COUNT']['page']
    maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth(
        current_user)

    comments_count, paged, comments_tree_list = newsitem.bl.paginate_comments(
        comments_page, per_page, maxdepth)
    paged.page_arg_name = 'comments_page'
    if not comments_tree_list and paged.number != 1:
        abort(404)

    comment_ids = [x[0].id for x in comments_tree_list]
    if current_user.is_authenticated:
        comment_votes_cache = newsitem.bl.select_comment_votes(
            current_user._get_current_object(), comment_ids)
    else:
        comment_votes_cache = {i: 0 for i in comment_ids}

    data = {
        'page_title':
        newsitem.title,
        'newsitem':
        newsitem,
        'content':
        Markup(content),
        'comments_count':
        comments_count,
        'page_obj':
        paged,
        'comments_tree_list':
        comments_tree_list,
        'comment_form':
        CommentForm(),
        'comment_votes_cache':
        comment_votes_cache,
        'sub_comments':
        newsitem.bl.get_comments_subscription(
            current_user._get_current_object()),
    }

    return render_template('news/show.html', **data)
Beispiel #6
0
def add(news_id):
    newsitem = NewsItem.get(id=parse_news_id(news_id))
    if not newsitem:
        abort(404)

    # Все проверки доступа там
    return common_comment.add(
        'newsitem',
        newsitem,
        template='news/comment_work.html',
    )
Beispiel #7
0
def add(news_id):
    newsitem = NewsItem.get(id=parse_news_id(news_id))
    if not newsitem:
        abort(404)

    # Все проверки доступа там
    return common_comment.add(
        'newsitem',
        newsitem,
        template='news/comment_work.html',
    )
Beispiel #8
0
def ajax_tree(news_id, local_id):
    newsitem = NewsItem.get(id=parse_news_id(news_id))
    if not newsitem:
        abort(404)

    comment = newsitem.comments.select(lambda x: x.local_id == local_id).first()
    if not comment:
        abort(404)

    return common_comment.ajax_tree(
        'newsitem',
        comment,
        target=newsitem,
    )
Beispiel #9
0
def checknewscomments(news):
    orm.sql_debug(False)

    if not news:
        with orm.db_session:
            first_newsitem = orm.select(orm.min(x.id)
                                        for x in NewsItem).first()
            last_newsitem = orm.select(orm.max(x.id) for x in NewsItem).first()
        newsitem_id = first_newsitem
    else:
        i = 0

    while True:
        with orm.db_session:
            if not news:
                newsitem = NewsItem.select(lambda x: x.id >= newsitem_id and x.
                                           id <= last_newsitem).first()
                if not newsitem:
                    break
            else:
                if i >= len(news):
                    break
                if news[i].isdigit():
                    newsitem = NewsItem.get(id=int(news[i]))
                else:
                    newsitem = NewsItem.get(name=news[i])
                i += 1
                if not newsitem:
                    print('News item {} not found'.format(news[i - 1]))
                    continue

            print('News item {} ({})'.format(newsitem.id, newsitem.name))
            comments_list = newsitem.bl.select_comments().order_by(
                'c.date, c.id')
            check_comments_for(newsitem, comments_list)

            newsitem_id = newsitem.id + 1
Beispiel #10
0
def comments_subscribe(name):
    user = current_user._get_current_object()
    newsitem = NewsItem.get(name=name)
    if not newsitem:
        abort(404)

    newsitem.bl.subscribe_to_comments(
        user,
        email=request.form.get('email') == '1',
        tracker=request.form.get('tracker') == '1',
    )

    if request.form.get('short') == '1':
        return jsonify(success=True)
    return redirect(url_for('newsitem.show', name=newsitem.name))
Beispiel #11
0
def comments_subscribe(name):
    user = current_user._get_current_object()
    newsitem = NewsItem.get(name=name)
    if not newsitem:
        abort(404)

    newsitem.bl.subscribe_to_comments(
        user,
        email=request.form.get('email') == '1',
        tracker=request.form.get('tracker') == '1',
    )

    if request.form.get('short') == '1':
        return jsonify(success=True)
    return redirect(url_for('newsitem.show', name=newsitem.name))
Beispiel #12
0
def ajax_tree(news_id, local_id):
    newsitem = NewsItem.get(id=parse_news_id(news_id))
    if not newsitem:
        abort(404)

    comment = newsitem.comments.select(
        lambda x: x.local_id == local_id).first()
    if not comment:
        abort(404)

    return common_comment.ajax_tree(
        'newsitem',
        comment,
        target=newsitem,
    )
Beispiel #13
0
def ajax(news_id, page):
    newsitem = NewsItem.get(id=parse_news_id(news_id))
    if not newsitem:
        abort(404)

    per_page = current_user.comments_per_page or current_app.config['COMMENTS_COUNT']['page']
    link = url_for('news.show', name=newsitem.name, comments_page=page)

    return common_comment.ajax(
        'newsitem',
        newsitem,
        link,
        page,
        per_page,
        template_pagination='news/comments_pagination.html',
    )
Beispiel #14
0
def ajax(news_id, page):
    newsitem = NewsItem.get(id=parse_news_id(news_id))
    if not newsitem:
        abort(404)

    per_page = current_user.comments_per_page or current_app.config[
        'COMMENTS_COUNT']['page']
    link = url_for('news.show', name=newsitem.name, comments_page=page)

    return common_comment.ajax(
        'newsitem',
        newsitem,
        link,
        page,
        per_page,
        template_pagination='news/comments_pagination.html',
    )
Beispiel #15
0
    def update(self, author, data):
        from mini_fiction.models import AdminLog

        data = Validator(NEWS_ITEM).validated(data, update=True)
        newsitem = self.model

        if not author.is_superuser and (newsitem.is_template
                                        or data.get('is_template')):
            raise ValidationError(
                {'is_template': [lazy_gettext('Access denied')]})

        if 'name' in data:
            from mini_fiction.models import NewsItem
            exist_newsitem = NewsItem.get(name=data['name'])
            if exist_newsitem and exist_newsitem.id != newsitem.id:
                raise ValidationError(
                    {'name': [lazy_gettext('Page already exists')]})

        if data.get('is_template', newsitem.is_template) and 'content' in data:
            self.check_renderability(author, data.get('name', newsitem.name),
                                     data['content'])

        if data.get('show') and not newsitem.show:
            self.hide_shown_newsitem()

        changed_fields = set()
        for key, value in data.items():
            if getattr(newsitem, key) != value:
                setattr(newsitem, key, value)
                changed_fields |= {
                    key,
                }

        if changed_fields:
            AdminLog.bl.create(
                user=author,
                obj=newsitem,
                action=AdminLog.CHANGE,
                fields=sorted(changed_fields),
            )

        return newsitem
Beispiel #16
0
def update(pk):
    newsitem = NewsItem.get(id=pk)
    if not newsitem:
        abort(404)

    form = NewsItemForm(
        data={
            'name': newsitem.name,
            'title': newsitem.title,
            'is_template': newsitem.is_template,
            'show': newsitem.show,
            'content': newsitem.content,
        })

    saved = False
    can_edit = True
    if not current_user.is_superuser:
        if newsitem.is_template:
            can_edit = False
        del form.is_template

    if can_edit and form.validate_on_submit():
        try:
            newsitem.bl.update(current_user._get_current_object(), form.data)
        except ValidationError as exc:
            form.set_errors(exc.errors)
        else:
            saved = True

    return render_template(
        'admin/news/work.html',
        page_title='{} — {}'.format(newsitem.name, newsitem.title
                                    or newsitem.name),
        newsitem=newsitem,
        form=form,
        can_edit=can_edit,
        edit=True,
        saved=saved,
    )
Beispiel #17
0
def delete(pk):
    newsitem = NewsItem.get(id=pk)
    if not newsitem:
        abort(404)

    if not current_user.is_superuser:
        if newsitem.is_template:
            abort(403)

    if request.method == 'POST':
        try:
            newsitem.bl.delete(current_user._get_current_object())
        except ValidationError:
            abort(403)
        else:
            return redirect(url_for('admin_news.index'))

    return render_template(
        'admin/news/delete.html',
        page_title=gettext('Delete'),
        newsitem=newsitem,
    )
Beispiel #18
0
def show(name, comments_page):
    newsitem = NewsItem.get(name=name)
    if not newsitem:
        abort(404)

    if newsitem.is_template:
        template = current_app.jinja_env.from_string(newsitem.content)
        template.name = 'db/news/{}.html'.format(name)
        content = render_template(template, newsitem_name=newsitem.name, newsitem_title=newsitem.title)
    else:
        content = newsitem.content

    per_page = current_user.comments_per_page or current_app.config['COMMENTS_COUNT']['page']
    maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth(current_user)

    comments_count, paged, comments_tree_list = newsitem.bl.paginate_comments(comments_page, per_page, maxdepth)
    paged.page_arg_name = 'comments_page'
    if not comments_tree_list and paged.number != 1:
        abort(404)

    comment_ids = [x[0].id for x in comments_tree_list]
    if current_user.is_authenticated:
        comment_votes_cache = newsitem.bl.select_comment_votes(current_user._get_current_object(), comment_ids)
    else:
        comment_votes_cache = {i: 0 for i in comment_ids}

    data = {
        'page_title': newsitem.title,
        'newsitem': newsitem,
        'content': Markup(content),
        'comments_count': comments_count,
        'page_obj': paged,
        'comments_tree_list': comments_tree_list,
        'comment_form': CommentForm(),
        'comment_votes_cache': comment_votes_cache,
        'sub_comments': newsitem.bl.get_comments_subscription(current_user._get_current_object()),
    }

    return render_template('news/show.html', **data)
Beispiel #19
0
def delete(pk):
    newsitem = NewsItem.get(id=pk)
    if not newsitem:
        abort(404)

    if not current_user.is_superuser:
        if newsitem.is_template:
            abort(403)

    if request.method == 'POST':
        try:
            newsitem.bl.delete(current_user._get_current_object())
        except ValidationError:
            abort(403)
        else:
            return redirect(url_for('admin_news.index'))

    return render_template(
        'admin/news/delete.html',
        page_title=gettext('Delete'),
        newsitem=newsitem,
    )
Beispiel #20
0
def update(pk):
    newsitem = NewsItem.get(id=pk)
    if not newsitem:
        abort(404)

    form = NewsItemForm(data={
        'name': newsitem.name,
        'title': newsitem.title,
        'is_template': newsitem.is_template,
        'show': newsitem.show,
        'content': newsitem.content,
    })

    saved = False
    can_edit = True
    if not current_user.is_superuser:
        if newsitem.is_template:
            can_edit = False
        del form.is_template

    if can_edit and form.validate_on_submit():
        try:
            newsitem.bl.update(current_user._get_current_object(), form.data)
        except ValidationError as exc:
            form.set_errors(exc.errors)
        else:
            saved = True

    return render_template(
        'admin/news/work.html',
        page_title='{} — {}'.format(newsitem.name, newsitem.title or newsitem.name),
        newsitem=newsitem,
        form=form,
        can_edit=can_edit,
        edit=True,
        saved=saved,
    )
Beispiel #21
0
    def update(self, author, data):
        from mini_fiction.models import AdminLog

        data = Validator(NEWS_ITEM).validated(data, update=True)
        newsitem = self.model

        if not author.is_superuser and (newsitem.is_template or data.get('is_template')):
            raise ValidationError({'is_template': [lazy_gettext('Access denied')]})

        if 'name' in data:
            from mini_fiction.models import NewsItem
            exist_newsitem = NewsItem.get(name=data['name'])
            if exist_newsitem and exist_newsitem.id != newsitem.id:
                raise ValidationError({'name': [lazy_gettext('Page already exists')]})

        if data.get('is_template', newsitem.is_template) and 'content' in data:
            self.check_renderability(author, data.get('name', newsitem.name), data['content'])

        if data.get('show') and not newsitem.show:
            self.hide_shown_newsitem()

        changed_fields = set()
        for key, value in data.items():
            if getattr(newsitem, key) != value:
                setattr(newsitem, key, value)
                changed_fields |= {key,}

        if changed_fields:
            AdminLog.bl.create(
                user=author,
                obj=newsitem,
                action=AdminLog.CHANGE,
                fields=sorted(changed_fields),
            )

        return newsitem
Beispiel #22
0
 def hide_shown_newsitem(self):
     from mini_fiction.models import NewsItem
     n = NewsItem.get(show=True)
     if n:
         n.show = False
Beispiel #23
0
 def hide_shown_newsitem(self):
     from mini_fiction.models import NewsItem
     n = NewsItem.get(show=True)
     if n:
         n.show = False