コード例 #1
0
ファイル: views.py プロジェクト: whpeddyc/8bitmooc
def view_thread(request, name, thread):
    me = Student.from_request(request)
    if "alerts" not in request.session: request.session["alerts"] = []
    if not me:
        request.session["alerts"].append(("alert-error","Please sign in first."))
        return redirect("sign-in")   
    
    # First, try to get the challenge, then thread.
    try:
        board = DiscussionBoard.objects.get(slug=name)
        topic = DiscussionTopic.objects.get(id=thread, board=board)
    except exceptions.ObjectDoesNotExist: raise Http404()
    if not me.ta and (topic.hidden or board.restricted > me.level): raise Http404()
    
    # If this is a POST, we are either replying to someone or we are voting.
    # Manage permissions respectively and redirect.
    if request.method == "POST":
        if "thread" in request.POST and (me.level >= board.wrestricted or me.ta):
            p = DiscussionPost()
            p.topic = topic
            p.author = me
            p.content = str(request.POST.get("content"))
            p.save()
            topic.save()
        elif "upvote" in request.POST or "downvote" in request.POST:
            upvote = True
            if "downvote" in request.POST: upvote = False
            p_id = request.POST["upvote" if upvote else "downvote"]
            if p_id.isdigit() and (me.modpoints > 0 or me.ta):
                p = DiscussionPost.objects.get(id=int(p_id))
                if upvote: p.upvotes += 1
                else:      p.downvotes += 1
                request.session["alerts"].append(("alert-success","Post %s."%("upvoted" if upvote else "downvoted")))
                LogEntry.log(request, "Thread %s"%("upvoted" if upvote else "downvoted"))
                p.save()
                me.modpoints -= 1
                me.award_xp(1)
                me.save()
        return redirect( "thread", name=board.slug, thread=topic.id )
    
    # Get all of the posts. Start on the last page by default.
    pagination = 20
    posts = DiscussionPost.objects.filter(hidden=False, topic=topic)
    pages = (len(posts)/pagination)+1
    page = pages-1
    if "page" in request.GET and request.GET["page"].isdigit():
        page = max(0,int(request.GET["page"])-1)
    
    return render( request, "forum_thread.html", {'board': board,
                                                  'topic': topic,
                                                  'student': me,
                                                  'posts': posts[pagination*page:pagination*(page+1)],
                                                  'alerts': request.session.pop('alerts', []),
                                                  'page': page+1,
                                                  'pages': pages })
コード例 #2
0
ファイル: views.py プロジェクト: isharacomix/8bitmooc
def view_board(request, category):
    me = Student.from_request(request)
    
    # First, try to get the board.
    try: board = DiscussionBoard.objects.get(slug=category)
    except exceptions.ObjectDoesNotExist: raise Http404()
    if not board.can_read(me): raise Http404()
    
    # Get the page number!
    page = 0
    pagination = 50
    if "page" in request.GET and request.GET["page"].isdigit():
        page = max(0,int(request.GET["page"])-1)
    
    # If this is a POST, we are creating a new topic. Redirect when finished.
    if request.method == "POST":
        if board.can_write(me):
            content = str(request.POST.get("content"))
            title = content[:100]+"..."
            if "title" in request.POST:
                title = request.POST["title"]
            
            t = DiscussionTopic()
            t.author = me
            t.board = board
            t.title = title
            t.save()
            
            p = DiscussionPost()
            p.topic = t
            p.author = me
            p.content = content
            p.save()
        
            return redirect( "thread", category=board.slug, thread=t.id )
        else:
            return redirect( "board", category=board.slug )
    
    # Get all of the topics, along with the last person who commented on them
    # and when that was.
    topic_tuples = []
    topics = DiscussionTopic.objects.filter(board=board)
    if not me.ta:
        topics = topics.filter(hidden=False)
    for t in topics[pagination*page:pagination*(page+1)]:
        posts = DiscussionPost.objects.filter(topic=t, hidden=False).order_by("-timestamp")
        count = len(posts)
        new = False
        if count == 0: posts = [None]
        else: new = ( posts[0].timestamp > me.unread_since )
        topic_tuples.append( (t,count,posts[0],new) )
    
    return render( request, "forum_topics.html", {'board': board,
                                                  'topics': topic_tuples,
                                                  'alerts': request.session.pop('alerts', []),
                                                  'page': page+1,
                                                  'pages': (len(topic_tuples)/pagination)+1,
                                                  'can_write': board.can_write(me) } )
コード例 #3
0
ファイル: views.py プロジェクト: whpeddyc/8bitmooc
def view_board(request, name):
    me = Student.from_request(request)
    if "alerts" not in request.session: request.session["alerts"] = []
    if not me:
        request.session["alerts"].append(("alert-error","Please sign in first."))
        return redirect("sign-in")   
    
    # First, try to get the board.
    try: board = DiscussionBoard.objects.get(slug=name)
    except exceptions.ObjectDoesNotExist: raise Http404()
    if board.restricted > me.level: raise Http404()
    
    # Get the page number!
    page = 0
    pagination = 200
    if "page" in request.GET and request.GET["page"].isdigit():
        page = max(0,int(request.GET["page"])-1)
    
    # If this is a POST, we are creating a new topic. Redirect when finished.
    if request.method == "POST" and (me.level >= board.wrestricted or me.ta):
        content = str(request.POST.get("content"))
        title = content[:100]+"..."
        if "title" in request.POST:
            title = request.POST["title"]
        
        t = DiscussionTopic()
        t.author = me
        t.board = board
        t.title = title
        t.save()
        
        p = DiscussionPost()
        p.topic = t
        p.author = me
        p.content = content
        p.save()
        
        return redirect( "thread", name=board.slug, thread=t.id )
    
    # Get all of the topics, along with the last person who commented on them
    # and when that was.
    topic_tuples = []
    for t in DiscussionTopic.objects.filter(hidden=False, board=board)[pagination*page:pagination*(page+1)]:
        posts = DiscussionPost.objects.filter(topic=t, hidden=False).order_by("-timestamp")
        count = len(posts)
        new = False
        if count == 0: posts = [None]
        else: new = ( posts[0].timestamp > me.last_login )
        topic_tuples.append( (t,count,posts[0],new) )
    
    return render( request, "forum_topics.html", {'board': board,
                                                  'topics': topic_tuples,
                                                  'student': me,
                                                  'alerts': request.session.pop('alerts', []),
                                                  'page': page+1,
                                                  'pages': (len(topic_tuples)/pagination)+1 } )
コード例 #4
0
ファイル: json.py プロジェクト: EM-Che/wasa2il
def discussion_comment_send(request):
    discussion = get_object_or_404(Discussion, id=request.REQUEST.get("discussion", 0))
    text = request.REQUEST.get("comment")
    if request.user not in discussion.forum.polity.members.all():
        return discussion_poll(request)

    comment = DiscussionPost()
    comment.user = request.user
    comment.text = text
    comment.discussion = discussion
    comment.save()
    return discussion_poll(request)
コード例 #5
0
ファイル: views.py プロジェクト: isharacomix/8bitmooc
def view_thread(request, category, thread):
    me = Student.from_request(request)
    
    # First, try to get the challenge, then thread.
    try:
        board = DiscussionBoard.objects.get(slug=category)
        topic = DiscussionTopic.objects.get(id=thread, board=board)
    except exceptions.ObjectDoesNotExist: raise Http404()
    if not board.can_read(me): raise Http404()
    
    
    # If this is a POST, we are either replying to someone or we are voting.
    # Manage permissions respectively and redirect.
    if request.method == "POST":
        if "hide" in request.POST and me.ta:
            try:
                p = DiscussionPost.objects.get(id=int(request.POST["hide"]))
                p.hidden = not p.hidden
                p.save()
            except: pass
        elif "topic" in request.POST and me.ta:
            if "lock" == request.POST.get("topic"):
                topic.locked = not topic.locked
            if "hide" == request.POST.get("topic"):
                topic.hidden = not topic.hidden
            topic.save()
        elif "content" in request.POST and board.can_write(me) and (not topic.locked):
            p = DiscussionPost()
            p.topic = topic
            p.author = me
            p.content = str(request.POST.get("content"))
            p.save()
            topic.save()
        return redirect( "thread", category=board.slug, thread=topic.id )
    
    # Get all of the posts. Start on the last page by default.
    pagination = 20
    posts = DiscussionPost.objects.filter(topic=topic)
    if not me.ta:
        post = posts.filter(hidden=False)
    pages = (len(posts)/pagination)+1
    page = pages-1
    if "page" in request.GET and request.GET["page"].isdigit():
        page = max(0,int(request.GET["page"])-1)
    
    return render( request, "forum_thread.html", {'board': board,
                                                  'topic': topic,
                                                  'posts': posts[pagination*page:pagination*(page+1)],
                                                  'alerts': request.session.pop('alerts', []),
                                                  'page': page+1,
                                                  'pages': pages,
                                                  'can_write': board.can_write(me) })
コード例 #6
0
ファイル: views.py プロジェクト: whpeddyc/8bitmooc
def view_board(request, name):
    me = Student.from_request(request)
    if "alerts" not in request.session: request.session["alerts"] = []
    if not me:
        request.session["alerts"].append(
            ("alert-error", "Please sign in first."))
        return redirect("sign-in")

    # First, try to get the board.
    try:
        board = DiscussionBoard.objects.get(slug=name)
    except exceptions.ObjectDoesNotExist:
        raise Http404()
    if board.restricted > me.level: raise Http404()

    # Get the page number!
    page = 0
    pagination = 200
    if "page" in request.GET and request.GET["page"].isdigit():
        page = max(0, int(request.GET["page"]) - 1)

    # If this is a POST, we are creating a new topic. Redirect when finished.
    if request.method == "POST" and (me.level >= board.wrestricted or me.ta):
        content = str(request.POST.get("content"))
        title = content[:100] + "..."
        if "title" in request.POST:
            title = request.POST["title"]

        t = DiscussionTopic()
        t.author = me
        t.board = board
        t.title = title
        t.save()

        p = DiscussionPost()
        p.topic = t
        p.author = me
        p.content = content
        p.save()

        return redirect("thread", name=board.slug, thread=t.id)

    # Get all of the topics, along with the last person who commented on them
    # and when that was.
    topic_tuples = []
    for t in DiscussionTopic.objects.filter(
            hidden=False,
            board=board)[pagination * page:pagination * (page + 1)]:
        posts = DiscussionPost.objects.filter(
            topic=t, hidden=False).order_by("-timestamp")
        count = len(posts)
        new = False
        if count == 0: posts = [None]
        else: new = (posts[0].timestamp > me.last_login)
        topic_tuples.append((t, count, posts[0], new))

    return render(
        request, "forum_topics.html", {
            'board': board,
            'topics': topic_tuples,
            'student': me,
            'alerts': request.session.pop('alerts', []),
            'page': page + 1,
            'pages': (len(topic_tuples) / pagination) + 1
        })
コード例 #7
0
ファイル: views.py プロジェクト: whpeddyc/8bitmooc
def view_thread(request, name, thread):
    me = Student.from_request(request)
    if "alerts" not in request.session: request.session["alerts"] = []
    if not me:
        request.session["alerts"].append(
            ("alert-error", "Please sign in first."))
        return redirect("sign-in")

    # First, try to get the challenge, then thread.
    try:
        board = DiscussionBoard.objects.get(slug=name)
        topic = DiscussionTopic.objects.get(id=thread, board=board)
    except exceptions.ObjectDoesNotExist:
        raise Http404()
    if not me.ta and (topic.hidden or board.restricted > me.level):
        raise Http404()

    # If this is a POST, we are either replying to someone or we are voting.
    # Manage permissions respectively and redirect.
    if request.method == "POST":
        if "thread" in request.POST and (me.level >= board.wrestricted
                                         or me.ta):
            p = DiscussionPost()
            p.topic = topic
            p.author = me
            p.content = str(request.POST.get("content"))
            p.save()
            topic.save()
        elif "upvote" in request.POST or "downvote" in request.POST:
            upvote = True
            if "downvote" in request.POST: upvote = False
            p_id = request.POST["upvote" if upvote else "downvote"]
            if p_id.isdigit() and (me.modpoints > 0 or me.ta):
                p = DiscussionPost.objects.get(id=int(p_id))
                if upvote: p.upvotes += 1
                else: p.downvotes += 1
                request.session["alerts"].append(
                    ("alert-success",
                     "Post %s." % ("upvoted" if upvote else "downvoted")))
                LogEntry.log(
                    request,
                    "Thread %s" % ("upvoted" if upvote else "downvoted"))
                p.save()
                me.modpoints -= 1
                me.award_xp(1)
                me.save()
        return redirect("thread", name=board.slug, thread=topic.id)

    # Get all of the posts. Start on the last page by default.
    pagination = 20
    posts = DiscussionPost.objects.filter(hidden=False, topic=topic)
    pages = (len(posts) / pagination) + 1
    page = pages - 1
    if "page" in request.GET and request.GET["page"].isdigit():
        page = max(0, int(request.GET["page"]) - 1)

    return render(
        request, "forum_thread.html", {
            'board': board,
            'topic': topic,
            'student': me,
            'posts': posts[pagination * page:pagination * (page + 1)],
            'alerts': request.session.pop('alerts', []),
            'page': page + 1,
            'pages': pages
        })