Added proper logging for the whole store and mailing application

pull/3/head
mtyton 2023-07-26 00:31:10 +02:00
rodzic dcae7a433f
commit a8b03dd1a1
6 zmienionych plików z 51 dodań i 10 usunięć

Wyświetl plik

@ -21,6 +21,7 @@ from sentry_sdk.integrations.django import DjangoIntegration
# -> GlitchTip error reporting
sentry_sdk.init(
dsn=os.environ.get("SENTRY_DSN", ''),
integrations=[DjangoIntegration()],
auto_session_tracking=False,
traces_sample_rate=0
@ -212,3 +213,18 @@ EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD", '')
EMAIL_PORT = os.environ.get('EMAIL_PORT', 587)
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', 'artel-sklep@tepewu.pl')
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"root": {
"handlers": ["console"],
"level": "WARNING",
},
}

Wyświetl plik

@ -14,6 +14,7 @@ from search import views as search_views
handler404 = 'artel.views.my_custom_page_not_found_view'
urlpatterns = [
path("django-admin/", admin.site.urls),
path("admin/", include(wagtailadmin_urls)),

Wyświetl plik

@ -6,6 +6,7 @@ services:
- "1025:1025"
- "8025:8025"
comfy:
restart: always
depends_on:
- smtp-server
- db
@ -16,7 +17,7 @@ services:
ports:
- "8001:8000"
volumes:
# - ./:/app
- ./:/app
- media:/app/media
environment:
- SECRET_KEY

Wyświetl plik

@ -1,3 +1,5 @@
import logging
from typing import Any
from dataclasses import dataclass
@ -11,6 +13,9 @@ from django.core.mail import EmailMessage
from django.conf import settings
logger = logging.getLogger(__name__)
@dataclass
class Attachment:
name: str
@ -34,7 +39,11 @@ def send_mail(
message.content_subtype = 'html'
for attachment in attachments:
message.attach(attachment.name, attachment.content, attachment.contenttype)
return bool(message.send())
sent = bool(message.send())
if not sent:
logger.exception(f"Sending email to {to} with subject {subject} caused an exception")
return sent
class MailTemplate(models.Model):
@ -54,6 +63,10 @@ class MailTemplate(models.Model):
def load_and_process_template(self, context: dict|Context):
if not self.template:
logger.exception(
f"Template file is missing for template with "+
f"pk={self.pk}, template_name={self.template_name}"
)
raise FileNotFoundError("Template file is missing")
if isinstance(context, dict):
context = Context(context)

Wyświetl plik

@ -92,7 +92,6 @@ class SessionCart(BaseCart):
self.session.modified = True
def add_item(self, item_id: int, quantity: int) -> None:
# TODO - add logging
product = self.validate_and_get_product(item_id)
author = product.author
quantity = int(quantity)

Wyświetl plik

@ -2,6 +2,7 @@ import pdfkit
import datetime
import builtins
import uuid
import logging
from decimal import Decimal
from typing import (
@ -40,6 +41,9 @@ from mailings.models import (
)
logger = logging.getLogger(__name__)
class BaseImageModel(models.Model):
image = models.ImageField()
is_main = models.BooleanField(default=False)
@ -179,7 +183,10 @@ class ProductManager(models.Manager):
# There should be only one
if not products.count() <= 1:
raise ValidationError("There should be only one product with given set of params")
logger.exception(
f"There should be only one product with given set of params, detected: " +
f"{products.count()}, params: {params}, template: {template}"
)
product = products.first()
if not product:
@ -268,9 +275,8 @@ def validate_param(sender, **kwargs):
for pk in pk_set:
try:
param = ProductCategoryParamValue.objects.get(pk=pk).param
except ProductCategoryParamValue.DoesNotExist:
# TODO log this
...
except ProductCategoryParamValue.DoesNotExist as e:
logger.exception(f"Product param validation failed with exception: {str(e)}")
count = product_instance.params.filter(productparam__param_value__param=param).count()
if count >= 1:
errors.append(ValueError("Product param with this key already exists."))
@ -320,7 +326,10 @@ class OrderProductManager(models.Manager):
pks = []
for item in items:
if item["quantity"] < 1:
# TODO - logging
logger.exception(
f"This is not possible to add less than one item to Order, omitting item: "+
f"{item['product']}"
)
continue
pk = self.create(
@ -420,8 +429,9 @@ class OrderManager(models.Manager):
sent = self._send_notifications(order, author, customer_data, docs)
if not sent:
# TODO - store data temporarily
raise Exception("Error while sending emails")
logger.exception(
f"Error while sending emails, for order: {order.order_number}"
)
return Order.objects.filter(pk__in=orders_pks)
@ -435,6 +445,7 @@ class PaymentMethod(models.Model):
def __str__(self) -> str:
return self.name
class DeliveryMethod(models.Model):
name = models.CharField(max_length=255)
description = models.TextField(blank=True)