Skip to content

Commit d2df365

Browse files
committed
added payment for tax-payer
1 parent 0f2037d commit d2df365

12 files changed

+169
-88
lines changed

tax/migrations/0001_initial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.6 on 2021-08-09 07:45
1+
# Generated by Django 3.2.6 on 2021-08-10 13:29
22

33
from django.conf import settings
44
from django.db import migrations, models

tax/models.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import timedelta
22

33
from django.contrib.auth import get_user_model
4+
from django.core.validators import MinLengthValidator
45
from django.db import models
56
from django.utils import timezone
67
from simple_history.models import HistoricalRecords
@@ -53,13 +54,13 @@ class Tax(models.Model):
5354
fines = models.PositiveIntegerField(
5455
default=0,
5556
blank=True,
56-
null=True
57+
null=True,
5758
)
5859

5960
total_amount = models.PositiveIntegerField(
6061
default=0,
6162
blank=True,
62-
null=True
63+
null=True,
6364
)
6465

6566
payment_status = models.BooleanField(default=False)
@@ -103,11 +104,10 @@ def tax_calculate(self):
103104
self.tax_amount = income*tax_rate
104105

105106
def payment(self):
106-
if self.tax_amount >= 0:
107-
self.payment_date = timezone.now()
108-
self.payment = True
109-
self.status = 'PAID'
110-
self.save()
107+
self.payment_date = timezone.now()
108+
self.payment = True
109+
self.status = 'PAID'
110+
self.save()
111111

112112
def save(self, *args, **kwargs):
113113
self.tax_calculate()

tax/serializers.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from rest_framework.serializers import (
2-
Serializer,
32
DictField,
3+
FloatField,
44
ListField,
55
ModelSerializer,
6-
SerializerMethodField,
7-
IntegerField,
8-
DateTimeField,
9-
StringRelatedField,
6+
Serializer,
7+
SerializerMethodField
108
)
119

1210
from tax.models import Tax
@@ -67,7 +65,5 @@ class Meta:
6765
fields = ('history', )
6866

6967

70-
class TaxReqSerializers(Serializer):
71-
income = IntegerField()
72-
deadline = DateTimeField()
73-
tax_payer = StringRelatedField()
68+
class TaxPaymentSerializers(Serializer):
69+
income = FloatField()

tax/urls.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from django.urls import path
22

33
from tax.views import (
4-
request_tax,
54
edit_tax,
65
list_tax,
7-
tax_history
6+
request_tax,
7+
tax_history,
8+
tax_payment
89
)
910

1011
app_name = "tax"
@@ -13,5 +14,6 @@
1314
path('create/', request_tax, name="create_tax"),
1415
path('list/', list_tax, name="list_tax"),
1516
path('edit/<int:id>/', edit_tax, name="edit_tax"),
17+
path('payment/<int:id>/', tax_payment, name="tax_payment"),
1618
path('list/<int:id>/history/', tax_history, name="tax_history"),
1719
]

tax/views.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
from rest_framework.decorators import api_view, permission_classes
77
from rest_framework.permissions import AllowAny, IsAuthenticated
88
from rest_framework.response import Response
9-
from rest_framework.status import HTTP_200_OK, HTTP_400_BAD_REQUEST, HTTP_201_CREATED, HTTP_401_UNAUTHORIZED
9+
from rest_framework.status import (
10+
HTTP_200_OK,
11+
HTTP_201_CREATED,
12+
HTTP_400_BAD_REQUEST,
13+
HTTP_401_UNAUTHORIZED,
14+
HTTP_204_NO_CONTENT
15+
)
1016

1117
from tax.models import Tax
1218
from tax.serializers import (
1319
TaxHistorySerializers,
14-
TaxReqSerializers,
20+
TaxPaymentSerializers,
1521
TaxSerializers
1622
)
1723
from user.models import User
@@ -181,8 +187,27 @@ def tax_history(request, id, *args, **kwargs):
181187
return Response(message, status=HTTP_400_BAD_REQUEST)
182188

183189

184-
@swagger_auto_schema(method='post')
190+
@swagger_auto_schema(method='post', request_body=TaxPaymentSerializers)
185191
@api_view(['POST'])
186192
@permission_classes([IsAuthenticated, IsTaxPayer])
187193
def tax_payment(request, id, *args, **kwargs):
188-
pass
194+
user = request.user
195+
data = request.data
196+
try:
197+
tax = Tax.objects.get(pk=id)
198+
except Exception as e:
199+
message = {'detail': 'Tax with id not found.'}
200+
return Response(message, status=HTTP_400_BAD_REQUEST)
201+
if tax.tax_payer == user:
202+
serializers = TaxPaymentSerializers(data)
203+
if (serializers.data.get('income') == tax.total_amount or tax.total_amount == 0) and tax.status != 'PAID':
204+
tax.payment()
205+
message = {
206+
'message': f'Payment of Rs. {tax.total_amount} is success.'}
207+
return Response(message, status=HTTP_204_NO_CONTENT)
208+
else:
209+
message = {'message': f'Amount to be paid Rs.{tax.total_amount}'}
210+
return Response(message, status=HTTP_204_NO_CONTENT)
211+
else:
212+
message = {'detail': 'Tax with id not for current user.'}
213+
return Response(message, status=HTTP_400_BAD_REQUEST)

user/admin.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from django.contrib import admin
22
from django.contrib.auth.models import Group
33

4+
from user.forms import UserChangeForm, UserCreationForm
45
from user.models import User
56

67

78
@admin.register(User)
89
class UserAdmin(admin.ModelAdmin):
10+
form = UserChangeForm
11+
add_form = UserCreationForm
912
list_display = (
1013
'email',
1114
'username',
@@ -16,6 +19,56 @@ class UserAdmin(admin.ModelAdmin):
1619
'user_type',
1720
'date_joined'
1821
)
22+
fieldsets = (
23+
(
24+
None,
25+
{
26+
'fields': (
27+
'username',
28+
'email',
29+
'password'
30+
)
31+
}
32+
),
33+
(
34+
'Personal info',
35+
{
36+
'fields': (
37+
'first_name',
38+
'last_name',
39+
'user_type',
40+
'state'
41+
)
42+
}
43+
),
44+
(
45+
'Permissions',
46+
{
47+
'fields': (
48+
'is_admin',
49+
'is_active',
50+
'is_superuser',
51+
'is_staff'
52+
)
53+
}
54+
),
55+
)
56+
add_fieldsets = (
57+
(
58+
None,
59+
{
60+
'classes': ('wide',),
61+
'fields': (
62+
'email',
63+
'user_type',
64+
'password1',
65+
'password2'
66+
)
67+
}
68+
),
69+
)
70+
search_fields = ('email', 'username', )
71+
ordering = ('-date_joined', )
1972

2073

2174
admin.site.unregister(Group)

user/forms.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from django import forms
2+
from django.contrib import admin
3+
from django.contrib.auth import get_user_model
4+
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
5+
from django.contrib.auth.forms import ReadOnlyPasswordHashField
6+
from django.utils.translation import ugettext
7+
from django.utils.translation import ugettext_lazy as _
8+
9+
10+
class UserCreationForm(forms.ModelForm):
11+
password1 = forms.CharField(
12+
label=_("Password"),
13+
widget=forms.PasswordInput
14+
)
15+
password2 = forms.CharField(
16+
label=_("Password confirmation"),
17+
widget=forms.PasswordInput,
18+
help_text=_("Enter the same password as above, for verification.")
19+
)
20+
21+
class Meta:
22+
model = get_user_model()
23+
fields = '__all__'
24+
25+
def clean_password2(self):
26+
password1 = self.cleaned_data.get("password1")
27+
password2 = self.cleaned_data.get("password2")
28+
if password1 and password2 and password1 != password2:
29+
raise forms.ValidationError("Passwords don't match")
30+
return password2
31+
32+
def save(self, commit=True):
33+
user = super(UserCreationForm, self).save(commit=False)
34+
user.set_password(self.cleaned_data["password1"])
35+
if commit:
36+
user.save()
37+
return user
38+
39+
40+
class UserChangeForm(forms.ModelForm):
41+
password = ReadOnlyPasswordHashField(
42+
label=_("Password"),
43+
help_text=_("Raw passwords are not stored, so there is no way to see "
44+
"this user's password, but you can change the password "
45+
"using <a href=\"password/\">this form</a>.")
46+
)
47+
48+
class Meta:
49+
model = get_user_model()
50+
fields = (
51+
'username',
52+
'email',
53+
'password',
54+
'user_type',
55+
'is_active',
56+
'is_admin'
57+
)
58+
59+
def __init__(self, *args, **kwargs):
60+
super(UserChangeForm, self).__init__(*args, **kwargs)
61+
f = self.fields.get('user_permissions', None)
62+
if f is not None:
63+
f.queryset = f.queryset.select_related('content_type')
64+
65+
def clean_password(self):
66+
return self.initial["password"]

user/migrations/0001_initial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.6 on 2021-08-09 07:45
1+
# Generated by Django 3.2.6 on 2021-08-10 13:29
22

33
from django.db import migrations, models
44
import user.usermanager
@@ -30,7 +30,7 @@ class Migration(migrations.Migration):
3030
('is_staff', models.BooleanField(default=False)),
3131
('is_active', models.BooleanField(default=True)),
3232
('user_type', models.CharField(choices=[('admin', 'ADMIN'), ('tax-payer', 'TAX-PAYER'), ('tax-accountant', 'TAX-ACCOUNTANT')], default='admin', max_length=14)),
33-
('state', models.CharField(choices=[('Andhra Pradesh', 'Andhra Pradesh'), ('Arunachal Pradesh', 'Arunachal Pradesh'), ('Assam', 'Assam'), ('Bihar', 'Bihar'), ('Chhattisgarh', 'Chhattisgarh'), ('Goa', 'Goa'), ('Gujarat', 'Gujarat'), ('Haryana', 'Haryana'), ('Himachal Pradesh', 'Himachal Pradesh'), ('Jharkhand', 'Jharkhand'), ('Karnataka', 'Karnataka'), ('Kerala', 'Kerala'), ('Madhya Pradesh', 'Madhya Pradesh'), ('Maharashtra', 'Maharashtra'), ('Manipur', 'Manipur'), ('Meghalaya', 'Meghalaya'), ('Mizora', 'Mizora'), ('Nagaland', 'Nagaland'), ('Odisha', 'Odisha'), ('Punjab', 'Punjab'), ('Rajasthan', 'Rajasthan'), ('Sikkim', 'Sikkim'), ('Tamil Nadu', 'Tamil Nadu'), ('Telangana', 'Telangana'), ('Tripura', 'Tripura'), ('Uttar Pradesh', 'Uttar Pradesh'), ('Uttarakhand', 'Uttarakhand'), ('West Bengal', 'West Bengal'), ('Andaman and Nicobar Island', 'Andaman and Nicobar Island'), ('Chandigarh', 'Chandigarh'), ('Dadra and Nagar Haveli and Daman and Diu', 'Dadra and Nagar Haveli and Daman and Diu'), ('Delhi', 'Delhi'), ('Ladakh', 'Ladakh'), ('Lakshadweep', 'Lakshadweep'), ('Jammu and Kashmir', 'Jammu and Kashmir'), ('Puducherry', 'Puducherry')], default='', max_length=100)),
33+
('state', models.CharField(blank=True, choices=[('Andhra Pradesh', 'Andhra Pradesh'), ('Arunachal Pradesh', 'Arunachal Pradesh'), ('Assam', 'Assam'), ('Bihar', 'Bihar'), ('Chhattisgarh', 'Chhattisgarh'), ('Goa', 'Goa'), ('Gujarat', 'Gujarat'), ('Haryana', 'Haryana'), ('Himachal Pradesh', 'Himachal Pradesh'), ('Jharkhand', 'Jharkhand'), ('Karnataka', 'Karnataka'), ('Kerala', 'Kerala'), ('Madhya Pradesh', 'Madhya Pradesh'), ('Maharashtra', 'Maharashtra'), ('Manipur', 'Manipur'), ('Meghalaya', 'Meghalaya'), ('Mizora', 'Mizora'), ('Nagaland', 'Nagaland'), ('Odisha', 'Odisha'), ('Punjab', 'Punjab'), ('Rajasthan', 'Rajasthan'), ('Sikkim', 'Sikkim'), ('Tamil Nadu', 'Tamil Nadu'), ('Telangana', 'Telangana'), ('Tripura', 'Tripura'), ('Uttar Pradesh', 'Uttar Pradesh'), ('Uttarakhand', 'Uttarakhand'), ('West Bengal', 'West Bengal'), ('Andaman and Nicobar Island', 'Andaman and Nicobar Island'), ('Chandigarh', 'Chandigarh'), ('Dadra and Nagar Haveli and Daman and Diu', 'Dadra and Nagar Haveli and Daman and Diu'), ('Delhi', 'Delhi'), ('Ladakh', 'Ladakh'), ('Lakshadweep', 'Lakshadweep'), ('Jammu and Kashmir', 'Jammu and Kashmir'), ('Puducherry', 'Puducherry')], default='', max_length=100, null=True, verbose_name='State')),
3434
('union_territories', models.BooleanField(default=False)),
3535
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
3636
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),

user/migrations/0002_auto_20210809_1351.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

user/migrations/0003_auto_20210809_1355.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

user/migrations/0004_alter_user_state.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

user/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ def get_user_edit(request, id, *args, **kwargs):
137137
return Response({'message': 'Not Allowed'}, status=HTTP_401_UNAUTHORIZED)
138138

139139
if request.user.user_type != 'admin':
140-
data.pop('user_type')
140+
if data.get('user_type'):
141+
data.pop('user_type')
141142
serializer = UserSerializer(user)
142143
try:
143144
serializer.update(instance=user, validated_data=data)

0 commit comments

Comments
 (0)