Esempio n. 1
0
def generate_division_jtable(request, option):
    """
    Generate the jtable data for rendering in the list template.

    :param request: The request for this jtable.
    :type request: :class:`django.http.HttpRequest`
    :param option: Action to take.
    :type option: str of either 'jtlist', 'jtdelete', or 'inline'.
    :returns: :class:`django.http.HttpResponse`
    """

    refresh = request.GET.get("refresh", "no")
    if refresh == "yes":
        target_user_stats()
    if option == "jtlist":
        limit = int(request.GET.get("jtPageSize", 25))
        skip = int(request.GET.get("jtStartIndex", 0))

        response = {}
        response["Result"] = "OK"
        fields = ["division", "email_count", "id", "schema_version"]
        response["TotalRecordCount"] = Division.objects().count()
        response["Records"] = (
            Division.objects().skip(skip).limit(limit).order_by("-email_count").only(*fields).to_dict()
        )
        # response['Records'] = [d.to_dict() for d in response['Records']]

        return HttpResponse(json.dumps(response, default=json_handler), content_type="application/json")

    type_ = "division"
    jtopts = {
        "title": "Divisions",
        "default_sort": "email_count DESC",
        "listurl": reverse("crits.targets.views.%ss_listing" % (type_,), args=("jtlist",)),
        "deleteurl": None,
        "searchurl": None,
        "fields": ["division", "email_count", "id"],
        "hidden_fields": ["_id"],
        "linked_fields": [],
    }
    jtable = build_jtable(jtopts, request)
    jtable["toolbar"] = [
        {
            "tooltip": "'Refresh division stats'",
            "text": "'Refresh'",
            "click": "function () {$.get('"
            + reverse("crits.targets.views.%ss_listing" % (type_))
            + "', {'refresh': 'yes'}); $('target_listing').jtable('load');}",
        }
    ]
    if option == "inline":
        return render_to_response(
            "jtable.html",
            {"jtable": jtable, "jtid": "%s_listing" % type_, "button": "%ss_tab" % type_},
            RequestContext(request),
        )
    else:
        return render_to_response(
            "%s_listing.html" % type_, {"jtable": jtable, "jtid": "%s_listing" % type_}, RequestContext(request)
        )
Esempio n. 2
0
def target_user_stats():
    """
    Generate targets from email To/CC fields, then generate divisions from
    targets list.
    No cleanup or logic is being done on the To/CC fields. If they are not
    valid email addresses (user@domain), they do not get added as a target.
    """

    mapcode = """
        function () {
            try {
                this.to.forEach(function(z) {
                    emit(z.toLowerCase(), {count: 1});
                });
            } catch(err) {}
        }
    """
    reducecode = """
        function(k,v) {
            var count = 0;
            v.forEach(function(v) {
                count += v["count"];
            });
            return {count: count};
        }
    """
    m = Code(mapcode)
    r = Code(reducecode)
    results = Email.objects(to__exists=True).map_reduce(m, r, 'inline')
    for result in results:
        try:
            targs = Target.objects(email_address__iexact=result.key)
            if not targs:
                targs = [Target()]
                targs[0].email_address = result.key.strip().lower()

            for targ in targs:
                targ.email_count = result.value['count']
                targ.save()
        except:
            pass
    mapcode = """
        function() {
            if ("division" in this) {
                emit(this.division, {count: this.email_count})
            }
        }
    """
    m = Code(mapcode)
    try:
        results = Target.objects().map_reduce(m, r, 'inline')
        for result in results:
            div = Division.objects(division__iexact=result.key).first()
            if not div:
                div = Division()
                div.division = result.key
            div.email_count = result.value['count']
            div.save()
    except:
        raise
Esempio n. 3
0
def generate_division_jtable(request, option):
    """
    Generate the jtable data for rendering in the list template.

    :param request: The request for this jtable.
    :type request: :class:`django.http.HttpRequest`
    :param option: Action to take.
    :type option: str of either 'jtlist', 'jtdelete', or 'inline'.
    :returns: :class:`django.http.HttpResponse`
    """

    refresh = request.GET.get("refresh", "no")
    if refresh == "yes":
        target_user_stats()
    if option == "jtlist":
        limit = int(request.GET.get('jtPageSize', 25))
        skip = int(request.GET.get('jtStartIndex', 0))

        response = {}
        response['Result'] = "OK"
        fields = ["division", "email_count", "id", "schema_version"]
        response['TotalRecordCount'] = Division.objects().count()
        response['Records'] = Division.objects().skip(skip).limit(limit).\
                                order_by("-email_count").only(*fields).to_dict()
        #response['Records'] = [d.to_dict() for d in response['Records']]

        return HttpResponse(json.dumps(response, default=json_handler),
                            content_type="application/json")

    type_ = "division"
    jtopts = {
        'title':
        "Divisions",
        'default_sort':
        "email_count DESC",
        'listurl':
        reverse('crits.targets.views.%ss_listing' % (type_, ),
                args=('jtlist', )),
        'deleteurl':
        None,
        'searchurl':
        None,
        'fields': ["division", "email_count", "id"],
        'hidden_fields': ["_id"],
        'linked_fields': []
    }
    jtable = build_jtable(jtopts, request)
    jtable['toolbar'] = [
        {
            'tooltip':
            "'Refresh division stats'",
            'text':
            "'Refresh'",
            'click':
            "function () {$.get('" +
            reverse('crits.targets.views.%ss_listing' % (type_)) +
            "', {'refresh': 'yes'}); $('target_listing').jtable('load');}",
        },
    ]
    if option == "inline":
        return render_to_response(
            "jtable.html", {
                'jtable': jtable,
                'jtid': '%s_listing' % type_,
                'button': '%ss_tab' % type_
            }, RequestContext(request))
    else:
        return render_to_response("%s_listing.html" % type_, {
            'jtable': jtable,
            'jtid': '%s_listing' % type_
        }, RequestContext(request))