• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gcivil-nyu-org / INT2-Monday-Spring2024-Team-2 / 700

14 Apr 2024 02:25PM UTC coverage: 89.474% (-1.6%) from 91.062%
700

Pull #221

travis-pro

wuyasan
update the comments
Pull Request #221: Community v2

74 of 91 new or added lines in 8 files covered. (81.32%)

16 existing lines in 1 file now uncovered.

1683 of 1881 relevant lines covered (89.47%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.75
/Community/views.py
1
from django.shortcuts import render, get_object_or_404, redirect
1✔
2
from .forms import CreatePostForm, CreateReplyForm
1✔
3
from django.core.paginator import Paginator
1✔
4
from django.db.models import Count, Prefetch
1✔
5
from django.http import JsonResponse
1✔
6
from TutorRegister.models import Post, Reply, Vote
1✔
7
from TutorFilter.views import get_display_expertise
1✔
8
from TutorRegister.presets import EXPERTISE_CHOICES
1✔
9

10

11
def view_all_posts(request):
1✔
12
    userType = request.user.usertype.user_type
1✔
13
    labels = ["resource", "question"]
1✔
14

15
    posts = (
1✔
16
        Post.objects.select_related("user__usertype")
17
        .prefetch_related(
18
            Prefetch(
19
                "post_replies",
20
                queryset=Reply.objects.order_by("-reply_date").select_related(
21
                    "user__usertype"
22
                ),
23
                to_attr="ordered_replies",
24
            )
25
        )
26
        .annotate(reply_count=Count("post_replies"))
27
        .order_by("-post_date")
28
    )
29

30
    label = request.GET.get("label")
1✔
31

32
    if label:
1✔
NEW
33
        posts = posts.filter(label=label)
×
34

35
    all_topics = [item[0] for item in EXPERTISE_CHOICES]
1✔
36
    all_topics.append("{}")
1✔
37

38
    # Prepare processed topics for display in the dropdown menu
39
    processed_topics = [{topic: get_display_topic(topic)} for topic in all_topics]
1✔
40

41
    topic = request.GET.get("topic")
1✔
42

43
    if topic:
1✔
NEW
44
        if topic not in all_topics:
×
NEW
45
            for item in processed_topics:
×
NEW
46
                if topic in item.values():
×
NEW
47
                    topic = list(item.keys())[0]
×
NEW
48
                    break
×
NEW
49
        posts = posts.filter(topics=topic)
×
50

51
    for post in posts:
1✔
52
        post.topics = get_display_topic(post.topics)
1✔
53

54
    paginator = Paginator(posts, 5)
1✔
55

56
    page_number = request.GET.get("page")
1✔
57
    page_obj = paginator.get_page(page_number)
1✔
58

59
    context = {
1✔
60
        "baseTemplate": (
61
            "Dashboard/base_student.html"
62
            if userType == "student"
63
            else "Dashboard/base_tutor.html"
64
        ),
65
        "posts": page_obj,
66
        "labels": labels,
67
        "topics": processed_topics,
68
    }
69
    # return render all posts
70
    return render(request, "posts.html", context)
1✔
71

72

73
def view_post_detail(request, post_id):
1✔
74
    post = get_object_or_404(Post, pk=post_id)
1✔
75
    replies = Reply.objects.filter(post=post).order_by("-reply_date")
1✔
76
    userType = request.user.usertype.user_type
1✔
77
    num_r = replies.count()
1✔
78

79
    post.topics = get_display_topic(post.topics)
1✔
80

81
    paginator = Paginator(replies, 5)
1✔
82

83
    page_number = request.GET.get("page")
1✔
84
    page_obj = paginator.get_page(page_number)
1✔
85

86
    if request.method == "POST":
1✔
87
        form = CreateReplyForm(request.POST)
1✔
88
        if form.is_valid():
1✔
89
            reply = form.save(commit=False)
1✔
90
            reply.user = request.user
1✔
91
            reply.post = post
1✔
92
            reply.save()
1✔
93

94
        return redirect("Community:post_detail", post_id=post.id)
1✔
95
    else:
96
        form = CreateReplyForm()
1✔
97

98
    context = {
1✔
99
        "baseTemplate": (
100
            "Dashboard/base_student.html"
101
            if userType == "student"
102
            else "Dashboard/base_tutor.html"
103
        ),
104
        "replies": page_obj,
105
        "r_form": form,
106
        "post": post,
107
        "num_r": num_r,
108
    }
109

110
    return render(request, "post_detail.html", context)
1✔
111

112

113
def create_post(request):
1✔
114
    userType = request.user.usertype.user_type
1✔
115
    if request.method == "POST":
1✔
116
        form = CreatePostForm(request.POST, request.FILES)
1✔
117
        if form.is_valid():
1✔
118
            post = form.save(commit=False)
1✔
119
            post.user = request.user
1✔
120
            post.save()
1✔
121
            return redirect("Community:all_posts")
1✔
122
    else:
123
        form = CreatePostForm()
1✔
124

125
    context = {
1✔
126
        "form": form,
127
        "baseTemplate": (
128
            "Dashboard/base_student.html"
129
            if userType == "student"
130
            else "Dashboard/base_tutor.html"
131
        ),
132
    }
133

134
    return render(request, "create_post.html", context)
1✔
135

136

137
def vote(request, post_id, vote_type):
1✔
NEW
138
    post = get_object_or_404(Post, pk=post_id)
×
NEW
139
    user_react, created = Vote.objects.get_or_create(user=request.user, post=post)
×
140

NEW
141
    if vote_type == "upvote":
×
NEW
142
        new_value = 1 if user_react.value != 1 else 0
×
NEW
143
    elif vote_type == "downvote":
×
NEW
144
        new_value = -1 if user_react.value != -1 else 0
×
145

NEW
146
    user_react.value = new_value
×
NEW
147
    user_react.save()
×
148

NEW
149
    rating = post.get_rating()
×
150

NEW
151
    return JsonResponse({"rating": rating})
×
152

153

154
def get_display_topic(topic):
1✔
155
    if topic == "{}":
1✔
156
        return "Other"
1✔
157
    else:
158
        return get_display_expertise(topic)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc