def receipts(request, data, **kwargs): staff = Staff.objects.get(user=request.user) joined_workplaces = staff.workplaces.all() # Conditions on two kinds of url. if 'workplace_uuid' not in kwargs and 'page_num' not in kwargs: # No parameters # Test whether the staff has workplaces. if not joined_workplaces: data['no_workplaces'] = True return render(request, 'receipts/index.html', data) first_workplace_uuid = joined_workplaces[0].unique_id return redirect_with_data(request, data, '/receipts/' + str(first_workplace_uuid) + '/1/') else: # Two parameters data['no_workplaces'] = False data['joined_workplaces'] = joined_workplaces workplace_uuid = kwargs['workplace_uuid'] page_num = kwargs['page_num'] page_num = int(page_num) # If page number is zero. if page_num == 0: return custom_error_404(request, data) # If workplace_uuid is invalid... try: workplace = Company.objects.get(unique_id=workplace_uuid) except ValidationError: return custom_error_404(request, data) except ObjectDoesNotExist: return custom_error_404(request, data) # Get UUID. data['workplace_uuid'] = workplace.unique_id # Get all receipts in the company. receipt_records = Receipt.objects.filter(company=workplace) # If there is no receipt in company... if not receipt_records: data['no_receipt'] = True else: data['no_receipt'] = False # Get sliced records. data['receipt_records'], data['page_end'] = get_slice_and_page_end(receipt_records, page_num) # Get other necessary data data['page_range'] = range(1, data['page_end'] + 1) data['page_num'] = page_num # If sliced records are empty... if not data['receipt_records']: return custom_error_404(request, data) return render(request, 'receipts/index.html', data)
def delete(request, data): if request.method == 'POST': # Collect form data. receipt_id = request.POST['receipt_id'] # Get Receipt instance. receipt = Receipt.objects.get(id=receipt_id) # Delete receipt.delete() # Success data['alerts'].append(('success', 'Delete successfully!', 'You have successfully deleted a receipt.')) return redirect_with_data(request, data, '/receipts/' + request.POST['workplace_uuid'] + '/1/') else: return custom_error_404(request, data)