-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
205 lines (170 loc) · 6.81 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from django.shortcuts import render,redirect
from .forms import question
from django.http import HttpResponse,HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib import auth
from django.contrib import messages
from django.contrib.auth.models import User
from .models import Question
from django.contrib.auth.decorators import login_required
def Admin_dash(request):
context={
'title':"welcome to the Admin dashboard"
''
}
return render(request,"forum/Admin_dash.html",context)
def member_dashboard(request):
context={
'title':"welcome to the member dashboard"
}
return render(request,"forum/member_dashboard.html",context)
def categories(request):
context={
'title':"choose the category to ask the question"
'category': category
}
return render(request,"forum/category.html",context)
def ask_question(request):
if not request.user.is_authenticated ():
return HttpResponseRedirect(reverse("member_login"))
form=Question(request.POST or None)
context={
"title":"welcome to the forum dashboard"
"form":form,
}
if request.method=="POST":
form = Question(request.POST or None)
if form.is_valid():
print (form.cleaned_data)
question=fprm.cleaned_data['question']
subject=form.cleaned_data['subject']
User_Id=User.objects.get(pk=request.user.id)
question =Question.object.create(question=question,sub=subject,User_Id=User_Id)
question.save()
message.success(request,"The question is successfully posted.")
return redirect(reverse('ask_question'))
return render(request,"forum/posting_quest.html",context)
def latest_question(request):
question = Question.objects.all().order_by('-timestamp')
context={
'title':"Latest questions are",
'question':question,
}
return render(request,"forum/view_question.html",context)
def answer(request,pk):
if not request.user.is_authenticated ():
return HttpResponseRedirect(reverse("member_login"))
form=answer(request.POST or None)
context={
"title":"Add a answer"
"form":form,
}
if request.method=="POST":
form = answer(request.POST or None)
if form.is_valid():
print (form.cleaned_data)
answer=form.cleaned_data['answer']
Ques_Id=pk
user_Id=User.objects.get(pk=request.user.id)
ans =Answer.object.create(answer=answer,Ques_Id=Ques_id,User_Id=User_Id)
ans.save()
message.success(request,"The answer is successfully posted.")
return redirect(reverse('view_question'))
return render(request,"forum/posting_ans.html",context)
def view_answers(request,pk):
question = Question.objects.get(pk=pk)
try:
answer = Answer.objects.get(Ques_Id=question)
except exception as e:
context={
'answer':"this question is unanswered",
}
return render(request,"forum/view_answer.html",context)
context={
'title':"The answers are",
'answer': answer,
}
return render(request,"forum/view_answer.html",context)
def unanswered_ques(request):
question = Question.objects.all()
ans={}
for pk in question:
try:
answer = Answer.objects.get(Ques_id=pk)
except exception as e:
for j in ans :
j.append(pk)
context={
'title':"the answered questions are",
'question'= question,
'answer'= ans,
}
return render(request,"forum/unasnwered_ques.html",context)
def trending_question(request):
question = Question.objects.all().order_by('-Views')[:50]
context={
'title': "The most viewed questions are"
'question':question
}
return render(request,"forum/trending_question.html",question)
def view_comments(request,pk):
comment = comments.objects.all(pk=pk)
context={
'title':" comments"
'comment':comment
}
return render(request,"forum/view_comments.html",context)
def add_comments(request,pk):
if not request.user.is_authenticated ():
return HttpResponseRedirect(reverse("member_login"))
form=Comment(request.POST or None)
context={
"title":"Add a comment"
"form":form,
}
if request.method=="POST":
form = Comment(request.POST or None)
if form.is_valid():
print (form.cleaned_data)
comment=form.cleaned_data['comment']
Ans_Id=pk
user_Id=User.objects.get(pk=request.user.id)
comment = comment.object.create(comment=answ,Ques_Id=Ques_id,User_Id=User_Id)
question.save()
message.success(request,"The comment is successfully posted.")
return redirect(reverse('view_comments'))
return render(request,"forum/posting_comment.html",context)
def upvote(request,pk):
vo = Votes.objects.filter(Ans_Id=pk)
try:
vo.objects.get(User_Id=request.user.id)
HttpResponse("You have already voted for this answer")
expect e as Exception :
vote= vote.object.create(Ans_Id=pk,User_Id=request.user,UP/Down='U')
HttpResponse("you upvoted for this answer")
Totalvotes= vo.objects.filter(UP/Down='U')- vo.objects.filter(UP/Down='D')
return HttpResponse(Totalvotes)
def downvote(request,pk):
vo = Votes.objects.filter(Ans_Id=pk)
try:
vo.objects.get(User_Id=request.user.id)
HttpResponse("You have already voted for this answer")
expect e as Exception :
vote= vote.object.create(Ans_Id=pk,User_Id=request.user,UP/Down='U')
HttpResponse("you upvoted for this answer")
Totalvotes= vo.objects.filter(UP/Down='U')- vo.objects.filter(UP/Down='D')
return HttpResponse(Totalvotes)
#calculating views per question
#session record ke basis pr chl rha
def views(request):
question = get_object_or_404(Question, pk=question_id)
if not QuestionView.objects.filter(
question=question,
session=request.session.session_key):
view = QuestionView(question=question,
ip=request.META['REMOTE_ADDR'],
created=datetime.datetime.now(),
session=request.session.session_key)
view.save()
return HttpResponse(u"%s" % QuestionView.objects.filter(question=question).count())