Skip to content

Commit

Permalink
Added safety checks for mark_waived feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jsayles committed Aug 9, 2017
1 parent ab2db56 commit 1468098
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
20 changes: 18 additions & 2 deletions nadine/models/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.timezone import localtime, now
from django.urls import reverse
from django.db.models.signals import post_save
from django.core.exceptions import ObjectDoesNotExist

# from nadine.models.resource import Resource

Expand Down Expand Up @@ -70,11 +71,26 @@ def payer(self):
def billable(self):
return self.payment == 'Bill'

@property
def waived(self):
return self.payment == 'Waive'

@property
def free_trial(self):
return self.payment == 'Trial'

@property
def bill(self):
if self.line_item:
try:
return self.line_item.bill
return none
except ObjectDoesNotExist:
return None

def mark_waived(self):
if self.bill:
raise Exception("Trying to waive a CoworkingDay that is already associated with a bill (%d)" % self.bill.id)
self.payment = 'Waive'
self.save()

def __str__(self):
return '%s - %s' % (self.visit_date, self.user)
Expand Down
16 changes: 10 additions & 6 deletions staff/templates/staff/activity/for_date.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ <h5> Daily Activity <span id='dis-date'>- {{ activity_date|date:"l, M dS" }}</sp
</tr>
{% endfor %}
</table>
<span id='waiver'>
<form class='waiver-form' action='.' method='POST' onSubmit="return confirm('Waive all visits for this date?');">
<input type='submit' class='chip' value="Waive These Visits"/>
{% csrf_token %}
</form>
</span>

{% if can_waive %}
<span id='waiver'>
<form class='waiver-form' action='.' method='POST' onSubmit="return confirm('Waive all visits for this date?');">
<input type='submit' name="mark_waived" class='chip' value="Waive These Visits"/>
{% csrf_token %}
</form>
</span>
{% endif %}

<div class='records-count'>{{ daily_logs | length }} records found</div>
</div>

Expand Down
15 changes: 10 additions & 5 deletions staff/views/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,20 @@ def list(request):
def activity_for_date(request, activity_date):
daily_logs = CoworkingDay.objects.filter(visit_date=activity_date).reverse()

if request.method == 'POST':
visits = CoworkingDay.objects.filter(visit_date=activity_date)
for v in visits:
v.payment = 'Waive'
v.save()
if request.method == 'POST' and 'mark_waived' in request.POST:
for visit in daily_logs:
if visit.billable:
visit.mark_waived()
messages.success(request, 'All selected visits have been waived')

# We can only waive days not associated with a bill
has_activity = daily_logs.count() > 0
no_bills = daily_logs.filter(line_item__isnull=False).count() == 0
can_waive = has_activity and no_bills

context = {'daily_logs': daily_logs,
'activity_date': activity_date,
'can_waive': can_waive,
'next_date': activity_date + timedelta(days=1),
'previous_date': activity_date - timedelta(days=1)}
return render(request, 'staff/activity/for_date.html', context)
Expand Down

0 comments on commit 1468098

Please sign in to comment.