-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.py
243 lines (184 loc) · 7.12 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# coding=utf-8
from django.contrib.auth import login, authenticate
from django.http import HttpResponseForbidden
from django.core.urlresolvers import reverse_lazy
from django.views.generic import TemplateView, DeleteView, DetailView, ListView, UpdateView, CreateView, FormView, View
from django.views.generic.detail import SingleObjectMixin
from ask_app.models import *
from ask_app.forms import *
# class for template
class AskAppTemplateView(TemplateView):
template_name = 'base.html'
# edit this func to see popular users ans tags
def get_context_data(self, **kwargs):
data = super(AskAppTemplateView, self).get_context_data(**kwargs)
# edit here
data['best_users'] = User.objects.all()[:5]
data['tags'] = Tags.objects.all()[:5]
return data
# classes for working with Question models
# show all questions
class QuestionListView(ListView):
# use CustomManeger
queryset = Question.my.newest()
paginate_by = 5
def get_context_data(self, **kwargs):
data = super(QuestionListView, self).get_context_data(**kwargs)
data['best_users'] = User.objects.all()[:5]
data['tags'] = Tags.objects.all()[:3]
return data
# inclusion tags
# show hot answers
class QuestionListViewHot(ListView):
# use CustomManeger
queryset = Question.my.hot()
paginate_by = 5
def get_context_data(self, **kwargs):
data = super(QuestionListViewHot, self).get_context_data(**kwargs)
data['best_users'] = User.objects.all()[:5]
data['tags'] = Tags.objects.all()[:3]
return data
# create one question
class QuestionCreateView(CreateView):
model = Question
form_class = CreateQuestionForm
template_name_suffix = '_create_form'
success_url = reverse_lazy('home-page')
def get_context_data(self, **kwargs):
data = super(QuestionCreateView, self).get_context_data(**kwargs)
# edit here
data['best_users'] = User.objects.all()[:5]
data['tags'] = Tags.objects.all()[:5]
return data
def form_valid(self, form):
form.instance.author = self.request.user
return super(QuestionCreateView, self).form_valid(form)
# see one question, if we had POST - FormView, GET - DetailView
class QuestionDetailView(View):
def get(self, request, *args, **kwargs):
view = QuestionDetailViewShow.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = QuestionFormViewAddAnswer.as_view()
return view(request, *args, **kwargs)
class QuestionDetailViewShow(DetailView):
model = Question
success_url = reverse_lazy('home-page')
# function to add form
def get_context_data(self, **kwargs):
context = super(QuestionDetailViewShow, self).get_context_data(**kwargs)
context['form'] = CreateAnswerForm()
return context
class QuestionFormViewAddAnswer(SingleObjectMixin, FormView):
model = Question
form_class = CreateAnswerForm
success_url = reverse_lazy('home-page')
template_name = 'ask_app/question_detail.html'
# дичь - рефакторь
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return HttpResponseForbidden()
self.object = self.get_object()
return super(QuestionFormViewAddAnswer, self).post(request, *args, **kwargs)
# дичь - рефакторь
def form_valid(self, form):
valid = super(QuestionFormViewAddAnswer, self).form_valid(form)
# add elements to form, then save
form.instance.author = self.request.user
form.instance.which_question = self.get_queryset().get(pk=self.object.id)
form.save()
# answer.save()
return valid
def get_success_url(self):
return reverse_lazy('question-page', kwargs={'pk': self.object.pk})
# update one question
class QuestionUpdateView(UpdateView):
model = Question
template_name_suffix = '_update_form'
class QuestionDeleteView(DeleteView):
model = Question
# ------------------------------------- new shit
class QuestionUpdateRating(UpdateView):
model = Question
form_class = QuestionUpdateRatingForm
template_name_suffix = '_ajax_update_form'
success_url = reverse_lazy('home-page')
def form_valid(self, form):
question = self.get_queryset().get(id=self.object.id)
form.instance.rating = question.rating + int(self.request.POST['rating'])
form.save()
return super(QuestionUpdateRating, self).form_valid(form)
# ------------------------------------------------
# classes for working with Answers models
# display all elements
class AnswerListView(ListView):
model = Answer
# see one answer
class AnswerDetailView(DetailView):
model = Answer
# change one answer
class AnswerUpdateView(UpdateView):
model = Answer
template_name_suffix = '_update_form'
# delete one answer
class AnswerDeleteView(DeleteView):
model = Answer
success_url = reverse_lazy('home-page')
# ------------------------------------------ new shit
# ajax update rating
class AnswerChangeRating(UpdateView):
model = Answer
form_class = ChangeAnswerRatingForm
template_name_suffix = '_ajax_update_form'
success_url = reverse_lazy('home-page')
def form_valid(self, form):
answer = self.get_queryset().get(id=self.object.id)
answer.rating += int(self.request.POST['rating'])
answer.save()
return super(AnswerChangeRating, self).form_valid(form)
# ajax update flag
class AnswerChangeFlag(UpdateView):
model = Answer
form_class = ChangeAnswerFlagForm
template_name_suffix = '_ajax_update_form'
success_url = reverse_lazy('home-page')
def form_valid(self, form):
form.save()
return super(AnswerChangeFlag, self).form_valid(form)
# -------------------------------------------------------
# classes for working with Users models
# display all elements
class UserListView(ListView):
model = User
# create new user / registration
class UserCreateView(CreateView):
model = User
form_class = CreateUserForm
template_name_suffix = '_create_form'
success_url = reverse_lazy('home-page')
# function for auto login after creating new user
def form_valid(self, form):
valid = super(UserCreateView, self).form_valid(form)
username, password = form.cleaned_data.get('username'), form.cleaned_data.get('password1')
new_user = authenticate(username=username, password=password)
login(self.request, new_user)
return valid
def get_context_data(self, **kwargs):
data = super(UserCreateView, self).get_context_data(**kwargs)
# edit here
data['best_users'] = User.objects.all()[:5]
data['tags'] = Tags.objects.all()[:5]
return data
# see one user
class UserDetailView(DetailView):
model = User
# change one user
class UserUpdateView(UpdateView):
model = User
form_class = ChangeUserForm
template_name_suffix = '_update_form'
success_url = reverse_lazy('home-page')
# delete one user
class UserDeleteView(DeleteView):
model = User
success_url = reverse_lazy('home-page')