コード例 #1
0
ファイル: views.py プロジェクト: eshaverma/meriawaaz
def incidents_handler(request):
  if request.method == 'GET':
    incidents = Incident.objects.all()
    mod_incidents = []
    for item in incidents:
      mod_incidents.append(item.to_dict())
    return HttpResponse(json.dumps(mod_incidents), content_type="application/json")
  
  elif request.method == 'POST':
    data = json.loads(request.body)
    i_manager = IncidentManager()
    incident = i_manager.load_from_dict(data)
    incident.save()
    return HttpResponse(json.dumps(incident.to_dict()), content_type="application/json")
  
  else:
    return HttpResponseBadRequest('This method is not supported.')
コード例 #2
0
ファイル: views.py プロジェクト: eshaverma/meriawaaz
def incident_handler(request, incident_id):
  try:
    incident = Incident.objects.get(uid=incident_id)
    if request.method == 'GET':
      json_data = json.dumps(incident.to_dict())
      return HttpResponse(json_data, content_type="application/json")
    
    elif request.method == 'PUT' or request.method == 'POST':
      data = json.loads(request.body)
      i_manager = IncidentManager()
      incident = i_manager.load_from_dict(data, incident)
      incident.save()
      return HttpResponse(json.dumps(incident.to_dict()), content_type="application/json")
    
    else:
      return HttpResponseBadRequest()
  
  except Incident.DoesNotExist:
    return HttpResponse("", content_type="application/json")