Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from rest_framework.reverse import reverse
from rest_framework.decorators import api_view
from rest_framework.pagination import PageNumberPagination

from django.core.mail import send_mail
from django.conf import settings
from datetime import datetime
from django.http import HttpResponse

class Pagination(PageNumberPagination):

Expand All @@ -14,6 +17,19 @@ def get_page_size(self, request):
return self.max_page_size
return super().get_page_size(request)

# Permit to send feedback emails from users
@api_view(['POST'])
def feedback(request, format=None):
Comment on lines +20 to +22
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Permit to send feedback emails from users
@api_view(['POST'])
def feedback(request, format=None):
@api_view(["POST"])
def feedback(request, format=None):
"""
Send feedback emails from users
"""

Use Python Docstrings

data = request.data
if len(data['message']) < 2:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(data['message']) < 2:
if len(data.get("message", "")) < 2:

If there is no message in the POST data, get won't throw an error.

return HttpResponse(status=500)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should raise a 400 Bad Request error, you can maybe use ValidationError with a message like "Veuillez ajouter un message". Using a Serializer with a Model could help


subject = "[WOOLLY][FeedBack] - " + data['reason'] + " - " + data['sender']['name']
message = 'Utilisateur : ' + data['sender']['name'] + '\n' + 'ID : ' + data['sender']['id'] + '\n' + 'Email : ' + data['sender']['email'] + '\n' + 'Date : ' + datetime.now().strftime("%d/%m/%Y, %H:%M:%S") + '\n' + 'Raison : ' + data['reason'] + '\n \n' + 'Message :' + '\n' + data['message']
message = message + '\n\nCe message a été généré automatiquement, merci de ne pas y répondre'
Comment on lines +27 to +29
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subject = "[WOOLLY][FeedBack] - " + data['reason'] + " - " + data['sender']['name']
message = 'Utilisateur : ' + data['sender']['name'] + '\n' + 'ID : ' + data['sender']['id'] + '\n' + 'Email : ' + data['sender']['email'] + '\n' + 'Date : ' + datetime.now().strftime("%d/%m/%Y, %H:%M:%S") + '\n' + 'Raison : ' + data['reason'] + '\n \n' + 'Message :' + '\n' + data['message']
message = message + '\n\nCe message a été généré automatiquement, merci de ne pas y répondre'
subject = f"[WOOLLY][FeedBack] - {data['reason']} - {data['sender']['name']}"
message = "\n".join([
f"Utilisateur: {data['sender']['name']}",
f"Identifiant: {data['sender']['id']}",
f"Email: {data['sender']['email']}",
f"Date: {datetime.now().isoformat(sep=' ', timespec='seconds')}"
f"Raison: {data['reason']}",
"",
{data['message']},
"",
"Ce message a été généré automatiquement, merci de ne pas y répondre",
])

You should also check that all the data attributes are present, maybe create a Model and a Serializer ?

email_from = settings.EMAIL_HOST_USER
send_mail(subject, message, email_from, settings.FEEDBACK_EMAILS )
return Response(data)

@api_view(['GET'])
def api_root(request, format=None):
Expand Down Expand Up @@ -45,6 +61,8 @@ def api_root(request, format=None):
'orderlinefields': reverse('orderlinefields-list', **kwargs),
'orderlineitems': reverse('orderlineitems-list', **kwargs),


# TODO PaymentMethods
# 'paymentmethods': reverse('paymentmethods-list', **kwargs),

})
5 changes: 5 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ PORTAL_CALLBACK=

DATABASE_URL=postgresql://woolly:passwd@localhost:5432/woolly
EMAIL_URL=stmp://user@domain@localhost:25?ssl=False

FEEDBACK_EMAILS=toto@orange.fr

EMAIL_HOST_PASSWORD=
EMAIL_HOST_USER=example@gmail.com
22 changes: 15 additions & 7 deletions woolly_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def make_path(rel: str) -> str:
# Services Configuration
# --------------------------------------------------------------------------


# PayUTC Payment services
PAYUTC = {
'app_key': env.str("PAYUTC_APP_KEY"),
Expand Down Expand Up @@ -65,13 +66,20 @@ def make_path(rel: str) -> str:
}

# Email server
email = env.dj_email_url("EMAIL_URL")
EMAIL_HOST = email["EMAIL_HOST"]
EMAIL_PORT = email["EMAIL_PORT"]
EMAIL_HOST_USER = email["EMAIL_HOST_USER"]
EMAIL_HOST_PASSWORD = email["EMAIL_HOST_PASSWORD"]
EMAIL_USE_SSL = email["EMAIL_USE_SSL"]
EMAIL_USE_TLS = email["EMAIL_USE_TLS"]
# email = env.dj_email_url("EMAIL_URL")
# EMAIL_HOST = email["EMAIL_HOST"]
# EMAIL_PORT = email["EMAIL_PORT"]
# EMAIL_HOST_USER = email["EMAIL_HOST_USER"]
# EMAIL_HOST_PASSWORD = email["EMAIL_HOST_PASSWORD"]
# EMAIL_USE_SSL = email["EMAIL_USE_SSL"]
# EMAIL_USE_TLS = email["EMAIL_USE_TLS"]
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = env.str("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = env.str("EMAIL_HOST_PASSWORD")
Comment on lines +69 to +81
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to revert that and use something like this in your personal .env:

EMAIL_URL=stmp://user@domain:password@smtp.gmail.com:587?tls=True

FEEDBACK_EMAILS = env.list("FEEDBACK_EMAILS")

# --------------------------------------------------------------------------
# Debug & Security
Expand Down
4 changes: 3 additions & 1 deletion woolly_api/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from django.conf.urls import url, include
from django.contrib import admin

from core.views import api_root
from core.views import api_root, feedback

urlpatterns = [
url(r'^$', api_root, name='root'), # Api Root pour la documentation
url(r'^admin/', admin.site.urls, name='admin'), # Administration du site en backoffice
url(r'^', include('authentication.urls')), # Routes d'authentification
url(r'^', include('sales.urls')), # Routes pour les ventes
url(r'^', include('payment.urls')), # Routes pour les paiements
url(r'^feedback/?$', feedback, name='feedback'),

]