Example #1
0
    def create_product(self, category=None, stock=0, name="Product", price=100):
        if not category:
            category = self.create_category(name="Cat for {}".format(name))

        prod = Product(name=name, category=category, price=price, stock=stock)
        prod.save()
        return prod
Example #2
0
 def test_product_quantity_defaults_to_zero(self):
     cat = self.create_category()
     prod = Product(pk=10, category=cat, name="Prod 1", price=10)
     prod.save()
     user = self.create_user()
     request_mock = Mock()
     request_mock.user = user
     self.assertEqual(prod.get_cart_quantity(request_mock), 0)
Example #3
0
    def save(self, **kwargs):
        kwargs['commit'] = False
        command = super().save(**kwargs)

        customer = AgripoUser.objects.get(pk=self.customer.pk)
        customer.first_name = self.cleaned_data["first_name"]
        customer.last_name = self.cleaned_data["last_name"]
        customer.save()

        try:
            customer.customerdata.phone
        except CustomerData.DoesNotExist:
            CustomerData.objects.create(customer=customer)

        customer.customerdata.phone = self.cleaned_data["phone"]
        customer.customerdata.save()

        command.customer = AgripoUser.objects.get(pk=self.customer.pk)
        command.save()
        cart_products = Product.static_get_cart_products(self.customer)
        command.total = 0
        command_content = ''
        for product in cart_products:
            the_product = Product.objects.get(id=product['id'])
            quantity = product['quantity']
            command.total += quantity * the_product.price
            command_product = CommandProduct(command=command, product=the_product, quantity=quantity)
            command_product.save()
            the_product.buy(quantity)
            command_content += "<strong>{}</strong> x <strong>{} ({} FCFA)</strong><br />".format(
                quantity, the_product.name, quantity * the_product.price)

        command_content += "Total : <strong>{} FCFA</strong><br />".format(command.total)
        command.save()
        Product.static_clear_cart(self.customer)

        title = 'Agripo - Commande validée'
        html_content = 'Bonjour,<br />Votre commande a été validée avec succès.<br /><br />'
        html_content += 'Elle sera disponible au point de rendez-vous suivant : {}<br /><br />'.format(
            self.cleaned_data['delivery'])
        html_content += "Contenu de votre commande : <br />{}<br /><br />".format(command_content)
        html_content += "Nous vous remercions pour votre confiance.<br /><br />L'équipe d'Agripo"
        content = strip_tags(html_content.replace("<br />", "\n"))
        mail_from = settings.EMAIL_HOST_USER
        mail_to = [customer.email]
        try:
            send_mail(title, content, mail_from, mail_to, fail_silently=False, html_message=html_content)
        except smtplib.SMTPException:
            # @todo log this!!!
            pass

        return command
Example #4
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        cart_products = Product.static_get_cart_products(self.request.user)
        context['products'] = []
        context['total'] = 0
        for product in cart_products:
            product_data = Product.objects.get(id=product['id'])
            product_total = product['quantity'] * product_data.price
            context['products'].append({
                'product': product_data,
                'quantity': product['quantity'],
                'total': product_total})
            context['total'] += product_total

        return context
Example #5
0
    def create_command(self, user=None, delivery=None, delivery_point_name="One delivery point", new_product_name="Product name"):
        if not user:
            user = self.create_user()

        if not delivery:
            delivery = self.create_delivery(delivery_point_name=delivery_point_name)

        if not Product.static_get_cart_products(user):
            product = self.create_product(stock=5, name=new_product_name)
            product.save()
            product.set_cart_quantity(user, 2)

        command = Command(delivery=delivery, customer=user)
        command.save()
        command.validate()
        return command
Example #6
0
def get_cart(request):
    """
    Sends a JSON object containing the products and quantities in current cart
    """
    cart_products = Product.static_get_cart_products(request.user)
    data = dict()
    data['products'] = []
    total = 0
    for cart_product in cart_products:
        product = Product.objects.get(id=cart_product['id'])
        product_total = cart_product['quantity'] * product.price
        data['products'].append(
            dict(id=cart_product['id'], name=product.name, quantity=cart_product['quantity'], price=product_total)
        )
        total += product_total

    data['total'] = total
    return JsonResponse(data)
Example #7
0
    def dispatch(self, request, *args, **kwargs):
        cart_products = Product.static_get_cart_products(request.user)
        if not cart_products:
            return redirect('/la-boutique/')

        return super().dispatch(request, *args, **kwargs)
Example #8
0
 def test_validating_command_empties_session_cart(self):
     command = self._validate_command()
     self.assertEqual(Product.static_get_cart_products(command.customer), [])