From 8e5c07fadca57d3931a6b72421fece6e6426247f Mon Sep 17 00:00:00 2001 From: Aswanth Vc Date: Sun, 17 Nov 2024 12:18:55 +0530 Subject: [PATCH 1/3] fix(register): Accept interest details on register api --- api/register/register_views.py | 8 +- api/register/serializers.py | 141 +++++++++++++++------------------ 2 files changed, 68 insertions(+), 81 deletions(-) diff --git a/api/register/register_views.py b/api/register/register_views.py index 84f886b5..d765cd26 100644 --- a/api/register/register_views.py +++ b/api/register/register_views.py @@ -219,14 +219,10 @@ 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.UserSerializer( - data=data.pop("user"), context={"request": request} + create_user = serializers.RegisterSerializer( + data=data, context={"request": request} ) - if not create_user.is_valid(): return CustomResponse(message=create_user.errors).get_failure_response() diff --git a/api/register/serializers.py b/api/register/serializers.py index 4f51202a..bd990dda 100644 --- a/api/register/serializers.py +++ b/api/register/serializers.py @@ -363,81 +363,6 @@ class Meta: ] -class RegisterSerializer(serializers.Serializer): - user = UserSerializer() - organization = UserOrgLinkSerializer(required=False) - referral = ReferralSerializer(required=False) - integration = IntegrationSerializer(required=False) - mentor = MentorSerializer(required=False) - - def create(self, validated_data): - with transaction.atomic(): - user = UserSerializer().create(validated_data.pop("user")) - - if organizations := validated_data.pop("organization", None): - organizations.update({"user": user}) - UserOrgLinkSerializer().create(organizations) - - if referral := validated_data.pop("referral", None): - referral.update({"user": user}) - ReferralSerializer().create(referral) - - if integration := validated_data.pop("integration", None): - integration.update({"user": user}) - IntegrationSerializer().create(integration) - - if mentor := validated_data.pop("mentor", None): - mentor["user"] = user - MentorSerializer().create(mentor) - - return user - - class Meta: - model = User - fields = [ - "user", - "organization", - "referral", - "param", - "mentor", - ] - - -class UserCountrySerializer(serializers.ModelSerializer): - country_name = serializers.CharField(source="name") - - class Meta: - model = Country - fields = ["country_name"] - - -class UserStateSerializer(serializers.ModelSerializer): - state_name = serializers.CharField(source="name") - - class Meta: - model = State - fields = ["state_name"] - - -class UserZoneSerializer(serializers.ModelSerializer): - zone_name = serializers.CharField(source="name") - - class Meta: - model = Zone - fields = ["zone_name"] - - -class LocationSerializer(serializers.ModelSerializer): - location = serializers.SerializerMethodField() - - class Meta: - model = District - fields = ("id", "location") - - def get_location(self, obj): - return f"{obj.name}, {obj.zone.state.name}, {obj.zone.state.country.name}" - - class UserInterestSerializer(serializers.ModelSerializer): id = serializers.CharField(read_only=True) user = serializers.CharField(read_only=True) @@ -506,3 +431,69 @@ class Meta: "created_at", "updated_at", ] + + +class RegisterSerializer(serializers.Serializer): + user = UserSerializer() + interests = UserInterestSerializer(required=True) + integration = IntegrationSerializer(required=False) + referral = ReferralSerializer(required=False) + + def create(self, validated_data): + with transaction.atomic(): + user = UserSerializer().create(validated_data.pop("user")) + UserInterestSerializer(context={"user": user}).create( + validated_data.pop("interests") + ) + if integration := validated_data.pop("integration", None): + integration.update({"user": user}) + IntegrationSerializer().create(integration) + + if referral := validated_data.pop("referral", None): + referral.update({"user": user}) + ReferralSerializer().create(referral) + return user + + class Meta: + model = User + fields = [ + "user", + "interests", + "integration", + "referral", + ] + + +class UserCountrySerializer(serializers.ModelSerializer): + country_name = serializers.CharField(source="name") + + class Meta: + model = Country + fields = ["country_name"] + + +class UserStateSerializer(serializers.ModelSerializer): + state_name = serializers.CharField(source="name") + + class Meta: + model = State + fields = ["state_name"] + + +class UserZoneSerializer(serializers.ModelSerializer): + zone_name = serializers.CharField(source="name") + + class Meta: + model = Zone + fields = ["zone_name"] + + +class LocationSerializer(serializers.ModelSerializer): + location = serializers.SerializerMethodField() + + class Meta: + model = District + fields = ("id", "location") + + def get_location(self, obj): + return f"{obj.name}, {obj.zone.state.name}, {obj.zone.state.country.name}" From e7704e94c8588155fa569b8fce4b717f7d56939e Mon Sep 17 00:00:00 2001 From: aswanthabam Date: Sat, 4 Jan 2025 14:16:35 +0530 Subject: [PATCH 2/3] fix(leaderboard): fix leaderboard apis --- api/leaderboard/leaderboard_view.py | 17 +++++++++++------ api/leaderboard/serializers.py | 5 ++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/api/leaderboard/leaderboard_view.py b/api/leaderboard/leaderboard_view.py index 234672f9..2210043f 100644 --- a/api/leaderboard/leaderboard_view.py +++ b/api/leaderboard/leaderboard_view.py @@ -26,7 +26,7 @@ def get(self, request): queryset=UserOrganizationLink.objects.filter( org__org_type=OrganizationType.COLLEGE.value ).select_related("org"), - to_attr="colleges" + to_attr="colleges", ) ) .order_by("-wallet_user__karma")[:20] @@ -50,7 +50,6 @@ def get(self, request): exist_in_guild=True, ) .annotate( - full_name=F("full_name"), institution=F("user_organization_link_user__org__title"), total_karma=Coalesce( Sum( @@ -70,12 +69,18 @@ def get(self, request): "total_karma", "institution", ) - .order_by("-total_karma") + .order_by("-total_karma")[:20] ) + data = [ + { + "full_name": student.full_name, + "total_karma": student.total_karma, + "institution": student.institution, + } + for student in student_monthly_leaderboard + ] - return CustomResponse( - response=student_monthly_leaderboard - ).get_success_response() + return CustomResponse(response=data).get_success_response() class CollegeLeaderboard(APIView): diff --git a/api/leaderboard/serializers.py b/api/leaderboard/serializers.py index 1edc862d..454b2370 100644 --- a/api/leaderboard/serializers.py +++ b/api/leaderboard/serializers.py @@ -78,9 +78,8 @@ def get_rank(self, obj): class StudentLeaderboardSerializer(serializers.ModelSerializer): institution = serializers.SerializerMethodField() - total_karma = serializers.IntegerField( - source="wallet_user.karma", default=0) - full_name = serializers.CharField(source="full_name") + total_karma = serializers.IntegerField(source="wallet_user.karma", default=0) + full_name = serializers.CharField() def get_institution(self, user): return user.colleges[0].org.title if user.colleges else None From 545729dd6e2c4e986c64a097bcb58935fc2d792f Mon Sep 17 00:00:00 2001 From: aswanthabam Date: Sat, 4 Jan 2025 14:45:54 +0530 Subject: [PATCH 3/3] feat: added workflow dispatch github actions --- .github/workflows/dev-deploy-dispatch.yml | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/dev-deploy-dispatch.yml diff --git a/.github/workflows/dev-deploy-dispatch.yml b/.github/workflows/dev-deploy-dispatch.yml new file mode 100644 index 00000000..db0767bd --- /dev/null +++ b/.github/workflows/dev-deploy-dispatch.yml @@ -0,0 +1,38 @@ +name: Dev CI + +on: + workflow_dispatch: + inputs: + branch: + description: "Branch to deploy" + required: true + default: "dev-server" + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Configure SSH key + uses: webfactory/ssh-agent@v0.4.1 + with: + ssh-private-key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} + + - name: Deploy + env: + REMOTE_IP: ${{ secrets.DEV_REMOTE_IP }} + PROJECT_PATH: ${{ secrets.PROJECT_PATH }} + BRANCH_NAME: ${{ github.event.inputs.branch }} + run: | + ssh -o StrictHostKeyChecking=no ubuntu@$REMOTE_IP " + cd $PROJECT_PATH && + git fetch origin && + git checkout $BRANCH_NAME && + git reset --hard origin/$BRANCH_NAME && + docker-compose down && + docker-compose up --build -d + "