def internal_share(self, user, email, type, id):
receiving_user = None
# Attempt to get the object instance we want to share
try:
apps.get_model(self.AVAILABLE_TYPES[type]).objects.get(id=id)
except ObjectDoesNotExist:
# If it doesn't exist, respond with a 404
return Response({"message": f"Object with id {id} does not exist"}, status=status.HTTP_404_NOT_FOUND)
# Attempt to get the receiving user by e-mail address
try:
receiving_user = UserProfile.objects.get(user__email=email)
except ObjectDoesNotExist:
return Response({"message": f"User with email {email} does not exist"}, status=status.HTTP_404_NOT_FOUND)
# Create our sharable object using the source user, receiving user, id, and model
# This will auto-populate in the receiving user's received shares on their profile
Sharable.objects.create(sharing_user=user.profile,
receiving_user=receiving_user,
sharable_id=id,
sharable_model=self.AVAILABLE_TYPES[type])
return Response({"message": f"Position shared internally to user with email {email}"}, status=status.HTTP_202_ACCEPTED)
评论列表
文章目录