Ejemplo n.º 1
0
def purchase_add(request):
  if request.method == 'POST':
    form = PurchaseForm(request.POST)
    p_sn = generate_sn(prefix = 'PR')
    if form.is_valid():
      p = form.save(commit=False)
      p.sn = p_sn
      p.save()
      serializer = JSONSimpleSerializer()
      return HttpResponse(serializer.serialize([p,], use_natural_foreign_keys=True))
  else:
    form = PurchaseForm()
    return render_to_response('modal/main_form.html',{'action':'Add', 'module':module, 'form': form},context_instance=RequestContext(request))
Ejemplo n.º 2
0
def purchase(request, product_id):

    if request.method == 'POST':
        form = PurchaseForm(request.POST)
        if form.is_valid():
            url = "https://careers.undercovertourist.com/assignment/1/products/" + product_id + "/purchase"
            data = {
                "customer_email": form.cleaned_data['customer_email'],
                "customer_name": form.cleaned_data['customer_name'],
                "customer_phone": form.cleaned_data['customer_phone'],
                "quantity": form.cleaned_data['quantity']
            }
            fail_context = {
                'quantity': form.cleaned_data['quantity'],
                'inventory': get_quantity(product_id)
            }
            if form.cleaned_data['quantity'] > get_quantity(product_id):
                return render(request, 'undercovertourist_app/Fail.html',
                              fail_context)

            result = requests.post(url, data=data, headers=headers)
            print result.json()
            product = get_productdetails(product_id)

            customerpurchase = Customer_purchasedata(
                customer_email=form.cleaned_data['customer_email'],
                customer_name=form.cleaned_data['customer_name'],
                customer_phone=form.cleaned_data['customer_phone'],
                product_name=product['name'],
                product_price=product['price'],
                quantity=form.cleaned_data['quantity'],
                confirmation_code=result.json()['confirmation_code'],
                product_id=product_id)
            customerpurchase.save()
            success_result = result.json()
            print success_result['confirmation_code']
            print success_result['product']['name']
            success_context = {
                'confirmation_code': success_result['confirmation_code'],
                'product': success_result['product']['name']
            }
            return render(request, 'undercovertourist_app/success.html',
                          success_context)

    else:
        form = PurchaseForm(initial={'item_id': product_id})

    return render(request, 'undercovertourist_app/purchase.html',
                  {'form': form})
Ejemplo n.º 3
0
def purchase_update(request):
  if request.method == 'POST':
    postdata = request.POST.copy()
    try:
      p = PurchaseOrder.objects.get(sn=postdata.pop('sn')[0])
    except PurchaseOrder.DoesNotExist:
      raise Http404(module + ' not found')
    form = PurchaseForm(postdata, instance=p)
    print form.errors
    if form.is_valid():
      p_saved = form.save()
      serializer = JSONSimpleSerializer()
      return HttpResponse(serializer.serialize([p_saved,],use_natural_foreign_keys=True))
  else:
    return HttpResponse('Invalid request.')
Ejemplo n.º 4
0
def product(request, product_id):
    prod = get_object_or_404(Product, pk=product_id)
    if request.method == 'POST':
        form = PurchaseForm(request.POST)
        if form.is_valid():
            Purchase.objects.create(cart=Cart.get(request),
                                    product=prod,
                                    quantity=form.cleaned_data['quantity'])
            messages.success(request, _('Your purchase was successfully added to the Cart'))
    else:
        form = PurchaseForm(initial={'quantity': 1})
    return render(request, 'shopping_cart/product.html',
                  {
                    'product': prod,
                    'form': form
                  })
Ejemplo n.º 5
0
def edit(request, id):
    sales = get_object_or_404(Purchase, pk=id)
    PurchaseItemFormSet = inlineformset_factory(
        Purchase,
        PurchaseItem,
        formset=MinimumRequiredFormSet,
        form=PurchaseItemForm
    )
    if request.method == "POST":
        form = PurchaseForm(request.POST, instance=sales)
        formset = PurchaseItemFormSet(
            request.POST,
            instance=sales,
            minimum_forms=1,
            minimum_forms_message="At least 1 purchase item is required."
        )
        if form.is_valid() and formset.is_valid():
            purchase = form.save()
            items = formset.save()
            for item in items:
                item.purchase = purchase
                item.save()
            messages.success(request, 'Record has been saved successfully.')
            return HttpResponseRedirect(".")
        else:
            messages.error(request, 'Failed to save record. Please correct the errors below.', extra_tags='danger')
    else:
        form = PurchaseForm(instance=sales)
        formset = PurchaseItemFormSet(
            instance=sales,
            minimum_forms=1
        )

    context = {
        'page_header': "Edit Purchase ID: %s" % id,
        'page_title': "Edit Purchase ID: %s" % id,
        'form': form,
        'formset': formset
    }

    return render(
        request,
        'purchase/edit.html',
        context
    )
Ejemplo n.º 6
0
def add(request):
    PurchaseItemFormSet = inlineformset_factory(
        Purchase,
        PurchaseItem,
        formset=MinimumRequiredFormSet,
        form=PurchaseItemForm
    )
    if request.method == "POST":
        form = PurchaseForm(request.POST)
        formset = PurchaseItemFormSet(
            request.POST,
            minimum_forms=1,
            minimum_forms_message="At least 1 purchase item is required."
        )
        if form.is_valid() and formset.is_valid():
            purchase = form.save()
            items = formset.save(commit=False)
            for item in items:
                item.purchase = purchase
                item.save()
            messages.success(request, 'Record has been saved successfully.')
            return HttpResponseRedirect(".")
        else:
            messages.error(request, 'Failed to save record. Please correct the errors below.', extra_tags='danger')
    else:
        form = PurchaseForm()
        formset = PurchaseItemFormSet(
            minimum_forms=1
        )

    context = {
        'page_header': "Add New Purchase",
        'page_title': "Add New Purchase",
        'form': form,
        'formset': formset
    }

    return render(
        request,
        'purchase/add.html',
        context
    )
Ejemplo n.º 7
0
def add_edit_item(request, model_name, add_edit, edit_id):
    """Add/Edit page for the inventory"""
    if add_edit == "add":
        edit_id = None
    if _checkModel(model_name):
        directory = "asset_inventory/add_edit_Pages/"
        # Adding or Editing a Job
        if model_name == "Job":
            templateFile = directory + "Job.html"
            post_data = request.POST or None

            try:
                Job_Instance = Job.objects.get(pk=edit_id)
            except Job.DoesNotExist:
                Job_Instance = None
                if add_edit == "edit":
                    return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Job", "add", "new"]))

            Job_Form = JobForm(post_data, instance=Job_Instance)

            if Job_Form.is_valid():
                Job_Occurance = Job_Form.save()
                return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Job"]))
            return render_to_response(templateFile, {"form": Job_Form}, context_instance=RequestContext(request))
        # Adding or Editing a Location
        elif model_name == "Location":
            templateFile = directory + "Location.html"
            post_data = request.POST or None

            try:
                Location_Instance = Location.objects.get(pk=edit_id)
            except Location.DoesNotExist:
                Location_Instance = None
                if add_edit == "edit":
                    return HttpResponseRedirect(
                        reverse("asset_inventory_add_edit_item", args=["Location", "add", "new"])
                    )

            Location_Form = LocationForm(post_data, instance=Location_Instance)

            if Location_Form.is_valid():
                Location_Occurance = Location_Form.save()
                return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Location"]))

            return render_to_response(templateFile, {"form": Location_Form}, context_instance=RequestContext(request))

        # Adding or Editing a Person
        elif model_name == "Person":
            templateFile = directory + "Person.html"
            post_data = request.POST or None

            try:
                Person_Instance = Person.objects.get(pk=edit_id)
            except Person.DoesNotExist:
                Person_Instance = None
                if add_edit == "edit":
                    return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Person", "add", "new"]))

            Person_Form = PersonForm(post_data, instance=Person_Instance)

            if Person_Form.is_valid():
                Person_Occurance = Person_Form.save()
                return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Person"]))

            return render_to_response(templateFile, {"form": Person_Form}, context_instance=RequestContext(request))

        # Adding or Editing a Purchase
        elif model_name == "Purchase":
            templateFile = directory + "Purchase.html"
            post_data = request.POST or None

            try:
                Purchase_Instance = Purchase.objects.get(pk=edit_id)
            except Purchase.DoesNotExist:
                Purchase_Instance = None
                if add_edit == "edit":
                    return HttpResponseRedirect(
                        reverse("asset_inventory_add_edit_item", args=["Purchase", "add", "new"])
                    )

            Purchase_Form = PurchaseForm(post_data, instance=Purchase_Instance)

            if Purchase_Form.is_valid():
                Purchase_Occurance = Purchase_Form.save()
                return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Purchase"]))

            return render_to_response(templateFile, {"form": Purchase_Form}, context_instance=RequestContext(request))
        # Adding or Editing a Status
        elif model_name == "Status":
            templateFile = directory + "Status.html"
            post_data = request.POST or None

            try:
                Status_Instance = Status.objects.get(pk=edit_id)
            except Status.DoesNotExist:
                Status_Instance = None
                if add_edit == "edit":
                    return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Status", "add", "new"]))

            Status_Form = StatusForm(post_data, instance=Status_Instance)

            if Status_Form.is_valid():
                Status_Occurance = Status_Form.save()
                return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Status"]))

            return render_to_response(templateFile, {"form": Status_Form}, context_instance=RequestContext(request))
        # Adding or Editing a Esign
        elif model_name == "Esign":
            templateFile = directory + "Esign.html"
            post_data = request.POST or None

            try:
                Esign_Instance = Esign.objects.get(pk=edit_id)
            except Esign.DoesNotExist:
                Esign_Instance = None
                if add_edit == "edit":
                    return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Esign", "add", "new"]))

            Esign_Form = EsignForm(post_data, instance=Esign_Instance)

            if Esign_Form.is_valid():
                Esign_Occurance = Esign_Form.save()
                return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Esign"]))

            return render_to_response(templateFile, {"form": Esign_Form}, context_instance=RequestContext(request))
        # Adding or Editing a Device
        elif model_name == "Device":
            templateFile = directory + "Device.html"
            post_data = request.POST or None

            try:
                Device_Instance = Device.objects.get(pk=edit_id)
            except Device.DoesNotExist:
                Device_Instance = None
                if add_edit == "edit":
                    return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Device", "add", "new"]))

            Device_Form = DeviceForm(post_data, instance=Device_Instance)

            if Device_Form.is_valid():
                Device_Occurance = Device_Form.save()
                return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Device"]))

            return render_to_response(templateFile, {"form": Device_Form}, context_instance=RequestContext(request))
    # This is a catch all response, if some unforseen error manages
    # to occur with the calling of add/edit pages
    else:
        render_to_response(reverse("homepage"))