Fakultas Ilmu Komputer UI

Commit 28e62449 authored by inez nabila's avatar inez nabila
Browse files

Merge branch 'coldfix-pbi9' into 'staging'

Coldfix pbi9

See merge request ppl-fasilkom-ui/itproject-2020/group-b/pilar-backend!92
parents f52a6b29 1646ea88
# Generated by Django 3.0.7 on 2020-11-24 11:32
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0003_auto_20201122_1402'),
]
operations = [
migrations.RemoveField(
model_name='user',
name='number_of_cash_donations',
),
migrations.RemoveField(
model_name='user',
name='number_of_goods_donations',
),
migrations.RemoveField(
model_name='user',
name='number_of_transactions',
),
]
......@@ -40,15 +40,6 @@ 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']
......
......@@ -156,6 +156,9 @@ class ReportProgramDonationSerializer(serializers.Serializer): # pylint: disable
class UserSerializer(serializers.ModelSerializer):
total_transactions = serializers.SerializerMethodField('get_total_transactions')
total_program_donations_goods = serializers.SerializerMethodField('get_total_program_donations_goods')
total_program_donations_cash = serializers.SerializerMethodField('get_total_program_donations_cash')
class Meta:
extra_kwargs = {'password': {'write_only': True}}
......@@ -171,13 +174,22 @@ class UserSerializer(serializers.ModelSerializer):
'urban_village',
'sub_district',
'profile_picture',
'number_of_transactions',
'number_of_cash_donations',
'number_of_goods_donations',
'total_transactions',
'total_program_donations_goods',
'total_program_donations_cash',
]
model = models.User
read_only_fields = ['id']
def get_total_transactions(self, obj): # pylint: disable=no-self-use
return obj.transactions.filter(transaction_status='005').count()
def get_total_program_donations_goods(self, obj): # pylint: disable=no-self-use
return obj.program_donations.filter(donation_type='GDS').filter(donation_status='002').count()
def get_total_program_donations_cash(self, obj): # pylint: disable=no-self-use
return obj.program_donations.filter(donation_type='CSH').filter(donation_status='002').count()
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
......
......@@ -260,19 +260,16 @@ class CartCompleteTransaction(rest_framework_views.APIView):
def post(self, request, _format=None):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = request.user
transaction = shortcuts.get_object_or_404(
models.Transaction,
id=serializer.validated_data['transaction'],
user=user
user=request.user
)
if transaction.transaction_status != '004':
raise rest_framework_exceptions.PermissionDenied(_(
'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)
......@@ -492,7 +489,7 @@ class UserList(generics.ListCreateAPIView):
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()
......@@ -844,19 +841,12 @@ 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