]> git.openstreetmap.org Git - osqa.git/blob - forum/views/admin.py
Set the faq page to be markdown and added the markdown tips the the side bar of the...
[osqa.git] / forum / views / admin.py
1 from datetime import datetime, timedelta
2
3 from django.shortcuts import render_to_response
4 from django.core.urlresolvers import reverse
5 from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, Http404
6 from django.template import RequestContext
7 from django.utils.translation import ugettext as _
8 from django.db.models import Sum
9
10 from forum.settings.base import Setting
11 from forum.settings.forms import SettingsSetForm
12
13 from forum.models import Activity, Question, Answer, User, Node
14 from forum import const
15 from forum import settings
16
17 def super_user_required(fn):
18     def wrapper(request, *args, **kwargs):
19         if request.user.is_authenticated() and request.user.is_superuser:
20             return fn(request, *args, **kwargs)
21         else:
22             raise Http404
23
24     return wrapper
25
26 @super_user_required
27 def index(request):
28     return render_to_response('osqaadmin/index.html', {
29         'sets': get_all_sets(),
30         'settings_pack': settings.SETTINGS_PACK,
31         'statistics': get_statistics(),
32         'recent_activity': get_recent_activity(),
33     }, context_instance=RequestContext(request))
34
35 @super_user_required    
36 def settings_set(request, set_name):
37     set = Setting.sets.get(set_name, None)
38
39     if set is None:
40         raise Http404
41
42     if request.POST:
43         form = SettingsSetForm(set, data=request.POST, files=request.FILES)
44
45         if form.is_valid():
46             form.save()
47             request.user.message_set.create(message=_("'%s' settings saved succesfully") % set_name)
48
49             if set_name in ('minrep', 'badges', 'repgain'):
50                 settings.SETTINGS_PACK.set_value("custom")
51     else:
52         form = SettingsSetForm(set)
53
54     return render_to_response('osqaadmin/set.html', {
55         'form': form,
56         'markdown': set.markdown,
57         'sets': get_all_sets(),
58     }, context_instance=RequestContext(request))
59
60 def get_all_sets():
61     return sorted(Setting.sets.values(), lambda s1, s2: s1.weight - s2.weight)
62
63 def get_recent_activity():
64     return Activity.objects.filter(activity_type__in=(
65             const.TYPE_ACTIVITY_ASK_QUESTION, const.TYPE_ACTIVITY_ANSWER,
66             const.TYPE_ACTIVITY_COMMENT_QUESTION, const.TYPE_ACTIVITY_COMMENT_ANSWER,
67             const.TYPE_ACTIVITY_MARK_ANSWER)).order_by('-active_at')[0:10]
68
69 def get_statistics():
70     return {
71         'total_users': User.objects.all().count(),
72         'users_last_24': User.objects.filter(date_joined__gt=(datetime.now() - timedelta(days=1))).count(),
73         'total_questions': Question.objects.filter(deleted=False).count(),
74         'questions_last_24': Question.objects.filter(deleted=False, added_at__gt=(datetime.now() - timedelta(days=1))).count(),
75         'total_answers': Answer.objects.filter(deleted=False).count(),
76         'answers_last_24': Answer.objects.filter(deleted=False, added_at__gt=(datetime.now() - timedelta(days=1))).count(),
77     }
78
79 @super_user_required      
80 def go_bootstrap(request):
81     #todo: this is the quick and dirty way of implementing a bootstrap mode
82     try:
83         from forum_modules.default_badges import settings as dbsets
84         dbsets.POPULAR_QUESTION_VIEWS.set_value(100)
85         dbsets.NOTABLE_QUESTION_VIEWS.set_value(200)
86         dbsets.FAMOUS_QUESTION_VIEWS.set_value(300)
87         dbsets.NICE_ANSWER_VOTES_UP.set_value(2)
88         dbsets.NICE_QUESTION_VOTES_UP.set_value(2)
89         dbsets.GOOD_ANSWER_VOTES_UP.set_value(4)
90         dbsets.GOOD_QUESTION_VOTES_UP.set_value(4)
91         dbsets.GREAT_ANSWER_VOTES_UP.set_value(8)
92         dbsets.GREAT_QUESTION_VOTES_UP.set_value(8)
93         dbsets.FAVORITE_QUESTION_FAVS.set_value(1)
94         dbsets.STELLAR_QUESTION_FAVS.set_value(3)
95         dbsets.DISCIPLINED_MIN_SCORE.set_value(3)
96         dbsets.PEER_PRESSURE_MAX_SCORE.set_value(-3)
97         dbsets.CIVIC_DUTY_VOTES.set_value(15)
98         dbsets.PUNDIT_COMMENT_COUNT.set_value(10)
99         dbsets.SELF_LEARNER_UP_VOTES.set_value(2)
100         dbsets.STRUNK_AND_WHITE_EDITS.set_value(10)
101         dbsets.ENLIGHTENED_UP_VOTES.set_value(2)
102         dbsets.GURU_UP_VOTES.set_value(4)
103         dbsets.NECROMANCER_UP_VOTES.set_value(2)
104         dbsets.NECROMANCER_DIF_DAYS.set_value(30)
105         dbsets.TAXONOMIST_USE_COUNT.set_value(5)
106     except:
107         pass
108
109     settings.REP_TO_VOTE_UP.set_value(0)
110     settings.REP_TO_VOTE_DOWN.set_value(15)
111     settings.REP_TO_FLAG.set_value(15)
112     settings.REP_TO_COMMENT.set_value(0)
113     settings.REP_TO_LIKE_COMMENT.set_value(0)
114     settings.REP_TO_UPLOAD.set_value(0)
115     settings.REP_TO_CLOSE_OWN.set_value(60)
116     settings.REP_TO_REOPEN_OWN.set_value(120)
117     settings.REP_TO_RETAG.set_value(150)
118     settings.REP_TO_EDIT_WIKI.set_value(200)
119     settings.REP_TO_EDIT_OTHERS.set_value(400)
120     settings.REP_TO_CLOSE_OTHERS.set_value(600)
121     settings.REP_TO_DELETE_COMMENTS.set_value(400)
122     settings.REP_TO_VIEW_FLAGS.set_value(30)
123
124     settings.INITIAL_REP.set_value(1)
125     settings.MAX_REP_BY_UPVOTE_DAY.set_value(300)
126     settings.REP_GAIN_BY_UPVOTED.set_value(15)
127     settings.REP_LOST_BY_UPVOTE_CANCELED.set_value(15)
128     settings.REP_LOST_BY_DOWNVOTED.set_value(1)
129     settings.REP_LOST_BY_DOWNVOTING.set_value(0)
130     settings.REP_GAIN_BY_DOWNVOTE_CANCELED.set_value(1)
131     settings.REP_GAIN_BY_CANCELING_DOWNVOTE.set_value(0)
132     settings.REP_GAIN_BY_ACCEPTED.set_value(25)
133     settings.REP_LOST_BY_ACCEPTED_CANCELED.set_value(25)
134     settings.REP_GAIN_BY_ACCEPTING.set_value(5)
135     settings.REP_LOST_BY_CANCELING_ACCEPTED.set_value(5)
136     settings.REP_LOST_BY_FLAGGED.set_value(2)
137     settings.REP_LOST_BY_FLAGGED_3_TIMES.set_value(30)
138     settings.REP_LOST_BY_FLAGGED_5_TIMES.set_value(100)
139
140     settings.SETTINGS_PACK.set_value("bootstrap")
141
142     request.user.message_set.create(message=_('Bootstrap mode enabled'))
143     return HttpResponseRedirect(reverse('admin_index'))
144
145 @super_user_required
146 def go_defaults(request):
147     for setting in Setting.sets['badges']:
148         setting.to_default()
149     for setting in Setting.sets['minrep']:
150         setting.to_default()
151     for setting in Setting.sets['repgain']:
152         setting.to_default()
153
154     settings.SETTINGS_PACK.set_value("default")
155
156     request.user.message_set.create(message=_('All values reverted to defaults'))
157     return HttpResponseRedirect(reverse('admin_index'))
158
159
160 @super_user_required
161 def recalculate_denormalized(request):
162     for n in Node.objects.all():
163         n = n.leaf
164         n.vote_up_count = n.votes.filter(canceled=False, vote=1).count()
165         n.vote_down_count = n.votes.filter(canceled=False, vote=-1).count()
166         n.save()
167
168     for u in User.objects.all():
169         u.reputation = u.reputes.filter(canceled=False).aggregate(reputation=Sum('value'))['reputation']
170         u.save()
171
172     request.user.message_set.create(message=_('All values recalculated'))
173     return HttpResponseRedirect(reverse('admin_index'))
174