mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-16 22:09:25 +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
|
||||
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
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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()),
|
||||
]
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue