Skip to content

Commit

Permalink
Merge pull request #2261 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
Dev Server
  • Loading branch information
nashnsulthan authored Oct 29, 2024
2 parents 3c49f5d + df33a95 commit cc26835
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 58 deletions.
16 changes: 7 additions & 9 deletions api/dashboard/lc/dash_lc_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,13 +678,9 @@ class CircleMeetDetailSerializer(serializers.ModelSerializer):

def get_is_attendee_report_submitted(self, obj):
user_id = self.context.get("user_id")
return (
CircleMeetAttendeeReport.objects.select_related(
"meet_task__meet", "attendee__user"
)
.filter(meet_task__meet=obj, attendee__user_id=user_id)
.exists()
)
return CircleMeetAttendees.objects.filter(
meet=obj, user_id=user_id, is_report_submitted=True
).exists()

def get_is_report_submitted(self, obj):
return obj.is_report_submitted
Expand Down Expand Up @@ -941,9 +937,11 @@ def get_held_on(self, obj):
.values_list("joined_at", flat=True)
.first()
)

def get_report_submitted_attendees(self, obj):
return CircleMeetAttendees.objects.filter(meet=obj,is_report_submitted=True).count()
return CircleMeetAttendees.objects.filter(
meet=obj, is_report_submitted=True
).count()

def get_join_count(self, obj):
return CircleMeetAttendees.objects.filter(
Expand Down
37 changes: 35 additions & 2 deletions api/dashboard/organisation/organisation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Department,
OrgAffiliation,
Organization,
UnverifiedOrganization,
UserOrganizationLink,
District,
)
Expand All @@ -30,6 +31,8 @@
OrganizationKarmaTypeGetPostPatchDeleteSerializer,
OrganizationKarmaLogGetPostPatchDeleteSerializer,
OrganizationImportSerializer,
OrganizationVerifySerializer,
UnverifiedOrganizationsSerializer,
)


Expand Down Expand Up @@ -713,9 +716,39 @@ def post(self, request):
return CustomResponse(
response={"To Organisations not present"}
).get_failure_response()

UserOrganizationLink.objects.filter(org=from_org).update(org=to_org)
from_org.delete()
return CustomResponse(
response={"Organisations transferred successfully"}
response={"Organisations transferred successfully"}
).get_success_response()


class UnverifiedOrganizationsListAPI(APIView):
permission_classes = [CustomizePermission]

def get(self, request):
unverified_orgs = UnverifiedOrganization.objects.filter(verified__isnull=True)
seializer = UnverifiedOrganizationsSerializer(unverified_orgs, many=True)
return CustomResponse(response=seializer.data).get_success_response()


class VerifyOrganizationAPI(APIView):
permission_classes = [CustomizePermission]

def post(self, request, uorg_id):
user_id = JWTUtils.fetch_user_id(request)
unverifed_org = UnverifiedOrganization.objects.filter(id=uorg_id).first()
if not unverifed_org:
return CustomResponse(
general_message="Organization does not exist"
).get_failure_response()
serialzier = OrganizationVerifySerializer(
data=request.data, context={"user_id": user_id}
)
if serialzier.is_valid():
serialzier.update(unverifed_org, serialzier.validated_data)
return CustomResponse(
general_message="Organization verified successfully"
).get_success_response()
return CustomResponse(general_message=serialzier.errors).get_failure_response()
77 changes: 65 additions & 12 deletions api/dashboard/organisation/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
import uuid

from django.db import models
Expand All @@ -8,13 +9,15 @@
from db.organization import (
Organization,
District,
UnverifiedOrganization,
UserOrganizationLink,
Zone,
State,
OrgAffiliation,
Department,
OrgKarmaType,
OrgKarmaLog,
College
College,
)
from utils.permission import JWTUtils
from utils.types import OrganizationType
Expand Down Expand Up @@ -116,7 +119,9 @@ def create(self, validated_data):
validated_data["updated_by_id"] = user_id
orgobj = Organization.objects.create(**validated_data)
if validated_data.get("org_type") == OrganizationType.COLLEGE.value:
College.objects.create(org=orgobj, created_by_id=user_id, updated_by_id=user_id)
College.objects.create(
org=orgobj, created_by_id=user_id, updated_by_id=user_id
)
return orgobj

def update(self, instance, validated_data):
Expand Down Expand Up @@ -274,8 +279,8 @@ def get_update_summary(self, instance):
# Simulate the transaction
for relation in Organization._meta.related_objects:
if isinstance(
relation,
(models.ForeignKey, models.OneToOneField, models.ManyToManyField),
relation,
(models.ForeignKey, models.OneToOneField, models.ManyToManyField),
):
related_model = relation.related_model
related_field_name = relation.field.name
Expand All @@ -286,7 +291,7 @@ def get_update_summary(self, instance):
field.name
for field in related_model._meta.fields
if isinstance(field, models.ForeignKey)
and field.related_model == Organization
and field.related_model == Organization
),
None,
)
Expand All @@ -297,7 +302,7 @@ def get_update_summary(self, instance):
continue

if existing_relations := related_model.objects.filter(
**{related_field_name: instance}
**{related_field_name: instance}
):
update_summary.append(
{
Expand Down Expand Up @@ -326,8 +331,8 @@ def update(self, instance, validated_data):
for relation in Organization._meta.related_objects:
# Determine related model and related field name
if isinstance(
relation,
(models.ForeignKey, models.OneToOneField, models.ManyToManyField),
relation,
(models.ForeignKey, models.OneToOneField, models.ManyToManyField),
):
related_model = relation.related_model
related_field_name = relation.field.name
Expand All @@ -339,7 +344,7 @@ def update(self, instance, validated_data):
field.name
for field in related_model._meta.fields
if isinstance(field, models.ForeignKey)
and field.related_model == Organization
and field.related_model == Organization
),
None,
)
Expand All @@ -355,7 +360,7 @@ def update(self, instance, validated_data):
# Handle OneToOne relationships: delete existing relation if it points to the instance
if isinstance(relation, (models.OneToOneField, models.OneToOneRel)):
if existing_relation := related_model.objects.filter(
**{related_field_name: instance}
**{related_field_name: instance}
).first():
existing_relation.delete()

Expand Down Expand Up @@ -424,7 +429,55 @@ class Meta:
def to_representation(self, instance):
representation = super().to_representation(instance)

representation['affiliation_id'] = instance.affiliation.title if instance.affiliation else None
representation['district_id'] = instance.district.name if instance.district else None
representation["affiliation_id"] = (
instance.affiliation.title if instance.affiliation else None
)
representation["district_id"] = (
instance.district.name if instance.district else None
)

return representation


class OrganizationVerifySerializer(serializers.ModelSerializer):
verified = serializers.BooleanField(required=True)
org_id = serializers.PrimaryKeyRelatedField(
queryset=Organization.objects.all(), required=True
)

def update(self, instance, validated_data):
if instance.verified:
raise serializers.ValidationError("Organization already verified")
instance.verified = validated_data.get("verified")
instance.org = validated_data.get("org_id")
instance.verified_by_id = self.context.get("user_id")
instance.verified_at = datetime.now(timezone.utc)
instance.save()
if instance.verified:
if UserOrganizationLink.objects.filter(
user_id=instance.created_by_id, org_id=instance.org_id
).exists():
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"),
verified=True,
created_by_id=instance.verified_by_id,
)
return instance

class Meta:
model = UnverifiedOrganization
fields = ["verified", "org_id"]


class UnverifiedOrganizationsSerializer(serializers.ModelSerializer):
id = serializers.CharField(read_only=True)
created_by = serializers.CharField(source="created_by.full_name", read_only=True)

class Meta:
model = UnverifiedOrganization
fields = ["id", "title", "org_type", "created_by", "created_at"]
95 changes: 72 additions & 23 deletions api/dashboard/organisation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,76 @@

urlpatterns = [
# path('add', portal_views.AddPortal.as_view()),
path('institutes/create/', organisation_views.InstitutionPostUpdateDeleteAPI.as_view()),
path('institutes/edit/<str:org_code>/', organisation_views.InstitutionPostUpdateDeleteAPI.as_view()),
path('institutes/delete/<str:org_code>/', organisation_views.InstitutionPostUpdateDeleteAPI.as_view()),
path('institutes/<str:org_type>/csv/', organisation_views.InstitutionCSVAPI.as_view()),
path('institutes/info/<str:org_code>/', organisation_views.InstitutionDetailsAPI.as_view()),
path('institutes/prefill/<str:org_code>/', organisation_views.InstitutionPrefillAPI.as_view()),
path('institutes/<str:org_type>/', organisation_views.InstitutionAPI.as_view()),
path('institutes/<str:org_type>/<str:district_id>/', organisation_views.InstitutionAPI.as_view()),
path('institutes/org/affiliation/show/', organisation_views.AffiliationGetPostUpdateDeleteAPI.as_view()),
path('institutes/org/affiliation/create/', organisation_views.AffiliationGetPostUpdateDeleteAPI.as_view()),
path('institutes/org/affiliation/edit/<str:affiliation_id>/', organisation_views.AffiliationGetPostUpdateDeleteAPI.as_view()),
path('institutes/org/affiliation/delete/<str:affiliation_id>/', organisation_views.AffiliationGetPostUpdateDeleteAPI.as_view()),
path('departments/', organisation_views.DepartmentAPI.as_view()),
path('departments/create/', organisation_views.DepartmentAPI.as_view()),
path('departments/edit/<str:department_id>/', organisation_views.DepartmentAPI.as_view()),
path('departments/delete/<str:department_id>/', organisation_views.DepartmentAPI.as_view()),
path('affiliation/list/', organisation_views.AffiliationListAPI.as_view()),
path('merge_organizations/<str:organisation_id>/', organisation_views.OrganizationMergerView.as_view()),
path('karma-type/create/', organisation_views.OrganizationKarmaTypeGetPostPatchDeleteAPI.as_view()),
path('karma-log/create/', organisation_views.OrganizationKarmaLogGetPostPatchDeleteAPI.as_view()),
path('base-template/', organisation_views.OrganisationBaseTemplateAPI.as_view()),
path('import/', organisation_views.OrganisationImportAPI.as_view()),
path('transfer/', organisation_views.TransferAPI.as_view()),
path(
"institutes/create/",
organisation_views.InstitutionPostUpdateDeleteAPI.as_view(),
),
path(
"institutes/edit/<str:org_code>/",
organisation_views.InstitutionPostUpdateDeleteAPI.as_view(),
),
path(
"institutes/delete/<str:org_code>/",
organisation_views.InstitutionPostUpdateDeleteAPI.as_view(),
),
path(
"institutes/<str:org_type>/csv/", organisation_views.InstitutionCSVAPI.as_view()
),
path(
"institutes/info/<str:org_code>/",
organisation_views.InstitutionDetailsAPI.as_view(),
),
path(
"institutes/prefill/<str:org_code>/",
organisation_views.InstitutionPrefillAPI.as_view(),
),
path("institutes/<str:org_type>/", organisation_views.InstitutionAPI.as_view()),
path(
"institutes/<str:org_type>/<str:district_id>/",
organisation_views.InstitutionAPI.as_view(),
),
path(
"institutes/org/affiliation/show/",
organisation_views.AffiliationGetPostUpdateDeleteAPI.as_view(),
),
path(
"institutes/org/affiliation/create/",
organisation_views.AffiliationGetPostUpdateDeleteAPI.as_view(),
),
path(
"institutes/org/affiliation/edit/<str:affiliation_id>/",
organisation_views.AffiliationGetPostUpdateDeleteAPI.as_view(),
),
path(
"institutes/org/affiliation/delete/<str:affiliation_id>/",
organisation_views.AffiliationGetPostUpdateDeleteAPI.as_view(),
),
path("departments/", organisation_views.DepartmentAPI.as_view()),
path("departments/create/", organisation_views.DepartmentAPI.as_view()),
path(
"departments/edit/<str:department_id>/",
organisation_views.DepartmentAPI.as_view(),
),
path(
"departments/delete/<str:department_id>/",
organisation_views.DepartmentAPI.as_view(),
),
path("affiliation/list/", organisation_views.AffiliationListAPI.as_view()),
path(
"merge_organizations/<str:organisation_id>/",
organisation_views.OrganizationMergerView.as_view(),
),
path(
"karma-type/create/",
organisation_views.OrganizationKarmaTypeGetPostPatchDeleteAPI.as_view(),
),
path(
"karma-log/create/",
organisation_views.OrganizationKarmaLogGetPostPatchDeleteAPI.as_view(),
),
path("base-template/", organisation_views.OrganisationBaseTemplateAPI.as_view()),
path("import/", organisation_views.OrganisationImportAPI.as_view()),
path("transfer/", organisation_views.TransferAPI.as_view()),
path("verify/list/", organisation_views.UnverifiedOrganizationsListAPI.as_view()),
path("verify/<str:uorg_id>/", organisation_views.VerifyOrganizationAPI.as_view()),
]
34 changes: 22 additions & 12 deletions api/dashboard/user/dash_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,28 +373,38 @@ def post(self, request):
return CustomResponse(
response={"user_id": user.id, "profile_pic": uploaded_file_url}
).get_success_response()


class UserAddOrgAPI(APIView):
def post(self,request):
user = User.objects.filter(id=JWTUtils.fetch_user_id(request)).first()
def post(self, request):
user_id = JWTUtils.fetch_user_id(request)
if not (user := cache.get(f"db_user_{user_id}")):
user = User.objects.filter(id=user_id).first()
if user is None:
return CustomResponse(
general_message="No user data available"
).get_failure_response()
serializer=dash_user_serializer.UserOrgLinkSerializer(data=request.data,context={'user':user})
if(serializer.is_valid()):
serializer = dash_user_serializer.UserOrgLinkSerializer(
data=request.data, context={"user": user}
)
if serializer.is_valid():
serializer.save()
return CustomResponse(general_message="organisation linked successfully").get_success_response()
return CustomResponse(
general_message="organisation linked successfully"
).get_success_response()
return CustomResponse(response=serializer.errors).get_failure_response()
def get(self,request):

def get(self, request):
user = User.objects.filter(id=JWTUtils.fetch_user_id(request)).first()
if user is None:
return CustomResponse(
general_message="No user data available"
).get_failure_response()

links = UserOrganizationLink.objects.filter(user=user,org__org_type=OrganizationType.COLLEGE.value).select_related('org','department')
serializer=dash_user_serializer.GetUserLinkSerializer(instance=links,many=True)

links = UserOrganizationLink.objects.filter(
user=user, org__org_type=OrganizationType.COLLEGE.value
).select_related("org", "department")
serializer = dash_user_serializer.GetUserLinkSerializer(
instance=links, many=True
)
return CustomResponse(response=serializer.data).get_success_response()

Loading

0 comments on commit cc26835

Please sign in to comment.