Example #1
0
def run_query(query_field, exclude_kwargs, order_by, request, active_filters):
    "run a query"
    # if query_field in ['from_address', 'from_domain']:
    #         data = Message.messages.from_user(request).values(query_field).\
    #         exclude(**exclude_kwargs).annotate(num_count=Count(query_field),
    #        size=Sum('size')).order_by(order_by)
    #     if query_field in ['to_address', 'to_domain']:
    #         data = Message.messages.to_user(request).values(query_field).\
    #         exclude(**exclude_kwargs).annotate(num_count=Count(query_field),
    #        size=Sum('size')).order_by(order_by)
    # if query_field in ['to_address', 'to_domain']:
    #     if not request.session.get('filter_by', False):
    #         data = Recipient.messages.for_user(request).values(
    #                 query_field).exclude(**exclude_kwargs).annotate(
    #                 num_count=Count(query_field), size=Sum('message__size')
    #                 ).order_by(order_by)
    #         return data[:10]

    data = (
        Message.messages.for_user(request)
        .values(query_field)
        .exclude(**exclude_kwargs)
        .annotate(num_count=Count(query_field), size=Sum("size"))
        .order_by(order_by)
    )
    data = apply_filter(data, request, active_filters)
    data = data[:10]
    return data
Example #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
Example #3
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
Example #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
Example #5
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
Example #6
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"), 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
Example #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
    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)[:50]
        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')[:50]
    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':
            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) 
        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():
        if not list_all:
            message_list = map(jsonify_msg_list, message_list)
            pg = None
        else:
            p = Paginator(message_list, 50)
            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 = simplejson.dumps({'items':message_list, 'paginator':pg})
        return HttpResponse(json, mimetype='application/javascript')

    if list_all:
        return object_list(request, template_name='messages/index.html',
        queryset=message_list, paginate_by=50, 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},
        allow_empty=True)
    else:
        return object_list(request, template_name='messages/index.html',
        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})
Example #8
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
    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)[:50]
        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')[:50]
    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, 50)
            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, 50)
            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=50, 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})