Skip to content

Commit

Permalink
Merge pull request #213 from FJNR-inc/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
RignonNoel authored Oct 4, 2019
2 parents 718f12c + b8810cb commit 3db7c05
Show file tree
Hide file tree
Showing 27 changed files with 862 additions and 187 deletions.
76 changes: 61 additions & 15 deletions blitz_api/services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from datetime import datetime

import pytz
Expand All @@ -12,6 +13,7 @@

from rest_framework.pagination import PageNumberPagination

from log_management.models import Log
from .exceptions import MailServiceError
from django.core.mail import send_mail as django_send_mail

Expand Down Expand Up @@ -43,7 +45,21 @@ def send_mail(users, context, template):
# use this SendinBlue template
message.template_id = MAIL_SERVICE["TEMPLATES"].get(template)
message.merge_global_data = context
response = message.send() # return number of successfully sent emails
try:
# return number of successfully sent emails
response = message.send()
except Exception as err:
additional_data = {
'email': user.email,
'context': context,
'template': template
}
Log.error(
source='SENDING_BLUE_TEMPLATE',
message=err,
additional_data=json.dumps(additional_data)
)
raise

if not response:
failed_emails.append(user.email)
Expand Down Expand Up @@ -136,13 +152,28 @@ def notify_user_of_new_account(email, password):
merge_data
)

return django_send_mail(
"Bienvenue à Thèsez-vous?",
plain_msg,
settings.DEFAULT_FROM_EMAIL,
[email],
html_message=msg_html,
)
try:
return django_send_mail(
"Bienvenue à Thèsez-vous?",
plain_msg,
settings.DEFAULT_FROM_EMAIL,
[email],
html_message=msg_html,
)
except Exception as err:
additional_data = {
'title': "Bienvenue à Thèsez-vous?",
'default_from': settings.DEFAULT_FROM_EMAIL,
'user_email': email,
'merge_data': merge_data,
'template': 'notify_user_of_new_account'
}
Log.error(
source='SENDING_BLUE_TEMPLATE',
message=err,
additional_data=json.dumps(additional_data)
)
raise


def notify_user_of_change_email(email, activation_url, first_name):
Expand All @@ -163,13 +194,28 @@ def notify_user_of_change_email(email, activation_url, first_name):
merge_data
)

return django_send_mail(
"Confirmation de votre nouvelle adresse courriel",
plain_msg,
settings.DEFAULT_FROM_EMAIL,
[email],
html_message=msg_html,
)
try:
return django_send_mail(
"Confirmation de votre nouvelle adresse courriel",
plain_msg,
settings.DEFAULT_FROM_EMAIL,
[email],
html_message=msg_html,
)
except Exception as err:
additional_data = {
'title': "Confirmation de votre nouvelle adresse courriel",
'default_from': settings.DEFAULT_FROM_EMAIL,
'user_email': email,
'merge_data': merge_data,
'template': 'notify_user_of_change_email'
}
Log.error(
source='SENDING_BLUE_TEMPLATE',
message=err,
additional_data=json.dumps(additional_data)
)
raise


class ExportPagination(PageNumberPagination):
Expand Down
1 change: 1 addition & 0 deletions blitz_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'workplace',
'store',
'retirement',
'log_management',
'storages',
'anymail',
'simple_history',
Expand Down
45 changes: 45 additions & 0 deletions blitz_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,51 @@ def create(self, request, *args, **kwargs):

return response

@action(detail=True, permission_classes=[IsAdminUser])
def send_email_confirm(self, request, pk):
user = self.get_object()

if settings.LOCAL_SETTINGS['EMAIL_SERVICE'] is True:
FRONTEND_SETTINGS = settings.LOCAL_SETTINGS[
'FRONTEND_INTEGRATION'
]

# Get the token of the saved user and send it with an email
activate_token = ActionToken.objects.get(
user=user,
type='account_activation',
)

activate_token.expires = timezone.now() + timezone.timedelta(
minutes=settings.ACTIVATION_TOKENS['MINUTES']
)

# Setup the url for the activation button in the email
activation_url = FRONTEND_SETTINGS['ACTIVATION_URL'].replace(
"{{token}}",
activate_token.key
)

response_send_mail = services.send_mail(
[user],
{
"activation_url": activation_url,
"first_name": user.first_name,
"last_name": user.last_name,
},
"CONFIRM_SIGN_UP",
)

if response_send_mail:
content = {
'detail': _("The account was created but no email was "
"sent. If your account is not "
"activated, contact the administration."),
}
return Response(content, status=status.HTTP_200_OK)

return Response(status=status.HTTP_200_OK)


class UsersActivation(APIView):
"""
Expand Down
Empty file added log_management/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions log_management/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

from django.contrib import admin

from log_management.models import Log


class LogAdmin(admin.ModelAdmin):
list_display = ('source', 'level', 'error_code', 'message',)
search_fields = (
'message', 'additional_data', 'level', 'source', 'error_code',
'traceback_data')
list_filter = (
'level',
'source',
'error_code',
)


admin.site.register(Log, LogAdmin)
6 changes: 6 additions & 0 deletions log_management/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class LogManagementConfig(AppConfig):
name = 'log_management'
verbose_name = 'Log'
30 changes: 30 additions & 0 deletions log_management/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 2.2.5 on 2019-10-03 10:33

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Log',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('source', models.CharField(max_length=100, verbose_name='Source')),
('level', models.CharField(max_length=100, verbose_name='Level')),
('message', models.TextField(verbose_name='Message')),
('error_code', models.CharField(blank=True, max_length=100, null=True, verbose_name='Error Code')),
('additional_data', models.TextField(blank=True, null=True, verbose_name='Additional data')),
('traceback_data', models.TextField(blank=True, null=True, verbose_name='TraceBack')),
],
options={
'verbose_name': 'Log',
'verbose_name_plural': 'Logs',
},
),
]
Empty file.
69 changes: 69 additions & 0 deletions log_management/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import json
import traceback

from django.db import models

from django.utils.translation import ugettext_lazy as _


class Log(models.Model):

LEVEL_ERROR = 'ERROR'
LEVEL_INFO = 'INFO'
LEVEL_DEBUG = 'DEBUG'

source = models.CharField(
max_length=100,
verbose_name=_("Source"),
)

level = models.CharField(
max_length=100,
verbose_name=_("Level"),
)

message = models.TextField(
verbose_name=_("Message"),
)

error_code = models.CharField(
blank=True,
null=True,
max_length=100,
verbose_name=_("Error Code"),
)

additional_data = models.TextField(
blank=True,
null=True,
verbose_name=_("Additional data"),
)

traceback_data = models.TextField(
blank=True,
null=True,
verbose_name=_("TraceBack"),
)

class Meta:
verbose_name = _("Log")
verbose_name_plural = _("Logs")

@classmethod
def error(cls, source, message, error_code=None, additional_data=None):
traceback_data = ''.join(traceback.format_stack(limit=10))
new_log = Log(
level=cls.LEVEL_ERROR,
source=source,
message=message,
traceback_data=traceback_data
)

if error_code:
new_log.error_code = error_code
if additional_data:
new_log.additional_data = additional_data

new_log.save()

return new_log
Empty file.
23 changes: 23 additions & 0 deletions log_management/tests/test_logs_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json

from django.test import TestCase

from log_management.models import Log


class LogsTests(TestCase):
def test_create_error(self):
additional_data = {
'title': "Place exclusive pour 24h",
'default_from': 'from@email',
'user_email': 'user.email',
'merge_data': 'merge_data',
'template': 'reserved_place'
}
new_log = Log.error(
source='SENDING_BLUE_TEMPLATE',
message='err',
additional_data=json.dumps(additional_data)
)

self.assertEqual(new_log.source, 'SENDING_BLUE_TEMPLATE')
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pycodestyle==2.5.0
coveralls==1.8.2
responses==0.10.6
six==1.12.0
awscli==1.16.240
awscli==1.16.252
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Django==2.2.5
Django==2.2.6
djangorestframework==3.10.3
django-cors-headers==3.1.0
django-cors-headers==3.1.1
django-filter==2.2.0
coreapi==2.3.3
factory-boy==2.12.0
Pygments==2.4.2
Markdown==3.1.1
django-anymail==6.1.0
Pillow==6.1.0
Pillow==6.2.0
django-simple-history==2.7.3
djangorestframework-filters==0.11.1
python-decouple==3.1
Expand Down
43 changes: 43 additions & 0 deletions retirement/migrations/0019_auto_20191001_2257.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 2.2.5 on 2019-10-02 02:57

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('retirement', '0018_auto_20190906_1322'),
]

operations = [
migrations.AlterField(
model_name='historicalretreat',
name='carpool_url',
field=models.TextField(blank=True, null=True, verbose_name='Carpool URL'),
),
migrations.AlterField(
model_name='historicalretreat',
name='form_url',
field=models.TextField(blank=True, null=True, verbose_name='Form URL'),
),
migrations.AlterField(
model_name='historicalretreat',
name='review_url',
field=models.TextField(blank=True, null=True, verbose_name='Review URL'),
),
migrations.AlterField(
model_name='retreat',
name='carpool_url',
field=models.TextField(blank=True, null=True, verbose_name='Carpool URL'),
),
migrations.AlterField(
model_name='retreat',
name='form_url',
field=models.TextField(blank=True, null=True, verbose_name='Form URL'),
),
migrations.AlterField(
model_name='retreat',
name='review_url',
field=models.TextField(blank=True, null=True, verbose_name='Review URL'),
),
]
Loading

0 comments on commit 3db7c05

Please sign in to comment.