Example #1
0
 def test_quantity_cant_be_0(self):
     product = self.create_product(stock=5)
     user = self.create_user()
     product.set_cart_quantity(user, 2)
     command = self.create_command(user=user)
     cp = CommandProduct(command=command, product=product, quantity=0)
     with self.assertRaises(ValidationError):
         cp.full_clean()
Example #2
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