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
|
"""
: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.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.views import static
from django.db.models import Max, Count
from askbot.forms import FeedbackForm
from askbot.utils.forms import get_next_url
from askbot.utils.mail import mail_moderators
from askbot.models import BadgeData, Award, User
from askbot.models import badges as badge_data
from askbot.skins.loaders import ENV
from askbot.conf import settings as askbot_settings
from askbot import skins
import askbot
def generic_view(request, template = None, page_class = None):
template = ENV.get_template(template)
context = RequestContext(request, {'page_class': page_class})
return HttpResponse(template.render(context))
def config_variable(request, variable_name = None, mimetype = None):
"""Print value from the configuration settings
as response content. All parameters are required.
"""
output = getattr(askbot_settings, variable_name, '')
return HttpResponse(output, mimetype = mimetype)
def about(request, template='about.html'):
return generic_view(request, template = template, page_class = 'meta')
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 faq(request):
template = ENV.get_template('faq.html')
data = {
'view_name':'faq',
'gravatar_faq_url': reverse('faq') + '#gravatar',
#'send_email_key_url': reverse('send_email_key'),
'ask_question_url': reverse('ask'),
'page_class': 'meta',
}
context = RequestContext(request, data)
return HttpResponse(template.render(context))
def feedback(request):
data = {'view_name':'feedback', 'page_class': 'meta'}
form = None
if request.method == "POST":
form = FeedbackForm(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)
message = render_to_response('feedback_email.txt',data,context_instance=RequestContext(request))
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(initial={'next':get_next_url(request)})
data['form'] = form
context = RequestContext(request, data)
template = ENV.get_template('feedback.html')
return HttpResponse(template.render(context))
feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
def privacy(request):
context = RequestContext(request, {'view_name':'privacy', 'page_class': 'meta'})
template = ENV.get_template('privacy.html')
return HttpResponse(template.render(context))
def logout(request):#refactor/change behavior?
#currently you click logout and you get
#to this view which actually asks you again - do you really want to log out?
#I guess rationale was to tell the user that s/he may be still logged in
#through their external login sytem and we'd want to remind them about it
#however it might be a little annoying
#why not just show a message: you are logged out of forum, but
#if you really want to log out -> go to your openid provider
data = {
'view_name':'logout',
'next' : get_next_url(request),
'page_class': 'meta',
}
context = RequestContext(request, data)
template = ENV.get_template('logout.html')
return HttpResponse(template.render(context))
def badges(request):#user status/reputation system
#todo: supplement database data with the stuff from badges.py
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']
template = ENV.get_template('badges.html')
data = {
'active_tab': 'badges',
'badges' : badges,
'view_name': 'badges',
'mybadges' : my_badges,
'feedback_faq_url' : reverse('feedback'),
}
context = RequestContext(request, data)
return HttpResponse(template.render(context))
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'
)
template = ENV.get_template('badge.html')
data = {
'view_name': badge,
'active_tab': 'badges',
'badge_recipients' : badge_recipients,
'badge' : badge,
'page_class': 'meta',
}
context = RequestContext(request, data)
return HttpResponse(template.render(context))
def media(request, skin, resource):
"""view that serves static media from any skin
uses django static serve view, where document root is
adjusted according to the current skin selection
in production this views should be by-passed via server configuration
for the better efficiency of serving static files
"""
dir = skins.utils.get_path_to_skin(skin)
return static.serve(request, '/media/' + resource, document_root = dir)
|