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