views.py 8.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Dalahro's avatar
Dalahro committed
23
# App de recherche pour re2o
chirac's avatar
chirac committed
24
# Augustin lemesle, Gabriel Détraz, Goulven Kermarec
Dalahro's avatar
Dalahro committed
25
# Gplv2
26 27 28

from __future__ import unicode_literals

Dalahro's avatar
Dalahro committed
29
from django.shortcuts import render
30
from django.shortcuts import get_object_or_404
31
from django.template.context_processors import csrf
Dalahro's avatar
Dalahro committed
32
from django.template import Context, RequestContext, loader
chirac's avatar
chirac committed
33
from django.contrib.auth.decorators import login_required
Dalahro's avatar
Dalahro committed
34 35

from django.db.models import Q
Dalahro's avatar
Dalahro committed
36
from users.models import User, Ban, Whitelist
37
from machines.models import Machine, Interface
Dalahro's avatar
Dalahro committed
38
from topologie.models import Port, Switch
Dalahro's avatar
Dalahro committed
39
from cotisations.models import Facture
40
from preferences.models import GeneralOption
41 42 43 44 45 46 47
from search.forms import (
    SearchForm,
    SearchFormPlus,
    CHOICES_USER,
    CHOICES_AFF,
    initial_choices
)
48
from re2o.utils import SortTable
49

50 51 52 53 54 55 56 57 58 59
def get_results(query, request, filters={}):
    start = filters.get('s', None)
    end = filters.get('e', None)
    user_state = filters.get('u', initial_choices(CHOICES_USER))
    aff = filters.get('a', initial_choices(CHOICES_AFF))

    options, created = GeneralOption.objects.get_or_create()
    max_result = options.search_display_page

    user_state_filter = Q()
60
    for s in user_state:
61
        user_state_filter |= Q(state = s)
62
    
63
    connexion = []
64
   
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    results = {
        'users_list': User.objects.none(),
        'machines_list' : Machine.objects.none(),
        'facture_list' : Facture.objects.none(),
        'ban_list' : Ban.objects.none(),
        'white_list': Whitelist.objects.none(),
        'port_list': Port.objects.none(),
        'switch_list': Switch.objects.none()
    }

    users_filter = Q(
        user__pseudo__icontains = query
    ) | Q(
        user__adherent__name__icontains = query
    ) | Q(
        user__surname__icontains = query
    )
    if not request.user.has_perms(('cableur',)):
        users_filter &= Q(user = request.user)

    # Users
    if '0' in aff:
        filter_user_list = Q(
            adherent__room__name__icontains = query
        ) | Q(
            club__room__name__icontains = query
        ) | Q(
            pseudo__icontains = query
        ) | Q(
            adherent__name__icontains = query
        ) | Q(
            surname__icontains = query
        ) & user_state_filter
        if not request.user.has_perms(('cableur',)):
            filter_user_list &= Q(id=request.user.id)
100 101 102 103 104 105 106
        results['users_list'] = User.objects.filter(filter_user_list)
        results['users_list'] = SortTable.sort(
            results['users_list'],
            request.GET.get('col'),
            request.GET.get('order'),
            SortTable.USERS_INDEX
        )
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    # Machines
    if '1' in aff:
        filter_machine_list = Q(
            user__pseudo__icontains = query
        ) | Q(
            user__adherent__name__icontains = query
        ) | Q(
            user__surname__icontains = query
        ) | Q(
            interface__mac_address__icontains = query
        ) | Q(
            interface__ipv4__ipv4__icontains = query
        ) | Q(
            interface__domain__name__icontains = query
        ) | Q(
            interface__domain__related_domain__name__icontains = query
        )
        if not request.user.has_perms(('cableur',)):
            filter_machine_list &= Q(machine__user__id=request.user.id)
127 128 129 130 131 132 133
        results['machines_list'] = Machine.objects.filter(filter_machine_list)
        results['machines_list'] = SortTable.sort(
            results['machines_list'],
            request.GET.get('col'),
            request.GET.get('order'),
            SortTable.MACHINES_INDEX
        )
134 135 136

    # Factures
    if '2' in aff:
137
        filter_facture_list = users_filter
138
        if start != None:
139
            filter_facture_list &= Q(date__gte=start)
140
        if end != None:
141 142 143 144 145 146 147 148
            filter_facture_list &= Q(date__lte=end)
        results['facture_list'] = Facture.objects.filter(filter_facture_list)
        results['facture_list'] = SortTable.sort(
            results['facture_list'],
            request.GET.get('col'),
            request.GET.get('order'),
            SortTable.COTISATIONS_INDEX
        )
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    # Bans
    if '3' in aff:
        date_filter = Q()
        if start != None:
            date_filter &= (
                Q(date_start__gte=start) & Q(date_end__gte=start)
            ) | (
                Q(date_start__lte=start) & Q(date_end__gte=start)
            ) | (
                Q(date_start__gte=start) & Q(date_end__lte=start)
            )
        if end != None:
            date_filter &= (
                Q(date_start__lte=end) & Q(date_end__lte=end)
            ) | (
                Q(date_start__lte=end) & Q(date_end__gte=end)
            ) | (
                Q(date_start__gte=end) & Q(date_end__lte=end)
            )
169 170 171 172 173 174 175
        results['ban_list'] = Ban.objects.filter(users_filter & date_filter)
        results['ban_list'] = SortTable.sort(
            results['ban_list'],
            request.GET.get('col'),
            request.GET.get('order'),
            SortTable.USERS_INDEX_BAN
        )
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

    # Whitelists
    if '4' in aff:
        date_filter = Q()
        if start != None:
            date_filter &= (
                Q(date_start__gte=start) & Q(date_end__gte=start)
            ) | (
                Q(date_start__lte=start) & Q(date_end__gte=start)
            ) | (
                Q(date_start__gte=start) & Q(date_end__lte=start)
            )
        if end != None:
            date_filter &= (
                Q(date_start__lte=end) & Q(date_end__lte=end)
            ) | (
                Q(date_start__lte=end) & Q(date_end__gte=end)
            ) | (
                Q(date_start__gte=end) & Q(date_end__lte=end)
            )
196 197 198 199 200 201 202
        results['white_list'] = Whitelist.objects.filter(users_filter & date_filter)
        results['white_list'] = SortTable.sort(
            results['white_list'],
            request.GET.get('col'),
            request.GET.get('order'),
            SortTable.USERS_INDEX_WHITE
        )
203 204 205

    # Switch ports
    if '5' in aff and request.user.has_perms(('cableur',)):
206 207 208 209 210 211 212
        results['port_list'] = Port.objects.filter(details__icontains=query)
        results['port_list'] = SortTable.sort(
            results['port_list'],
            request.GET.get('col'),
            request.GET.get('order'),
            SortTable.TOPOLOGIE_INDEX_PORT
        )
213

214 215
    # Switches
    if '6' in aff and request.user.has_perms(('cableur')):
216 217 218 219 220 221 222 223 224 225
        results['switch_list'] = Switch.objects.filter(details__icontains=query)
        results['switch_list'] = SortTable.sort(
            results['switch_list'],
            request.GET.get('col'),
            request.GET.get('order'),
            SortTable.TOPOLOGIE_INDEX
        )

    for r in results.keys():
        results[r] = results[r].distinct()[:max_result]
226

227
    results.update({'max_result': max_result})
228

229
    return results
Dalahro's avatar
Dalahro committed
230

chirac's avatar
chirac committed
231
@login_required
Dalahro's avatar
Dalahro committed
232
def search(request):
233 234
    search_form = SearchForm(request.GET or None)
    if search_form.is_valid():
235 236 237 238 239 240 241 242 243
        return render(
            request,
            'search/index.html',
            get_results(
                search_form.cleaned_data.get('q', ''),
                request,
                search_form.cleaned_data
            )
        )
244
    return render(request, 'search/search.html', {'search_form' : search_form})
Dalahro's avatar
Dalahro committed
245

chirac's avatar
chirac committed
246
@login_required
Dalahro's avatar
Dalahro committed
247
def searchp(request):
248 249
    search_form = SearchFormPlus(request.GET or None)
    if search_form.is_valid():
250 251 252 253 254 255 256 257 258
        return render(
            request,
            'search/index.html',
            get_results(
                search_form.cleaned_data.get('q', ''),
                request,
                search_form.cleaned_data
            )
        )
259
    return render(request, 'search/search.html', {'search_form' : search_form})