Skip to content

Commit

Permalink
Merge branch 'refs/heads/dev-server' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
shaheenhyderk committed Sep 25, 2024
2 parents 26eb69f + 8894324 commit ed6312e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 26 deletions.
4 changes: 1 addition & 3 deletions api/dashboard/profile/profile_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,14 @@ def create_karma_activity_log(task_hashtag, karma_value):
return super().update(instance, validated_data)

class UserTermSerializer(serializers.ModelSerializer):
is_userterms_approved = serializers.SerializerMethodField()


class Meta:
model = UserSettings
fields =[
"is_userterms_approved",
"user",
]
def get_userterm(self, instance, validated_data):
def update(self, instance, validated_data):
instance.is_userterms_approved = validated_data.get('is_userterms_approved', instance.is_userterms_approved)
instance.save()
return instance
46 changes: 30 additions & 16 deletions api/dashboard/profile/profile_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from . import profile_serializer
from .profile_serializer import LinkSocials
from .profile_serializer import UserTermSerializer


class UserProfileEditView(APIView):
Expand Down Expand Up @@ -438,29 +439,42 @@ def get(self, request, muid):
).get_failure_response()

class UsertermAPI(APIView):
def post(self, request,muid):
user = User.objects.get(muid=muid)
def post(self, request, muid):
try:
user = User.objects.get(muid=muid)
except User.DoesNotExist:
return CustomResponse(response="The user does not exist").get_failure_response()

try:
settings = UserSettings.objects.get(user=user)
except UserSettings.DoesNotExist:
return CustomResponse(response="The user setting doesn't exists").get_failure_response()
return CustomResponse(response="The user settings don't exist").get_failure_response()

serializer = UserTermSerializer(settings, data={"is_userterms_approved": True}, partial=True)
if serializer.is_valid():
settings = serializer.save()
if settings.is_userterms_approved:
response_data = {"message": "User terms have been successfully approved."}
return CustomResponse(response=response_data).get_success_response()
else:
return CustomResponse(response="The given muid seems to be invalid").get_failure_response()
return CustomResponse(response=response_data).get_failure_response()

def get(self,request,muid):
user = User.objects.get(muid=muid)
try:
settings = UserSettings.objects.get(user=user)
except UserSettings.DoesNotExist:
return CustomResponse(response="The user settings doesn't exists").get_failure_response()
if settings.is_userterms_approved:
return CustomResponse(response=response_data).get_success_response()
else:
return CustomResponse(response=response_data).get_failure_response()
response_data = {"message": "The user terms have not been successfully approved."}
return CustomResponse(response=response_data).get_failure_response()
return CustomResponse(response="Invalid data provided").get_failure_response()

def get(self, request, muid):
try:
user = User.objects.get(muid=muid)
except User.DoesNotExist:
return CustomResponse(response="The user does not exist").get_failure_response()

try:
settings = UserSettings.objects.get(user=user)
except UserSettings.DoesNotExist:
return CustomResponse(response="The user settings don't exist").get_failure_response()

if settings.is_userterms_approved:
response_data = {"message": "User terms are approved."}
return CustomResponse(response=response_data).get_success_response()
else:
response_data = {"message": "User terms are not approved."}
return CustomResponse(response=response_data).get_failure_response()
2 changes: 1 addition & 1 deletion api/dashboard/profile/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
path('socials/<str:muid>/', profile_view.GetSocialsAPI.as_view()),
path('qrcode-get/<str:uuid>/', profile_view.QrcodeRetrieveAPI.as_view()),
path('change-password/', profile_view.ResetPasswordAPI.as_view()),
path('userterm-approved/,<str:muid>/',profile_view.UsertermAPI.as_view())
path('userterm-approved/<str:muid>/',profile_view.UsertermAPI.as_view())
]
7 changes: 5 additions & 2 deletions api/dashboard/user/dash_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from utils.types import RoleType, WebHookActions, WebHookCategory
from utils.utils import CommonUtils, DateTimeUtils, DiscordWebhooks, send_template_mail
from . import dash_user_serializer
from django.core.cache import cache

BE_DOMAIN_NAME = decouple_config("BE_DOMAIN_NAME")

Expand All @@ -22,8 +23,10 @@ class UserInfoAPI(APIView):

def get(self, request):
user_muid = JWTUtils.fetch_muid(request)
user = User.objects.filter(muid=user_muid).first()

user = cache.get(f"db_user_{user_muid}")
if not user:
user = User.objects.filter(muid=user_muid).first()
cache.set(f"db_user_{user_muid}", user, timeout=10)
if user is None:
return CustomResponse(
general_message="No user data available"
Expand Down
16 changes: 14 additions & 2 deletions api/register/register_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
from rest_framework.views import APIView

from db.organization import Country, Department, District, Organization, State, Zone
from django.utils.decorators import method_decorator
from db.task import InterestGroup
from db.user import Role, User
from utils.response import CustomResponse
from utils.types import OrganizationType
from utils.utils import send_template_mail
from . import serializers
from .register_helper import get_auth_token
from django.views.decorators.cache import cache_page
from django.core.cache import cache


class UserRegisterValidateAPI(APIView):
Expand All @@ -24,12 +27,14 @@ def put(self, request):


class RoleAPI(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
roles = Role.objects.all().values("id", "title")
return CustomResponse(response={"roles": roles}).get_success_response()


class CollegesAPI(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
colleges = Organization.objects.filter(
org_type=OrganizationType.COLLEGE.value
Expand All @@ -39,6 +44,7 @@ def get(self, request):


class DepartmentAPI(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
department_serializer = Department.objects.all().values("id", "title")

Expand All @@ -52,6 +58,7 @@ def get(self, request):


class CompanyAPI(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
company_queryset = Organization.objects.filter(
org_type=OrganizationType.COMPANY.value
Expand All @@ -76,7 +83,7 @@ def post(self, request):
return CustomResponse(general_message="Invalid muid").get_failure_response()

serializer = serializers.LearningCircleUserSerializer(user)
id, muid, full_name, email, phone = serializer.data.values()
id, muid, full_name, email, phone = serializer.data.values()

name = full_name

Expand All @@ -103,8 +110,9 @@ def post(self, request):

if not create_user.is_valid():
return CustomResponse(message=create_user.errors).get_failure_response()

user = create_user.save()
cache.set(f"db_user_{user.muid}", user, timeout=20)
password = request.data["user"]["password"]

res_data = get_auth_token(user.muid, password)
Expand All @@ -123,6 +131,7 @@ def post(self, request):


class CountryAPI(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
countries = Country.objects.all()

Expand Down Expand Up @@ -203,6 +212,7 @@ def post(self, request):


class CommunityAPI(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
community_queryset = Organization.objects.filter(
org_type=OrganizationType.COMMUNITY.value
Expand All @@ -218,6 +228,7 @@ def get(self, request):


class AreaOfInterestAPI(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
aoi_queryset = InterestGroup.objects.all()

Expand Down Expand Up @@ -245,6 +256,7 @@ def post(self, request):


class UserCountryAPI(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
country = Country.objects.all()

Expand Down
24 changes: 22 additions & 2 deletions mulearnbackend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
https://docs.djangoproject.com/en/4.1/ref/settings/
"""


import os
from pathlib import Path

Expand Down Expand Up @@ -94,11 +93,14 @@

WSGI_APPLICATION = "mulearnbackend.wsgi.application"

REDIS_HOST = decouple_config("REDIS_HOST")
REDIS_PORT = decouple_config("REDIS_PORT")

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(decouple_config("REDIS_HOST"), decouple_config("REDIS_PORT"))],
"hosts": [(REDIS_HOST, REDIS_PORT)],
},
},
}
Expand All @@ -114,9 +116,27 @@
"PASSWORD": decouple_config("DATABASE_PASSWORD"),
"HOST": decouple_config("DATABASE_HOST"),
"PORT": decouple_config("DATABASE_PORT"),
"CONN_MAX_AGE": 600,
}
}

CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
"LOCATION": "my_cache_table",
},
"redis": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://{REDIS_HOST}:{REDIS_PORT}/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
},
}

# Use the Redis cache as the default cache
CACHES["default"] = CACHES["redis"]

# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ qrcode==7.4.2
pymysql==1.0.2
razorpay==1.4.2
reportlab==4.2.0
django_redis==5.4.0

0 comments on commit ed6312e

Please sign in to comment.