Skip to content

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

Add base definitions for customer events

NyanKiyoshie6a469c8453a
diff --git a/saleor/account/__init__.py b/saleor/account/__init__.py
index e69de29bb2..40492af34d 100755
--- a/saleor/account/__init__.py
+++ b/saleor/account/__init__.py
@@ -0,0 +1,43 @@
+from django.utils.translation import pgettext_lazy
+
+
+class CustomerEvents:
+ """The different customer event types."""
+
+ # Order related events
+ PLACED_ORDER = "placed_order" # created an order
+ NOTE_ADDED_TO_ORDER = "note_added_to_order" # added a note to one of their orders
+ DIGITAL_LINK_DOWNLOADED = "digital_link_downloaded" # downloaded a digital good
+
+ # Staff actions over customers events
+ CUSTOMER_DELETED = "customer_deleted" # staff user deleted a customer
+
+ CHOICES = [
+ (
+ PLACED_ORDER,
+ pgettext_lazy(
+ "Event from a customer that placed an order", "An order was placed"
+ ),
+ ),
+ (
+ NOTE_ADDED_TO_ORDER,
+ pgettext_lazy(
+ "Event from a customer that added a note to one of their orders",
+ "A note was added",
+ ),
+ ),
+ (
+ DIGITAL_LINK_DOWNLOADED,
+ pgettext_lazy(
+ "Event from a customer that downloaded an ordered digital good",
+ "A digital good was downloaded",
+ ),
+ ),
+ (
+ CUSTOMER_DELETED,
+ pgettext_lazy(
+ "Event from a staff user that deleted a customer",
+ "A customer was deleted",
+ ),
+ ),
+ ]
diff --git a/saleor/account/migrations/0027_customerevent.py b/saleor/account/migrations/0027_customerevent.py
new file mode 100644
index 0000000000..a91a5d2c4f
--- /dev/null
+++ b/saleor/account/migrations/0027_customerevent.py
@@ -0,0 +1,77 @@
+# Generated by Django 2.2.1 on 2019-05-09 10:37
+
+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 = [
+ ("order", "0070_drop_update_event_and_rename_events"),
+ ("account", "0026_user_avatar"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="CustomerEvent",
+ 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=[
+ ("PLACED_ORDER", "placed_order"),
+ ("NOTE_ADDED_TO_ORDER", "note_added_to_order"),
+ ("DIGITAL_LINK_DOWNLOADED", "digital_link_downloaded"),
+ ("CUSTOMER_DELETED", "customer_deleted"),
+ ],
+ max_length=255,
+ ),
+ ),
+ (
+ "parameters",
+ django.contrib.postgres.fields.jsonb.JSONField(
+ blank=True,
+ default=dict,
+ encoder=saleor.core.utils.json_serializer.CustomJsonEncoder,
+ ),
+ ),
+ (
+ "order",
+ models.ForeignKey(
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ to="order.Order",
+ ),
+ ),
+ (
+ "user",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="events",
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ ],
+ options={"ordering": ("date",)},
+ )
+ ]
diff --git a/saleor/account/models.py b/saleor/account/models.py
index a554ff8b6f..f892fa9d36 100644
--- a/saleor/account/models.py
+++ b/saleor/account/models.py
@@ -6,15 +6,19 @@ from django.contrib.auth.models import (
BaseUserManager,
PermissionsMixin,
)
+from django.contrib.postgres.fields import JSONField
from django.db import models
from django.db.models import Q, Value
from django.forms.models import model_to_dict
from django.utils import timezone
+from django.utils.timezone import now
from django.utils.translation import pgettext_lazy
from django_countries.fields import Country, CountryField
from phonenumber_field.modelfields import PhoneNumber, PhoneNumberField
from versatileimagefield.fields import VersatileImageField
+from ..core.utils.json_serializer import CustomJsonEncoder
+from . import CustomerEvents
from .validators import validate_possible_number
@@ -198,3 +202,26 @@ class CustomerNote(models.Model):
class Meta:
ordering = ("date",)
+
+
+class CustomerEvent(models.Model):
+ """Model used to store events that happened during the customer lifecycle."""
+
+ date = models.DateTimeField(default=now, editable=False)
+ type = models.CharField(
+ max_length=255,
+ choices=[
+ (type_name.upper(), type_name) for type_name, _ in CustomerEvents.CHOICES
+ ],
+ )
+
+ order = models.ForeignKey("order.Order", on_delete=models.SET_NULL, null=True)
+ parameters = JSONField(blank=True, default=dict, encoder=CustomJsonEncoder)
+
+ user = models.ForeignKey(User, related_name="events", on_delete=models.CASCADE)
+
+ class Meta:
+ ordering = ("date",)
+
+ def __repr__(self):
+ return f"{self.__class__.__name__}(type=%r, user=%r)" % (self.type, self.user)
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index e7280c7ce8..d81508ae80 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -20,7 +20,6 @@ from geolite2 import geolite2
from prices import MoneyRange
from versatileimagefield.image_warmer import VersatileImageFieldWarmer
-from ...account.models import User
from ...account.utils import get_random_avatar
from ...celeryconf import app
from ...core.i18n import COUNTRY_CODE_CHOICES
@@ -136,6 +135,8 @@ def serialize_decimal(obj):
def create_superuser(credentials):
+ from ...account.models import User
+
user, created = User.objects.get_or_create(
email=credentials["email"],
defaults={"is_active": True, "is_staff": True, "is_superuser": True},
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 97b41141f3..b09980d927 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -458,4 +458,4 @@ class OrderEvent(models.Model):
ordering = ("date",)
def __repr__(self):
- return "OrderEvent(type=%r, user=%r)" % (self.type, self.user)
+ return f"{self.__class__.__name__}(type=%r, user=%r)" % (self.type, self.user)