Skip to content
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

Upgrade dependencies #26

Open
wants to merge 9 commits into
base: notification
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions backend/langate/network/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Device(models.Model):
A device is a machine connected to the network
"""

name = models.CharField(max_length=100)
name = models.CharField(max_length=50)
mac = models.CharField(
max_length=17,
unique=True,
Expand Down Expand Up @@ -156,4 +156,7 @@ def edit_device(device: Device, mac, name, mark=None):
device.mark = mark
netcontrol.query("set_mark", { "mac": device.mac, "mark": mark })

device.save()
try:
device.save()
except Exception as e:
raise ValidationError(_("The data provided is invalid")) from e
1 change: 1 addition & 0 deletions backend/langate/network/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
path("marks/", views.MarkList.as_view(), name="mark-list"),
path("mark/<int:old>/move/<int:new>/", views.MarkMove.as_view(), name="mark-move"),
path("games/", views.GameList.as_view(), name="game-list"),
path("userdevices/<int:pk>/", views.UserDeviceDetail.as_view(), name="user-device-detail"),
]
62 changes: 62 additions & 0 deletions backend/langate/network/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,65 @@ def patch(self, request):
save_settings(SETTINGS)

return Response(SETTINGS["games"], status=status.HTTP_201_CREATED)

class UserDeviceDetail(APIView):
"""
API endpoint that allows a user to edit or delete their devices
"""
permission_classes = [permissions.IsAuthenticated]

@swagger_auto_schema(
responses={
204: "Device deleted",
403: {"error": _("You are not allowed to delete this device")},
404: {"error": _("Device not found")},
}
)
def delete(self, request, pk):
"""
Delete a device by its primary key
"""
try:
device = UserDevice.objects.get(pk=pk)

# Check if the user is the owner of the device
if device.user != request.user:
return Response({"error": _("You are not allowed to delete this device")}, status=status.HTTP_403_FORBIDDEN)

DeviceManager.delete_device(device.mac)
return Response(status=status.HTTP_204_NO_CONTENT)
except UserDevice.DoesNotExist:
return Response({"error": _("Device not found")}, status=status.HTTP_404_NOT_FOUND)

@swagger_auto_schema(
responses={
200: "Device updated",
400: {"error": _("Bad request")},
403: {"error": _("You are not allowed to edit this device")},
404: {"error": _("Device not found")},
}
)
def patch(self, request, pk):
"""
Update a device by its primary key
"""
try:
device = UserDevice.objects.get(pk=pk)

# Check if the user is the owner of the device
if device.user != request.user:
return Response({"error": _("You are not allowed to edit this device")}, status=status.HTTP_403_FORBIDDEN)

DeviceManager.edit_device(
device,
request.data.get("mac", device.mac),
request.data.get("name", device.name),
request.data.get("mark", device.mark),
)

return Response(status=status.HTTP_200_OK)

except UserDevice.DoesNotExist:
return Response({"error": _("Device not found")}, status=status.HTTP_404_NOT_FOUND)
except ValidationError as e:
return Response({"error": e.message}, status=status.HTTP_400_BAD_REQUEST)
10 changes: 5 additions & 5 deletions backend/langate/user/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def send_valid_data(data):

self.assertEqual(request.status_code, 404)
self.assertEqual(
request.data["user"][0],
request.data["error"][0],
_("Bad Username or password"),
)

Expand Down Expand Up @@ -411,7 +411,7 @@ def test_create_user_invalid_data(self):
)

self.assertEqual(request.status_code, 400)
self.assertEqual(request.data["username"][0], _("This field is required."))
self.assertEqual(request.data["error"]["username"][0], _("This field is required."))

# Missing password
request = self.client.post(
Expand All @@ -423,7 +423,7 @@ def test_create_user_invalid_data(self):
)

self.assertEqual(request.status_code, 400)
self.assertEqual(request.data["password"][0], _("This field is required."))
self.assertEqual(request.data["error"]["password"][0], _("This field is required."))

# Bad password
request = self.client.post(
Expand Down Expand Up @@ -457,7 +457,7 @@ def test_create_user_invalid_data(self):
)

self.assertEqual(request.status_code, 400)
self.assertEqual(request.data["role"][0], _('"badrole" is not a valid choice.'))
self.assertEqual(request.data["error"]["role"][0], _('"badrole" is not a valid choice.'))

def test_patch_user(self):
"""
Expand Down Expand Up @@ -552,7 +552,7 @@ def test_patch_user_invalid_data(self):
)

self.assertEqual(request.status_code, 400)
self.assertEqual(request.data["user"], [_("Password cannot be changed")])
self.assertEqual(request.data["error"], [_("Password cannot be changed")])

class UserChangePassword(TestCase):
"""
Expand Down
25 changes: 14 additions & 11 deletions backend/langate/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def patch(self, request, *args, **kwargs):
if "password" in request.data:
del request.data["password"]
return Response(
{"user": [_("Password cannot be changed")]},
{"error": [_("Password cannot be changed")]},
status=status.HTTP_400_BAD_REQUEST,
)
return self.partial_update(request, *args, **kwargs)
Expand Down Expand Up @@ -179,7 +179,7 @@ class UserLogin(APIView):
400: openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"user": openapi.Schema(
"error": openapi.Schema(
type=openapi.TYPE_ARRAY,
items=openapi.Schema(
type=openapi.TYPE_STRING,
Expand All @@ -191,7 +191,7 @@ class UserLogin(APIView):
404: openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"user": openapi.Schema(
"error": openapi.Schema(
type=openapi.TYPE_ARRAY,
items=openapi.Schema(
type=openapi.TYPE_STRING,
Expand All @@ -212,7 +212,7 @@ def post(self, request):
user = serializer.check_validity(data)
if user is None:
return Response(
{"user": [_("Bad Username or password")]},
{"error": [_("Bad Username or password")]},
status=status.HTTP_404_NOT_FOUND,
)
login(request, user)
Expand Down Expand Up @@ -267,7 +267,7 @@ def post(self, request):

return Response(status=status.HTTP_200_OK, data=user)
return Response(
{"user": [_("Invalid data format")]},
{"error": [_("Invalid data format")]},
status=status.HTTP_400_BAD_REQUEST,
)

Expand Down Expand Up @@ -383,7 +383,7 @@ def get(self, request):
400: openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"user": openapi.Schema(
"error": openapi.Schema(
type=openapi.TYPE_ARRAY,
items=openapi.Schema(
type=openapi.TYPE_STRING,
Expand All @@ -409,7 +409,10 @@ def post(self, request):
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(
{"error": serializer.errors},
status=status.HTTP_400_BAD_REQUEST
)

class ChangePassword(APIView):
"""
Expand Down Expand Up @@ -443,7 +446,7 @@ class ChangePassword(APIView):
400: openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"user": openapi.Schema(
"error": openapi.Schema(
type=openapi.TYPE_ARRAY,
items=openapi.Schema(
type=openapi.TYPE_STRING,
Expand All @@ -455,7 +458,7 @@ class ChangePassword(APIView):
404: openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"user": openapi.Schema(
"error": openapi.Schema(
type=openapi.TYPE_ARRAY,
items=openapi.Schema(
type=openapi.TYPE_STRING,
Expand All @@ -474,14 +477,14 @@ def post(self, request, pk):
user = User.objects.get(pk=pk)
except User.DoesNotExist:
return Response(
{"user": [_("User not found")]},
{"error": [_("User not found")]},
status=status.HTTP_404_NOT_FOUND,
)
if "password" in request.data:
user.set_password(request.data["password"])
user.save()
return Response(status=status.HTTP_200_OK)
return Response(
{"user": [_("Invalid data format")]},
{"error": [_("Invalid data format")]},
status=status.HTTP_400_BAD_REQUEST,
)
16 changes: 7 additions & 9 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
django==4.1.12
django==4.1.13
djangorestframework==3.14.0
markdown==3.4.1
psycopg2==2.9.5
uvicorn==0.21.1
markdown==3.7
psycopg2==2.9.9
uvicorn==0.31.1
gunicorn==20.1.0
django-cors-headers==3.14
tzdata==2023.3
pymongo==3.12.3
djongo==1.3.6
requests==2.31.0
django-cors-headers==4.5.0
tzdata==2024.2
requests==2.32.3
drf-yasg==1.21.7
Loading
Loading