Skip to content

Commit

Permalink
Merge pull request #1430 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
karma voucher update
  • Loading branch information
adnankattekaden authored Oct 27, 2023
2 parents b529fa2 + 6a88115 commit 00e3ea7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 9 deletions.
4 changes: 3 additions & 1 deletion api/dashboard/karma_voucher/karma_voucher_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -37,6 +37,8 @@ class Meta:
"month",
"week",
"claimed",
"description",
"event",
"created_by",
"updated_by",
"created_at",
Expand Down
82 changes: 74 additions & 8 deletions api/dashboard/karma_voucher/karma_voucher_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -208,7 +220,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()
Expand Down
39 changes: 39 additions & 0 deletions api/top100_coders/top100_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions db/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 00e3ea7

Please sign in to comment.