-
Notifications
You must be signed in to change notification settings - Fork 155
/
views.py
281 lines (234 loc) · 9.39 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from django.shortcuts import (
render,
get_object_or_404,
redirect,
HttpResponse,
HttpResponseRedirect,
)
from django.contrib.auth.decorators import login_required
from django.views import generic
from django.urls import reverse
from django.contrib.auth import login, authenticate, logout
from django.contrib import messages
from .forms import AccountAuthenticationForm, CustomerCreationForm, SubscriptionForm
from .models import Account, Customer, Subscription
from django.utils.timezone import now
import datetime
import calendar
EXPIRED_USER = "expired"
PENDING_USER = "pending"
ACTIVE_USER = "active"
CURRENT_DATE = now().strftime("%Y-%m-%d")
def generate_balance(subs):
current_balance = 0
balance = []
for sub in subs:
if sub["debit_amount"] != 0:
current_balance += sub["debit_amount"]
else:
if current_balance - sub["credit_amount"] > 0:
current_balance = current_balance - sub["credit_amount"]
else:
current_balance = 0
balance.append(current_balance)
return balance
def generate_expire_date(subs):
current_date = now().strftime("%Y-%m-%d")
expire_dates = []
first_sub = True
expired_date = None
for sub in subs:
initial_date = sub["sub_date"]
package = sub["package"]
if first_sub:
expired_date = initial_date + datetime.timedelta(days=package * 30)
first_sub = False
else:
if initial_date.strftime("%Y-%m-%d") < expired_date.strftime("%Y-%m-%d"):
expired_date = expired_date + datetime.timedelta(days=package * 30)
else:
expired_date = initial_date + datetime.timedelta(days=package * 30)
expire_dates.append(expired_date)
return expire_dates
def save_subs(request, form, customer):
p_t = int(form.cleaned_data["package"]) # package
credit_debit_radio = form.cleaned_data["debit_or_credit_radio"]
amount = form.cleaned_data["amount"]
subs = form.save(commit=False) # subscription object
customer.activation = ACTIVE_USER
subs.credit_amount = 0
subs.debit_amount = 0
if credit_debit_radio == "D":
subs.debit_amount = amount
else:
subs.credit_amount = amount
subs.customer = customer
subs.package = p_t
subs.save()
customer.save() # save in customer table
def main(request):
return render(request, "dashboard/index.html")
def handle404(request, exception):
return render(request, "dashboard/400.html", status=404)
def handler500(request):
return render(request, "dashboard/500.html", status=500)
@login_required(login_url="/login")
def dashboard_view(request):
context = {}
customers = Customer.objects.filter(user=request.user)
active_users = Customer.objects.filter(activation=ACTIVE_USER,user=request.user)
pending_users = Customer.objects.filter(activation=PENDING_USER,user=request.user)
expired_users = Customer.objects.filter(activation=EXPIRED_USER,user=request.user)
context["customers"] = customers
context["active_users"] = active_users
context["pending_users"] = pending_users
context["expired_users"] = expired_users
return render(request, "dashboard/dashboard.html", context)
@login_required(login_url="/login")
def customer_view(request):
context = {}
customers = Customer.objects.filter(user=request.user)
context["customers"] = customers
return render(request, "dashboard/customers.html", context)
@login_required(login_url="/login")
def staff_view(request):
return render(request, "dashboard/staff.html")
@login_required(login_url="/login")
def customer_profile_view(request, customer_id):
context = {}
customer = get_object_or_404(
Customer.objects.all(), user=request.user, pk=customer_id
)
subs = Subscription.objects.filter(customer=customer)
balance = generate_balance(subs.values())
exp_dates = generate_expire_date(subs.values())
current_exp_date = customer.exp_date.strftime("%Y-%m-%d")
if len(subs) > 0:
if current_exp_date != exp_dates[-1].strftime("%Y-%m-%d"):
customer.exp_date = exp_dates[-1]
customer.save()
context["customer"] = customer
context["subs"] = zip(balance, subs, exp_dates)
return render(request, "dashboard/customer_profile.html", context)
@login_required(login_url="/login")
def customer_subscription_view(request, customer_id):
context = {}
customer = get_object_or_404(
Customer.objects.all(), user=request.user, pk=customer_id
)
context["customer"] = customer
if request.POST:
form = SubscriptionForm(request.POST)
if form.is_valid():
save_subs(request, form, customer)
return redirect(reverse("customer_profile", args=[customer_id]))
else:
initial_date = now().strftime("%Y-%m-%d")
form = SubscriptionForm(initial={"sub_date": initial_date})
context["form"] = form
return render(request, "dashboard/add-subscription.html", context)
@login_required(login_url="/login")
def create_customer_view(request):
context = {}
if request.POST:
form = CustomerCreationForm(request.POST)
if form.is_valid():
customer = form.save(commit=False)
customer.user = request.user
# customer.activation = PENDING_USER
customer.save()
return redirect(reverse("customer"))
else:
form = CustomerCreationForm(initial={"reg_date": CURRENT_DATE})
context["form"] = form
return render(request, "dashboard/add-customer.html", context)
def edit_customer_view(request, customer_id):
context = {}
context["edit"] = True
customer = get_object_or_404(Customer, pk=customer_id, user=request.user)
if request.POST:
form = CustomerCreationForm(request.POST, instance=customer)
if form.is_valid():
name = form.cleaned_data["full_name"]
address = form.cleaned_data["address"]
phone = form.cleaned_data["phone"]
reg_date = form.cleaned_data["reg_date"]
remarks = form.cleaned_data["remarks"]
isp = form.cleaned_data["isp"]
customer = form.save(commit=False)
customer.name = name
customer.address = address
customer.phone = phone
customer.reg_date = reg_date
customer.remarks = remarks
customer.isp = isp
customer.save()
return redirect(reverse("customer_profile", args=[customer_id]))
else:
form = CustomerCreationForm(instance=customer)
context["form"] = form
return render(request, "dashboard/add-customer.html", context)
def edit_subscription_view(request, customer_id, subscription_id):
context = {}
customer = get_object_or_404(
Customer.objects.all(), user=request.user, pk=customer_id
)
sub_to_edit = get_object_or_404(Subscription, pk=subscription_id, customer=customer)
context["customer"] = customer
context["edit"] = True
if request.POST:
form = SubscriptionForm(request.POST, instance=sub_to_edit)
if form.is_valid():
save_subs(request, form, customer)
return redirect(reverse("customer_profile", args=[customer_id]))
else:
form = SubscriptionForm(instance=sub_to_edit)
context["form"] = form
return render(request, "dashboard/add-subscription.html", context)
def login_view(request):
context = {}
user = request.user
if user.is_authenticated:
return redirect("dashboard")
if request.POST:
form = AccountAuthenticationForm(request.POST)
if form.is_valid():
username = request.POST["username"]
password = request.POST["password"]
user = authenticate(username=username, password=password)
if user:
login(request, user)
try:
redirect_link = request.GET["next"]
except:
redirect_link = "/cms"
return redirect(redirect_link)
else:
form = AccountAuthenticationForm()
context["form"] = form
return render(request, "dashboard/login.html", context)
@login_required(login_url="/login")
def profile_view(request):
context = {}
user = request.user
context["user_info"] = user
return render(request, "dashboard/profile.html", context)
@login_required(login_url="/login")
def logout_view(request):
logout(request)
return redirect("login")
@login_required(login_url="/login")
def delete_user(request, id):
if request.method == "POST":
pi = Customer.objects.get(pk=id, user=request.user)
pi.delete()
return HttpResponse("{}, deleted!".format(pi.full_name))
@login_required(login_url="/login")
def delete_subscription_view(request, customer_id, subscription_id):
if request.method == "POST":
customer = get_object_or_404(
Customer.objects.all(), user=request.user, pk=customer_id
)
pi = get_object_or_404(Subscription, pk=subscription_id, customer=customer)
pi.delete()
return HttpResponse("{}, deleted!".format(pi.sub_date))