def post(self, request, **kwargs):
        """
        Creates a new device or updates an existing one to `is_active=True`.
        Expects two non-options POST parameters: `token` and `service`.
        """
        devices = Device.objects.filter(token=request.POST.get('token'),
                                        service__id=int(request.POST.get('service', 0)))
        if devices.exists():
            device = devices.get()
            device.is_active = True

            #conditionally set other data
            device.platform = request.POST.get("platform", None)
            device.display = request.POST.get("display", None)
            device.os_version = request.POST.get("os_version", None)
            
            device.save()
            return JSONResponse(device)
        form = DeviceForm(request.POST)
        if form.is_valid():
            device = form.save(commit=False)
            device.is_active = True

            #conditionally set other data
            device.platform = request.POST.get("platform", None)
            device.display = request.POST.get("display", None)
            device.os_version = request.POST.get("os_version", None)

            device.save()
            return JSONResponse(device, status=201)
        return JSONResponse(form.errors, status=400)
Пример #2
0
 def post(self, request, **kwargs):
     """
     Creates a new device or updates an existing one to `is_active=True`.
     Expects two non-options POST parameters: `token` and `service`.
     """
     devices = Device.objects.filter(token=request.POST.get('token'),
                                     service__id=int(
                                         request.POST.get('service', 0)))
     if devices.exists():
         device = devices.get()
         device.is_active = True
         device.save()
         return JSONResponse(device)
     form = DeviceForm(request.POST)
     if form.is_valid():
         device = form.save(commit=False)
         device.is_active = True
         device.save()
         return JSONResponse(device, status=201)
     return JSONResponse(form.errors, status=400)
Пример #3
0
 def post(self, request, **kwargs):
     """
     Creates a new device or updates an existing one to `is_active=True`.
     Expects two non-options POST parameters: `token` and `service`.
     """
     data = simplejson.loads(request.raw_post_data)
     
     devices = Device.objects.filter(token=data.get('token'),
                                     service__id=int(data.get('service', 0)))
     if devices.exists():
         device = devices.get()
         device.is_active = True
         device.save()
         return JSONResponse(device)
     form = DeviceForm(data)
     if form.is_valid():
         device = form.save(commit=False)
         device.is_active = True
         device.save()
         if request.user:
             device.users.add(request.user)
         device.save()
         return JSONResponse(device, status=201)
     return JSONResponse(form.errors, status=400)