Skip to content

Commit

Permalink
Merge pull request #2121 from gtech-mulearn/dev-server
Browse files Browse the repository at this point in the history
Dev server
  • Loading branch information
jelanmathewjames authored Jul 5, 2024
2 parents bce4b0a + d80982d commit 87b86c6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
4 changes: 3 additions & 1 deletion api/donate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def post(self, request):
transaction_details = {
"Amount": float(data['amount']) / 100,
"Currency": data.get('currency', None),
"payment_id":data['id'],
"Payment_method":data['method'],
"Name": data['notes']['name'],
"Email": data['notes']['email'],
}
Expand All @@ -119,6 +121,6 @@ def post(self, request):
if serializer.is_valid():
serializer.save()

return create_receipt(transaction_details)
return CustomResponse(response = transaction_details).get_success_response()
except razorpay.errors.SignatureVerificationError as e:
return CustomResponse(general_message = "Payment Verification Failed").get_failure_response()
70 changes: 44 additions & 26 deletions api/launchpad/launchpad_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get(self, request):
appraiser_approved=True,
task__hashtag='#lp24-introduction',
).values('user')

users = User.objects.filter(
karma_activity_log_user__task__event="launchpad",
karma_activity_log_user__appraiser_approved=True,
Expand All @@ -44,7 +44,7 @@ def get(self, request):
)
).filter(
Q(user_organization_link_user__id__in=UserOrganizationLink.objects.filter(
org__org_type__in=allowed_org_types
org__org_type__in=["College", "School", "Company", "Community"]
).values("id")) | Q(user_organization_link_user__id__isnull=True)
).annotate(
karma=Subquery(total_karma_subquery, output_field=IntegerField()),
Expand Down Expand Up @@ -106,10 +106,20 @@ def get(self, request):
Q(level__in=allowed_levels) | Q(level__isnull=True)
).distinct()

if district := request.query_params.get("district"):
users = users.filter(district_name=district)
if org := request.query_params.get("org"):
users = users.filter(org=org)
if level := request.query_params.get("level"):
users = users.filter(level=level)
if state := request.query_params.get("state"):
users = users.filter(state=state)

paginated_queryset = CommonUtils.get_paginated_queryset(
users,
request,
["full_name", "level", "org", "district_name", "state"]
["full_name", "level", "org", "district_name", "state"],
sort_fields={"full_name": "full_name", "org": "org", "district_name": "district_name", "state": "state", "level": "level"}
)

serializer = LaunchpadParticipantsSerializer(
Expand Down Expand Up @@ -188,27 +198,35 @@ def get(self, request):
state=F("district__zone__state__name"),
total_users=Count("user_organization_link_org__user"),
level1 = Count(
"user_organization_link_org__user",
"user_organization_link_org__user",
filter=Q(user_organization_link_org__user__user_role_link_user__role__title=LaunchPadLevels.LEVEL_1.value)
),
level2 = Count(
"user_organization_link_org__user",
"user_organization_link_org__user",
filter=Q(user_organization_link_org__user__user_role_link_user__role__title=LaunchPadLevels.LEVEL_2.value)
),
level3 = Count(
"user_organization_link_org__user",
"user_organization_link_org__user",
filter=Q(user_organization_link_org__user__user_role_link_user__role__title=LaunchPadLevels.LEVEL_3.value)
),
level4 = Count(
"user_organization_link_org__user",
"user_organization_link_org__user",
filter=Q(user_organization_link_org__user__user_role_link_user__role__title=LaunchPadLevels.LEVEL_4.value)
)
).order_by("-total_users")


if district := request.query_params.get("district"):
org = org.filter(district_name=district)
if title := request.query_params.get("title"):
org = org.filter(title=title)
if state := request.query_params.get("state"):
org = org.filter(state=state)

paginated_queryset = CommonUtils.get_paginated_queryset(
org,
request,
["title", "district_name", "state"]
["title", "district_name", "state"],
sort_fields={"title": "title", "district_name": "district_name", "state": "state"}
)

serializer = CollegeDataSerializer(
Expand All @@ -230,7 +248,7 @@ def post(self, request):
serializer = LaunchpadUserSerializer(data=data)
if not serializer.is_valid():
return CustomResponse(message=serializer.errors).get_failure_response()

colleges = data.get('colleges')
errors = {}
error = False
Expand All @@ -255,7 +273,7 @@ def post(self, request):
if error:
return CustomResponse(message=errors).get_failure_response()
return CustomResponse(general_message="Successfully added user").get_success_response()

def get(self, request):
auth_mail = request.query_params.get('current_user', None)
if not LaunchPadUsers.objects.filter(email=auth_mail, role=LaunchPadRoles.ADMIN.value).exists():
Expand All @@ -273,7 +291,7 @@ def get(self, request):
return CustomResponse().paginated_response(
data=serializer.data, pagination=paginated_queryset.get("pagination")
)

def put(self, request, email):
data = request.data
auth_mail = data.pop('current_user', None)
Expand All @@ -299,32 +317,32 @@ def get(self, request, email):
return CustomResponse(general_message="User not found").get_failure_response()
serializer = LaunchpadUserListSerializer(user)
return CustomResponse(response=serializer.data).get_success_response()


class UserProfile(APIView):

def get(self, request):
auth_mail = request.query_params('current_user', None)
auth_mail = request.query_params.get("current_user", None)
if not LaunchPadUsers.objects.filter(email=auth_mail).exists():
return CustomResponse(general_message="Unauthorized").get_failure_response()
user = LaunchPadUsers.objects.get(email=auth_mail)
serializer = LaunchpadUserListSerializer(user)
return CustomResponse(data=serializer.data).get_success_response()
return CustomResponse(response=serializer.data).get_success_response()

def put(self, request):
data = request.data
auth_mail = data.pop('current_user', None)
auth_mail = auth_mail[0] if isinstance(auth_mail, list) else auth_mail
if not (user := LaunchPadUsers.objects.filter(email=auth_mail).first()):
return CustomResponse(general_message="Unauthorized").get_failure_response()

serializer = UserProfileUpdateSerializer(user, data=data)
if serializer.is_valid():
serializer.save()
return CustomResponse(general_message="Successfully updated user").get_success_response()
return CustomResponse(message=serializer.errors).get_failure_response()


class UserBasedCollegeData(APIView):

def get(self, request):
Expand All @@ -336,7 +354,7 @@ def get(self, request):
college_ids = [college.college_id for college in colleges]

allowed_levels = LaunchPadLevels.get_all_values()

org = Organization.objects.filter(
org_type="College",
id__in=college_ids
Expand All @@ -354,19 +372,19 @@ def get(self, request):
state=F("district__zone__state__name"),
total_users=Count("user_organization_link_org__user"),
level1 = Count(
"user_organization_link_org__user",
"user_organization_link_org__user",
filter=Q(user_organization_link_org__user__user_role_link_user__role__title=LaunchPadLevels.LEVEL_1.value)
),
level2 = Count(
"user_organization_link_org__user",
"user_organization_link_org__user",
filter=Q(user_organization_link_org__user__user_role_link_user__role__title=LaunchPadLevels.LEVEL_2.value)
),
level3 = Count(
"user_organization_link_org__user",
"user_organization_link_org__user",
filter=Q(user_organization_link_org__user__user_role_link_user__role__title=LaunchPadLevels.LEVEL_3.value)
),
level4 = Count(
"user_organization_link_org__user",
"user_organization_link_org__user",
filter=Q(user_organization_link_org__user__user_role_link_user__role__title=LaunchPadLevels.LEVEL_4.value)
)
).order_by("-total_users")
Expand Down Expand Up @@ -403,7 +421,7 @@ def post(self, request):
return CustomResponse(general_message={'Empty csv file.'}).get_failure_response()
errors = {}
error = False

for data in excel_data[1:]:
not_found_colleges = []
data['colleges'] = data['colleges'].split(",") if data.get('colleges') else []
Expand Down
12 changes: 11 additions & 1 deletion api/launchpad/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,22 @@ def update(self, instance, validated_data):

class UserProfileUpdateSerializer(serializers.ModelSerializer):
id = serializers.CharField(max_length=36, read_only=True)
full_name = serializers.CharField(required=False, allow_blank=True, allow_null=True)
phone_number = serializers.CharField(required=False, allow_blank=True, allow_null=True)
district = serializers.CharField(required=False, allow_blank=True, allow_null=True)
zone = serializers.CharField(required=False, allow_blank=True, allow_null=True)
email = serializers.EmailField(required=False)
colleges = serializers.SerializerMethodField()

class Meta:
model = LaunchPadUsers
fields = ("id", "full_name", "phone_number", "district", "zone", "email")
fields = ("id", "full_name", "phone_number", "district", "zone", "email", "colleges")

def validate(self, attrs):
if LaunchPadUsers.objects.filter(email=attrs.get("email")).exclude(id=self.instance.id).exists():
raise serializers.ValidationError("Email already exists")
return super().validate(attrs)

def update(self, instance, validated_data):
instance.full_name = validated_data.get("full_name", instance.full_name)
instance.phone_number = validated_data.get("phone_number", instance.phone_number)
Expand Down

0 comments on commit 87b86c6

Please sign in to comment.