<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_state_view(self, request):
 """ Changes the experiment state """

        if not request.user.has_perm('experiments.change_experiment'):
            return HttpResponseForbidden()

        try:
            state = int(request.POST.get("state", ""))
        except ValueError:
            return HttpResponseBadRequest()

        try:
            experiment = Experiment.objects.get(name=request.POST.get("experiment"))
        except Experiment.DoesNotExist:
            return HttpResponseBadRequest()

        experiment.state = state

        if state == 0:
            experiment.end_date = timezone.now()
        else:
            experiment.end_date = None

        experiment.save()

        return HttpResponse()