Beispiel #1
0
    def index(self):
        page = get_page_number(tk.request.params)
        total_rows = Thread.all().count()
        total_pages = ((Thread.all().count() - 1) / self.paginated_by + 1) or 1
        if not 0 < page <= total_pages:
            # redirect for delete page parameter reason
            redirect_to('forum_index')
        thread_list = Thread.all().offset(
            (page - 1) * self.paginated_by).limit(self.paginated_by)

        c.page = Page(collection=thread_list,
                      page=page,
                      item_count=total_rows,
                      items_per_page=self.paginated_by)
        return self.__render('forum_index.html', {'thread_list': thread_list})
Beispiel #2
0
 def thread_ban(self, id):
     do_if_user_not_sysadmin()
     thread = Thread.get_by_id(id=id)
     if not thread:
         abort(404)
     thread.ban()
     if tk.request.GET.get('with_user'):
         BannedUser.ban(thread.author_id)
     tk.redirect_to(tk.url_for('forum_activity'))
Beispiel #3
0
    def unsubscribe(self, base64_name, thread_id):
        log.debug('Unsubscribing %s %s', base64.b64decode(base64_name),
                  thread_id)
        thread = Thread.get_by_id(thread_id)
        user = User.get(base64.b64decode(base64_name))
        if not thread or not user:
            abort(404)

        Unsubscription.add(user.id, thread.id)
        flash_success(tk._('You successfully unsibsribed'))
        tk.redirect_to(thread.get_absolute_url())
Beispiel #4
0
 def board_show(self, slug):
     board = Board.get_by_slug(slug)
     if not board:
         abort(404)
     page = get_page_number(tk.request.params)
     total_rows = Thread.filter_board(board_slug=board.slug).count()
     total_pages = (total_rows / self.paginated_by + 1) or 1
     if not 0 < page <= total_pages:
         # redirect for delete page parameter reason
         redirect_to('forum_index')
     thread_list = Thread.filter_board(board_slug=board.slug).offset(
         (page - 1) * self.paginated_by).limit(self.paginated_by)
     c.page = Page(collection=thread_list,
                   page=page,
                   item_count=total_rows,
                   items_per_page=self.paginated_by)
     return self.__render('forum_index.html', {
         'board': board,
         'thread_list': thread_list
     })
Beispiel #5
0
def send_notifications_on_new_post(post, lang):
    from ckan.model import User
    template_dir = os.path.join(os.path.dirname(__file__), 'templates')
    locale_dir = os.path.join(os.path.dirname(__file__), 'i18n')
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
                             extensions=['jinja2.ext.i18n'])
    translations = Translations.load(locale_dir, [lang],
                                     domain='ckanext-forum')
    env.install_gettext_translations(translations)
    env.globals['get_locale'] = lambda: lang
    post_author = User.get(post.author_id)

    thread = Thread.get_by_id(post.thread_id)
    author_ids = set([p.author_id
                      for p in thread.forum_posts] + [thread.author_id])
    author_ids -= set([
        u.user_id for u in Unsubscription.filter_by_thread_id(post.thread_id)
    ])

    for author_id in author_ids:
        if author_id == post_author.id:
            continue
        user = User.get(author_id)
        unsubscribe_url = tk.url_for('forum_unsubscribe',
                                     base64_name=base64.b64encode(user.name),
                                     thread_id=thread.id)
        context = {
            'user_name':
            user.name,
            'site_title':
            tk.config.get('ckan.site_title'),
            'site_url':
            tk.config.get('ckan.site_url'),
            'post_content':
            post.content,
            'title':
            env.globals['gettext']('New post'),
            'unsubscribe_url':
            urljoin(tk.config['ckan.site_url'], unsubscribe_url),
            'username':
            post_author.name,
            'thread_url':
            urljoin(tk.config['ckan.site_url'], thread.get_absolute_url()),
        }
        template = env.get_template('forum_new_post_mail.html')
        body = template.render(context)
        log.debug('Email body %s', body)
        tk.get_action('send_mail')(
            {}, {
                'to': user.email,
                'subject': env.globals['gettext']('New post'),
                'message_html': body
            })
Beispiel #6
0
def forum_create_thread(context, data_dict):
    """
    Create new thread on board
    data_dict:
        board_id - Board id for thread
        name - Thread name
        content - Thread text
        can_post - False if thread is closed for posts
    :return Thread
    """
    thread = Thread()
    thread.board_id = logic.get_or_bust(data_dict, 'board_id')
    thread.name = logic.get_or_bust(data_dict, 'name')
    thread.content = strip_tags(logic.get_or_bust(data_dict, 'content'))
    thread.author = logic.get_or_bust(context, 'auth_user_obj')
    if 'can_post' in data_dict and not data_dict['can_post']:
        thread.can_post = False
    thread.save()
    return thread
Beispiel #7
0
 def activity(self):
     do_if_user_not_sysadmin()
     page = get_page_number(tk.request.params)
     total_rows = Thread.all().count()
     total_pages = ((total_rows - 1) / self.paginated_by + 1) or 1
     if not 0 < page <= total_pages:
         # redirect for delete page parameter reason
         redirect_to('forum_activity')
     thread_activity = Thread.all().order_by(Thread.created.desc())
     post_activity = Post.all().order_by(Post.created.desc())
     activity = [
         dict(id=i.id,
              url=i.get_absolute_url(),
              ban_url=tk.url_for('forum_thread_ban', id=i.id),
              type=tk._('Thread'),
              content=i.content,
              author_name=i.author.name,
              created=i.created) for i in thread_activity
     ]
     activity += [
         dict(id=i.id,
              url=i.get_absolute_url(),
              ban_url=tk.url_for('forum_post_ban', id=i.id),
              type=tk._('Post'),
              content=i.content,
              author_name=i.author.name,
              created=i.created) for i in post_activity
     ]
     activity = sorted(activity, key=itemgetter('created'), reverse=True)
     start_elem = int((page - 1) * self.paginated_by)
     end_elem = int(page * self.paginated_by)
     c.page = Page(collection=thread_activity,
                   page=page,
                   item_count=total_rows,
                   items_per_page=self.paginated_by)
     return self.__render('forum_activity.html',
                          {'activity': activity[start_elem:end_elem]})
Beispiel #8
0
def forum_create_post(context, data_dict):
    """
    Create post in thread
    data_dict:
        thread_id - Thread id for post
        content - Post text
    :return Post
    """
    thread = Thread.get_by_id(logic.get_or_bust(data_dict, 'thread_id'))
    if thread.can_post:
        post = Post()
        post.thread = thread
        post.content = strip_tags(logic.get_or_bust(data_dict, 'content'))
        post.author_id = logic.get_or_bust(context, 'auth_user_obj').id
        post.save()
        return post
Beispiel #9
0
 def thread_show(self, slug, id):
     thread = Thread.get_by_id(id=id)
     if not thread:
         abort(404)
     if not thread.active and (not c.userobj or not c.userobj.sysadmin):
         abort(404)
     form = CreatePostForm(tk.request.POST)
     if tk.request.POST:
         if c.userobj is None:
             tk.redirect_to(tk.url_for(controller='user', action='login'))
         if BannedUser.check_by_id(c.userobj):
             flash_error(tk._('You are banned'))
             tk.redirect_to(thread.get_absolute_url())
         if form.validate():
             post = tk.get_action('forum_create_post')(
                 {
                     'auth_user_obj': c.userobj
                 }, {
                     'thread_id': id,
                     'content': form.data['content']
                 })
             if post:
                 jobs.enqueue(
                     send_notifications_on_new_post,
                     [post, tk.request.environ.get('CKAN_LANG')])
                 flash_success(tk._('You successfully create comment'))
             else:
                 flash_error(tk._('Thread is closed for comments'))
             return tk.redirect_to(thread.get_absolute_url())
         else:
             flash_error(tk._('You have errors in form'))
     page = get_page_number(tk.request.params)
     total_rows = Post.filter_thread(thread.id).count()
     total_pages = (total_rows / self.paginated_by + 1) or 1
     if not 0 < page <= total_pages:
         # redirect for delete page parameter reason
         redirect_to('forum_index')
     posts_list = Post.filter_thread(thread.id).offset(
         (page - 1) * self.paginated_by).limit(self.paginated_by)
     c.page = Page(collection=posts_list,
                   page=page,
                   item_count=total_rows,
                   items_per_page=self.paginated_by)
     context = {'thread': thread, 'form': form, 'posts': posts_list}
     return self.__render('thread.html', context)