+@admin_page
+def maintenance(request):
+ if request.POST:
+ if 'close' in request.POST or 'adjust' in request.POST:
+ form = MaintenanceModeForm(request.POST)
+
+ if form.is_valid():
+ settings.MAINTAINANCE_MODE.set_value({
+ 'allow_ips': form.cleaned_data['ips'],
+ 'message': form.cleaned_data['message']})
+
+ if 'close' in request.POST:
+ message = _('Maintenance mode enabled')
+ else:
+ message = _('Settings adjusted')
+
+ request.user.message_set.create(message=message)
+
+ return HttpResponseRedirect(reverse('admin_maintenance'))
+ elif 'open' in request.POST:
+ settings.MAINTAINANCE_MODE.set_value(None)
+ request.user.message_set.create(message=_("Your site is now running normally"))
+ return HttpResponseRedirect(reverse('admin_maintenance'))
+ else:
+ form = MaintenanceModeForm(initial={'ips': request.META['REMOTE_ADDR'],
+ 'message': _('Currently down for maintenance. We\'ll be back soon')})
+
+ return ('osqaadmin/maintenance.html', {'form': form, 'in_maintenance': settings.MAINTAINANCE_MODE.value is not None
+ })
+
+
+@admin_page
+def flagged_posts(request):
+ return ('osqaadmin/flagged_posts.html', {
+ 'flagged_posts': get_flagged_posts(),
+ })
+
+@admin_page
+def static_pages(request):
+ pages = Page.objects.all()
+
+ return ('osqaadmin/static_pages.html', {
+ 'pages': pages,
+ })
+
+@admin_page
+def edit_page(request, id=None):
+ if id:
+ page = get_object_or_404(Page, id=id)
+ else:
+ page = None
+
+ if request.POST:
+ form = PageForm(page, request.POST)
+
+ if form.is_valid():
+ if form.has_changed():
+ if not page:
+ page = NewPageAction(user=request.user, ip=request.META['REMOTE_ADDR']).save(data=form.cleaned_data
+ ).node
+ else:
+ EditPageAction(user=request.user, node=page, ip=request.META['REMOTE_ADDR']).save(
+ data=form.cleaned_data)
+
+ if ('publish' in request.POST) and (not page.published):
+ PublishAction(user=request.user, node=page, ip=request.META['REMOTE_ADDR']).save()
+ elif ('unpublish' in request.POST) and page.published:
+ page.nstate.published.cancel(ip=request.META['REMOTE_ADDR'])
+
+ return HttpResponseRedirect(reverse('admin_edit_page', kwargs={'id': page.id}))
+
+ else:
+ form = PageForm(page)
+
+ if page:
+ published = page.published
+ else:
+ published = False
+
+ return ('osqaadmin/edit_page.html', {
+ 'page': page,
+ 'form': form,
+ 'published': published
+ })
+
+@admin_page
+def moderation(request):
+ if request.POST:
+ if not 'ids' in request.POST:
+ verify = None
+ else:
+ sort = {
+ 'high-rep': '-reputation',
+ 'newer': '-date_joined',
+ 'older': 'date_joined',
+ }.get(request.POST.get('sort'), None)
+
+ if sort:
+ try:
+ limit = int(request.POST['limit'])
+ except:
+ limit = 5
+
+ verify = User.objects.order_by(sort)[:limit]
+ else:
+ verify = None
+
+ if verify:
+ possible_cheaters = []
+ verify = User.objects.order_by(sort)[:5]
+
+ cheat_score_sort = lambda c1, c2: cmp(c2.fdata['fake_score'], c1.fdata['fake_score'])
+
+ for user in verify:
+ possible_fakes = []
+ affecters = User.objects.filter(actions__node__author=user, actions__canceled=False).annotate(
+ affect_count=models.Count('actions')).order_by('-affect_count')
+ user_ips = set(Action.objects.filter(user=user).values_list('ip', flat=True).distinct('ip'))
+
+ for affecter in affecters:
+ if affecter == user:
+ continue
+
+ data = {'affect_count': affecter.affect_count}
+
+ total_actions = affecter.actions.filter(canceled=False).exclude(node=None).count()
+ ratio = (float(affecter.affect_count) / float(total_actions)) * 100
+
+ if total_actions > 10 and ratio > 50:
+ data['total_actions'] = total_actions
+ data['action_ratio'] = ratio
+
+ affecter_ips = set(
+ Action.objects.filter(user=affecter).values_list('ip', flat=True).distinct('ip'))
+ cross_ips = len(user_ips & affecter_ips)
+
+ data['cross_ip_count'] = cross_ips
+ data['total_ip_count'] = len(affecter_ips)
+ data['cross_ip_ratio'] = (float(data['cross_ip_count']) / float(data['total_ip_count'])) * 100
+
+ if affecter.email_isvalid:
+ email_score = 0
+ else:
+ email_score = 50.0
+
+ data['fake_score'] = ((data['cross_ip_ratio'] + data['action_ratio'] + email_score) / 100) * 4
+
+ affecter.fdata = data
+ possible_fakes.append(affecter)
+
+ if len(possible_fakes) > 0:
+ possible_fakes = sorted(possible_fakes, cheat_score_sort)
+ possible_cheaters.append((user, possible_fakes))
+
+ return ('osqaadmin/moderation.html', {'cheaters': possible_cheaters})
+
+ return ('osqaadmin/moderation.html', {})
+
+
+