示例#1
0
def consumables(request):
    if request.method == "GET":
        rate_dict = rates.rate_class.get_consumable_rates(
            Consumable.objects.all())

        dictionary = {
            'users':
            User.objects.filter(is_active=True),
            'consumables':
            Consumable.objects.filter(visible=True).order_by(
                'category', 'name'),
            'rates':
            rate_dict,
        }
        return render(request, 'consumables/consumables.html', dictionary)
    elif request.method == "POST":
        form = ConsumableWithdrawForm(request.POST)
        if form.is_valid():
            withdraw = form.save(commit=False)
            try:
                check_billing_to_project(withdraw.project, withdraw.customer,
                                         withdraw.consumable)
            except ProjectChargeException as e:
                return HttpResponseBadRequest(e.msg)
            add_withdraw_to_session(request, withdraw)
        else:
            return HttpResponseBadRequest(form.errors.as_ul())
        return render(request, "consumables/consumables_order.html")
示例#2
0
def consumables(request):
    form = ConsumableWithdrawForm(request.POST or None,
                                  initial={'quantity': 1})
    rate_dict = rates.rate_class.get_consumable_rates(Consumable.objects.all())

    dictionary = {
        'users':
        User.objects.filter(is_active=True),
        'consumables':
        Consumable.objects.filter(visible=True).order_by('category', 'name'),
        'rates':
        rate_dict,
    }

    if form.is_valid():
        withdraw = form.save(commit=False)
        make_withdrawal(consumable=withdraw.consumable,
                        merchant=request.user,
                        customer=withdraw.customer,
                        quantity=withdraw.quantity,
                        project=withdraw.project)
        form = ConsumableWithdrawForm(initial={'quantity': 1})
        messages.success(
            request,
            f'The withdrawal of {withdraw.quantity} of {withdraw.consumable} for {withdraw.customer} was successfully logged and will be billed to project {withdraw.project}.',
            extra_tags="data-speed=9000")
    else:
        if hasattr(form, 'cleaned_data') and 'customer' in form.cleaned_data:
            dictionary['projects'] = form.cleaned_data[
                'customer'].active_projects()

    dictionary['form'] = form
    return render(request, 'consumables.html', dictionary)
示例#3
0
def consumables(request):
    form = ConsumableWithdrawForm(request.POST or None,
                                  initial={'quantity': 1})

    dictionary = {
        'users':
        User.objects.filter(is_active=True),
        'consumables':
        Consumable.objects.filter(visible=True).order_by('category', 'name'),
    }

    if form.is_valid():
        withdraw = form.save(commit=False)
        withdraw.merchant = request.user
        withdraw.save()
        withdraw.consumable.quantity -= withdraw.quantity
        withdraw.consumable.save()
        dictionary[
            'success'] = 'The withdraw for {} was successfully logged.'.format(
                withdraw.customer)
        form = ConsumableWithdrawForm(initial={'quantity': 1})
    else:
        if hasattr(form, 'cleaned_data') and 'customer' in form.cleaned_data:
            dictionary['projects'] = form.cleaned_data[
                'customer'].active_projects()

    dictionary['form'] = form
    return render(request, 'consumables.html', dictionary)
示例#4
0
def consumables(request):
	form = ConsumableWithdrawForm(request.POST or None, initial={'quantity': 1})

	dictionary = {
		'users': User.objects.filter(is_active=True),
		'recent': ConsumableWithdraw.objects.filter(merchant=request.user, active_flag=True).order_by('-date')[:10]
	}

	if request.user.is_superuser:
		dictionary['cores'] = Core.objects.all()
		dictionary['consumables'] = Consumable.objects.filter(visible=True).order_by('category', 'name')
	else:
		dictionary['active_core'] = request.session.get('active_core')
		dictionary['consumables'] = Consumable.objects.filter(visible=True, core_id__in=request.user.core_ids.all()).order_by('category', 'name')

	if form.is_valid():
		withdraw = form.save(commit=False)
		withdraw.merchant = request.user
		withdraw.updated = timezone.now()
		withdraw.save()
		withdraw.consumable.quantity -= withdraw.quantity
		withdraw.consumable.save()
		dictionary['success'] = 'The withdraw for {} was successfully logged.'.format(withdraw.customer)
		form = ConsumableWithdrawForm(initial={'quantity': 1})
	else:
		if hasattr(form, 'cleaned_data') and 'customer' in form.cleaned_data:
			dictionary['projects'] = form.cleaned_data['customer'].active_projects()

	dictionary['form'] = form
	return render(request, 'consumables.html', dictionary)