Skip to content

Commit

Permalink
format: apply linters
Browse files Browse the repository at this point in the history
  • Loading branch information
tuneerroy committed Sep 28, 2023
1 parent f295bd4 commit 15bba49
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 54 deletions.
35 changes: 6 additions & 29 deletions backend/laundry/urls.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
from django.urls import path
from django.views.decorators.cache import cache_page
from backend.utils.cache import MINUTE_IN_SECONDS, MONTH_IN_SECONDS_APPROX

from laundry.views import (
HallInfo,
HallUsage,
Ids,
MultipleHallInfo,
Preferences,
Status,
)
from laundry.views import HallInfo, HallUsage, Ids, MultipleHallInfo, Preferences, Status
from utils.cache import MINUTE_IN_SECONDS, MONTH_IN_SECONDS_APPROX


urlpatterns = [
path(
"hall/<hall_id>/",
cache_page(MINUTE_IN_SECONDS)(HallInfo),
name="hall-info",
),
path(
"usage/<hall_id>/",
cache_page(MINUTE_IN_SECONDS)(HallUsage),
name="hall-usage",
),
path("hall/<hall_id>/", cache_page(MINUTE_IN_SECONDS)(HallInfo), name="hall-info",),
path("usage/<hall_id>/", cache_page(MINUTE_IN_SECONDS)(HallUsage), name="hall-usage",),
path(
"rooms/<hall_ids>",
cache_page(MINUTE_IN_SECONDS)(MultipleHallInfo),
name="multiple-hall-info",
),
path(
"halls/ids/",
cache_page(MONTH_IN_SECONDS_APPROX)(Ids),
name="hall-ids",
),
path(
"status/",
cache_page(30)(Status),
name="status",
),
path("halls/ids/", cache_page(MONTH_IN_SECONDS_APPROX)(Ids), name="hall-ids",),
path("status/", cache_page(30)(Status), name="status",),
path("preferences/", Preferences.as_view(), name="preferences"),
]
35 changes: 10 additions & 25 deletions backend/laundry/views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import calendar
import datetime

from django.core.cache import cache
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.utils.timezone import make_aware
from requests.exceptions import HTTPError
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.cache import cache

from laundry.api_wrapper import check_is_working, hall_status
from laundry.models import LaundryRoom, LaundrySnapshot
from laundry.serializers import LaundryRoomSerializer
from utils.cache import MONTH_IN_SECONDS_APPROX


class Ids(APIView):
Expand All @@ -21,9 +22,7 @@ class Ids(APIView):
"""

def get(self, request):
return Response(
LaundryRoomSerializer(LaundryRoom.objects.all(), many=True).data
)
return Response(LaundryRoomSerializer(LaundryRoom.objects.all(), many=True).data)


class HallInfo(APIView):
Expand All @@ -33,13 +32,9 @@ class HallInfo(APIView):

def get(self, request, hall_id):
try:
return Response(
hall_status(get_object_or_404(LaundryRoom, hall_id=hall_id))
)
return Response(hall_status(get_object_or_404(LaundryRoom, hall_id=hall_id)))
except HTTPError:
return Response(
{"error": "The laundry api is currently unavailable."}, status=503
)
return Response({"error": "The laundry api is currently unavailable."}, status=503)


class MultipleHallInfo(APIView):
Expand Down Expand Up @@ -72,17 +67,13 @@ def get_snapshot_info(hall_id):
now = timezone.localtime()

# start is beginning of day, end is 27 hours after start
start = make_aware(
datetime.datetime(year=now.year, month=now.month, day=now.day)
)
start = make_aware(datetime.datetime(year=now.year, month=now.month, day=now.day))
end = start + datetime.timedelta(hours=27)

# filters for LaundrySnapshots within timeframe
room = get_object_or_404(LaundryRoom, hall_id=hall_id)

snapshots = LaundrySnapshot.objects.filter(
room=room, date__gt=start, date__lte=end
)
snapshots = LaundrySnapshot.objects.filter(room=room, date__gt=start, date__lte=end)

# adds all the LaundrySnapshots from the same weekday within the previous 28 days
for week in range(1, 4):
Expand Down Expand Up @@ -141,12 +132,8 @@ def compute_usage(hall_id):
"day_of_week": calendar.day_name[timezone.localtime().weekday()],
"start_date": min_date.date(),
"end_date": max_date.date(),
"washer_data": {
x: HallUsage.safe_division(data[x][0], data[x][2]) for x in range(27)
},
"dryer_data": {
x: HallUsage.safe_division(data[x][1], data[x][2]) for x in range(27)
},
"washer_data": {x: HallUsage.safe_division(data[x][0], data[x][2]) for x in range(27)},
"dryer_data": {x: HallUsage.safe_division(data[x][1], data[x][2]) for x in range(27)},
"total_number_of_washers": room.total_washers,
"total_number_of_dryers": room.total_dryers,
}
Expand Down Expand Up @@ -184,9 +171,7 @@ def post(self, request):
preferences = profile.laundry_preferences

if "rooms" not in request.data:
return Response(
{"success": False, "error": "No rooms provided"}, status=400
)
return Response({"success": False, "error": "No rooms provided"}, status=400)

halls = [
get_object_or_404(LaundryRoom, hall_id=int(hall_id))
Expand Down

0 comments on commit 15bba49

Please sign in to comment.