Exemple #1
0
def post_show(request, pid):
    "Returns a question with all answers"
    user = request.user

    # populate the session data
    sess = middleware.Session(request)
    tab = "posts"
    pill = sess.get_tab()

    auth = user.is_authenticated()
    layout = settings.USER_PILL_BAR if auth else settings.ANON_PILL_BAR

    params = html.Params(tab=tab, pill=pill, layout=layout)

    query = get_post_manager(request)

    try:
        root = query.get(id=pid)
        # update the views for the question
        models.update_post_views(post=root,
                                 request=request,
                                 minutes=const.POST_VIEW_UPDATE)
        counts = sess.get_counts()

    except models.Post.DoesNotExist, exc:
        messages.warning(
            request,
            'The post that you are looking for does not exists. Perhaps it was deleted!'
        )
        return html.redirect("/")
Exemple #2
0
def show_tag(request, tag_name=None):
    "Display posts by a certain tag"
    user = request.user
    # populate the session data
    sess = middleware.Session(request)

    # get the sort order
    sort_type = sess.sort_order()

    # select based on history
    tab, pill = "posts", sess.get_tab()

    params = html.Params(nav='', tab=tab, sort='')

    # the params object will carry
    layout = settings.USER_PILL_BAR if auth else settings.ANON_PILL_BAR

    # wether to show the type of the post
    params = html.Params(tab=tab,
                         pill=pill,
                         sort=sort_type,
                         sort_choices=SORT_CHOICES,
                         layout=layout,
                         title="Tagged as %s" % tag_name)

    msg = 'Filtering by tag: <b>%s</b>. Subscribe to an <a href="/feeds/tag/%s/">RSS feed</a> to this tag.' % (
        tag_name, tag_name)
    messages.info(request, msg)
    posts = models.query_by_tags(user=user, text=tag_name)
    posts = apply_sort(request=request, posts=posts, order=sort_type)
    page = get_page(request, posts, per_page=20)

    return html.template(request, name='index.html', page=page, params=params)
Exemple #3
0
def post_show(request, pid):
    """
    Displays a post and its children
    """
    user = request.user

    # populate the session data
    sess = middleware.Session(request)
    tab = "posts"
    pill = sess.get_tab()

    auth = user.is_authenticated()
    layout = settings.USER_PILL_BAR if auth else settings.ANON_PILL_BAR

    params = html.Params(tab=tab, pill=pill, layout=layout)

    query = get_post_manager(request)

    try:
        root = query.get(id=pid)
        if not root.top_level:
            return html.redirect(root.get_absolute_url())

        # update the views for the question
        models.update_post_views(post=root, request=request)
        counts = sess.get_counts()

    except models.Post.DoesNotExist, exc:
        messages.warning(
            request,
            'The post that you are looking for does not exists. Perhaps it was deleted!'
        )
        return html.redirect("/")
Exemple #4
0
    def get_context_data(self, **kwargs):
        context = super(MessageView, self).get_context_data(**kwargs)

        user = self.request.user

        note_types = [NOTE_USER, NOTE_PRIVATE]

        q = Q(target=user, type__in=note_types)
        e = Q(sender=user, type=NOTE_USER)

        notes = models.Note.objects.filter(q).exclude(e)
        notes = notes.select_related('author',
                                     'author__profile').order_by('-date')
        page = html.get_page(request=self.request, obj_list=notes, per_page=25)

        # evaluate the query here so that the unread status is kept
        page.object_list = list(page.object_list)

        # reset the counts
        models.Note.objects.filter(target=user,
                                   unread=True).update(unread=False)
        models.UserProfile.objects.filter(user=user).update(new_messages=0)

        sess = middleware.Session(self.request)
        counts = sess.get_counts("message_count")
        sess.save()

        # the params object will carry
        layout = settings.USER_PILL_BAR

        params = html.Params(tab="",
                             pill="messages",
                             sort='',
                             since='',
                             layout=layout,
                             title="Your Messages")

        context['page'] = page
        context['params'] = params
        context['counts'] = counts

        return context
Exemple #5
0
def index(request, tab='all'):
    user = request.user
    auth = user.is_authenticated()

    # asking for an invalid tab
    if tab not in VALID_TABS:
        msg = html.sanitize('Unknown content type "%s"' % tab)
        messages.error(request, msg)
        return html.redirect("/")

    # populate the session data
    sess = middleware.Session(request)

    # get the sort order
    sort_type = sess.sort_order()

    # set the last active tab
    sess.set_tab(tab)

    # get the numerical value for these posts
    post_type = POST_TYPE_MAP.get(tab, tab)

    # override the sort order if the content so requires
    sort_type = 'creation' if tab == 'recent' else sort_type

    # the params object will carry
    layout = settings.USER_PILL_BAR if auth else settings.ANON_PILL_BAR

    # wether to show the type of the post
    show_type = post_type in ('all', 'recent')

    if tab in VALID_PILLS:
        tab, pill = "posts", tab
    else:
        tab, pill = tab, ""
    params = html.Params(tab=tab,
                         pill=pill,
                         sort=sort_type,
                         sort_choices=SORT_CHOICES,
                         layout=layout,
                         show_type=show_type,
                         title="Bioinformatics Answers")

    # this will fill in the query (q) and the match (m)parameters
    params.parse(request)

    # returns the object manager that contains all or only visible posts
    posts = get_post_manager(request)

    # filter posts by type
    posts = filter_by_type(request=request, posts=posts, post_type=post_type)

    # sticky is not active on recent and all pages
    sticky = (tab != 'recent') and (pill != 'all')

    # order may change if it is invalid search
    posts = apply_sort(request=request,
                       posts=posts,
                       order=sort_type,
                       sticky=sticky)

    # this is necessary because the planet posts require more attributes
    if tab == 'planet':
        models.decorate_posts(posts, request.user)

    # get the counts for the session
    counts = sess.get_counts(post_type)
    page = get_page(request, posts, per_page=POSTS_PER_PAGE)

    # save the session
    sess.save()

    # try to set a more informative title
    title_map = dict(questions="Bioinformatics Questions",
                     unanswered="Unanswered Questions",
                     tutorials="Bioinformatics Tutorials",
                     jobs="Bioinformatics Jobs",
                     videos="Bioinformatics Videos",
                     news='Bioinformatics News',
                     tools="Bioinformatics Tools",
                     recent="Recent bioinformatics posts",
                     planet="Bioinformatics Planet")
    params.title = title_map.get(tab, params.title)

    return html.template(request,
                         name='index.html',
                         page=page,
                         params=params,
                         counts=counts)
Exemple #6
0
def index(request, tab='all'):
    user = request.user
    auth = user.is_authenticated()

    # asking for an invalid tab
    if tab not in VALID_TABS:
        msg = html.sanitize('Unknown content type "%s"' % tab)
        messages.error(request, msg)
        return html.redirect("/")

    # populate the session data
    sess = middleware.Session(request)

    # get the sort order
    sort_type = sess.sort_order()

    # parse the date request
    since = request.GET.get('since', DATE_FILTER[0]).lower()

    # set the last active tab
    sess.set_tab(tab)

    # get the numerical value for these posts
    post_type = POST_TYPE_MAP.get(tab, tab)

    # override the sort order if the content so requires
    sort_type = 'creation' if tab == 'recent' else sort_type

    # this here needs to be reworked TODO
    if tab == "best":
        sort_type = "votes"
        since = request.GET.get('since', 'this week')
        messages.info(request,
                      "Most <b>upvoted</b> active posts of <b>%s!</b>" % since)

    elif tab == "bookmarked":
        sort_type = "bookmark"
        since = request.GET.get('since', 'this month')
        messages.info(
            request,
            "Most <b>bookmarked</b> active posts of <b>%s!</b>" % since)
    elif tab == "myvotes":
        sort_type = "votes__date"
        messages.info(
            request,
            "Posts created by you that have received up-votes from other users"
        )
    elif tab == "mybookmarks":
        sort_type = "votes__date"
        messages.info(request, "Your bookmarked posts")
    elif tab == "myposts":
        sort_type = "creation"
        messages.info(request, "Posts created by you")

    # the params object will carry
    layout = settings.USER_PILL_BAR if auth else settings.ANON_PILL_BAR

    # wether to show the type of the post
    show_type = post_type in ('all', 'recent')

    show_search = True

    if tab in VALID_PILLS:
        tab, pill = "posts", tab
    else:
        tab, pill = tab, ""

    params = html.Params(tab=tab,
                         pill=pill,
                         sort=sort_type,
                         sort_choices=SORT_CHOICES,
                         date_filter=DATE_FILTER,
                         since=since,
                         layout=layout,
                         show_type=show_type,
                         title="Bioinformatics Answers",
                         show_search=show_search)

    # this will fill in the query (q) and the match (m)parameters
    params.parse(request)

    # returns the object manager that contains all or only visible posts
    posts = get_post_manager(request)

    # filter posts by type
    posts = filter_by_type(request=request, posts=posts, post_type=post_type)

    # apply date filtering
    posts = filter_by_date(request=request, posts=posts, since=since)

    # reduce SQL query count by preselecting data that will be displayed
    posts = posts.select_related('author', 'author__profile', 'lastedit_user',
                                 'lastedit_user__profile')

    # sticky is not active on recent and all pages
    sticky = (tab != 'recent') and (pill not in ('all', "best", "bookmarked"))

    # hackfix
    sticky = False if pill.startswith("my") else sticky

    # order may change if it is invalid search
    posts = apply_sort(request=request,
                       posts=posts,
                       order=sort_type,
                       sticky=sticky)

    # get the counts for the session
    counts = sess.get_counts(post_type)

    page = get_page(request, posts, per_page=settings.POSTS_PER_PAGE)

    # save the session
    sess.save()

    # try to set a more informative title
    title_map = dict(
        questions="Bioinformatics Questions",
        unanswered="Unanswered Questions",
        tutorials="Bioinformatics Tutorials",
        jobs="Bioinformatics Jobs",
        videos="Bioinformatics Videos",
        news='Bioinformatics News',
        tools="Bioinformatics Tools",
        recent="Recent bioinformatics posts",
        planet="Bioinformatics Planet",
        galaxy="Galaxy on Biostar",
        bookmarked="Most bookmarked",
    )

    params.title = title_map.get(pill) or title_map.get(tab, params.title)

    return html.template(request,
                         name='index.html',
                         page=page,
                         params=params,
                         counts=counts)