def target_details(request, email_address=None): """ Target modification form generation. :param request: Django request. :type request: :class:`django.http.HttpRequest` :param email_address: The email address of the Target to get details for.. :type email_address: str :returns: :class:`django.http.HttpResponse` """ if email_address is None: form = TargetInfoForm() else: target = class_from_value('Target', email_address) if not target: form = TargetInfoForm(initial={'email_address': email_address}) else: form = TargetInfoForm(initial=target.to_dict()) return render_to_response('target_form.html', {'form': form}, RequestContext(request))
def add_update_target(request): """ Add/update a Target. Should be an AJAX POST. :param request: Django request. :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.method == "POST": email = request.POST['email_address'] new_email = email.strip().lower() form = TargetInfoForm(request.POST) analyst = request.user.username if form.is_valid(): data = form.cleaned_data results = upsert_target(data, analyst) if results['success']: message = '<div>Click here to view the new target: <a href=' message += '"%s">%s</a></div>' % (reverse( 'crits.targets.views.target_info', args=[new_email ]), new_email) result = {'message': message} else: result = results result['form'] = form.as_table() else: result = {'message': ['<div>Form is invalid!</div>']} result['form'] = form.as_table() if request.is_ajax(): return HttpResponse(json.dumps(result), content_type="application/json") else: return HttpResponseRedirect( reverse('crits.targets.views.target_info', args=[email])) else: return render_to_response("error.html", {"error": "Expected AJAX POST"}, RequestContext(request))
def add_update_target(request): """ Add/update a Target. Should be an AJAX POST. :param request: Django request. :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.method == "POST": email = request.POST['email_address'] new_email = email.strip().lower() form = TargetInfoForm(request.POST) analyst = request.user.username if form.is_valid(): data = form.cleaned_data results = upsert_target(data, analyst) if results['success']: message = '<div>Click here to view the new target: <a href=' message += '"%s">%s</a></div>' % (reverse('crits.targets.views.target_info', args=[new_email]), new_email) result = {'message': message} else: result = results result['form'] = form.as_table() else: result = {'message': ['<div>Form is invalid!</div>']} result['form'] = form.as_table() if request.is_ajax(): return HttpResponse(json.dumps(result), content_type="application/json") else: return HttpResponseRedirect(reverse('crits.targets.views.target_info', args=[email])) else: return render_to_response("error.html", {"error" : "Expected AJAX POST" }, RequestContext(request))
def get_target_details(email_address, analyst): """ Generate the data to render the Target details template. :param email_address: The email address of the target. :type email_address: str :param analyst: The user requesting this information. :type analyst: str :returns: template (str), arguments (dict) """ template = None if not email_address: template = "error.html" args = {'error': "Must provide an email address."} return template, args # check for exact match first target = Target.objects(email_address=email_address).first() if not target: # if no exact match, look for case-insensitive match target = Target.objects(email_address__iexact=email_address).first() if not target: target = Target() target.email_address = email_address.strip().lower() form = TargetInfoForm(initial={'email_address': email_address}) email_list = target.find_emails(analyst) form = TargetInfoForm(initial=target.to_dict()) if form.fields.get(form_consts.Common.BUCKET_LIST_VARIABLE_NAME) != None: form.fields.pop(form_consts.Common.BUCKET_LIST_VARIABLE_NAME) if form.fields.get(form_consts.Common.TICKET_VARIABLE_NAME) != None: form.fields.pop(form_consts.Common.TICKET_VARIABLE_NAME) subscription = { 'type': 'Target', 'id': target.id, 'subscribed': is_user_subscribed("%s" % analyst, 'Target', target.id) } #objects objects = target.sort_objects() #relationships relationships = target.sort_relationships("%s" % analyst, meta=True) # relationship relationship = {'type': 'Target', 'value': target.id} #comments if target.id: comments = { 'comments': target.get_comments(), 'url_key': email_address } else: comments = {'comments': [], 'url_key': email_address} #screenshots screenshots = target.get_screenshots(analyst) # favorites favorite = is_user_favorite("%s" % analyst, 'Target', target.id) # analysis results service_results = target.get_analysis_results() args = { 'objects': objects, 'relationships': relationships, 'relationship': relationship, 'comments': comments, 'favorite': favorite, 'subscription': subscription, 'screenshots': screenshots, 'email_list': email_list, 'target_detail': target, 'service_results': service_results, 'form': form } return template, args