Skip to content

Commit

Permalink
ZDL-95: Integrate sending of email after order
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanAquino committed Dec 26, 2021
1 parent e224fed commit 74d09c3
Show file tree
Hide file tree
Showing 10 changed files with 660 additions and 22 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,3 @@ dmypy.json
.pytype/

# End of https://www.toptal.com/developers/gitignore/api/django
static
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ python manage.py createsuperuser
python manage.py runserver
```

#### Access on browser
```
http://localhost:8000/api-docs
```

### Setup with Docker (Alternative)
```
docker-compose up -d
Expand Down
25 changes: 25 additions & 0 deletions orders/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from typing import Dict

from django.conf import settings
from django.core.mail import send_mail
from django.db import models
from django.db.models.signals import post_delete
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.html import strip_tags
from pydantic import EmailStr
from validate_email import validate_email

from authentication.models import User
from products.models import Product
Expand Down Expand Up @@ -43,6 +51,23 @@ def get_cart_items(self):
total = sum([item.quantity for item in items])
return total

@staticmethod
def send_email_notification(
customer_email: EmailStr, template: str, subject: str, context_data: Dict
):
if validate_email(email_address=customer_email, check_smtp=False):
html_message = render_to_string(template, context_data)
plain_message = strip_tags(html_message)

send_mail(
subject,
plain_message,
settings.EMAIL_HOST_USER,
[customer_email, settings.EMAIL_HOST_USER],
fail_silently=False,
html_message=html_message,
)


class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
Expand Down
3 changes: 3 additions & 0 deletions orders/tests/test_orders_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from unittest.mock import patch

import pytest

Expand Down Expand Up @@ -149,6 +150,8 @@ def test_update_cart(logged_in_client):


@pytest.mark.django_db
@patch("django.core.mail.send_mail", lambda **kwargs: kwargs)
@patch("validate_email.validate_email", lambda z, x: x)
def test_process_order(logged_in_client, logged_in_user):
"""
Test process order
Expand Down
55 changes: 38 additions & 17 deletions orders/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from datetime import datetime
from uuid import uuid4

from rest_condition import Or
from rest_framework import mixins, status, viewsets
Expand Down Expand Up @@ -105,7 +106,8 @@ def update_cart(self, request):
)
def process_order(self, request):
request_data = self.get_serializer(data=request.data)
transaction_id = datetime.datetime.now().timestamp()
transaction_id = str(uuid4())
transaction_timestamp = datetime.now().strftime("%b %d, %Y %H:%M")
customer = request.user
self.check_object_permissions(self.request, customer)

Expand All @@ -116,18 +118,37 @@ def process_order(self, request):
order.complete = True
order.save()

if order.shipping:
shipping = ShippingAddress.objects.create(
customer=customer,
order=order,
address=request_data.validated_data["address"],
city=request_data.validated_data["city"],
state=request_data.validated_data["state"],
zipcode=request_data.validated_data["zipcode"],
)
shipping.save()

return Response(
ShippingAddressSerializer(shipping).data, status=status.HTTP_201_CREATED
)
return Response("Order not found", status=status.HTTP_400_BAD_REQUEST)
if not order.shipping:
return Response("Order not found", status=status.HTTP_400_BAD_REQUEST)

shipping = ShippingAddress.objects.create(
customer=customer,
order=order,
address=request_data.validated_data["address"],
city=request_data.validated_data["city"],
state=request_data.validated_data["state"],
zipcode=request_data.validated_data["zipcode"],
)
shipping.save()

Order.send_email_notification(
customer.email,
"invoice_email_template.html",
f"Order Being Processed: {order.transaction_id}",
{
"user_first_name": customer.first_name,
"user_last_name": customer.last_name,
"shipping_address": shipping.address,
"shipping_city": shipping.city,
"shipping_state": shipping.state,
"shipping_zipcode": shipping.zipcode,
"invoice_code": transaction_id,
"order_items": order.order_items,
"order": order,
"date_ordered": transaction_timestamp,
},
)

return Response(
ShippingAddressSerializer(shipping).data, status=status.HTTP_201_CREATED
)
8 changes: 6 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ attrs==19.3.0
black==20.8b1
certifi==2020.6.20
chardet==3.0.4
charset-normalizer==2.0.9
click==7.1.2
colorama==0.4.4
coreapi==2.3.3
Expand All @@ -14,12 +15,14 @@ Django==3.0.8
django-cors-headers==3.7.0
djangorestframework==3.11.0
djangorestframework-simplejwt==4.4.0
dnspython==2.1.0
drf-yasg==1.17.1
factory-boy==3.2.0
Faker==8.8.0
filelock==3.4.0
flake8==3.8.3
gunicorn==20.0.4
idna==2.10
idna==3.0
importlib-metadata==1.7.0
inflection==0.5.1
iniconfig==1.0.1
Expand All @@ -37,6 +40,7 @@ Pillow==7.2.0
pluggy==0.13.1
psycopg2-binary==2.8.6
py==1.9.0
py3-validate-email==1.0.5
pycodestyle==2.6.0
pydantic==1.6.1
pyflakes==2.2.0
Expand All @@ -48,7 +52,7 @@ pytest-django==3.9.0
python-dateutil==2.8.1
pytz==2020.1
regex==2020.7.14
requests==2.24.0
requests==2.26.0
rest-condition==1.0.3
ruamel.yaml==0.16.12
ruamel.yaml.clib==0.2.2
Expand Down
Binary file added static/images/zadala-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 74d09c3

Please sign in to comment.