diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-10-11 19:00:29 +0200 |
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-10-11 19:00:29 +0200 |
| commit | 72de941ef9ec59b282e056c28339a414c2b8b162 (patch) | |
| tree | 3db3213d2e87cfcdeb7d1245fe239ec3188eb01e /ishtar_common/views.py | |
| parent | 3120af6c41157b71561d6fd5a68fc6022f6dbadf (diff) | |
| parent | f9e0e0518c087febb5bf0eb89eac34f685559970 (diff) | |
| download | Ishtar-72de941ef9ec59b282e056c28339a414c2b8b162.tar.bz2 Ishtar-72de941ef9ec59b282e056c28339a414c2b8b162.zip | |
Merge branch 'master' into v0.9
Diffstat (limited to 'ishtar_common/views.py')
| -rw-r--r-- | ishtar_common/views.py | 107 |
1 files changed, 90 insertions, 17 deletions
diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 5f3e80c77..3406cf7af 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -55,6 +55,7 @@ from xhtml2odt import xhtml2odt from menus import menu from archaeological_files.models import File +from archaeological_operations.models import Operation from archaeological_context_records.models import ContextRecord from archaeological_finds.models import Find @@ -62,6 +63,7 @@ from archaeological_operations.forms import DashboardForm as DashboardFormOpe from archaeological_files.forms import DashboardForm as DashboardFormFile from ishtar_common.forms import FinalForm, FinalDeleteForm +from ishtar_common.widgets import JQueryAutoComplete from ishtar_common.utils import get_random_item_image_link, shortify from ishtar_common import forms_common as forms from ishtar_common import wizards @@ -198,12 +200,33 @@ def get_autocomplete_generic(model, extra={'available': True}): return func -def shortcut_menu(request): - from archaeological_operations.models import Operation - from archaeological_files.models import File - from archaeological_context_records.models import ContextRecord - from archaeological_finds.models import Find +def activate_all_search(request): + request.session['SHORTCUT_SEARCH'] = 'all' + return HttpResponse('OK', mimetype='text/plain') + + +def activate_own_search(request): + request.session['SHORTCUT_SEARCH'] = 'own' + return HttpResponse('OK', mimetype='text/plain') + + +def activate_advanced_shortcut_menu(request): + if not hasattr(request.user, 'ishtaruser'): + return HttpResponse('KO', mimetype='text/plain') + request.user.ishtaruser.advanced_shortcut_menu = True + request.user.ishtaruser.save() + return HttpResponse('OK', mimetype='text/plain') + + +def activate_simple_shortcut_menu(request): + if not hasattr(request.user, 'ishtaruser'): + return HttpResponse('KO', mimetype='text/plain') + request.user.ishtaruser.advanced_shortcut_menu = False + request.user.ishtaruser.save() + return HttpResponse('OK', mimetype='text/plain') + +def shortcut_menu(request): profile = get_current_profile() CURRENT_ITEMS = [] if profile.files: @@ -213,6 +236,27 @@ def shortcut_menu(request): CURRENT_ITEMS.append((_(u"Context record"), ContextRecord)) if profile.find: CURRENT_ITEMS.append((_(u"Find"), Find)) + if hasattr(request.user, 'ishtaruser') and \ + request.user.ishtaruser.advanced_shortcut_menu: + dct = {'current_menu': [], 'menu': [], + 'SHORTCUT_SEARCH': request.session['SHORTCUT_SEARCH'] + if 'SHORTCUT_SEARCH' in request.session else 'own'} + + for lbl, model in CURRENT_ITEMS: + model_name = model.SLUG + current = model_name in request.session \ + and request.session[model_name] + + dct['menu'].append(( + lbl, model_name, current or 0, + JQueryAutoComplete( + reverse('get-' + model.SLUG + '-shortcut'), + model).render( + model.SLUG + '-shortcut', value=current, + attrs={'id': 'current_' + model.SLUG}))) + return render_to_response( + 'ishtar/blocks/advanced_shortcut_menu.html', + dct, context_instance=RequestContext(request)) dct = {'current_menu': []} current_selected_item = None for lbl, model in CURRENT_ITEMS: @@ -222,7 +266,8 @@ def shortcut_menu(request): current = model_name in request.session and request.session[model_name] items = [] for item in model.get_owns(request.user, - menu_filtr=current_selected_item): + menu_filtr=current_selected_item, + limit=100): pk = unicode(item.pk) if item.IS_BASKET: pk = "basket-" + pk @@ -249,10 +294,6 @@ def shortcut_menu(request): def get_current_items(request): - from archaeological_files.models import File - from archaeological_operations.models import Operation - from archaeological_context_records.models import ContextRecord - from archaeological_finds.models import Find currents = {} for key, model in (('file', File), ('operation', Operation), @@ -290,6 +331,7 @@ def update_current_item(request, item_type=None, pk=None): request.session[item_type] = request.POST['value'] else: request.session[item_type] = str(pk) + request.session['SHORTCUT_SEARCH'] = 'all' currents = get_current_items(request) # reinit when descending item are not relevant @@ -516,6 +558,9 @@ def get_item(model, func_name, default_name, extra_request_keys=[], break # max right reach if force_own: own = True + if full == 'shortcut' and 'SHORTCUT_SEARCH' in request.session and \ + request.session['SHORTCUT_SEARCH'] == 'own': + own = True EMPTY = '' if 'type' in dct: data_type = dct.pop('type') @@ -544,6 +589,8 @@ def get_item(model, func_name, default_name, extra_request_keys=[], request_items = request.method == 'POST' and request.POST \ or request.GET dct = base_request.copy() + if full == 'shortcut': + dct['cached_label__icontains'] = request.GET.get('term', None) and_reqs, or_reqs = [], [] try: old = 'old' in request_items and int(request_items['old']) @@ -577,7 +624,7 @@ def get_item(model, func_name, default_name, extra_request_keys=[], q = Q(**{req_key: val}) reqs = reqs | q and_reqs.append(reqs) - if 'submited' not in request_items: + if 'submited' not in request_items and full != 'shortcut': # default search # an item is selected in the default menu if default_name in request.session and \ @@ -706,6 +753,22 @@ def get_item(model, func_name, default_name, extra_request_keys=[], for and_req in and_reqs: query = query & and_req + # manage hierarchic in shortcut menu + if full == 'shortcut': + ASSOCIATED_ITEMS = { + Operation: (File, 'associated_file__pk'), + ContextRecord: (Operation, 'operation__pk'), + Find: (ContextRecord, 'base_finds__context_record__pk'), + } + if model in ASSOCIATED_ITEMS: + upper_model, upper_key = ASSOCIATED_ITEMS[model] + model_name = upper_model.SLUG + current = model_name in request.session \ + and request.session[model_name] + if current: + dct = {upper_key: current} + query = query & Q(**dct) + items = model.objects.filter(query).distinct() # print(items.query) q = request_items.get('sidx') @@ -731,6 +794,8 @@ def get_item(model, func_name, default_name, extra_request_keys=[], for idx, col in enumerate(table_cols): if col in model.CONTEXTUAL_TABLE_COLS[contxt]: table_cols[idx] = model.CONTEXTUAL_TABLE_COLS[contxt][col] + if full == 'shortcut': + table_cols = ['cached_label'] # manage sort tables manual_sort_key = None @@ -773,6 +838,9 @@ def get_item(model, func_name, default_name, extra_request_keys=[], pass start = (page_nb - 1) * row_nb end = page_nb * row_nb + if full == 'shortcut': + start = 0 + end = 20 items_nb = items.count() if manual_sort_key: items = items.all() @@ -887,13 +955,18 @@ def get_item(model, func_name, default_name, extra_request_keys=[], if hasattr(model, 'COL_LINK') and k in model.COL_LINK: value = link_ext_template.format(value, value) res[k] = value + if full == 'shortcut': + res['value'] = res.pop('cached_label') rows.append(res) - data = json.dumps({ - "records": items_nb, - "rows": rows, - "page": page_nb, - "total": (items_nb / row_nb + 1) if row_nb else items_nb, - }) + if full == 'shortcut': + data = json.dumps(rows) + else: + data = json.dumps({ + "records": items_nb, + "rows": rows, + "page": page_nb, + "total": (items_nb / row_nb + 1) if row_nb else items_nb, + }) return HttpResponse(data, mimetype='text/plain') elif data_type == "csv": response = HttpResponse(mimetype='text/csv') |
