]> git.openstreetmap.org Git - osqa.git/blob - forum/views/admin.py
fixes osqa-101: made it so the faq page reflex the admin settings.
[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         'sets': get_all_sets(),
57     }, context_instance=RequestContext(request))
58
59 def get_all_sets():
60     return sorted(Setting.sets.values(), lambda s1, s2: s1.weight - s2.weight)
61
62 def get_recent_activity():
63     return Activity.objects.filter(activity_type__in=(
64             const.TYPE_ACTIVITY_ASK_QUESTION, const.TYPE_ACTIVITY_ANSWER,
65             const.TYPE_ACTIVITY_COMMENT_QUESTION, const.TYPE_ACTIVITY_COMMENT_ANSWER,
66             const.TYPE_ACTIVITY_MARK_ANSWER)).order_by('-active_at')[0:10]
67
68 def get_statistics():
69     return {
70         'total_users': User.objects.all().count(),
71         'users_last_24': User.objects.filter(date_joined__gt=(datetime.now() - timedelta(days=1))).count(),
72         'total_questions': Question.objects.filter(deleted=False).count(),
73         'questions_last_24': Question.objects.filter(deleted=False, added_at__gt=(datetime.now() - timedelta(days=1))).count(),
74         'total_answers': Answer.objects.filter(deleted=False).count(),
75         'answers_last_24': Answer.objects.filter(deleted=False, added_at__gt=(datetime.now() - timedelta(days=1))).count(),
76     }
77
78 @super_user_required      
79 def go_bootstrap(request):
80     #todo: this is the quick and dirty way of implementing a bootstrap mode
81     try:
82         from forum_modules.default_badges import settings as dbsets
83         dbsets.POPULAR_QUESTION_VIEWS.set_value(100)
84         dbsets.NOTABLE_QUESTION_VIEWS.set_value(200)
85         dbsets.FAMOUS_QUESTION_VIEWS.set_value(300)
86         dbsets.NICE_ANSWER_VOTES_UP.set_value(2)
87         dbsets.NICE_QUESTION_VOTES_UP.set_value(2)
88         dbsets.GOOD_ANSWER_VOTES_UP.set_value(4)
89         dbsets.GOOD_QUESTION_VOTES_UP.set_value(4)
90         dbsets.GREAT_ANSWER_VOTES_UP.set_value(8)
91         dbsets.GREAT_QUESTION_VOTES_UP.set_value(8)
92         dbsets.FAVORITE_QUESTION_FAVS.set_value(1)
93         dbsets.STELLAR_QUESTION_FAVS.set_value(3)
94         dbsets.DISCIPLINED_MIN_SCORE.set_value(3)
95         dbsets.PEER_PRESSURE_MAX_SCORE.set_value(-3)
96         dbsets.CIVIC_DUTY_VOTES.set_value(15)
97         dbsets.PUNDIT_COMMENT_COUNT.set_value(10)
98         dbsets.SELF_LEARNER_UP_VOTES.set_value(2)
99         dbsets.STRUNK_AND_WHITE_EDITS.set_value(10)
100         dbsets.ENLIGHTENED_UP_VOTES.set_value(2)
101         dbsets.GURU_UP_VOTES.set_value(4)
102         dbsets.NECROMANCER_UP_VOTES.set_value(2)
103         dbsets.NECROMANCER_DIF_DAYS.set_value(30)
104         dbsets.TAXONOMIST_USE_COUNT.set_value(5)
105     except:
106         pass
107
108     settings.REP_TO_VOTE_UP.set_value(0)
109     settings.REP_TO_VOTE_DOWN.set_value(15)
110     settings.REP_TO_FLAG.set_value(15)
111     settings.REP_TO_COMMENT.set_value(0)
112     settings.REP_TO_LIKE_COMMENT.set_value(0)
113     settings.REP_TO_UPLOAD.set_value(0)
114     settings.REP_TO_CLOSE_OWN.set_value(60)
115     settings.REP_TO_REOPEN_OWN.set_value(120)
116     settings.REP_TO_RETAG.set_value(150)
117     settings.REP_TO_EDIT_WIKI.set_value(200)
118     settings.REP_TO_EDIT_OTHERS.set_value(400)
119     settings.REP_TO_CLOSE_OTHERS.set_value(600)
120     settings.REP_TO_DELETE_COMMENTS.set_value(400)
121     settings.REP_TO_VIEW_FLAGS.set_value(30)
122
123     settings.INITIAL_REP.set_value(1)
124     settings.MAX_REP_BY_UPVOTE_DAY.set_value(300)
125     settings.REP_GAIN_BY_UPVOTED.set_value(15)
126     settings.REP_LOST_BY_UPVOTE_CANCELED.set_value(15)
127     settings.REP_LOST_BY_DOWNVOTED.set_value(1)
128     settings.REP_LOST_BY_DOWNVOTING.set_value(0)
129     settings.REP_GAIN_BY_DOWNVOTE_CANCELED.set_value(1)
130     settings.REP_GAIN_BY_CANCELING_DOWNVOTE.set_value(0)
131     settings.REP_GAIN_BY_ACCEPTED.set_value(25)
132     settings.REP_LOST_BY_ACCEPTED_CANCELED.set_value(25)
133     settings.REP_GAIN_BY_ACCEPTING.set_value(5)
134     settings.REP_LOST_BY_CANCELING_ACCEPTED.set_value(5)
135     settings.REP_LOST_BY_FLAGGED.set_value(2)
136     settings.REP_LOST_BY_FLAGGED_3_TIMES.set_value(30)
137     settings.REP_LOST_BY_FLAGGED_5_TIMES.set_value(100)
138
139     settings.SETTINGS_PACK.set_value("bootstrap")
140
141     request.user.message_set.create(message=_('Bootstrap mode enabled'))
142     return HttpResponseRedirect(reverse('admin_index'))
143
144 @super_user_required
145 def go_defaults(request):
146     for setting in Setting.sets['badges']:
147         setting.to_default()
148     for setting in Setting.sets['minrep']:
149         setting.to_default()
150     for setting in Setting.sets['repgain']:
151         setting.to_default()
152
153     settings.SETTINGS_PACK.set_value("default")
154
155     request.user.message_set.create(message=_('All values reverted to defaults'))
156     return HttpResponseRedirect(reverse('admin_index'))
157
158
159 @super_user_required
160 def recalculate_denormalized(request):
161     for n in Node.objects.all():
162         n = n.leaf
163         n.vote_up_count = n.votes.filter(canceled=False, vote=1).count()
164         n.vote_down_count = n.votes.filter(canceled=False, vote=-1).count()
165         n.save()
166
167     for u in User.objects.all():
168         u.reputation = u.reputes.filter(canceled=False).aggregate(reputation=Sum('value'))['reputation']
169         u.save()
170
171     request.user.message_set.create(message=_('All values recalculated'))
172     return HttpResponseRedirect(reverse('admin_index'))
173