Ejemplo n.º 1
0
def run_query(query_field, exclude_kwargs, order_by, request, active_filters):
    "run a query"
    data = Message.messages.for_user(request).values(query_field).exclude(
    **exclude_kwargs).annotate(num_count=Count(query_field),
    total_size=Sum('size')).order_by(order_by)
    data = apply_filter(data, request, active_filters)
    data = data[:10]
    return data
Ejemplo n.º 2
0
def run_query(query_field, exclude_kwargs, order_by, request, active_filters):
    "run a query"
    data = Message.messages.for_user(request).values(query_field).exclude(
    **exclude_kwargs).annotate(num_count=Count(query_field),
    total_size=Sum('size')).order_by(order_by)
    data = apply_filter(data, request, active_filters)
    data = data[:10]
    return data
Ejemplo n.º 3
0
def run_hosts_query(request, active_filters):
    "run the top hosts query"
    data = Message.messages.for_user(request).values('clientip').exclude(
        Q(clientip__exact='') | Q(clientip__exact='127.0.0.1') |
        Q(clientip__isnull=True)).annotate(num_count=Count('clientip'),
        total_size=Sum('size'), virus_total=Sum('virusinfected'),
        spam_total=Sum('spam')).order_by('-num_count')
    data = apply_filter(data, request, active_filters)
    data = data[:10]
    return data
Ejemplo n.º 4
0
def run_hosts_query(request, active_filters):
    "run the top hosts query"
    data = Message.messages.for_user(request).values('clientip').exclude(
        Q(clientip__exact='') | Q(clientip__exact='127.0.0.1') |
        Q(clientip__isnull=True)).annotate(num_count=Count('clientip'),
        total_size=Sum('size'), virus_total=Sum('virusinfected'),
        spam_total=Sum('spam')).order_by('-num_count')
    data = apply_filter(data, request, active_filters)
    data = data[:10]
    return data
Ejemplo n.º 5
0
def run_query(reportid, request, active_filters):
    "run a query"
    query_field = REPORT_DICT[reportid]['queryfield'] if reportid not in [13, 14] else 'query'
    exclude_kwargs = REPORT_DICT[reportid]['exclude']
    order_by = REPORT_DICT[reportid]['order']
    size_field = REPORT_DICT[reportid]['size']
    model = REPORT_DICT[reportid]['model']
    if reportid in [7, 8]:
        data = model.objects.values(query_field, 'url')\
                .exclude(**exclude_kwargs)\
                .annotate(num_count=Count(query_field),
                total_size=Sum(size_field))\
                .order_by(order_by)
    else:
        data = model.objects.values(query_field)\
                .exclude(**exclude_kwargs)\
                .annotate(num_count=Count(query_field),
                total_size=Sum(size_field))\
                .order_by(order_by)
    data = apply_filter(data, request, active_filters, True, reportid)
    data = data[:10]
    return data
Ejemplo n.º 6
0
def index(request,
          list_all=0,
          page=1,
          view_type='full',
          direction='dsc',
          order_by='timestamp',
          quarantine_type=None):
    """index"""
    active_filters = []
    ordering = order_by
    form = None
    num_of_recent_msgs = getattr(settings, 'BARUWA_NUM_RECENT_MESSAGES', 50)
    template_name = 'mail/messages/index.html'
    if direction == 'dsc':
        ordering = order_by
        order_by = '-%s' % order_by

    if not list_all:
        last_ts = request.META.get('HTTP_X_LAST_TIMESTAMP', None)
        if not last_ts is None:
            last_ts = last_ts.strip()
            if not re.match(
                    r'^(\d{4})\-(\d{2})\-(\d{2})(\s)(\d{2})\:(\d{2})\:(\d{2})$',
                    last_ts):
                last_ts = None
        if not last_ts is None and request.is_ajax():
            message_list = Message.messages.for_user(request).values(
                'id', 'timestamp', 'from_address', 'to_address', 'subject',
                'size', 'sascore', 'highspam', 'spam', 'virusinfected',
                'otherinfected', 'whitelisted', 'blacklisted', 'nameinfected',
                'scaned').filter(timestamp__gt=last_ts)[:num_of_recent_msgs]
        else:
            message_list = Message.messages.for_user(request).values(
                'id', 'timestamp', 'from_address', 'to_address', 'subject',
                'size', 'sascore', 'highspam', 'spam', 'virusinfected',
                'otherinfected', 'whitelisted', 'blacklisted', 'nameinfected',
                'scaned')[:num_of_recent_msgs]
    else:
        if view_type == 'archive':
            message_list = Archive.messages.for_user(request).values(
                'id', 'timestamp', 'from_address', 'to_address', 'subject',
                'size', 'sascore', 'highspam', 'spam', 'virusinfected',
                'otherinfected', 'whitelisted', 'blacklisted', 'nameinfected',
                'scaned').order_by(order_by)
        elif view_type == 'quarantine':
            template_name = 'mail/messages/quarantine.html'
            message_list = Message.quarantine.for_user(request).values(
                'id', 'timestamp', 'from_address', 'to_address', 'subject',
                'size', 'sascore', 'highspam', 'spam', 'virusinfected',
                'otherinfected', 'whitelisted', 'blacklisted', 'isquarantined',
                'nameinfected', 'scaned').order_by(order_by)
            if quarantine_type == 'spam':
                message_list = message_list.filter(spam=1)
            if quarantine_type == 'policyblocked':
                message_list = message_list.filter(spam=0)
            form = BulkQuarantineProcessForm()
            form.fields['altrecipients'].widget.attrs['size'] = '55'
            message_list = apply_filter(message_list, request, active_filters)
            p = Paginator(message_list, num_of_recent_msgs)
            if page == 'last':
                page = p.num_pages
            po = p.page(page)
            choices = [(message['id'], message['id'])
                       for message in po.object_list]
            form.fields['message_id']._choices = choices
            form.fields['message_id'].widget.choices = choices
            request.session['quarantine_choices'] = choices
            request.session.modified = True
        else:
            message_list = Message.messages.for_user(request).values(
                'id', 'timestamp', 'from_address', 'to_address', 'subject',
                'size', 'sascore', 'highspam', 'spam', 'virusinfected',
                'otherinfected', 'whitelisted', 'blacklisted', 'nameinfected',
                'scaned').order_by(order_by)
        message_list = apply_filter(message_list, request, active_filters)
    if request.is_ajax():
        sys_status = jsonify_status(status(request))
        if not list_all:
            message_list = map(jsonify_msg_list, message_list)
            pg = None
        else:
            p = Paginator(message_list, num_of_recent_msgs)
            if page == 'last':
                page = p.num_pages
            po = p.page(page)
            message_list = po.object_list
            message_list = map(jsonify_msg_list, message_list)
            page = int(page)
            ap = 2
            startp = max(page - ap, 1)
            if startp <= 3:
                startp = 1
            endp = page + ap + 1
            pn = [n for n in range(startp, endp) if n > 0 and n <= p.num_pages]
            pg = {
                'page': page,
                'pages': p.num_pages,
                'page_numbers': pn,
                'next': po.next_page_number(),
                'previous': po.previous_page_number(),
                'has_next': po.has_next(),
                'has_previous': po.has_previous(),
                'show_first': 1 not in pn,
                'show_last': p.num_pages not in pn,
                'view_type': view_type,
                'direction': direction,
                'order_by': ordering,
                'quarantine_type': quarantine_type
            }
        json = anyjson.dumps({
            'items': message_list,
            'paginator': pg,
            'status': sys_status
        })
        return HttpResponse(json, mimetype='application/javascript')

    if list_all:
        return object_list(request,
                           template_name=template_name,
                           queryset=message_list,
                           paginate_by=num_of_recent_msgs,
                           page=page,
                           extra_context={
                               'view_type': view_type,
                               'direction': direction,
                               'order_by': ordering,
                               'active_filters': active_filters,
                               'list_all': list_all,
                               'quarantine_type': quarantine_type,
                               'quarantine_form': form
                           },
                           allow_empty=True)
    else:
        return object_list(request,
                           template_name=template_name,
                           queryset=message_list,
                           extra_context={
                               'view_type': view_type,
                               'direction': direction,
                               'order_by': ordering,
                               'active_filters': active_filters,
                               'list_all': list_all,
                               'quarantine_type': quarantine_type
                           })
Ejemplo n.º 7
0
def index(request, list_all=0, page=1, view_type='full', direction='dsc',
        order_by='timestamp', quarantine_type=None):
    """index"""
    active_filters = []
    ordering = order_by
    form = None
    num_of_recent_msgs = getattr(settings, 'BARUWA_NUM_RECENT_MESSAGES', 50)
    template_name = 'messages/index.html'
    if direction == 'dsc':
        ordering = order_by
        order_by = '-%s' % order_by

    if not list_all:
        last_ts = request.META.get('HTTP_X_LAST_TIMESTAMP', None)
        if not last_ts is None:
            last_ts = last_ts.strip()
            if not re.match(
                r'^(\d{4})\-(\d{2})\-(\d{2})(\s)(\d{2})\:(\d{2})\:(\d{2})$',
                last_ts):
                last_ts = None
        if not last_ts is None and request.is_ajax():
            message_list = Message.messages.for_user(request).values(
            'id', 'timestamp', 'from_address', 'to_address', 'subject',
            'size', 'sascore', 'highspam', 'spam', 'virusinfected',
            'otherinfected', 'whitelisted', 'blacklisted', 'nameinfected',
            'scaned').filter(timestamp__gt=last_ts)[:num_of_recent_msgs]
        else:
            message_list = Message.messages.for_user(request).values(
            'id', 'timestamp', 'from_address', 'to_address', 'subject',
            'size', 'sascore', 'highspam', 'spam', 'virusinfected',
            'otherinfected', 'whitelisted', 'blacklisted', 'nameinfected',
            'scaned')[:num_of_recent_msgs]
    else:
        if view_type == 'archive':
            message_list = Archive.messages.for_user(request).values(
            'id', 'timestamp', 'from_address', 'to_address', 'subject',
            'size', 'sascore', 'highspam', 'spam', 'virusinfected',
            'otherinfected', 'whitelisted', 'blacklisted', 'nameinfected',
            'scaned').order_by(order_by)
        elif view_type == 'quarantine':
            template_name = 'messages/quarantine.html'
            message_list = Message.quarantine.for_user(request).values(
            'id', 'timestamp', 'from_address', 'to_address', 'subject',
            'size', 'sascore', 'highspam', 'spam', 'virusinfected',
            'otherinfected', 'whitelisted', 'blacklisted', 'isquarantined',
            'nameinfected', 'scaned').order_by(order_by)
            if quarantine_type == 'spam':
                message_list = message_list.filter(spam=1)
            if quarantine_type == 'policyblocked':
                message_list = message_list.filter(spam=0)
            form = BulkQuarantineProcessForm()
            form.fields['altrecipients'].widget.attrs['size'] = '55'
            message_list = apply_filter(message_list, request, active_filters)
            p = Paginator(message_list, num_of_recent_msgs)
            if page == 'last':
                page = p.num_pages
            po = p.page(page)
            choices = [(message['id'], message['id']) for message in po.object_list]
            form.fields['message_id']._choices = choices
            form.fields['message_id'].widget.choices = choices
            request.session['quarantine_choices'] = choices
            request.session.modified = True
        else:
            message_list = Message.messages.for_user(request).values(
            'id', 'timestamp', 'from_address', 'to_address', 'subject',
            'size', 'sascore', 'highspam', 'spam', 'virusinfected',
            'otherinfected', 'whitelisted', 'blacklisted', 'nameinfected',
            'scaned').order_by(order_by)
        message_list = apply_filter(message_list, request, active_filters)
    if request.is_ajax():
        sys_status = jsonify_status(status(request))
        if not list_all:
            message_list = map(jsonify_msg_list, message_list)
            pg = None
        else:
            p = Paginator(message_list, num_of_recent_msgs)
            if page == 'last':
                page = p.num_pages
            po = p.page(page)
            message_list = po.object_list
            message_list = map(jsonify_msg_list, message_list)
            page = int(page)
            ap = 2
            startp = max(page - ap, 1)
            if startp <= 3:
                startp = 1
            endp = page + ap + 1
            pn = [n for n in range(startp, endp) if n > 0 and n <= p.num_pages]
            pg = {'page': page, 'pages': p.num_pages, 'page_numbers': pn,
            'next': po.next_page_number(), 'previous': po.previous_page_number(),
            'has_next': po.has_next(), 'has_previous': po.has_previous(),
            'show_first': 1 not in pn, 'show_last': p.num_pages not in pn,
            'view_type': view_type, 'direction': direction, 'order_by': ordering,
            'quarantine_type': quarantine_type}
        json = anyjson.dumps({'items': message_list, 'paginator': pg,
                                'status': sys_status})
        return HttpResponse(json, mimetype='application/javascript')

    if list_all:
        return object_list(request, template_name=template_name,
        queryset=message_list, paginate_by=num_of_recent_msgs, page=page,
        extra_context={'view_type': view_type, 'direction': direction,
        'order_by': ordering, 'active_filters': active_filters,
        'list_all': list_all, 'quarantine_type': quarantine_type,
        'quarantine_form': form},
        allow_empty=True)
    else:
        return object_list(request, template_name=template_name,
        queryset=message_list, extra_context={'view_type': view_type,
        'direction': direction, 'order_by': ordering,
        'active_filters': active_filters, 'list_all': list_all,
        'quarantine_type': quarantine_type})
Ejemplo n.º 8
0
def index(request,
          list_all=0,
          page=1,
          view_type='full',
          direction='dsc',
          order_by='id'):
    """index"""
    template_name = 'web/visits/index.html'
    active_filters = []
    num_of_recent_visits = getattr(settings, 'BARUWA_NUM_RECENT_MESSAGES', 50)
    ordering = order_by
    if direction == 'dsc':
        ordering = order_by
        order_by = '-%s' % order_by

    if not list_all:
        last_id = request.META.get('HTTP_X_LAST_ID', None)
        if not last_id is None:
            last_id = last_id.strip()
            if not re.match(r'^(\d+)$', last_id):
                last_id = None
        if not last_id is None and request.is_ajax():
            visit_list = Traffic.objects\
                        .filter(id__gt=last_id)\
                        .all()[:num_of_recent_visits]
        else:
            visit_list = Traffic.objects.all()[:num_of_recent_visits]
    else:
        visit_list = Traffic.objects.order_by(order_by)
        if view_type == 'url':
            inner_q = Urlfilterdeny.objects.values('traffic').query
            visit_list = visit_list.filter(id__in=inner_q)
        if view_type == 'virus':
            inner_q = Virusdetection.objects.values('traffic').query
            visit_list = visit_list.filter(id__in=inner_q)
        if view_type == 'search':
            inner_q = Searchquery.objects.values('traffic').query
            visit_list = visit_list.filter(id__in=inner_q)
        visit_list = apply_filter(visit_list, request, active_filters, True)
        # visit_list = visit_list.all()

    if request.is_ajax():
        sys_status = None
        if not list_all:
            visit_list = map(jsonify_visit_list, visit_list)
            pg = None
        else:
            p = Paginator(visit_list, num_of_recent_visits)
            if page == 'last':
                page = p.num_pages
            po = p.page(page)
            visit_list = po.object_list
            visit_list = map(jsonify_visit_list, visit_list)
            page = int(page)
            ap = 2
            startp = max(page - ap, 1)
            if startp <= 3:
                startp = 1
            endp = page + ap + 1
            pn = [n for n in range(startp, endp) if n > 0 and n <= p.num_pages]
            pg = {
                'page': page,
                'pages': p.num_pages,
                'page_numbers': pn,
                'next': po.next_page_number(),
                'previous': po.previous_page_number(),
                'has_next': po.has_next(),
                'has_previous': po.has_previous(),
                'show_first': 1 not in pn,
                'show_last': p.num_pages not in pn,
                'view_type': view_type,
                'direction': direction,
                'order_by': ordering
            }
        json = anyjson.dumps({
            'items': visit_list,
            'paginator': pg,
            'status': sys_status
        })
        return HttpResponse(json, mimetype='application/javascript')
    if list_all:
        return object_list(request,
                           template_name=template_name,
                           queryset=visit_list,
                           paginate_by=num_of_recent_visits,
                           page=page,
                           extra_context={
                               'view_type': view_type,
                               'direction': direction,
                               'order_by': ordering,
                               'active_filters': active_filters,
                               'list_all': list_all,
                               'app': 'web/visits/' + view_type
                           },
                           allow_empty=True)
    else:
        return object_list(request,
                           template_name=template_name,
                           queryset=visit_list,
                           extra_context={
                               'view_type': view_type,
                               'direction': direction,
                               'order_by': ordering,
                               'active_filters': active_filters,
                               'list_all': list_all
                           })
Ejemplo n.º 9
0
def index(request, list_all=0, page=1, view_type='full', direction='dsc',
        order_by='id'):
    """index"""
    template_name = 'web/visits/index.html'
    active_filters = []
    num_of_recent_visits = getattr(settings, 'BARUWA_NUM_RECENT_MESSAGES', 50)
    ordering = order_by
    if direction == 'dsc':
        ordering = order_by
        order_by = '-%s' % order_by

    if not list_all:
        last_id = request.META.get('HTTP_X_LAST_ID', None)
        if not last_id is None:
            last_id = last_id.strip()
            if not re.match(r'^(\d+)$', last_id):
                last_id = None
        if not last_id is None and request.is_ajax():
            visit_list = Traffic.objects\
                        .filter(id__gt=last_id)\
                        .all()[:num_of_recent_visits]
        else:
            visit_list = Traffic.objects.all()[:num_of_recent_visits]
    else:
        visit_list = Traffic.objects.order_by(order_by)
        if view_type == 'url':
            inner_q = Urlfilterdeny.objects.values('traffic').query
            visit_list = visit_list.filter(id__in=inner_q)
        if view_type == 'virus':
            inner_q = Virusdetection.objects.values('traffic').query
            visit_list = visit_list.filter(id__in=inner_q)
        if view_type == 'search':
            inner_q = Searchquery.objects.values('traffic').query
            visit_list = visit_list.filter(id__in=inner_q)
        visit_list = apply_filter(visit_list, request, active_filters, True)
        # visit_list = visit_list.all()
    
    if request.is_ajax():
        sys_status = None
        if not list_all:
            visit_list = map(jsonify_visit_list, visit_list)
            pg = None
        else:
            p = Paginator(visit_list, num_of_recent_visits)
            if page == 'last':
                page = p.num_pages
            po = p.page(page)
            visit_list = po.object_list
            visit_list = map(jsonify_visit_list, visit_list)
            page = int(page)
            ap = 2
            startp = max(page - ap, 1)
            if startp <= 3:
                startp = 1
            endp = page + ap + 1
            pn = [n for n in range(startp, endp) if n > 0 and n <= p.num_pages]
            pg = {'page': page, 'pages': p.num_pages, 'page_numbers': pn,
            'next': po.next_page_number(), 'previous': po.previous_page_number(),
            'has_next': po.has_next(), 'has_previous': po.has_previous(),
            'show_first': 1 not in pn, 'show_last': p.num_pages not in pn,
            'view_type': view_type, 'direction': direction, 'order_by': ordering}
        json = anyjson.dumps({'items': visit_list, 'paginator': pg,
                                'status': sys_status})
        return HttpResponse(json, mimetype='application/javascript')
    if list_all:
        return object_list(request, template_name=template_name,
        queryset=visit_list, paginate_by=num_of_recent_visits, page=page,
        extra_context={'view_type': view_type, 'direction': direction,
        'order_by': ordering, 'active_filters': active_filters,
        'list_all': list_all, 'app': 'web/visits/' + view_type},
        allow_empty=True)
    else:
        return object_list(request, template_name=template_name,
        queryset=visit_list, extra_context={'view_type': view_type,
        'direction': direction, 'order_by': ordering,
        'active_filters': active_filters, 'list_all': list_all})