addons-server/apps/zadmin/views.py

116 строки
3.9 KiB
Python
Исходник Обычный вид История

2010-08-10 21:40:50 +04:00
from urlparse import urlparse
2010-03-25 01:52:02 +03:00
from django import http
2010-06-30 02:28:40 +04:00
# I'm so glad we named a function in here settings...
from django.conf import settings as site_settings
2010-01-22 04:45:46 +03:00
from django.contrib import admin
2009-12-31 01:43:25 +03:00
from django.shortcuts import redirect
2010-02-24 08:08:24 +03:00
from django.views import debug
2009-12-31 01:43:25 +03:00
2010-06-30 02:28:40 +04:00
import commonware.log
from hera.contrib.django_forms import FlushForm
from hera.contrib.django_utils import get_hera, flush_urls
2010-03-25 01:52:02 +03:00
import jinja2
2009-12-31 01:43:25 +03:00
import jingo
from amo import messages
import amo.models
2009-12-31 01:43:25 +03:00
from addons.models import Addon
from files.models import Approval
2009-12-31 01:43:25 +03:00
from versions.models import Version
2010-06-30 02:28:40 +04:00
log = commonware.log.getLogger('z.zadmin')
2009-12-31 01:43:25 +03:00
2010-01-22 04:45:46 +03:00
@admin.site.admin_view
2009-12-31 01:43:25 +03:00
def flagged(request):
addons = Addon.objects.filter(admin_review=True).order_by('-created')
2009-12-31 01:43:25 +03:00
if request.method == 'POST':
ids = map(int, request.POST.getlist('addon_id'))
Addon.objects.filter(id__in=ids).update(admin_review=False)
2009-12-31 01:43:25 +03:00
# The sql update doesn't invalidate anything, do it manually.
invalid = [addon for addon in addons if addon.id in ids]
Addon.objects.invalidate(*invalid)
return redirect('zadmin.flagged')
2009-12-31 01:43:25 +03:00
sql = """SELECT {t}.* FROM {t} JOIN (
SELECT addon_id, MAX(created) AS created
FROM {t}
GROUP BY addon_id) as J
ON ({t}.addon_id = J.addon_id AND {t}.created = J.created)
WHERE {t}.addon_id IN {ids}"""
approvals_sql = sql + """
AND (({t}.reviewtype = 'nominated' AND {t}.action = %s)
OR ({t}.reviewtype = 'pending' AND {t}.action = %s))"""
ids = '(%s)' % ', '.join(str(a.id) for a in addons)
versions_sql = sql.format(t=Version._meta.db_table, ids=ids)
approvals_sql = approvals_sql.format(t=Approval._meta.db_table, ids=ids)
versions = dict((x.addon_id, x) for x in
Version.objects.raw(versions_sql))
approvals = dict((x.addon_id, x) for x in
Approval.objects.raw(approvals_sql,
[amo.STATUS_NOMINATED,
amo.STATUS_PENDING]))
2009-12-31 01:43:25 +03:00
for addon in addons:
addon.version = versions.get(addon.id)
addon.approval = approvals.get(addon.id)
2009-12-31 01:43:25 +03:00
return jingo.render(request, 'zadmin/flagged_addon_list.html',
2009-12-31 01:43:25 +03:00
{'addons': addons})
2010-02-24 08:08:24 +03:00
2010-06-30 02:28:40 +04:00
@admin.site.admin_view
def hera(request):
form = FlushForm(initial={'flushprefix': site_settings.SITE_URL})
2010-08-10 21:40:50 +04:00
boxes = []
configured = False # Default to not showing the form.
for i in site_settings.HERA:
hera = get_hera(i)
r = {'location': urlparse(i['LOCATION'])[1], 'stats': False}
if hera:
r['stats'] = hera.getGlobalCacheInfo()
configured = True
boxes.append(r)
if not configured:
messages.error(request, "Hera is not (or mis-)configured.")
2010-06-30 02:28:40 +04:00
form = None
if request.method == 'POST' and hera:
2010-06-30 02:28:40 +04:00
form = FlushForm(request.POST)
if form.is_valid():
expressions = request.POST['flushlist'].splitlines()
for url in expressions:
num = flush_urls([url], request.POST['flushprefix'], True)
msg = ("Flushed %d objects from front end cache for: %s"
% (len(num), url))
2010-06-30 02:28:40 +04:00
log.info("[Hera] (user:%s) %s" % (request.user, msg))
messages.success(request, msg)
return jingo.render(request, 'zadmin/hera.html',
2010-08-10 21:40:50 +04:00
{'form': form, 'boxes': boxes})
2010-06-30 02:28:40 +04:00
2010-02-24 08:08:24 +03:00
@admin.site.admin_view
def settings(request):
settings_dict = debug.get_safe_settings()
# sigh
settings_dict['HERA'] = []
for i in site_settings.HERA:
settings_dict['HERA'].append(debug.cleanse_setting('HERA', i))
return jingo.render(request, 'zadmin/settings.html',
{'settings_dict': settings_dict})
2010-03-25 01:52:02 +03:00
@admin.site.admin_view
def env(request):
return http.HttpResponse(u'<pre>%s</pre>' % (jinja2.escape(request)))