'Create a Deploy
Create a deploy for a given release.
:pparam string organization_slug: the organization short name
:pparam string version: the version identifier of the release.
:param string environment: the environment you\'re deploying to
:param string name: the optional name of the deploy
:param url url: the optional url that points to the deploy
:param datetime dateStarted: an optional date that indicates when
the deploy started
:param datetime dateFinished: an optional date that indicates when
the deploy ended. If not provided, the
current time is used.'

def post(self, request, organization, version):

    try:
        release = Release.objects.get(version=version, organization=organization)
    except Release.DoesNotExist:
        raise ResourceDoesNotExist
    if (not self.has_release_permission(request, organization, release)):
        raise PermissionDenied
    serializer = DeploySerializer(data=request.DATA)
    if serializer.is_valid():
        projects = list(release.projects.all())
        result = serializer.object
        env = Environment.objects.get_or_create(name=result['environment'], organization_id=organization.id)[0]
        for project in projects:
            env.add_project(project)
        deploy = Deploy.objects.create(organization_id=organization.id, release=release, environment_id=env.id, date_finished=result.get('dateFinished', timezone.now()), date_started=result.get('dateStarted'), name=result.get('name'), url=result.get('url'))
        Release.objects.filter(id=release.id).update(total_deploys=(F('total_deploys') + 1), last_deploy_id=deploy.id)
        Deploy.notify_if_ready(deploy.id)
        return Response(serialize(deploy, request.user), status=201)
    return Response(serializer.errors, status=400)
