Пример #1
0
  def post(self, request):
    #################
    # Setup
    #################

    headers = {
      "Content-Type": "application/json",
      "Allow": "GET, POST",
    }

    #################
    # Validation
    #################

    try:
      account_id = int(request.session["_auth_user_id"])
    except KeyError:
      return Response(status=status.HTTP_401_UNAUTHORIZED)

    try:
      account = Account.objects.get(user_id=account_id)
    except Account.DoesNotExist:
      errors = {"account_id": "Invalid account ID."}
      return Response(content=errors, headers=headers, status=status.HTTP_404_NOT_FOUND)

    # Check content-type header
    if not self.content_type.startswith('application/json'):
      errors = {"header_content_type": "Content-Type must be 'application/json'. Your Content-Type is " + str(self.content_type)}
      return Response(content=errors, headers=headers, status=status.HTTP_400_BAD_REQUEST)

    #################
    # Operation
    #################
    
    try:
      project_id = int(self.CONTENT["project_id"])
    except KeyError:
      errors = {"project_id": "Missing project ID."}
      return Response(content=errors, headers=headers, status=status.HTTP_404_NOT_FOUND)
    
    # Must be project owner to create a permission
    try:
      project = Project.objects.get(id=project_id, account=account)
    except Project.DoesNotExist:
      errors = {"project_id": "Invalid project ID."}
      return Response(content=errors, headers=headers, status=status.HTTP_404_NOT_FOUND)

    try:
      email = str(self.CONTENT['email']).lower().strip()
    except KeyError:
      errors = {"email": "Missing email address."}
      return Response(content=errors, headers=headers, status=status.HTTP_400_BAD_REQUEST)
    
    try:
      p_account = Account.objects.get(email=email)
    except Account.DoesNotExist:
      p_account = Account.create_invitation_account(email)
      if isinstance(p_account, dict):
        return Response(content=p_account, headers=headers, status=status.HTTP_400_BAD_REQUEST)
    else:
      try:
        Permission.objects.get(project=project, account=p_account)
      except Permission.DoesNotExist:
        pass
      else:
        # Send a 30X response instead for PUT to correct endpoint?
        errors = {"email": "This email address already has a permission."}
        return Response(content=errors, headers=headers, status=status.HTTP_400_BAD_REQUEST)

    try:
      permission = str(self.CONTENT['permission'])
    except KeyError:
      permission = 'client'
    else:
      if permission not in ['client', 'coworker']:
        permission = 'client'
    
    perm = Permission.create_record(account, project, p_account, self.CONTENT)
    if not isinstance(perm, Permission):
      # HTTP status 422: Unprocessable Entity (WebDAV; RFC 4918)
      return Response(content=perm, headers=headers, status=422)
    
    return Response(content=perm.record_to_dictionary(), headers=headers, status=status.HTTP_200_OK)