1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-31 14:42:10 +00:00

allow search files with specified suffix(es)

This commit is contained in:
lins05
2013-05-13 14:27:38 +08:00
parent 5539334bec
commit 0fbc6cd0f2
2 changed files with 24 additions and 8 deletions

View File

@@ -6,7 +6,7 @@ if getattr(settings, 'ENABLE_FILE_SEARCH', False):
from seafes import es_get_conn, es_search from seafes import es_get_conn, es_search
es_conn = None es_conn = None
def search_file_by_name(request, keyword, start, size): def search_file_by_name(request, keyword, suffixes, start, size):
owned_repos, shared_repos, groups_repos, pub_repo_list = get_user_repos(request.user) owned_repos, shared_repos, groups_repos, pub_repo_list = get_user_repos(request.user)
# unify the repo.owner property # unify the repo.owner property
@@ -35,7 +35,7 @@ if getattr(settings, 'ENABLE_FILE_SEARCH', False):
global es_conn global es_conn
if es_conn is None: if es_conn is None:
es_conn = es_get_conn() es_conn = es_get_conn()
files_found, total = es_search(es_conn, nonpub_repo_ids, keyword, start, size) files_found, total = es_search(es_conn, nonpub_repo_ids, keyword, suffixes, start, size)
if len(files_found) > 0: if len(files_found) > 0:
# construt a (id, repo) hash table for fast lookup # construt a (id, repo) hash table for fast lookup
@@ -59,12 +59,12 @@ if getattr(settings, 'ENABLE_FILE_SEARCH', False):
return files_found, total return files_found, total
def search_repo_file_by_name(request, repo, keyword, start, size): def search_repo_file_by_name(request, repo, keyword, suffixes, start, size):
global es_conn global es_conn
if es_conn is None: if es_conn is None:
es_conn = es_get_conn() es_conn = es_get_conn()
files_found, total = es_search(es_conn, [repo.id], keyword, start, size) files_found, total = es_search(es_conn, [repo.id], keyword, suffixes, start, size)
for f in files_found: for f in files_found:
f['repo'] = repo f['repo'] = repo

View File

@@ -5,9 +5,26 @@ from seaserv import get_repo
from seahub.auth.decorators import login_required from seahub.auth.decorators import login_required
from seahub.utils.search import search_file_by_name, search_repo_file_by_name from seahub.utils.search import search_file_by_name, search_repo_file_by_name
def parse_suffixes(suffixes):
if not suffixes:
return None
l = []
for suffix in suffixes.split(','):
suffix = suffix.strip()
if suffix:
l.append(suffix)
print l
return l
@login_required @login_required
def search(request): def search(request):
keyword = request.GET['q'] keyword = request.GET['q']
suffixes = parse_suffixes(request.GET.get('suffixes', None))
current_page = int(request.GET.get('page', '1')) current_page = int(request.GET.get('page', '1'))
per_page= int(request.GET.get('per_page', '25')) per_page= int(request.GET.get('per_page', '25'))
@@ -17,15 +34,14 @@ def search(request):
scale = request.GET.get('scale', None) scale = request.GET.get('scale', None)
repo_id = request.GET.get('search_repo_id', None) repo_id = request.GET.get('search_repo_id', None)
repo = None repo = None
if repo_id:
repo = get_repo(repo_id)
if scale == 'current' and repo_id: if scale == 'current' and repo_id:
repo = get_repo(repo_id)
if repo: if repo:
results, total = search_repo_file_by_name(request, repo, keyword, start, size) results, total = search_repo_file_by_name(request, repo, keyword, suffixes, start, size)
else: else:
results, total = [], 0 results, total = [], 0
else: else:
results, total = search_file_by_name(request, keyword, start, size) results, total = search_file_by_name(request, keyword, suffixes, start, size)
if total > current_page * per_page: if total > current_page * per_page:
has_more = True has_more = True