Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add ExportEvent model

IKarbowiak2028f1b072d0
diff --git a/saleor/csv/__init__.py b/saleor/csv/__init__.py
index e69de29bb2..0a20c36ad8 100644
--- a/saleor/csv/__init__.py
+++ b/saleor/csv/__init__.py
@@ -0,0 +1,19 @@
+class ExportEvents:
+ """The different csv events types."""
+
+ DATA_EXPORT_PENDING = "data_export_pending"
+ DATA_EXPORT_SUCCESS = "data_export_success"
+ DATA_EXPORT_FAILED = "data_export_failed"
+ DATA_EXPORT_DELETED = "data_export_deleted"
+ EXPORTED_FILE_SENT = "exported_file_sent"
+
+ CHOICES = [
+ (DATA_EXPORT_PENDING, "Data export was started."),
+ (DATA_EXPORT_SUCCESS, "Data export was completed successfully."),
+ (DATA_EXPORT_FAILED, "Data export failed."),
+ (DATA_EXPORT_DELETED, "Export file was started."),
+ (
+ EXPORTED_FILE_SENT,
+ "Email with link to download csv file was sent to the customer.",
+ ),
+ ]
diff --git a/saleor/csv/migrations/0002_exportevent.py b/saleor/csv/migrations/0002_exportevent.py
new file mode 100644
index 0000000000..1dbdca2f6f
--- /dev/null
+++ b/saleor/csv/migrations/0002_exportevent.py
@@ -0,0 +1,77 @@
+# Generated by Django 3.0.5 on 2020-05-07 07:06
+
+import django.contrib.postgres.fields.jsonb
+import django.db.models.deletion
+import django.utils.timezone
+from django.conf import settings
+from django.db import migrations, models
+
+import saleor.core.utils.json_serializer
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ("csv", "0001_initial"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="ExportEvent",
+ fields=[
+ (
+ "id",
+ models.AutoField(
+ auto_created=True,
+ primary_key=True,
+ serialize=False,
+ verbose_name="ID",
+ ),
+ ),
+ (
+ "date",
+ models.DateTimeField(
+ default=django.utils.timezone.now, editable=False
+ ),
+ ),
+ (
+ "type",
+ models.CharField(
+ choices=[
+ ("DATA_EXPORT_PENDING", "data_export_pending"),
+ ("DATA_EXPORT_SUCCESS", "data_export_success"),
+ ("DATA_EXPORT_FAILED", "data_export_failed"),
+ ("DATA_EXPORT_DELETED", "data_export_deleted"),
+ ("EXPORTED_FILE_SENT", "exported_file_sent"),
+ ],
+ max_length=255,
+ ),
+ ),
+ (
+ "parameters",
+ django.contrib.postgres.fields.jsonb.JSONField(
+ blank=True,
+ default=dict,
+ encoder=saleor.core.utils.json_serializer.CustomJsonEncoder,
+ ),
+ ),
+ (
+ "export_file",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="events",
+ to="csv.ExportFile",
+ ),
+ ),
+ (
+ "user",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="export_csv_events",
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ ],
+ ),
+ ]
diff --git a/saleor/csv/models.py b/saleor/csv/models.py
index cc71aaa243..34d5c75541 100644
--- a/saleor/csv/models.py
+++ b/saleor/csv/models.py
@@ -1,9 +1,32 @@
+from django.contrib.postgres.fields import JSONField
from django.db import models
+from django.utils import timezone
from ..account.models import User
from ..core.models import Job
+from ..core.utils.json_serializer import CustomJsonEncoder
+from . import ExportEvents
class ExportFile(Job):
created_by = models.ForeignKey(User, related_name="jobs", on_delete=models.CASCADE)
content_file = models.FileField(upload_to="csv_files", null=True)
+
+
+class ExportEvent(models.Model):
+ """Model used to store events that happened during the export file lifecycle."""
+
+ date = models.DateTimeField(default=timezone.now, editable=False)
+ type = models.CharField(
+ max_length=255,
+ choices=[
+ (type_name.upper(), type_name) for type_name, _ in ExportEvents.CHOICES
+ ],
+ )
+ parameters = JSONField(blank=True, default=dict, encoder=CustomJsonEncoder)
+ export_file = models.ForeignKey(
+ ExportFile, related_name="events", on_delete=models.CASCADE
+ )
+ user = models.ForeignKey(
+ User, related_name="export_csv_events", on_delete=models.CASCADE
+ )