-
Notifications
You must be signed in to change notification settings - Fork 18
Add REST endpoints for Activity (create/edit/delete) #196 #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8f9c0d6
3b0320f
1df1dae
d6509a5
e130b12
40e2230
7d851b5
d1e5db2
c70b978
981266b
c959168
c1e2506
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from rest_framework import serializers | ||
| from .models import Activity | ||
|
|
||
|
|
||
| class ActivitySerializer(serializers.ModelSerializer): | ||
| """Serializer for the Activities""" | ||
|
|
||
| class Meta: | ||
| model = Activity | ||
| fields = '__all__' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,73 @@ | |
|
|
||
| from .forms import ActivityModelForm | ||
| from .models import Activity | ||
| from .serializers import ActivitySerializer | ||
|
|
||
| from rest_framework.decorators import api_view | ||
| from rest_framework.response import Response | ||
| from rest_framework import status | ||
|
|
||
|
|
||
| @api_view(['GET', 'POST']) | ||
| def activity_list(request): | ||
| """ | ||
| Circle member(s) can create a Circle Activity via a POST request | ||
| Circle members and coordinators can view all Circle Activities via a GET request | ||
| """ | ||
| circle_id = Activity.objects.values_list("circle_id", flat=True).first() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Activities are logically scoped to a circle, let's pass in the
|
||
| circle = Circle.objects.get(id=circle_id) | ||
|
|
||
| if request.user in circle.companions: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using class-based views, we can use |
||
|
|
||
| if request.method == 'GET': | ||
| activities = Activity.objects.all() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would seem to be a heavy database query, since it could get a cursor to every activity for every circle. The activities should be scoped to the circle. |
||
| serializer = ActivitySerializer(activities, many=True) | ||
| return Response(serializer.data) | ||
|
|
||
| if request.method == 'POST': | ||
| serializer = ActivitySerializer(data=request.data) | ||
| if serializer.is_valid(): | ||
| serializer.save() | ||
| return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
|
||
| return Response(status=status.HTTP_403_FORBIDDEN) | ||
|
|
||
|
|
||
| @api_view(['GET', 'PUT', 'DELETE']) | ||
| def activity_details(request, id): | ||
| """ | ||
| Circle member(s) can update a single Circle Activity via a PUT request | ||
| Circle coordinator(s) can delete a single Circle Activity via a DELETE request | ||
| Circle members and coordinators can view a single Circle Activity via a GET request | ||
| """ | ||
| try: | ||
| activity = Activity.objects.get(pk=id) | ||
| except Activity.DoesNotExist: | ||
| return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| circle_id = Activity.objects.values_list("circle_id", flat=True).get(pk=id) | ||
| circle = Circle.objects.get(id=circle_id) | ||
|
|
||
| if request.user in circle.companions: | ||
|
|
||
| if request.method == 'GET': | ||
| serializer = ActivitySerializer(activity) | ||
| return Response(serializer.data) | ||
|
|
||
| if request.method == 'PUT': | ||
| serializer = ActivitySerializer(activity, data=request.data) | ||
| if serializer.is_valid(): | ||
| serializer.save() | ||
| return Response(serializer.data) | ||
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| if request.method == 'DELETE': | ||
| if request.user in circle.organizers: | ||
| activity.delete() | ||
| return Response(status=status.HTTP_204_NO_CONTENT) | ||
| return Response(status=status.HTTP_403_FORBIDDEN) | ||
|
|
||
| return Response(status=status.HTTP_403_FORBIDDEN) | ||
|
|
||
|
|
||
| class ActivityCreateView(UserPassesTestMixin, LoginRequiredMixin, View): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For cleaner code, let's use class-based API views.
https://www.django-rest-framework.org/tutorial/3-class-based-views/