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
|
"""
:synopsis: remaining "secondary" views for askbot
This module contains a collection of views displaying all sorts of secondary and mostly static content.
"""
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.template import RequestContext, Template
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.views import static
from django.views.decorators import csrf
from django.db.models import Max, Count
from askbot import skins
from askbot.conf import settings as askbot_settings
from askbot.forms import FeedbackForm
from askbot.mail import mail_moderators
from askbot.models import BadgeData, Award, User, Tag
from askbot.models import badges as badge_data
from askbot.skins.loaders import get_template, render_into_skin, render_text_into_skin
from askbot.utils.decorators import admins_only
from askbot.utils.forms import get_next_url
from askbot.utils import functions
def generic_view(request, template = None, page_class = None):
"""this may be not necessary, since it is just a rewrite of render_into_skin"""
if request is None: # a plug for strange import errors in django startup
return render_to_response('django_error.html')
return render_into_skin(template, {'page_class': page_class}, request)
def config_variable(request, variable_name = None, mimetype = None):
"""Print value from the configuration settings
as response content. All parameters are required.
"""
#todo add http header-based caching here!!!
output = getattr(askbot_settings, variable_name, '')
return HttpResponse(output, mimetype = mimetype)
def about(request, template='about.html'):
title = _('About %(site)s') % {'site': askbot_settings.APP_SHORT_NAME}
data = {
'title': title,
'page_class': 'meta',
'content': askbot_settings.FORUM_ABOUT
}
return render_into_skin('static_page.html', data, request)
def page_not_found(request, template='404.html'):
return generic_view(request, template)
def server_error(request, template='500.html'):
return generic_view(request, template)
def help(request):
data = {
'app_name': askbot_settings.APP_SHORT_NAME,
'page_class': 'meta'
}
return render_into_skin('help.html', data, request)
def faq(request):
if askbot_settings.FORUM_FAQ.strip() != '':
data = {
'title': _('FAQ'),
'content': askbot_settings.FORUM_FAQ,
'page_class': 'meta',
}
return render_into_skin(
'static_page.html',
data,
request
)
else:
data = {
'gravatar_faq_url': reverse('faq') + '#gravatar',
'ask_question_url': reverse('ask'),
'page_class': 'meta',
}
return render_into_skin('faq_static.html', data, request)
@csrf.csrf_protect
def feedback(request):
data = {'page_class': 'meta'}
form = None
if request.method == "POST":
form = FeedbackForm(is_auth = request.user.is_authenticated(),
data = request.POST)
if form.is_valid():
if not request.user.is_authenticated():
data['email'] = form.cleaned_data.get('email',None)
data['message'] = form.cleaned_data['message']
data['name'] = form.cleaned_data.get('name',None)
template = get_template('email/feedback_email.txt', request)
message = template.render(RequestContext(request, data))
mail_moderators(_('Q&A forum feedback'), message)
msg = _('Thanks for the feedback!')
request.user.message_set.create(message=msg)
return HttpResponseRedirect(get_next_url(request))
else:
form = FeedbackForm(is_auth = request.user.is_authenticated(),
initial={'next':get_next_url(request)})
data['form'] = form
return render_into_skin('feedback.html', data, request)
feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
def privacy(request):
data = {
'title': _('Privacy policy'),
'page_class': 'meta',
'content': askbot_settings.FORUM_PRIVACY
}
return render_into_skin('static_page.html', data, request)
def badges(request):#user status/reputation system
#todo: supplement database data with the stuff from badges.py
if askbot_settings.BADGES_MODE != 'public':
raise Http404
known_badges = badge_data.BADGES.keys()
badges = BadgeData.objects.filter(slug__in = known_badges).order_by('slug')
my_badges = []
if request.user.is_authenticated():
my_badges = Award.objects.filter(
user=request.user
).values(
'badge_id'
).distinct()
#my_badges.query.group_by = ['badge_id']
data = {
'active_tab': 'badges',
'badges' : badges,
'page_class': 'meta',
'mybadges' : my_badges,
'feedback_faq_url' : reverse('feedback'),
}
return render_into_skin('badges.html', data, request)
def badge(request, id):
#todo: supplement database data with the stuff from badges.py
badge = get_object_or_404(BadgeData, id=id)
badge_recipients = User.objects.filter(
award_user__badge = badge
).annotate(
last_awarded_at = Max('award_user__awarded_at'),
award_count = Count('award_user')
).order_by(
'-last_awarded_at'
)
data = {
'active_tab': 'badges',
'badge_recipients' : badge_recipients,
'badge' : badge,
'page_class': 'meta',
}
return render_into_skin('badge.html', data, request)
@admins_only
def list_suggested_tags(request):
"""moderators and administrators can list tags that are
in the moderation queue, apply suggested tag to questions
or cancel the moderation reuest."""
if askbot_settings.ENABLE_TAG_MODERATION == False:
raise Http404
tags = Tag.objects.filter(status = Tag.STATUS_SUGGESTED)
tags = tags.order_by('-used_count', 'name')
#paginate moderated tags
paginator = Paginator(tags, 20)
page_no = request.GET.get('page', '1')
try:
page = paginator.page(page_no)
except (EmptyPage, InvalidPage):
page = paginator.page(paginator.num_pages)
paginator_context = functions.setup_paginator({
'is_paginated' : True,
'pages': paginator.num_pages,
'page': page_no,
'has_previous': page.has_previous(),
'has_next': page.has_next(),
'previous': page.previous_page_number(),
'next': page.next_page_number(),
'base_url' : request.path
})
data = {
'tags': page.object_list,
'active_tab': 'tags',
'tab_id': 'suggested',
'page_class': 'moderate-tags-page',
'page_title': _('Suggested tags'),
'paginator_context' : paginator_context,
}
return render_into_skin('list_suggested_tags.html', data, request)
|