Repository: svthalia/concrexit

Issue:
Fix adding avatar through api v2
### Describe the bug
In api v1 the avatar can be set through `api/v1/members/me` with a multipart patch request with a file labelled `photo`. Api v2 should also allow this, but instead return 500.

### How to reproduce
Send a request to patch the photo to api v1 and see that it works.
Send the same request to api v2 and see the 500 response.

Note that I have not tried editing anything else through api v2 yet, so it might be that some other fields also don't work.



Discussion:
```
Traceback (most recent call last):
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/sentry_sdk/integrations/django/views.py", line 67, in sen>
    return callback(request, *args, **kwargs)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exc>
    raise exc
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/generics.py", line 229, in patch
    return self.partial_update(request, *args, **kwargs)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/mixins.py", line 82, in partial_update
    return self.update(request, *args, **kwargs)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/mixins.py", line 68, in update
    self.perform_update(serializer)
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/mixins.py", line 78, in perform_update
    serializer.save()
  File "/nix/store/hhyp4b0lhz0yxz9g640zlrxn8dsikpnp-python3-3.8.8-env/lib/python3.8/site-packages/rest_framework/serializers.py", line 200, in save
    self.instance = self.update(self.instance, validated_data)
  File "./members/api/v2/serializers/member.py", line 55, in update
    profile_data = validated_data.pop("profile")
KeyError: 'profile'
```

Resolving patch:
diff --git a/website/members/api/v2/serializers/member.py b/website/members/api/v2/serializers/member.py
--- a/website/members/api/v2/serializers/member.py
+++ b/website/members/api/v2/serializers/member.py
@@ -1,4 +1,5 @@
 from rest_framework import serializers
+from rest_framework.exceptions import ValidationError
 
 from members.api.v2.serializers.profile import ProfileSerializer
 from members.models import Member
@@ -52,6 +53,9 @@ def _membership_type(self, instance):
         return None
 
     def update(self, instance, validated_data):
+        if "profile" not in validated_data:
+            raise ValidationError("profile field is missing")
+
         profile_data = validated_data.pop("profile")
         instance.profile = self.fields["profile"].update(
             instance=instance.profile, validated_data=profile_data


Test patch: