Repository: svthalia/concrexit

Issue:
ValueError: day is out of range for month
In GitLab by _thaliatechnicie on Mar 4, 2020, 19:20

Sentry Issue: [CONCREXIT-24](https://sentry.io/organizations/thalia/issues/1538288408/?referrer=gitlab_integration)

```
ValueError: day is out of range for month
(11 additional frame(s) were not displayed)
...
  File "rest_framework/serializers.py", line 260, in data
    self._data = self.to_representation(self.instance)
  File "rest_framework/serializers.py", line 529, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "rest_framework/fields.py", line 1905, in to_representation
    return method(value)
  File "members/api/serializers.py", line 93, in _achievements
    return member_achievements(instance.user)
  File "members/services.py", line 72, in member_achievements
    earliest = earliest.replace(year=earliest.year + mentor_year.year)
```


Discussion:
In GitLab by _thaliatechnicie on Mar 4, 2020, 19:21

Sentry issue: [CONCREXIT-25](https://sentry.io/organizations/thalia/issues/1538863915/?referrer=gitlab_integration)
In GitLab by @se-bastiaan on Mar 4, 2020, 19:22

```
ValueError: day is out of range for month
  File "django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "django/views/generic/detail.py", line 107, in get
    context = self.get_context_data(object=self.object)
  File "members/views.py", line 180, in get_context_data
    achievements = member_achievements(member)
  File "members/services.py", line 72, in member_achievements
    earliest = earliest.replace(year=earliest.year + mentor_year.year)
```
In GitLab by @mariesophiesimon on Mar 4, 2020, 19:24

assigned to @mariesophiesimon
In GitLab by @mariesophiesimon on Mar 4, 2020, 19:24

created branch [`1025-valueerror-day-is-out-of-range-for-month`](/thalia/concrexit/compare/master...1025-valueerror-day-is-out-of-range-for-month) to address this issue (https://gitlab.science.ru.nl/thalia/concrexit/-/merge_requests/1518)
Hint: leap days

Resolving patch:
diff --git a/website/members/services.py b/website/members/services.py
--- a/website/members/services.py
+++ b/website/members/services.py
@@ -79,6 +79,9 @@ def member_achievements(member) -> List:
         # Ensure mentorships appear last but are sorted
         earliest = date.today()
         earliest = earliest.replace(year=earliest.year + mentor_year.year)
+        # Making sure it does not crash in leap years
+        if earliest.month == 2 and earliest.day == 29:
+            earliest = earliest.replace(day=28)
         if not achievements.get(name):
             achievements[name] = {
                 "name": name,


Test patch: