0
def artist(request):
    print("START")
    if request.method == "GET":
        try:
            artist = Artist.objects.get(user=request.user)
            print("artist exists")
        except Artist.DoesNotExist:
            print("artist doesn't exist")
            artist = Artist.objects.get_or_create(user=request.user)[0]
            print("a new artist object has been created")
        
        return HttpResponse(status=200)

Console:

START
artist doesn't exist
START
artist doesn't exist
a new artist object has been created
[15/Dec/2022 12:40:23] "GET /artist/ HTTP/1.1" 200 0
a new artist object has been created
[15/Dec/2022 12:40:23] "GET /artist/ HTTP/1.1" 200 0

on the client side, there is a button on the screen that sends a request that returns to the user their artist page, or if they don't have one, it will be created for them. And suppose he double clicks the button, then two objects will be created. The user field in the Artist is not unique, we ignore the fact that we can solve the problem on the client side with javascript and we assume that the server is a bit slow, so it doesn't have time to process two requests in such a short time.

The Artist model has a field that at the time of creation must be populated with some information based on their profile picture, i.e. i extract the dominant color from their picture to use it later on the page, it's irrelevant, we just assume it takes a bit longer to create

I also want to create the object without a form, I will use a form later to update the profile if the user wants. Also ignore creating the Artist object once the user is created ( using the signal thing)

Giovanni5454
  • 33
  • 1
  • 6
  • you can mitigate from the frontend https://stackoverflow.com/questions/16715075/preventing-multiple-clicks-on-button – Linh Nguyen Dec 15 '22 at 11:09
  • @LinhNguyen I wrote in the question that we ignore the frontend solution – Giovanni5454 Dec 15 '22 at 11:11
  • you can set a field in the model to unique to avoid adding multiple of the same data – Linh Nguyen Dec 15 '22 at 11:14
  • @LinhNguyen I don't want it to be unique, maybe a user can have multiple artist profiles :)) – Giovanni5454 Dec 15 '22 at 11:15
  • 1
    If you want to secure endpoint from multiple requests being sent by accident by some user in short span of time, you can user rate limiters – PTomasz Dec 15 '22 at 11:17
  • @TrueGopnik Yes, it's a good idea. I'm still bothered by the fact that checking if the field/object exists and the insertion process takes place at such a long interval, i.e. you should check if the object exists, wait for an insertion, and after that check from elsewhere if the object exists. I don't know exactly how it should work, but the time interval between check and insert should be a break interval for other checks or inserts in the Albums table (I know there could be other problems if things go like that). – Giovanni5454 Dec 15 '22 at 11:29
  • create a user in get request is not a good idea – Jisson Dec 15 '22 at 12:22

0 Answers0