def post(self, request, format=None): serializer = AlarmClockSerializer(data=request.data) if serializer.is_valid(): serializer.save() # create a job line last_alarmclock = AlarmClock.objects.latest('id') Utils.add_job_in_crontab(last_alarmclock) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): alarmclock = self.get_object(pk) serializer = AlarmClockSerializer(alarmclock, data=request.data) if serializer.is_valid(): serializer.save() updated_alarmclock = AlarmClock.objects.get(pk=pk) # remove the job from the crontab job_comment = "piclodio%s" % updated_alarmclock.id CrontabManager.remove_job(job_comment) # add it back with new info Utils.add_job_in_crontab(updated_alarmclock) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)