mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
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:
parent
3aacf66aaf
commit
46fe8d9397
4 changed files with 54 additions and 13 deletions
|
@ -1,7 +1,7 @@
|
||||||
# Use the official Python 3.11 image
|
# 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
|
# ARG BUILDPLATFORM
|
||||||
# FROM --platform=${BUILDPLATFORM} python:3.11.4-bookworm
|
FROM python:3.11.4-bookworm
|
||||||
|
|
||||||
ENV PYTHONBUFFERED 1
|
ENV PYTHONBUFFERED 1
|
||||||
|
|
||||||
|
|
|
@ -267,17 +267,17 @@ LEAFLET_CONFIG = {
|
||||||
REDIS_HOST = os.getenv('REDIS_HOST')
|
REDIS_HOST = os.getenv('REDIS_HOST')
|
||||||
REDIS_PORT = os.getenv('REDIS_PORT')
|
REDIS_PORT = os.getenv('REDIS_PORT')
|
||||||
|
|
||||||
|
|
||||||
# Django Redis Cache
|
# Django Redis Cache
|
||||||
CACHES = {
|
# CACHES = {
|
||||||
"default": {
|
# "default": {
|
||||||
"BACKEND": "django_redis.cache.RedisCache",
|
# "BACKEND": "django_redis.cache.RedisCache",
|
||||||
"LOCATION": "redis://redis:6379/0",
|
# "LOCATION": "redis://redis:6379/0",
|
||||||
"OPTIONS": {
|
# "OPTIONS": {
|
||||||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
# "CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||||||
}
|
# }
|
||||||
}
|
# }
|
||||||
}
|
# }
|
||||||
|
|
||||||
|
|
||||||
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
|
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
|
||||||
SESSION_CACHE_ALIAS = "default"
|
SESSION_CACHE_ALIAS = "default"
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
from .views import StudyGroupListView, StudyGroupMembershipViewSet
|
from .views import StudyGroupListView, StudyGroupListNearView, StudyGroupMembershipViewSet
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', StudyGroupListView.as_view()),
|
path('', StudyGroupListView.as_view()),
|
||||||
|
path('near/', StudyGroupListNearView.as_view()),
|
||||||
path('membership/', StudyGroupMembershipViewSet.as_view()),
|
path('membership/', StudyGroupMembershipViewSet.as_view()),
|
||||||
]
|
]
|
||||||
|
|
|
@ -5,6 +5,10 @@ from rest_framework.permissions import IsAuthenticated
|
||||||
from .serializers import StudyGroupSerializer
|
from .serializers import StudyGroupSerializer
|
||||||
from .models import StudyGroup
|
from .models import StudyGroup
|
||||||
from subjects.models import Subject
|
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.
|
# Create your views here.
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,6 +41,42 @@ class StudyGroupListView(generics.ListAPIView):
|
||||||
return studygroups
|
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):
|
class StudyGroupMembershipViewSet(generics.ListAPIView):
|
||||||
serializer_class = StudyGroupSerializer
|
serializer_class = StudyGroupSerializer
|
||||||
queryset = StudyGroup.objects.all()
|
queryset = StudyGroup.objects.all()
|
||||||
|
|
Loading…
Reference in a new issue