Skip to content

Commit

Permalink
Merge pull request #2239 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
Dev Server
  • Loading branch information
nashnsulthan authored Oct 8, 2024
2 parents eb7f607 + 08612be commit 3c603b7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 38 deletions.
4 changes: 2 additions & 2 deletions api/dashboard/profile/profile_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def get_rank(self, obj):
]
)
)
.order_by("-karma")
.order_by("-karma", "-updated_at", "created_at")
)
ranks = list(ranks.values_list("user_id", flat=True))
return ranks.index(obj.id) + 1
Expand Down Expand Up @@ -284,7 +284,7 @@ def get_rank(self, obj):
]
)
)
.order_by("-karma")
.order_by("-karma", "-updated_at", "created_at")
)

ranks = list(ranks.values_list("user_id", flat=True))
Expand Down
75 changes: 44 additions & 31 deletions api/dashboard/user/dash_user_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
from utils.types import OrganizationType
from utils.utils import DateTimeUtils
from db.user import DynamicRole, DynamicUser
from db.user import UserInterests

BE_DOMAIN_NAME = decouple_config('BE_DOMAIN_NAME')
BE_DOMAIN_NAME = decouple_config("BE_DOMAIN_NAME")


class UserDashboardSerializer(serializers.ModelSerializer):
karma = serializers.IntegerField(source="wallet_user.karma", default=None)
level = serializers.CharField(
source="user_lvl_link_user.level.name", default=None)
level = serializers.CharField(source="user_lvl_link_user.level.name", default=None)

class Meta:
model = User
Expand All @@ -39,6 +39,7 @@ class UserSerializer(serializers.ModelSerializer):
joined = serializers.CharField(source="created_at")
roles = serializers.SerializerMethodField()
dynamic_type = serializers.SerializerMethodField()
interest_selected = serializers.SerializerMethodField()

class Meta:
model = User
Expand All @@ -54,27 +55,39 @@ class Meta:
"roles",
"profile_pic",
"dynamic_type",
"interest_selected",
]

def get_interest_selected(self, obj):
print("SDAFAS")
if not UserInterests.objects.filter(user=obj).exists():
return "Please select your interests"
return None

def get_roles(self, obj):
return [
user_role_link.role.title
for user_role_link in obj.user_role_link_user.all()
]

def get_dynamic_type(self, obj):
return {dynamic_role.type for dynamic_role in
DynamicRole.objects.filter(role__title__in=self.get_roles(obj))}.union(
{dynamic_user.type for dynamic_user in DynamicUser.objects.filter(user=obj)})
return {
dynamic_role.type
for dynamic_role in DynamicRole.objects.filter(
role__title__in=self.get_roles(obj)
)
}.union(
{dynamic_user.type for dynamic_user in DynamicUser.objects.filter(user=obj)}
)


class CollegeSerializer(serializers.ModelSerializer):
org_type = serializers.CharField(source="org.org_type")
department = serializers.CharField(source="department.pk", allow_null=True)
country = serializers.CharField(
source="org.district.zone.state.country.pk", allow_null=True)
state = serializers.CharField(
source="org.district.zone.state.pk", allow_null=True)
source="org.district.zone.state.country.pk", allow_null=True
)
state = serializers.CharField(source="org.district.zone.state.pk", allow_null=True)
district = serializers.CharField(source="org.district.pk", allow_null=True)

class Meta:
Expand Down Expand Up @@ -126,18 +139,18 @@ class Meta:
"graduation_year",
"interest_groups",
"igs",
"district"
"district",
]

def validate(self, data):
if "id" not in data:
raise serializers.ValidationError("User id is a required field")

if (
"email" in data
and User.objects.filter(email=data["email"])
.exclude(id=data["user_id"].id)
.all()
"email" in data
and User.objects.filter(email=data["email"])
.exclude(id=data["user_id"].id)
.all()
):
raise serializers.ValidationError("This email is already in use")
return super().validate(data)
Expand Down Expand Up @@ -188,14 +201,16 @@ def update(self, instance, validated_data):
return super().update(instance, validated_data)

def get_organizations(self, user):
organization_links = user.user_organization_link_user.select_related(
"org")
organization_links = user.user_organization_link_user.select_related("org")
if not organization_links.exists():
return None

organizations_data = []
for link in organization_links:
if link.org.org_type == OrganizationType.COLLEGE.value or OrganizationType.SCHOOL.value:
if (
link.org.org_type == OrganizationType.COLLEGE.value
or OrganizationType.SCHOOL.value
):
serializer = CollegeSerializer(link)
else:
serializer = OrgSerializer(link)
Expand Down Expand Up @@ -259,18 +274,18 @@ class Meta:
"department",
"graduation_year",
"admin",
"district"
"district",
]

def to_representation(self, instance):
data = super().to_representation(instance)

if (
college := instance.user_organization_link_user.filter(
org__org_type=OrganizationType.COLLEGE.value
)
.select_related("org__district__zone__state__country", "department")
.first()
college := instance.user_organization_link_user.filter(
org__org_type=OrganizationType.COLLEGE.value
)
.select_related("org__district__zone__state__country", "department")
.first()
):
data.update(
{
Expand Down Expand Up @@ -307,16 +322,16 @@ def update(self, instance, validated_data):

with transaction.atomic():
if isinstance(
organization_ids := validated_data.pop("organizations", None), list
organization_ids := validated_data.pop("organizations", None), list
):
instance.user_organization_link_user.all().delete()
organizations = Organization.objects.filter(
id__in=organization_ids
).order_by("org_type")

if (
organizations.exists()
and organizations.first().org_type != OrganizationType.COLLEGE.value
organizations.exists()
and organizations.first().org_type != OrganizationType.COLLEGE.value
):
validated_data.pop("department", None)
validated_data.pop("graduation_year", None)
Expand All @@ -329,10 +344,8 @@ def update(self, instance, validated_data):
created_by=admin,
created_at=current_time,
verified=True,
department_id=validated_data.pop(
"department", None),
graduation_year=validated_data.pop(
"graduation_year", None),
department_id=validated_data.pop("department", None),
graduation_year=validated_data.pop("graduation_year", None),
)
for org in organizations
]
Expand All @@ -354,7 +367,7 @@ def update(self, instance, validated_data):
)

if isinstance(
interest_group_ids := validated_data.pop("interest_groups", None), list
interest_group_ids := validated_data.pop("interest_groups", None), list
):
instance.user_ig_link_user.all().delete()
UserIgLink.objects.bulk_create(
Expand Down
8 changes: 4 additions & 4 deletions api/dashboard/user/dash_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class UserInfoAPI(APIView):

def get(self, request):
user_muid = JWTUtils.fetch_muid(request)
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)
# 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
5 changes: 4 additions & 1 deletion api/register/register_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ class RegisterDataAPI(APIView):
def post(self, request):
data = request.data
data = {key: value for key, value in data.items() if value}
# create_user = serializers.UserSerializer(
# data=data.get("user"), context={"request": request}
# )

create_user = serializers.RegisterSerializer(
data=data, context={"request": request}
Expand All @@ -210,7 +213,7 @@ def post(self, request):
user = create_user.save()
cache.set(f"db_user_{user.muid}", user, timeout=20)
password = request.data["user"]["password"]

cache.set(f"flag_register_{user.muid}", True, timeout=5)
res_data = get_auth_token(user.muid, password)

response_data = serializers.UserDetailSerializer(user, many=False).data
Expand Down

0 comments on commit 3c603b7

Please sign in to comment.