Remove redis cache for django rest api for now as it returns stale data and improved dockerfile and study group viewsets

This commit is contained in:
Keannu Bernasol 2023-09-20 21:11:53 +08:00
parent 3aacf66aaf
commit 46fe8d9397
4 changed files with 54 additions and 13 deletions

View file

@ -1,7 +1,7 @@
# Use the official Python 3.11 image
FROM --platform=arm64 python:3.11.4-bookworm
# FROM --platform=arm64 python:3.11.4-bookworm
# ARG BUILDPLATFORM
# FROM --platform=${BUILDPLATFORM} python:3.11.4-bookworm
FROM python:3.11.4-bookworm
ENV PYTHONBUFFERED 1

View file

@ -267,17 +267,17 @@ LEAFLET_CONFIG = {
REDIS_HOST = os.getenv('REDIS_HOST')
REDIS_PORT = os.getenv('REDIS_PORT')
# Django Redis Cache
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://redis:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
# CACHES = {
# "default": {
# "BACKEND": "django_redis.cache.RedisCache",
# "LOCATION": "redis://redis:6379/0",
# "OPTIONS": {
# "CLIENT_CLASS": "django_redis.client.DefaultClient",
# }
# }
# }
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"

View file

@ -1,7 +1,8 @@
from django.urls import include, path
from .views import StudyGroupListView, StudyGroupMembershipViewSet
from .views import StudyGroupListView, StudyGroupListNearView, StudyGroupMembershipViewSet
urlpatterns = [
path('', StudyGroupListView.as_view()),
path('near/', StudyGroupListNearView.as_view()),
path('membership/', StudyGroupMembershipViewSet.as_view()),
]

View file

@ -5,6 +5,10 @@ from rest_framework.permissions import IsAuthenticated
from .serializers import StudyGroupSerializer
from .models import StudyGroup
from subjects.models import Subject
from student_status.models import StudentStatus
from rest_framework import generics, viewsets, exceptions
from django.contrib.gis.geos import fromstr
from django.contrib.gis.db.models.functions import Distance
# Create your views here.
@ -37,6 +41,42 @@ class StudyGroupListView(generics.ListAPIView):
return studygroups
class StudyGroupListNearView(generics.ListAPIView):
permission_classes = [IsAuthenticated]
serializer_class = StudyGroupSerializer
queryset = StudyGroup.objects.all()
def get_queryset(self):
user = self.request.user
user_status = StudentStatus.objects.filter(user=user).first()
user_location = fromstr(
user_status.location, srid=4326)
if user_status.active is False:
raise exceptions.ValidationError("Student Status is not active")
if not user.is_student:
raise PermissionDenied(
"You must be a student to view study groups"
)
# Get the user's course
user_course = user.course
print(user_course)
# Get subject names related to the user's course
subject_names = Subject.objects.filter(
course=user_course
).values_list('subject', flat=True)
print(subject_names)
# Now fetch the StudyGroups with the matching subject names that are within 50m
studygroups = StudyGroup.objects.filter(subject_name__in=subject_names).annotate(
distance=Distance('location', user_location)).filter(distance__lte=50)
return studygroups
class StudyGroupMembershipViewSet(generics.ListAPIView):
serializer_class = StudyGroupSerializer
queryset = StudyGroup.objects.all()