Skip to content

Commit

Permalink
Merge pull request #1341 from BLSQ/feat/IA-3004_configure_app_via_qrcode
Browse files Browse the repository at this point in the history
IA-3004: Display a QRCode in the project configuration popup
  • Loading branch information
bmonjoie authored May 31, 2024
2 parents abca918 + ae8e316 commit 8423281
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Form = {
};

type ProjectForm = {
id?: Form;
app_id: Form;
name: Form;
};
Expand Down Expand Up @@ -43,6 +44,16 @@ const ProjectInfos: FunctionComponent<Props> = ({
label={MESSAGES.appId}
required
/>
{currentProject.id?.value && (
<div style={{ textAlign: 'center' }}>
<img
width={200}
height={200}
alt="QRCode"
src={`/api/projects/${currentProject.id?.value}/qr_code/`}
/>
</div>
)}
</>
);

Expand Down
20 changes: 19 additions & 1 deletion iaso/api/projects/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from rest_framework import filters, permissions
from django.http import HttpResponse
from qr_code.qrcode.maker import make_qr_code_image
from qr_code.qrcode.utils import QRCodeOptions
from rest_framework import filters, permissions, status
from rest_framework.decorators import action


from iaso.models import Project
from .serializers import ProjectSerializer
Expand All @@ -25,3 +30,16 @@ def get_queryset(self):
"""Always filter the base queryset by account"""

return Project.objects.filter(account=self.request.user.iaso_profile.account)

@action(detail=True, methods=["get"])
def qr_code(self, request, *args, **kwargs):
"""Returns the qrcode image to configure the mobile application."""
project = self.get_object()
return HttpResponse(
status=status.HTTP_200_OK,
content_type="image/png",
content=make_qr_code_image(
data='{"url": "' + request.build_absolute_uri("/") + '", "app_id": "' + project.app_id + '"}',
qr_code_options=QRCodeOptions(size="S", image_format="png", error_correction="L"),
),
)
18 changes: 18 additions & 0 deletions iaso/tests/api/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ def test_projects_delete(self):
response = self.client.delete(f"/api/projects/{self.project_1.id}/", format="json")
self.assertJSONResponse(response, 405)

def test_qr_code_unauthenticated(self):
"""GET /projects/<project_id>/qr_code/: return the proper QR code"""
response = self.client.get(f"/api/projects/{self.project_1.id}/qr_code/")
self.assertEqual(401, response.status_code)

def test_qr_code(self):
"""GET /projects/<project_id>/qr_code/: return the proper QR code"""
self.client.force_authenticate(self.jane)
response = self.client.get(f"/api/projects/{self.project_1.id}/qr_code/")
self.assertEqual(200, response.status_code)
self.assertEqual("image/png", response["Content-Type"])

def test_qr_code_not_found(self):
"""GET /projects/<project_id>/qr_code/: return 404"""
self.client.force_authenticate(self.jane)
response = self.client.get(f"/api/projects/WRONG/qr_code/")
self.assertEqual(404, response.status_code)

def assertValidProjectListData(self, list_data: typing.Mapping, expected_length: int, paginated: bool = False):
self.assertValidListData(
list_data=list_data, expected_length=expected_length, results_key="projects", paginated=paginated
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ django-sql-dashboard==1.2 # https://github.com/simonw/django-sql-dashboard/tags
django-storages==1.14.2 # https://github.com/jschneier/django-storages/tags
django-translated-fields==0.12.0 # https://github.com/matthiask/django-translated-fields/tags
django-phonenumber-field[phonenumberslite]==7.3.0 # https://django-phonenumber-field.readthedocs.io/en/latest/
django-qr-code==4.0.1 # https://django-qr-code.readthedocs.io/en/latest/

# Django REST Framework.
# ------------------------------------------------------------------------------
Expand Down

0 comments on commit 8423281

Please sign in to comment.