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