Skip to content

Commit

Permalink
Merge pull request #2263 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
Dev Server
  • Loading branch information
nashnsulthan authored Nov 1, 2024
2 parents cc26835 + b6ed470 commit c722c8e
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 47 deletions.
120 changes: 79 additions & 41 deletions api/dashboard/ig/dash_ig_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
InterestGroupSerializer,
InterestGroupCreateUpdateSerializer,
)
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from api.dashboard.roles.dash_roles_serializer import RoleDashboardSerializer
from db.user import Role


class InterestGroupAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.ADMIN.value])
def get(self, request):
ig_queryset = (
InterestGroup.objects.select_related("created_by", "updated_by")
.prefetch_related("user_ig_link_ig").annotate(members=Count("user_ig_link_ig"))
.prefetch_related("user_ig_link_ig")
.annotate(members=Count("user_ig_link_ig"))
.all()
)
paginated_queryset = CommonUtils.get_paginated_queryset(
Expand Down Expand Up @@ -65,47 +69,63 @@ def post(self, request):
if serializer.is_valid():
serializer.save()

role_serializer = RoleDashboardSerializer(data={
'title': request_data.get("name"),
'description': request_data.get("name") + " Interest Group Member",
'created_by': request_data.get("created_by"),
'updated_by': request_data.get("updated_by"),
},context={'request': request})
role_serializer = RoleDashboardSerializer(
data={
"title": request_data.get("name"),
"description": request_data.get("name") + " Interest Group Member",
"created_by": request_data.get("created_by"),
"updated_by": request_data.get("updated_by"),
},
context={"request": request},
)

if role_serializer.is_valid():
role_serializer.save()
else:
return CustomResponse(general_message=role_serializer.errors).get_failure_response()

campus_role_serializer = RoleDashboardSerializer(data={
'title': RoleType.IG_CAMPUS_LEAD_ROLE(request_data.get("code")),
'description': request_data.get("name") + " Intrest Group Campus Lead",
'created_by': request_data.get("created_by"),
'updated_by': request_data.get("updated_by"),
},context={'request': request})
return CustomResponse(
general_message=role_serializer.errors
).get_failure_response()

campus_role_serializer = RoleDashboardSerializer(
data={
"title": RoleType.IG_CAMPUS_LEAD_ROLE(request_data.get("code")),
"description": request_data.get("name")
+ " Intrest Group Campus Lead",
"created_by": request_data.get("created_by"),
"updated_by": request_data.get("updated_by"),
},
context={"request": request},
)

if campus_role_serializer.is_valid():
campus_role_serializer.save()
else:
return CustomResponse(general_message=campus_role_serializer.errors).get_failure_response()

ig_lead_role_serializer = RoleDashboardSerializer(data={
'title': RoleType.IG_LEAD_ROLE(request_data.get("code")),
'description': request_data.get("name") + " Interest Group Lead",
'created_by': request_data.get("created_by"),
'updated_by': request_data.get("updated_by"),
},context={'request': request})
return CustomResponse(
general_message=campus_role_serializer.errors
).get_failure_response()

ig_lead_role_serializer = RoleDashboardSerializer(
data={
"title": RoleType.IG_LEAD_ROLE(request_data.get("code")),
"description": request_data.get("name") + " Interest Group Lead",
"created_by": request_data.get("created_by"),
"updated_by": request_data.get("updated_by"),
},
context={"request": request},
)

if ig_lead_role_serializer.is_valid():
ig_lead_role_serializer.save()
else:
return CustomResponse(general_message=ig_lead_role_serializer.errors).get_failure_response()
return CustomResponse(
general_message=ig_lead_role_serializer.errors
).get_failure_response()

DiscordWebhooks.general_updates(
WebHookCategory.INTEREST_GROUP.value,
WebHookActions.CREATE.value,
request_data.get("name"),
request_data.get("code")
request_data.get("code"),
)

return CustomResponse(
Expand Down Expand Up @@ -135,33 +155,39 @@ def put(self, request, pk):
ig_new_code = ig.code

ig_role = Role.objects.filter(title=ig_old_name).first()

if ig_role:
ig_role.title = ig_new_name
ig_role.description = ig_new_name + " Interest Group Member"
ig_role.save()

ig_campus_lead_role = Role.objects.filter(title=RoleType.IG_CAMPUS_LEAD_ROLE(ig_old_code)).first()


ig_campus_lead_role = Role.objects.filter(
title=RoleType.IG_CAMPUS_LEAD_ROLE(ig_old_code)
).first()

if ig_campus_lead_role:
ig_campus_lead_role.title = ig_new_code+' CampusLead'
ig_campus_lead_role.description = ig_new_name + " Interest Group Campus Lead"
ig_campus_lead_role.title = ig_new_code + " CampusLead"
ig_campus_lead_role.description = (
ig_new_name + " Interest Group Campus Lead"
)
ig_campus_lead_role.save()

ig_lead_role = Role.objects.filter(title=RoleType.IG_LEAD_ROLE(ig_old_code)).first()
ig_lead_role = Role.objects.filter(
title=RoleType.IG_LEAD_ROLE(ig_old_code)
).first()

if ig_lead_role:
ig_lead_role.title = RoleType.IG_LEAD_ROLE(ig_new_code)
ig_lead_role.description = ig_new_name + " Interest Group Lead"
ig_lead_role.save()

DiscordWebhooks.general_updates(
WebHookCategory.INTEREST_GROUP.value,
WebHookActions.EDIT.value,
ig_new_name,
ig_new_code,
ig_old_name,
ig_old_code
ig_old_code,
)
return CustomResponse(
response={"interestGroup": serializer.data}
Expand All @@ -176,18 +202,23 @@ def delete(self, request, pk):
if ig is None:
return CustomResponse(general_message="invalid ig").get_success_response()
ig_role = Role.objects.filter(title=ig.name).first()
if ig_role:ig_role.delete()
ig_campus_role = Role.objects.filter(title=RoleType.IG_CAMPUS_LEAD_ROLE(ig.code)).first()
if ig_campus_role:ig_campus_role.delete()
if ig_role:
ig_role.delete()
ig_campus_role = Role.objects.filter(
title=RoleType.IG_CAMPUS_LEAD_ROLE(ig.code)
).first()
if ig_campus_role:
ig_campus_role.delete()
ig_lead_role = Role.objects.filter(title=RoleType.IG_LEAD_ROLE(ig.code)).first()
if ig_lead_role:ig_lead_role.delete()
if ig_lead_role:
ig_lead_role.delete()
ig.delete()

DiscordWebhooks.general_updates(
WebHookCategory.INTEREST_GROUP.value,
WebHookActions.DELETE.value,
ig.name,
ig.code
ig.code,
)
return CustomResponse(
general_message="ig deleted successfully"
Expand All @@ -201,7 +232,8 @@ class InterestGroupCSV(APIView):
def get(self, request):
ig_serializer = (
InterestGroup.objects.select_related("created_by", "updated_by")
.prefetch_related("user_ig_link_ig").annotate(members=Count("user_ig_link_ig"))
.prefetch_related("user_ig_link_ig")
.annotate(members=Count("user_ig_link_ig"))
.all()
)

Expand Down Expand Up @@ -230,8 +262,14 @@ def get(self, request, pk):


class InterestGroupListApi(APIView):
@method_decorator(cache_page(60 * 10))
def get(self, request):
ig = InterestGroup.objects.all()
ig = (
InterestGroup.objects.all()
.select_related("created_by", "updated_by")
.prefetch_related("user_ig_link_ig")
.annotate(members=Count("user_ig_link_ig"))
)

serializer = InterestGroupSerializer(ig, many=True)

Expand Down
6 changes: 5 additions & 1 deletion api/dashboard/organisation/organisation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,11 @@ class UnverifiedOrganizationsListAPI(APIView):
permission_classes = [CustomizePermission]

def get(self, request):
unverified_orgs = UnverifiedOrganization.objects.filter(verified__isnull=True)
unverified_orgs = (
UnverifiedOrganization.objects.select_related("created_by", "department")
.filter(verified__isnull=True)
.order_by("-created_at")
)
seializer = UnverifiedOrganizationsSerializer(unverified_orgs, many=True)
return CustomResponse(response=seializer.data).get_success_response()

Expand Down
14 changes: 12 additions & 2 deletions api/dashboard/organisation/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,11 @@ def update(self, instance, validated_data):
raise serializers.ValidationError(
"Unable to assign organization to user"
)
print(validated_data.get("org_id"))
UserOrganizationLink.objects.create(
user_id=instance.created_by_id,
org=validated_data.get("org_id"),
department_id=instance.department_id,
graduation_year=instance.graduation_year,
verified=True,
created_by_id=instance.verified_by_id,
)
Expand All @@ -477,7 +478,16 @@ class Meta:
class UnverifiedOrganizationsSerializer(serializers.ModelSerializer):
id = serializers.CharField(read_only=True)
created_by = serializers.CharField(source="created_by.full_name", read_only=True)
department = serializers.CharField(source="department.title", read_only=True)

class Meta:
model = UnverifiedOrganization
fields = ["id", "title", "org_type", "created_by", "created_at"]
fields = [
"id",
"title",
"org_type",
"graduation_year",
"department",
"created_by",
"created_at",
]
2 changes: 1 addition & 1 deletion api/dashboard/user/dash_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get(self, 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)
cache.set(f"db_user_{user_muid}", user, timeout=60)
if user is None:
return CustomResponse(
general_message="No user data available"
Expand Down
2 changes: 1 addition & 1 deletion api/register/register_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def post(self, request):
return CustomResponse(message=create_user.errors).get_failure_response()

user = create_user.save()
cache.set(f"db_user_{user.muid}", user, timeout=20)
cache.set(f"db_user_{user.muid}", user, timeout=60)
password = request.data["user"]["password"]
cache.set(f"flag_register_{user.muid}", True, timeout=5)
res_data = get_auth_token(user.muid, password)
Expand Down
7 changes: 6 additions & 1 deletion api/register/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ def create(self, validated_data):


class UnverifiedOrganizationCreateSerializer(serializers.ModelSerializer):
graduation_year = serializers.IntegerField(required=False, allow_null=True)
department = serializers.PrimaryKeyRelatedField(
queryset=Department.objects.all(), required=False, allow_null=True
)

def create(self, validated_data):
validated_data["created_by_id"] = self.context.get("user_id")
return UnverifiedOrganization.objects.create(**validated_data)
Expand All @@ -285,7 +290,7 @@ def validate_org_type(self, org_type):

class Meta:
model = UnverifiedOrganization
fields = ["title", "org_type"]
fields = ["title", "org_type", "graduation_year", "department"]


class UserSerializer(serializers.ModelSerializer):
Expand Down
2 changes: 2 additions & 0 deletions db/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ class UnverifiedOrganization(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=lambda:str(uuid.uuid4()))
title = models.CharField(max_length=100)
org_type = models.CharField(max_length=25)
graduation_year = models.IntegerField(blank=True, null=True)
department = models.ForeignKey(Department, models.DO_NOTHING, related_name='unverified_organizations_dept', null=True)
verified = models.BooleanField(null=True)
verified_by = models.ForeignKey(User, models.DO_NOTHING, db_column='verified_by', related_name='unverified_organizations_verified_by', null=True)
verified_at = models.DateTimeField(null=True)
Expand Down

0 comments on commit c722c8e

Please sign in to comment.