Fakultas Ilmu Komputer UI

Commit 2227cf4d authored by inez nabila's avatar inez nabila
Browse files

refactor views and models

parent 17ab7085
# Generated by Django 3.0.7 on 2020-11-19 09:36
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0002_programprogress'),
]
operations = [
migrations.AddField(
model_name='user',
name='number_of_cash_donations',
field=models.PositiveIntegerField(default=0, verbose_name='number of cash donations'),
),
migrations.AddField(
model_name='user',
name='number_of_goods_donations',
field=models.PositiveIntegerField(default=0, verbose_name='number of goods donations'),
),
migrations.AddField(
model_name='user',
name='number_of_transactions',
field=models.PositiveIntegerField(default=0, verbose_name='number of transactions'),
),
]
......@@ -40,6 +40,9 @@ class User(auth_models.AbstractUser):
verbose_name=_('profile picture')
)
otp = db_models.CharField(blank=True, max_length=6, null=True, verbose_name=_('OTP'))
number_of_transactions = db_models.PositiveIntegerField(default=0, verbose_name=_('number of transactions'))
number_of_cash_donations = db_models.PositiveIntegerField(default=0, verbose_name=_('number of cash donations'))
number_of_goods_donations = db_models.PositiveIntegerField(default=0, verbose_name=_('number of goods donations'))
class Meta:
ordering = ['username', 'id']
......
......@@ -266,7 +266,8 @@ class CartCompleteTransaction(rest_framework_views.APIView):
'Transaction cannot be completed unless the status is "Being shipped".'
))
transaction.transaction_status = '005'
user.number_of_transactions += 1
user.save()
transaction.save()
return response.Response(status=status.HTTP_204_NO_CONTENT)
......@@ -482,7 +483,7 @@ class UserList(generics.ListCreateAPIView):
rest_framework_filters.SearchFilter,
]
filterset_fields = ['username', 'phone_number']
ordering_fields = ['username', 'full_name', 'phone_number']
ordering_fields = ['username', 'full_name', 'phone_number', 'number_of_transactions', 'number_of_cash_donations', 'number_of_goods_donations']
pagination_class = paginations.SmallResultsSetPagination
permission_classes = [rest_framework_permissions.IsAdminUser]
queryset = models.User.objects.all()
......@@ -816,12 +817,19 @@ class ProgramDonationDetail(generics.RetrieveUpdateAPIView):
def update(self, request, *args, **kwargs):
instance = self.get_object()
user = request.user
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
if instance.donation_status in ('002', '003'):
raise rest_framework_exceptions.PermissionDenied(_(
'Cannot update program donation because it has a completed or canceled status.'
))
if serializer.validated_data['donation_status'] == '002':
if instance.donation_type == 'CSH':
user.number_of_cash_donations += 1
elif instance.donation_type == 'GDS':
user.number_of_goods_donations += 1
user.save()
return super().update(request, *args, **kwargs) # pylint: disable=no-member
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment