コード例 #1
0
ファイル: views.py プロジェクト: rakhmatulin/hackathon14
def search(request):
    query = request.GET.get('query', '')
    devices = Device.objects.select_related('employer').\
        order_by('-date_action')
    if query:
        devices = devices.filter(
            Q(sku__icontains=query) | Q(model__icontains=query) | Q(os__icontains=query))
    devices_by_room = Device.get_devices_by_room(query)
    return render(request, 'search.html', locals())
コード例 #2
0
ファイル: views.py プロジェクト: rakhmatulin/hackathon14
def index(request):
    device_filter = DevicesFilterForm.FILTER_CHOICE[0][0]
    data = request.GET.copy()
    if not 'device_filter' in data:
        data['device_filter'] = device_filter
    form = DevicesFilterForm(data)
    if form.is_valid():
        device_filter = form.cleaned_data['device_filter']
    order_params = {
        1: '-date_action',
        2: 'model',
        3: 'employer',
    }
    order_extra_params = {
        1: 'date_action',
        3: 'employer_id',
    }
    order_param = order_params[device_filter]
    devices_query = Device.objects.select_related('employer')
    if device_filter in (1, 3):
        order_extra_param = order_extra_params[device_filter]
        devices_query = devices_query.extra(select={
            'null_values': 'CASE WHEN %s.%s IS NULL THEN 0 ELSE 1 END' %
            (Device._meta.db_table, order_extra_param)}
        ).order_by('-null_values', order_param)
    else:
        devices_query = devices_query.order_by(order_param)
    page = data.get('page')
    paginator = Paginator(devices_query, PAGINATOR_COUNT)
    try:
        devices = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        devices = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        devices = paginator.page(paginator.num_pages)
    devices_by_room = Device.get_devices_by_room(False)
    return render(request, 'index.html', locals())