From 1831aefd19b7be543eb4080fe019bb69af419039 Mon Sep 17 00:00:00 2001 From: Adnan Kattekaden Date: Wed, 25 Oct 2023 21:43:22 +0530 Subject: [PATCH 1/5] leaderboard query --- api/top100_coders/top100_view.py | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/api/top100_coders/top100_view.py b/api/top100_coders/top100_view.py index 4d6560ff..a0fef7bc 100644 --- a/api/top100_coders/top100_view.py +++ b/api/top100_coders/top100_view.py @@ -25,6 +25,45 @@ def get(self, request): ORDER BY total_karma DESC, time_ LIMIT 100; """ + query2 = """ + SELECT * from (SELECT + u.id, + u.first_name, + u.last_name, + SUM(kal.karma) AS total_karma, + org.title AS org, + org.dis, + org.state, + u.profile_pic, + MAX(kal.created_at) AS time_, + kal.created_at AS created_at + FROM + karma_activity_log AS kal + INNER JOIN user AS u ON kal.user_id = u.id + INNER JOIN task_list AS tl ON tl.id = kal.task_id + LEFT JOIN ( + SELECT + uol.user_id, + org.id, + org.title AS title, + d.name dis, + s.name state + FROM + user_organization_link AS uol + INNER JOIN organization AS org ON org.id = uol.org_id + LEFT JOIN district AS d ON d.id = org.district_id + LEFT JOIN zone AS z ON z.id = d.zone_id + LEFT JOIN state AS s ON s.id = z.state_id + GROUP BY uol.user_id + ) AS org ON org.user_id = u.id + WHERE + tl.event = 'TOP100' + AND kal.appraiser_approved = TRUE + AND DATE(kal.created_at) < CURDATE() - INTERVAL 1 DAY + GROUP BY u.id + ORDER BY total_karma DESC, time_ + ) + """ with connection.cursor() as cursor: cursor.execute(query) results = cursor.fetchall() From 2fe79f8887e3a15dc725727b237384c7c8b1f271 Mon Sep 17 00:00:00 2001 From: lordgrim18 Date: Fri, 27 Oct 2023 05:49:22 +0530 Subject: [PATCH 2/5] feat(karma_voucher): add mailing system for single create --- .../karma_voucher/karma_voucher_view.py | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/api/dashboard/karma_voucher/karma_voucher_view.py b/api/dashboard/karma_voucher/karma_voucher_view.py index 2e5a7d74..26039d52 100644 --- a/api/dashboard/karma_voucher/karma_voucher_view.py +++ b/api/dashboard/karma_voucher/karma_voucher_view.py @@ -208,7 +208,61 @@ def get(self, request): def post(self, request): serializer = VoucherLogCreateSerializer(data=request.data, context={'request': request}) if serializer.is_valid(): - serializer.save() + with transaction.atomic(): + id = serializer.save().id + voucher = VoucherLog.objects.filter(id=id).values( + 'code', + 'user__first_name', + 'user__last_name', + 'user__email', + 'task__hashtag', + 'month', + 'week', + 'karma' + ).first() + if not voucher : + transaction.set_rollback(True) + return CustomResponse(general_message='Something went wrong. Please try again.').get_failure_response() + code = voucher['code'] + month = voucher['month'] + week = voucher['week'] + karma = voucher['karma'] + task_hashtag = voucher['task__hashtag'] + full_name = voucher['user__first_name'] if voucher['user__last_name'] is None else f"{voucher['user__first_name']} {voucher['user__last_name']}" + email = voucher['user__email'] + + # Preparing email context and attachment + from_mail = decouple.config("FROM_MAIL") + subject = "Congratulations on earning Karma points!" + text = f"""Greetings from GTech µLearn! + + Great news! You are just one step away from claiming your internship/contribution Karma points. + + Name: {full_name} + Email: {email} + + To claim your karma points copy this `voucher {code}` and paste it #task-dropbox channel along with your voucher image. + """ + + month_week = f'{month}/{week}' + karma_voucher_image = generate_karma_voucher( + name=str(full_name), karma=str(int(karma)), code=code, hashtag=task_hashtag, + month=month_week) + karma_voucher_image.seek(0) + email_obj = EmailMessage( + subject=subject, + body=text, + from_email=from_mail, + to=[email], + ) + attachment = MIMEImage(karma_voucher_image.read()) + attachment.add_header( + 'Content-Disposition', + 'attachment', + filename=f'{str(full_name)}.jpg', + ) + email_obj.attach(attachment) + email_obj.send(fail_silently=False) return CustomResponse(general_message='Voucher created successfully', response=serializer.data).get_success_response() return CustomResponse(message=serializer.errors).get_failure_response() From eed39e9f750ec23008d102c1e8576548137e6b78 Mon Sep 17 00:00:00 2001 From: lordgrim18 Date: Fri, 27 Oct 2023 06:17:43 +0530 Subject: [PATCH 3/5] feat(karma_voucher): add event and description field for bulk import and get method --- .../karma_voucher/karma_voucher_serializer.py | 4 ++- .../karma_voucher/karma_voucher_view.py | 26 ++++++++++++++----- db/task.py | 2 ++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/api/dashboard/karma_voucher/karma_voucher_serializer.py b/api/dashboard/karma_voucher/karma_voucher_serializer.py index 21a81cf7..e40d124f 100644 --- a/api/dashboard/karma_voucher/karma_voucher_serializer.py +++ b/api/dashboard/karma_voucher/karma_voucher_serializer.py @@ -16,7 +16,7 @@ class VoucherLogCSVSerializer(serializers.ModelSerializer): class Meta: model = VoucherLog fields = ['id', 'code', 'user_id', 'task_id', 'karma', 'month', 'week', 'claimed', 'created_by_id', - 'updated_by_id', 'created_at', 'updated_at'] + 'updated_by_id', 'created_at', 'updated_at', 'event', 'description'] class VoucherLogSerializer(serializers.ModelSerializer): @@ -37,6 +37,8 @@ class Meta: "month", "week", "claimed", + "description", + "event", "created_by", "updated_by", "created_at", diff --git a/api/dashboard/karma_voucher/karma_voucher_view.py b/api/dashboard/karma_voucher/karma_voucher_view.py index 26039d52..088355b0 100644 --- a/api/dashboard/karma_voucher/karma_voucher_view.py +++ b/api/dashboard/karma_voucher/karma_voucher_view.py @@ -31,7 +31,7 @@ def post(self, request): if not excel_data: return CustomResponse(general_message={'Empty csv file.'}).get_failure_response() - temp_headers = ['muid', 'karma', 'hashtag', 'month', 'week'] + temp_headers = ['muid', 'karma', 'hashtag', 'month', 'week', 'event', 'description'] first_entry = excel_data[0] for key in temp_headers: if key not in first_entry: @@ -106,7 +106,9 @@ def post(self, request): 'task': task_hashtag, 'karma': karma, 'month': month, - 'week': week + 'week': week, + 'description': row.get('description'), + 'event': row.get('event') }) # Serializing and saving valid voucher rows to the database @@ -120,7 +122,9 @@ def post(self, request): 'month', 'week', 'karma', - 'task__hashtag' + 'task__hashtag', + 'event', + 'description' ) if len(vouchers_to_send) != len(valid_rows): transaction.set_rollback(True) @@ -137,6 +141,12 @@ def post(self, request): task_hashtag = voucher['task__hashtag'] full_name = user_dict.get(muid)[2] email = user_dict.get(muid)[1] + description = voucher['description'] + time_or_event = f'{month}/{week}' + + event = voucher['event'] + if event: + time_or_event = f'{event}/{description}' # Preparing email context and attachment from_mail = decouple.config("FROM_MAIL") @@ -151,10 +161,9 @@ def post(self, request): To claim your karma points copy this `voucher {code}` and paste it #task-dropbox channel along with your voucher image. """ - month_week = f'{month}/{week}' karma_voucher_image = generate_karma_voucher( name=str(full_name), karma=str(int(karma)), code=code, hashtag=task_hashtag, - month=month_week) + month=time_or_event) karma_voucher_image.seek(0) email_obj = EmailMessage( subject=subject, @@ -186,7 +195,8 @@ def get(self, request): search_fields=["user__first_name", "user__last_name", "task__title", "karma", "month", "week", "claimed", "updated_by__first_name", "updated_by__last_name", - "created_by__first_name", "created_by__last_name"], + "created_by__first_name", "created_by__last_name", + "description", "event", "code"], sort_fields={'user': 'user__first_name', 'code': 'code', @@ -197,7 +207,9 @@ def get(self, request): 'month': 'month', 'updated_by': 'updated_by__first_name', 'updated_at': 'updated_at', - 'created_at': 'created_at' + 'created_at': 'created_at', + 'event': 'event', + 'description': 'description' } ) voucher_serializer = VoucherLogSerializer(paginated_queryset.get('queryset'), many=True).data diff --git a/db/task.py b/db/task.py index ec852369..8bd35a03 100644 --- a/db/task.py +++ b/db/task.py @@ -349,6 +349,8 @@ class VoucherLog(models.Model): week = models.CharField(max_length=2) month = models.CharField(max_length=10) claimed = models.BooleanField() + event = models.CharField(max_length=50, blank=True, null=True) + description = models.TextField(blank=True, null=True) updated_by = models.ForeignKey( User, on_delete=models.CASCADE, From 4993f0afb5fd08459374bbbeeba319d4e871a3f5 Mon Sep 17 00:00:00 2001 From: lordgrim18 Date: Fri, 27 Oct 2023 06:25:36 +0530 Subject: [PATCH 4/5] fix(voucher_log) --- db/task.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/db/task.py b/db/task.py index 8bd35a03..ec852369 100644 --- a/db/task.py +++ b/db/task.py @@ -349,8 +349,6 @@ class VoucherLog(models.Model): week = models.CharField(max_length=2) month = models.CharField(max_length=10) claimed = models.BooleanField() - event = models.CharField(max_length=50, blank=True, null=True) - description = models.TextField(blank=True, null=True) updated_by = models.ForeignKey( User, on_delete=models.CASCADE, From bef4b0418cdafdc37dba13061cc03e8fe8737f32 Mon Sep 17 00:00:00 2001 From: lordgrim18 Date: Fri, 27 Oct 2023 06:27:40 +0530 Subject: [PATCH 5/5] fix(voucher_log) --- db/task.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/db/task.py b/db/task.py index 19a08b48..99577415 100644 --- a/db/task.py +++ b/db/task.py @@ -203,6 +203,8 @@ class VoucherLog(models.Model): week = models.CharField(max_length=2) month = models.CharField(max_length=10) claimed = models.BooleanField() + event = models.CharField(max_length=50, blank=True, null=True) + description = models.TextField(blank=True, null=True) updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="updated_by", related_name="voucher_log_updated_by") updated_at = models.DateTimeField(auto_now=True) created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="created_by", related_name="voucher_log_created_by")