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 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")