2015-10-08 09:00:22 +00:00
|
|
|
import itertools
|
|
|
|
from collections import OrderedDict
|
2015-04-26 13:53:00 +00:00
|
|
|
from functools import update_wrapper
|
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
from django.contrib import admin
|
2021-04-21 12:27:18 +00:00
|
|
|
from django.urls import reverse
|
2015-10-08 09:00:22 +00:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.utils.safestring import mark_safe
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-04-26 13:53:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
from .dashboard import *
|
2015-04-26 13:53:00 +00:00
|
|
|
from .options import *
|
2015-10-08 09:00:22 +00:00
|
|
|
from ..core import accounts, services
|
2015-04-26 13:53:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
# monkey-patch admin.site in order to porvide some extra admin urls
|
|
|
|
|
|
|
|
urls = []
|
|
|
|
def register_url(pattern, view, name=""):
|
|
|
|
global urls
|
|
|
|
urls.append((pattern, view, name))
|
2015-08-31 11:58:59 +00:00
|
|
|
admin.site.register_url = register_url
|
2015-04-26 13:53:00 +00:00
|
|
|
|
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
site_get_urls = admin.site.get_urls
|
2015-04-26 13:53:00 +00:00
|
|
|
def get_urls():
|
|
|
|
def wrap(view, cacheable=False):
|
|
|
|
def wrapper(*args, **kwargs):
|
2015-08-31 11:58:59 +00:00
|
|
|
return admin.site.admin_view(view, cacheable)(*args, **kwargs)
|
|
|
|
wrapper.admin_site = admin.site
|
2015-04-26 13:53:00 +00:00
|
|
|
return update_wrapper(wrapper, view)
|
|
|
|
global urls
|
|
|
|
extra_patterns = []
|
|
|
|
for pattern, view, name in urls:
|
|
|
|
extra_patterns.append(
|
|
|
|
url(pattern, wrap(view), name=name)
|
|
|
|
)
|
|
|
|
return site_get_urls() + extra_patterns
|
2015-08-31 11:58:59 +00:00
|
|
|
admin.site.get_urls = get_urls
|
2015-10-08 09:00:22 +00:00
|
|
|
|
|
|
|
|
2015-10-08 13:54:39 +00:00
|
|
|
def get_model(model_name, model_name_map):
|
|
|
|
try:
|
|
|
|
return model_name_map[model_name.lower()]
|
|
|
|
except KeyError:
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2015-10-08 09:00:22 +00:00
|
|
|
def search(request):
|
|
|
|
query = request.GET.get('q', '')
|
2015-10-08 13:54:39 +00:00
|
|
|
search_term = query
|
|
|
|
models = set()
|
|
|
|
selected_models = set()
|
|
|
|
model_name_map = {}
|
|
|
|
for service in itertools.chain(services, accounts):
|
|
|
|
if service.search:
|
|
|
|
models.add(service.model)
|
|
|
|
model_name_map[service.model._meta.model_name] = service.model
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-10-08 13:54:39 +00:00
|
|
|
# Account direct access
|
|
|
|
if search_term.endswith('!'):
|
2015-10-08 09:00:22 +00:00
|
|
|
from ..contrib.accounts.models import Account
|
2015-10-08 13:54:39 +00:00
|
|
|
search_term = search_term.replace('!', '')
|
2015-10-08 09:00:22 +00:00
|
|
|
try:
|
2015-10-08 13:54:39 +00:00
|
|
|
account = Account.objects.get(username=search_term)
|
2015-10-08 09:00:22 +00:00
|
|
|
except Account.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
account_url = reverse('admin:accounts_account_change', args=(account.pk,))
|
|
|
|
return redirect(account_url)
|
2015-10-08 13:54:39 +00:00
|
|
|
# Search for specific model
|
|
|
|
elif ':' in search_term:
|
|
|
|
new_search_term = []
|
|
|
|
for part in search_term.split():
|
|
|
|
if ':' in part:
|
|
|
|
model_name, term = part.split(':')
|
|
|
|
model = get_model(model_name, model_name_map)
|
|
|
|
# Retry with singular version
|
|
|
|
if model is None and model_name.endswith('s'):
|
|
|
|
model = get_model(model_name[:-1], model_name_map)
|
|
|
|
if model is None:
|
|
|
|
new_search_term.append(':'.join((model_name, term)))
|
|
|
|
else:
|
|
|
|
selected_models.add(model)
|
|
|
|
new_search_term.append(term)
|
|
|
|
else:
|
|
|
|
new_search_term.append(part)
|
|
|
|
search_term = ' '.join(new_search_term)
|
|
|
|
if selected_models:
|
|
|
|
models = selected_models
|
2015-10-08 09:00:22 +00:00
|
|
|
results = OrderedDict()
|
|
|
|
models = sorted(models, key=lambda m: m._meta.verbose_name_plural.lower())
|
|
|
|
total = 0
|
|
|
|
for model in models:
|
|
|
|
try:
|
|
|
|
modeladmin = admin.site._registry[model]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
qs = modeladmin.get_queryset(request)
|
2015-10-08 13:54:39 +00:00
|
|
|
qs, search_use_distinct = modeladmin.get_search_results(request, qs, search_term)
|
2015-10-08 09:00:22 +00:00
|
|
|
if search_use_distinct:
|
|
|
|
qs = qs.distinct()
|
|
|
|
num = len(qs)
|
|
|
|
if num:
|
|
|
|
total += num
|
|
|
|
results[model._meta] = qs
|
|
|
|
title = _("{total} search results for '<tt>{query}</tt>'").format(total=total, query=query)
|
|
|
|
context = {
|
|
|
|
'title': mark_safe(title),
|
|
|
|
'total': total,
|
|
|
|
'columns': min(int(total/17), 3),
|
|
|
|
'query': query,
|
2015-10-08 13:54:39 +00:00
|
|
|
'search_term': search_term,
|
2015-10-08 09:00:22 +00:00
|
|
|
'results': results,
|
|
|
|
'search_autofocus': True,
|
|
|
|
}
|
|
|
|
return render(request, 'admin/orchestra/search.html', context)
|
|
|
|
|
|
|
|
|
|
|
|
admin.site.register_url(r'^search/$', search, 'orchestra_search_view')
|