Esempio n. 1
0
File: views.py Progetto: vsbca/crits
def edit_campaign(request, ctype, objectid):
    """
    Edit an attributed Campaign for a top-level object. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param ctype: CRITs type for the top-level object.
    :type ctype: str
    :param objectid: The ObjectId of the top-level object.
    :type objectid: str
    :returns: :class:`django.http.HttpResponse`
    """
    user = request.user

    if request.method == "POST" and request.is_ajax():
        form = CampaignForm(request.POST)
        acl = get_acl_object(ctype)
        if user.has_access_to(acl.CAMPAIGNS_EDIT):
            if form.is_valid():
                data = form.cleaned_data
                campaign = data['name']
                confidence = data['confidence']
                description = data['description']
                related = data['related']
                analyst = request.user.username
                try:
                    date = datetime.datetime.strptime(data['date'],
                                                      settings.PY_DATETIME_FORMAT)
                except ValueError:
                    date = datetime.datetime.now()

                result = campaign_edit(ctype,
                                       objectid,
                                       campaign,
                                       confidence,
                                       description,
                                       date,
                                       related,
                                       analyst)
                if result['success']:
                    return HttpResponse(json.dumps(result),
                                        content_type="application/json")
                else:
                    result.update({'form': form.as_table()})
                    return HttpResponse(json.dumps(result),
                                        content_type="application/json")
            else:
                return HttpResponse(json.dumps({'success': False,
                                                'form': form.as_table()}),
                                    content_type="application/json")
        else:
            result ={'success': False,
                     'message':'User does not have permission to edit Campaign.'}
            return HttpResponse(json.dumps(result),
                                content_type="application/json")
    else:
        return HttpResponse(json.dumps({'success': False}),
                            content_type="application/json")
Esempio n. 2
0
def campaign_add(request, ctype, objectid):
    """
    Attribute a Campaign to a top-level object. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param ctype: CRITs type for the top-level object.
    :type ctype: str
    :param objectid: The ObjectId of the top-level object.
    :type objectid: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        form = CampaignForm(request.POST)
        result = {}
        if form.is_valid():
            data = form.cleaned_data
            campaign = data["name"]
            confidence = data["confidence"]
            description = data["description"]
            related = data["related"]
            analyst = request.user.username
            result = campaign_addh(ctype, objectid, campaign, confidence, description, related, analyst)
            if result["success"]:
                return HttpResponse(json.dumps(result), mimetype="application/json")
        result["form"] = form.as_table()
        result["success"] = False
        return HttpResponse(json.dumps(result), mimetype="application/json")
    else:
        return HttpResponse(
            json.dumps({"success": False, "message": "Expected AJAX request."}), mimetype="application/json"
        )
Esempio n. 3
0
def campaign_add(request, ctype, objectid):
    """
    Attribute a Campaign to a top-level object. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param ctype: CRITs type for the top-level object.
    :type ctype: str
    :param objectid: The ObjectId of the top-level object.
    :type objectid: str
    :returns: :class:`django.http.HttpResponse`
    """
    user = request.user

    if request.method == "POST" and request.is_ajax():
        form = CampaignForm(request.POST)
        result = {}
        acl = get_acl_object(ctype)

        if user.has_access_to(acl.CAMPAIGNS_ADD):
            if form.is_valid():
                data = form.cleaned_data
                campaign = data['name']
                confidence = data['confidence']
                description = data['description']
                related = data['related']
                analyst = request.user.username
                result = campaign_addh(campaign,
                                       confidence,
                                       description,
                                       related,
                                       analyst,
                                       ctype,
                                       objectid,
                                       update=False)
                if result['success']:
                    return HttpResponse(json.dumps(result),
                                        content_type="application/json")
            result['form'] = form.as_table()
            result['success'] = False
            return HttpResponse(json.dumps(result),
                                content_type="application/json")
        else:
            result['success'] = False
            result[
                'message'] = 'User does not have permission to add campaign to TLO.'
            return HttpResponse(json.dumps(result),
                                content_type="application/json")
    else:
        return HttpResponse(json.dumps({
            'success': False,
            'message': "Expected AJAX request."
        }),
                            content_type="application/json")
Esempio n. 4
0
def campaign_add(request, ctype, objectid):
    """
    Attribute a Campaign to a top-level object. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param ctype: CRITs type for the top-level object.
    :type ctype: str
    :param objectid: The ObjectId of the top-level object.
    :type objectid: str
    :returns: :class:`django.http.HttpResponse`
    """
    user = request.user

    if request.method == "POST" and request.is_ajax():
        form = CampaignForm(request.POST)
        result = {}
        acl = get_acl_object(ctype)

        if user.has_access_to(acl.CAMPAIGNS_ADD):
            if form.is_valid():
                data = form.cleaned_data
                campaign = data['name']
                confidence = data['confidence']
                description = data['description']
                related = data['related']
                analyst= request.user.username
                result = campaign_addh(campaign,
                                       confidence,
                                       description,
                                       related,
                                       analyst,
                                       ctype,
                                       objectid,
                                       update=False)
                if result['success']:
                    return HttpResponse(json.dumps(result),
                                        content_type="application/json")
            result['form'] = form.as_table()
            result['success'] = False
            return HttpResponse(json.dumps(result),
                                content_type="application/json")
        else:
            result['success'] = False
            result['message'] = 'User does not have permission to add campaign to TLO.'
            return HttpResponse(json.dumps(result),
                                content_type="application/json")
    else:
        return HttpResponse(json.dumps({'success': False,
                                        'message': "Expected AJAX request."}),
                            content_type="application/json")
Esempio n. 5
0
def edit_campaign(request, ctype, objectid):
    """
    Edit an attributed Campaign for a top-level object. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param ctype: CRITs type for the top-level object.
    :type ctype: str
    :param objectid: The ObjectId of the top-level object.
    :type objectid: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        form = CampaignForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            campaign = data["name"]
            confidence = data["confidence"]
            description = data["description"]
            related = data["related"]
            analyst = request.user.username
            try:
                date = datetime.datetime.strptime(data["date"], settings.PY_DATETIME_FORMAT)
            except ValueError:
                date = datetime.datetime.now()

            result = campaign_edit(ctype, objectid, campaign, confidence, description, date, related, analyst)
            if result["success"]:
                return HttpResponse(json.dumps(result), mimetype="application/json")
            else:
                result.update({"form": form.as_table()})
                return HttpResponse(json.dumps(result), mimetype="application/json")
        else:
            return HttpResponse(json.dumps({"success": False, "form": form.as_table()}), mimetype="application/json")
    else:
        return HttpResponse(json.dumps({"success": False}), mimetype="application/json")