Repository: DjangoGirls/djangogirls

Issue:
Event.MultipleObjectsReturned: get() returned more than one Event -- it returned 2!
Sentry Issue: [DJANGO-GIRLS-WEBSITE-4V](https://sentry.io/organizations/django-girls/issues/3800729247/?referrer=github_integration)

```
Event.MultipleObjectsReturned: get() returned more than one Event -- it returned 2!
(1 additional frame(s) were not displayed)
...
  File "django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "applications/views.py", line 21, in apply
    event = get_event(page_url, request.user.is_authenticated, False)
  File "core/utils.py", line 31, in get_event
    event = Event.objects.get(page_url=page_url)
  File "django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "django/db/models/query.py", line 439, in get
    raise self.model.MultipleObjectsReturned(
```


Discussion:
I'd love to understand a bit more the context of this issue. It seems this is caused by multiple `Event` instances with the same `page_url`, but I'm not sure why we wouldn't have unique URLs in the first place. Is it because there have been multiple editions in the same city (hence the same slug) and those past editions still exist in the database?
@marksweb Would you have context of this one?
@vanessa Sure, it seems to be exclusively triggered by the googlebot

The URL it's hitting is https://djangogirls.org/en/cagayandeoro/apply/

And the code triggering the error is here; https://github.com/DjangoGirls/djangogirls/blob/main/core/utils.py#L31

We've seen things like this before, and I've made changes to the code around this area; https://github.com/DjangoGirls/djangogirls/commit/496c5f334990d5cc3254045e71a9b4a0644fcfa6

There are parts of the system, like with the `Event` model where the application will do a `get()` on data that isn't unique and it doesn't necessarily handle multiple objects returned - mostly I think, to highlight where there is an admin fix required. Perhaps we need validation to check if a `page_url` value is in use already. In terms of the multiple objects issue - I don't think that should return a valid page to the user. But I think it should send a chosen error to the user (rather than a 500 page) and log the error to sentry to highlight the issue.

https://sentry.io/share/issue/ae412b36dcd04a7abc63f3976c011758/
@vanessa when a new event for a city that has hosted before is created the old one should be be given a number so no two events have the same `page_url`. This only works when the organizers are consistent in naming their city. This city had 3 events with the city name `Cagayan de Oro` so their `page_url`s were `cagayandeoro1`, `cagayandeoro2` and `cagayandeoro`. When the application for the 4th event was made, the city was now `Cagayan de Oro City` which becomes a new event altogether and therefore the event `page_url` is not archived properly so both 3 and 4 had the same `page_url` `cagayandeoro`. 

The organizers created the data error which result in these server errors.

I have sorted this now, however, if this is to be fixed, the solution would be to look at the code for archiving old events and creating new events and `page_url`s.

Closing these issues now. If someone wants to work on a fix, I can create a new error that references these two errors.

Resolving patch:
diff --git a/core/utils.py b/core/utils.py
--- a/core/utils.py
+++ b/core/utils.py
@@ -27,6 +27,8 @@ def get_event(page_url, is_user_authenticated, is_preview):
         event = Event.objects.get(page_url=page_url)
     except Event.DoesNotExist:
         return None
+    except Event.MultipleObjectsReturned:
+        event = Event.objects.filter(page_url=page_url).first()
 
     if not (is_user_authenticated or is_preview) and not event.is_page_live:
         past = event.date <= now_approx


Test patch: