Skip to content

Commit

Permalink
[PATCH] Minor patch in onboarding (#1401)
Browse files Browse the repository at this point in the history
* feat(karma_voucher): add create method

* fix(karma_voucher): create method

* [PATCH] Minor patch in middlewares (#1397)

* [FEAT] Configure CustomException Error handling

* [PATCH] Log non-JSON data (#1388)

* [PATCH] Minor patch in middlewares

---------

Co-authored-by: Adnan Kattekaden <[email protected]>
Co-authored-by: Aashish Vinu <[email protected]>

* feat(karma_voucher): add update and delete method

* [PATCH] Minor patch in onboarding (#1400)

---------

Co-authored-by: lordgrim18 <[email protected]>
Co-authored-by: Adnan Kattekaden <[email protected]>
Co-authored-by: Aashish Vinu <[email protected]>
  • Loading branch information
4 people authored Oct 22, 2023
1 parent e781150 commit 675798c
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 460 deletions.
107 changes: 104 additions & 3 deletions api/dashboard/karma_voucher/karma_voucher_serializer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import uuid
from rest_framework import serializers

from db.task import VoucherLog

from db.task import VoucherLog, TaskList
from db.user import User
from utils.permission import JWTUtils
from utils.utils import DateTimeUtils
from utils.karma_voucher import generate_ordered_id

class VoucherLogCSVSerializer(serializers.ModelSerializer):
user_id = serializers.CharField(required=True, allow_null=False)
Expand Down Expand Up @@ -39,3 +42,101 @@ class Meta:
"updated_at",
"muid"
]

class VoucherLogCreateSerializer(serializers.ModelSerializer):
user = serializers.CharField(required=True, error_messages={
'required': 'user field must not be left blank.'
})
task = serializers.CharField(required=True, error_messages={
'required': 'task field must not be left blank.'
})
karma = serializers.IntegerField(required=True, error_messages={
'required': 'karma field must not be left blank.'
})
month = serializers.CharField(required=True, error_messages={
'required': 'month field must not be left blank.'
})
week = serializers.CharField(required=True, error_messages={
'required': 'week field must not be left blank.'
})

class Meta:
model = VoucherLog
fields = [
"user",
"task",
"karma",
"month",
"week",
]

def create(self, validated_data):
user_id = JWTUtils.fetch_user_id(self.context.get('request'))
validated_data['user_id'] = validated_data.pop('user')
validated_data['task_id'] = validated_data.pop('task')
validated_data['id'] = uuid.uuid4()

existing_codes = set(VoucherLog.objects.values_list('code', flat=True))
count = 1
while generate_ordered_id(count) in existing_codes:
count += 1

validated_data['code'] = generate_ordered_id(count)
validated_data['claimed'] = False
validated_data['updated_by_id'] = user_id
validated_data['updated_at'] = DateTimeUtils.get_current_utc_time()
validated_data['created_by_id'] = user_id
validated_data['created_at'] = DateTimeUtils.get_current_utc_time()
return VoucherLog.objects.create(**validated_data)

def validate_user(self, value):
if not User.objects.filter(id=value).exists():
raise serializers.ValidationError("Enter a valid user")
return value

def validate_task(self, value):
if not TaskList.objects.filter(id=value).exists():
raise serializers.ValidationError("Enter a valid task")
return value

def validate_karma(self, value):
if value <= 0:
raise serializers.ValidationError("Enter a valid karma")
return value

def validate_week(self, value):
if len(value) != 2:
raise serializers.ValidationError("Week must have exactly two characters.")
return value

class VoucherLogUpdateSerializer(serializers.ModelSerializer):
user = serializers.CharField(required=False)
task = serializers.CharField(required=False)
karma = serializers.IntegerField(required=False)
month = serializers.CharField(required=False)
week = serializers.CharField(required=False)

class Meta:
model = VoucherLog
fields = [
"user",
"task",
"karma",
"month",
"week",
]

def update(self, instance, validated_data):
instance.user_id = validated_data.get('user', instance.user)
instance.task_id = validated_data.get('task', instance.task)
instance.karma = validated_data.get('karma', instance.karma)
instance.month = validated_data.get('month', instance.month)
instance.week = validated_data.get('week', instance.week)

instance.updated_by_id = self.context.get('user_id')
instance.updated_at = DateTimeUtils.get_current_utc_time()
instance.save()
return instance

def destroy(self, obj):
obj.delete()
34 changes: 32 additions & 2 deletions api/dashboard/karma_voucher/karma_voucher_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from utils.types import RoleType
from utils.utils import DateTimeUtils
from utils.utils import ImportCSV, CommonUtils
from .karma_voucher_serializer import VoucherLogCSVSerializer, VoucherLogSerializer
from .karma_voucher_serializer import VoucherLogCSVSerializer, VoucherLogSerializer, VoucherLogCreateSerializer, VoucherLogUpdateSerializer


class ImportVoucherLogAPI(APIView):
Expand Down Expand Up @@ -203,7 +203,37 @@ def get(self, request):
voucher_serializer = VoucherLogSerializer(paginated_queryset.get('queryset'), many=True).data
return CustomResponse().paginated_response(data=voucher_serializer,
pagination=paginated_queryset.get('pagination'))


@role_required([RoleType.ADMIN.value, RoleType.FELLOW.value, RoleType.ASSOCIATE.value])
def post(self, request):
serializer = VoucherLogCreateSerializer(data=request.data, context={'request': request})
if serializer.is_valid():
serializer.save()
return CustomResponse(general_message='Voucher created successfully',
response=serializer.data).get_success_response()
return CustomResponse(message=serializer.errors).get_failure_response()

@role_required([RoleType.ADMIN.value, RoleType.FELLOW.value, RoleType.ASSOCIATE.value])
def patch(self, request, voucher_id):
user_id = JWTUtils.fetch_user_id(request)
context = {'user_id': user_id}
voucher = VoucherLog.objects.filter(id=voucher_id).first()
serializer = VoucherLogUpdateSerializer(voucher, data=request.data, context=context)
if serializer.is_valid():
serializer.save()
return CustomResponse(general_message='Voucher updated successfully').get_success_response()
return CustomResponse(message=serializer.errors).get_failure_response()

@role_required([RoleType.ADMIN.value, RoleType.FELLOW.value, RoleType.ASSOCIATE.value])
def delete(self, request, voucher_id):
if voucher_log := VoucherLog.objects.filter(id=voucher_id).first():
VoucherLogUpdateSerializer().destroy(voucher_log)
return CustomResponse(
general_message=f'Voucher successfully deleted'
).get_success_response()
return CustomResponse(
general_message=f'Invalid Voucher'
).get_failure_response()

class ExportVoucherLogAPI(APIView):
authentication_classes = [CustomizePermission]
Expand Down
4 changes: 4 additions & 0 deletions api/dashboard/karma_voucher/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
path('', karma_voucher_view.VoucherLogAPI.as_view()),
path('import/', karma_voucher_view.ImportVoucherLogAPI.as_view()),
path('export/', karma_voucher_view.ExportVoucherLogAPI.as_view()),

path('create/', karma_voucher_view.VoucherLogAPI.as_view()),
path('update/<str:voucher_id>/', karma_voucher_view.VoucherLogAPI.as_view()),
path('delete/<str:voucher_id>/', karma_voucher_view.VoucherLogAPI.as_view()),
]
Loading

0 comments on commit 675798c

Please sign in to comment.