Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

[enhancement] Canvas server #11

Merged
merged 2 commits into from
Nov 20, 2021
Merged

[enhancement] Canvas server #11

merged 2 commits into from
Nov 20, 2021

Conversation

VallariAg
Copy link
Contributor

@VallariAg VallariAg commented Nov 19, 2021

Solves #3

GET /api/v1/image/ -> return all images of the signed in user
image

POST /api/v1/image/ -> add a new image
image

GET /api/v1/image/<canvas_id> -> gets the image by id
image

POST /api/v1/image/<canvas_id> -> updates the image location
image

Copy link
Owner

@atb00ker atb00ker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work, Vallari.
Do as many of these changes you find time for today and create an issue for the rest to do them when you find time in the future! 😄
Let me know when you are done fixing / making issues, I'll merge it.

To fix the commit problem, I did:

git fetch origin/main
git rebase origin main

@@ -0,0 +1,24 @@
# Generated by Django 3.2.9 on 2021-11-18 16:59

from django.db import migrations, models
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually (not mandatory), we give descriptive names to files instead of 0002_auto_20211118_1659.py.
It's also nice to summarize the work of one PR in one migration file only. (Unless multiple make sense!) to keep the number of migration files low.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I've created an issue for this. Let me know if I do merging and renaming of these migration files right.

class Canvas(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
image_path = models.CharField(max_length=128)
username = models.ForeignKey(User, related_name='canvas', on_delete=models.CASCADE)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we would need a drawing column here as well, do store the image drawings. 😄

Copy link
Contributor Author

@VallariAg VallariAg Nov 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! It created another migration file. I'll merge all three of them in one.

fields = (
'id',
'image_path',
'username',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need username in our response.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👍

Comment on lines +22 to +25
def get(self, request):
username = request.user
all_canvas = Canvas.objects.filter(username=username)
serializer = CanvasSerializer(all_canvas, many=True)
return Response(serializer.data)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to do this in less lines?
Checkout List API View: https://www.django-rest-framework.org/api-guide/generic-views/#listapiview

(You can check it out in the future, right now this will work.)

Copy link
Contributor Author

@VallariAg VallariAg Nov 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! 🎉
They will probably handle any error conditions I missed like not passing correct data in body..
I'll change all these to generic-views later 👍

Comment on lines 28 to 43
def post(self, request):
img_path = request.data["image_path"]
canvas_id = uuid.uuid4()
new_canvas = {
'image_path': img_path,
'id': canvas_id,
'username': str(request.user)
}
serializer = CanvasSerializer(data=new_canvas)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

canvas = Canvas.objects.create(username=request.user, image_path=img_path, id=canvas_id)
canvas.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout CreateAPIView: https://www.django-rest-framework.org/api-guide/generic-views/#createapiview

(You can check it out in the future, right now this will work.)

Comment on lines +45 to +71
class CanvasWithIDView(APIView):
permission_classes = (IsAuthenticated,)
authentication_classes = (TokenAuthentication,)


def get(self, request, *args, **kwargs):
canvas_id = self.kwargs['canvas_id']
all_canvas = Canvas.objects.filter(id=canvas_id)
serializer = CanvasSerializer(all_canvas, many=True)
return Response(serializer.data)

def post(self, request, *args, **kwargs):
img_path = request.data["image_path"]
canvas_id = self.kwargs['canvas_id']
new_canvas = {
'image_path': img_path,
'id': canvas_id,
'username': str(request.user)
}
serializer = CanvasSerializer(data=new_canvas)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

canvas = Canvas.objects.filter(id=canvas_id)
canvas.update(image_path=img_path)
return Response(serializer.data, status=status.HTTP_201_CREATED)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout RetrieveUpdateAPIView: https://www.django-rest-framework.org/api-guide/generic-views/#retrieveupdateapiview

(You can check it out in the future, right now this will work.)

@@ -19,6 +19,6 @@

urlpatterns = [
path("api/v1/", include("server.users.urls")),
path("api/v1/", include("server.face_paint.urls")),
path("api/v1/image/", include("server.canvas.urls")),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, more specific path comes first.
Imagine if someone creates image/ path in server.users.urls mistakenly.
Wouldn't all our request get to that module instead of this one?
(Because django matches URL patterns in order, first come, first serve!)

    path("api/v1/image/", include("server.canvas.urls")),
    path("api/v1/", include("server.users.urls")),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I understand! Fixed it :)

@VallariAg VallariAg linked an issue Nov 20, 2021 that may be closed by this pull request
@atb00ker atb00ker merged commit 67f2d56 into main Nov 20, 2021
@atb00ker atb00ker deleted the canvas-server branch November 20, 2021 19:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Canvas Server
2 participants