Skip to content

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

Update csv models

IKarbowiak98485eac35ff
diff --git a/saleor/csv/emails.py b/saleor/csv/emails.py
index 4c8a8f7b56..e9502a0517 100644
--- a/saleor/csv/emails.py
+++ b/saleor/csv/emails.py
@@ -21,7 +21,10 @@ EXPORT_TEMPLATES = {
def send_email_with_link_to_download_file(
export_file: "ExportFile", template_name: str
):
- recipient_email = export_file.created_by.email
+ user = export_file.user
+ if not user:
+ return
+ recipient_email = user.email
send_kwargs, ctx = get_email_context()
ctx["csv_link"] = build_absolute_uri(export_file.content_file.url)
send_templated_mail(
@@ -30,12 +33,15 @@ def send_email_with_link_to_download_file(
context=ctx,
**send_kwargs,
)
- events.export_file_sent_event(export_file=export_file, user=export_file.created_by)
+ events.export_file_sent_event(export_file=export_file, user=user)
@app.task
def send_export_failed_info(export_file: "ExportFile", template_name: str):
- recipient_email = export_file.created_by.email
+ user = export_file.user
+ if not user:
+ return
+ recipient_email = user.email
send_kwargs, ctx = get_email_context()
send_templated_mail(
template_name=EXPORT_TEMPLATES[template_name],
@@ -43,6 +49,4 @@ def send_export_failed_info(export_file: "ExportFile", template_name: str):
context=ctx,
**send_kwargs,
)
- events.export_failed_info_sent_event(
- export_file=export_file, user=export_file.created_by
- )
+ events.export_failed_info_sent_event(export_file=export_file, user=user)
diff --git a/saleor/csv/migrations/0001_initial.py b/saleor/csv/migrations/0001_initial.py
index ec13de7739..c0e59af068 100644
--- a/saleor/csv/migrations/0001_initial.py
+++ b/saleor/csv/migrations/0001_initial.py
@@ -1,4 +1,4 @@
-# Generated by Django 3.0.6 on 2020-06-05 07:00
+# Generated by Django 3.0.6 on 2020-06-09 07:31
import django.contrib.postgres.fields.jsonb
import django.db.models.deletion
@@ -14,6 +14,7 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
+ ("app", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
@@ -47,10 +48,20 @@ class Migration(migrations.Migration):
("updated_at", models.DateTimeField(auto_now=True)),
("content_file", models.FileField(null=True, upload_to="export_files")),
(
- "created_by",
+ "app",
models.ForeignKey(
+ null=True,
on_delete=django.db.models.deletion.CASCADE,
- related_name="jobs",
+ related_name="export_files",
+ to="app.App",
+ ),
+ ),
+ (
+ "user",
+ models.ForeignKey(
+ null=True,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="export_files",
to=settings.AUTH_USER_MODEL,
),
),
@@ -85,7 +96,7 @@ class Migration(migrations.Migration):
"Data export was completed successfully.",
),
("export_failed", "Data export failed."),
- ("export_deleted", "Export file was started."),
+ ("export_deleted", "Export file was deleted."),
(
"exported_file_sent",
"Email with link to download file was sent to the "
@@ -119,6 +130,7 @@ class Migration(migrations.Migration):
(
"user",
models.ForeignKey(
+ null=True,
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 d2cbd795a7..0a9dd53c0c 100644
--- a/saleor/csv/models.py
+++ b/saleor/csv/models.py
@@ -3,13 +3,19 @@ from django.db import models
from django.utils import timezone
from ..account.models import User
+from ..app.models import App
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)
+ user = models.ForeignKey(
+ User, related_name="export_files", on_delete=models.CASCADE, null=True
+ )
+ app = models.ForeignKey(
+ App, related_name="export_files", on_delete=models.CASCADE, null=True
+ )
content_file = models.FileField(upload_to="export_files", null=True)
@@ -23,5 +29,5 @@ class ExportEvent(models.Model):
ExportFile, related_name="events", on_delete=models.CASCADE
)
user = models.ForeignKey(
- User, related_name="export_csv_events", on_delete=models.CASCADE
+ User, related_name="export_csv_events", on_delete=models.CASCADE, null=True
)
diff --git a/saleor/csv/tasks.py b/saleor/csv/tasks.py
index 5c4b29f95a..dc1163d62b 100644
--- a/saleor/csv/tasks.py
+++ b/saleor/csv/tasks.py
@@ -18,10 +18,11 @@ def on_task_failure(self, exc, task_id, args, kwargs, einfo):
events.export_failed_event(
export_file=export_file,
- user=export_file.created_by,
+ user=export_file.user,
message=str(exc),
error_type=str(einfo.type),
)
+
send_export_failed_info(export_file, "export_failed")
@@ -32,7 +33,7 @@ def on_task_success(self, retval, task_id, args, kwargs):
export_file.status = JobStatus.SUCCESS
export_file.save(update_fields=["status", "updated_at"])
- events.export_success_event(export_file=export_file, user=export_file.created_by)
+ events.export_success_event(export_file=export_file, user=export_file.user)
@app.task(on_success=on_task_success, on_failure=on_task_failure)
diff --git a/saleor/csv/tests/test_emails.py b/saleor/csv/tests/test_emails.py
index d79b22c7b6..e3105fcc35 100644
--- a/saleor/csv/tests/test_emails.py
+++ b/saleor/csv/tests/test_emails.py
@@ -24,7 +24,7 @@ def test_send_email_with_link_to_download_file(
"domain": "mirumee.com",
"site_name": "mirumee.com",
}
- recipients = [export_file.created_by.email]
+ recipients = [export_file.user.email]
expected_call_kwargs = {
"context": ctx,
"from_email": site_settings.default_from_email,
@@ -41,7 +41,7 @@ def test_send_email_with_link_to_download_file(
assert ExportEvent.objects.filter(
export_file=export_file,
- user=export_file.created_by,
+ user=export_file.user,
type=ExportEvents.EXPORTED_FILE_SENT,
)
@@ -62,7 +62,7 @@ def test_send_export_failed_info(
"domain": "mirumee.com",
"site_name": "mirumee.com",
}
- recipients = [export_file.created_by.email]
+ recipients = [export_file.user.email]
expected_call_kwargs = {
"context": ctx,
"from_email": site_settings.default_from_email,
@@ -79,6 +79,6 @@ def test_send_export_failed_info(
assert ExportEvent.objects.filter(
export_file=export_file,
- user=export_file.created_by,
+ user=export_file.user,
type=ExportEvents.EXPORT_FAILED_INFO_SENT,
)
diff --git a/saleor/csv/tests/test_tasks.py b/saleor/csv/tests/test_tasks.py
index f816a14f9f..567352c6a0 100644
--- a/saleor/csv/tests/test_tasks.py
+++ b/saleor/csv/tests/test_tasks.py
@@ -25,7 +25,7 @@ def test_export_products_task(export_products_mock, export_file):
export_products_mock.called_once_with()
-@patch("saleor.csv.utils.export.send_export_failed_info")
+@patch("saleor.csv.tasks.send_export_failed_info")
def test_on_task_failure(send_export_failed_info_mock, export_file):
exc = Exception("Test")
task_id = "task_id"
@@ -49,9 +49,7 @@ def test_on_task_failure(send_export_failed_info_mock, export_file):
assert export_file.created_at
assert export_file.updated_at != previous_updated_at
export_failed_event = ExportEvent.objects.get(
- export_file=export_file,
- user=export_file.created_by,
- type=ExportEvents.EXPORT_FAILED,
+ export_file=export_file, user=export_file.user, type=ExportEvents.EXPORT_FAILED,
)
assert export_failed_event.parameters == {
"message": str(exc),
@@ -81,6 +79,6 @@ def test_on_task_success(export_file):
assert export_file.created_at
assert ExportEvent.objects.filter(
export_file=export_file,
- user=export_file.created_by,
+ user=export_file.user,
type=ExportEvents.EXPORT_SUCCESS,
)
diff --git a/saleor/csv/utils/export.py b/saleor/csv/utils/export.py
index 56d62a4350..dbe100905b 100644
--- a/saleor/csv/utils/export.py
+++ b/saleor/csv/utils/export.py
@@ -6,7 +6,7 @@ from django.utils import timezone
from ...product.models import Product
from .. import FileTypes
-from ..emails import send_email_with_link_to_download_file, send_export_failed_info
+from ..emails import send_email_with_link_to_download_file
from .products_data import get_export_fields_and_headers_info, get_products_data
if TYPE_CHECKING:
diff --git a/saleor/graphql/csv/filters.py b/saleor/graphql/csv/filters.py
index 138da13b4c..a75c9a7923 100644
--- a/saleor/graphql/csv/filters.py
+++ b/saleor/graphql/csv/filters.py
@@ -5,19 +5,19 @@ from ..core.types import FilterInputObjectType
from ..utils.filters import filter_by_query_param
-def filter_created_by(qs, _, value):
+def filter_user(qs, _, value):
user_fields = [
- "created_by__pk",
- "created_by__first_name",
- "created_by__last_name",
- "created_by__email",
+ "user__pk",
+ "user__first_name",
+ "user__last_name",
+ "user__email",
]
qs = filter_by_query_param(qs, value, user_fields)
return qs
class ExportFileFilter(BaseJobFilter):
- created_by = django_filters.CharFilter(method=filter_created_by)
+ user = django_filters.CharFilter(method=filter_user)
class ExportFileFilterInput(FilterInputObjectType):
diff --git a/saleor/graphql/csv/mutations.py b/saleor/graphql/csv/mutations.py
index b1ff4bbd82..f1581bf538 100644
--- a/saleor/graphql/csv/mutations.py
+++ b/saleor/graphql/csv/mutations.py
@@ -79,7 +79,7 @@ class ExportProducts(BaseMutation):
export_info = cls.get_export_info(input["export_info"])
file_type = input["file_type"]
- export_file = csv_models.ExportFile.objects.create(created_by=user)
+ export_file = csv_models.ExportFile.objects.create(user=user)
export_started_event(export_file=export_file, user=user)
export_products_task.delay(export_file.pk, scope, export_info, file_type)
diff --git a/saleor/graphql/csv/sorters.py b/saleor/graphql/csv/sorters.py
index b51903cb89..fc94c75656 100644
--- a/saleor/graphql/csv/sorters.py
+++ b/saleor/graphql/csv/sorters.py
@@ -7,7 +7,7 @@ class ExportFileSortField(graphene.Enum):
STATUS = ["status"]
CREATED_AT = ["created_at"]
UPDATED_AT = ["updated_at"]
- CREATED_BY = ["created_by__id"]
+ USER = ["user__id"]
@property
def description(self):
diff --git a/saleor/graphql/csv/tests/mutations/test_export_products.py b/saleor/graphql/csv/tests/mutations/test_export_products.py
index 04fcd95ae6..dc018cfda2 100644
--- a/saleor/graphql/csv/tests/mutations/test_export_products.py
+++ b/saleor/graphql/csv/tests/mutations/test_export_products.py
@@ -19,7 +19,7 @@ EXPORT_PRODUCTS_MUTATION = """
createdAt
updatedAt
url
- createdBy {
+ user {
email
}
}
@@ -82,7 +82,7 @@ def test_export_products_mutation(
assert not data["csvErrors"]
assert data["exportFile"]["id"]
assert export_file_data["createdAt"]
- assert export_file_data["createdBy"]["email"] == staff_api_client.user.email
+ assert export_file_data["user"]["email"] == staff_api_client.user.email
assert ExportEvent.objects.filter(
user=user, type=ExportEvents.EXPORT_PENDING
).exists()
@@ -133,7 +133,7 @@ def test_export_products_mutation_ids_scope(
assert not data["csvErrors"]
assert data["exportFile"]["id"]
assert export_file_data["createdAt"]
- assert export_file_data["createdBy"]["email"] == staff_api_client.user.email
+ assert export_file_data["user"]["email"] == staff_api_client.user.email
assert ExportEvent.objects.filter(
user=user, type=ExportEvents.EXPORT_PENDING
).exists()
@@ -198,7 +198,7 @@ def test_export_products_mutation_with_warehouse_and_attribute_ids(
assert not data["csvErrors"]
assert data["exportFile"]["id"]
assert export_file_data["createdAt"]
- assert export_file_data["createdBy"]["email"] == staff_api_client.user.email
+ assert export_file_data["user"]["email"] == staff_api_client.user.email
assert ExportEvent.objects.filter(
user=user, type=ExportEvents.EXPORT_PENDING
).exists()
diff --git a/saleor/graphql/csv/tests/queries/test_export_file.py b/saleor/graphql/csv/tests/queries/test_export_file.py
index b0b9501708..428fb64bc3 100644
--- a/saleor/graphql/csv/tests/queries/test_export_file.py
+++ b/saleor/graphql/csv/tests/queries/test_export_file.py
@@ -12,7 +12,7 @@ EXPORT_FILE_QUERY = """
createdAt
updatedAt
url
- createdBy{
+ user{
email
}
events{
@@ -44,7 +44,7 @@ def test_query_export_file(
assert data["createdAt"]
assert data["updatedAt"]
assert not data["url"]
- assert data["createdBy"]["email"] == staff_api_client.user.email
+ assert data["user"]["email"] == staff_api_client.user.email
assert len(data["events"]) == 1
event = data["events"][0]
assert event["date"]
@@ -75,7 +75,7 @@ def test_query_export_file_as_app(
assert data["createdAt"]
assert data["updatedAt"]
assert not data["url"]
- assert data["createdBy"]["email"] == export_file.created_by.email
+ assert data["user"]["email"] == export_file.user.email
assert len(data["events"]) == 1
event = data["events"][0]
assert event["date"]
diff --git a/saleor/graphql/csv/tests/queries/test_export_files.py b/saleor/graphql/csv/tests/queries/test_export_files.py
index ea8f1b583e..ec5e2105fa 100644
--- a/saleor/graphql/csv/tests/queries/test_export_files.py
+++ b/saleor/graphql/csv/tests/queries/test_export_files.py
@@ -19,7 +19,7 @@ FILTER_EXPORT_FILES_QUERY = """
createdAt
updatedAt
url
- createdBy{
+ user{
email
}
}
@@ -39,7 +39,7 @@ SORT_EXPORT_FILES_QUERY = """
updatedAt
createdAt
url
- createdBy{
+ user{
email
}
}
@@ -134,11 +134,11 @@ def test_filter_export_files_by_user(
is_active=True,
)
- export_file_list[1].created_by = second_staff_user
+ export_file_list[1].user = second_staff_user
export_file_list[1].save()
query = FILTER_EXPORT_FILES_QUERY
- variables = {"filter": {"createdBy": staff_user.email}}
+ variables = {"filter": {"user": staff_user.email}}
response = staff_api_client.post_graphql(
query, variables=variables, permissions=[permission_manage_products]
@@ -159,9 +159,9 @@ def test_sort_export_files_query_by_user(
is_active=True,
)
- ExportFile.objects.create(created_by=second_staff_user)
+ ExportFile.objects.create(user=second_staff_user)
query = SORT_EXPORT_FILES_QUERY
- variables = {"sortBy": {"field": "CREATED_BY", "direction": "DESC"}}
+ variables = {"sortBy": {"field": "USER", "direction": "DESC"}}
response = staff_api_client.post_graphql(
query,
@@ -172,13 +172,13 @@ def test_sort_export_files_query_by_user(
nodes = content["data"]["exportFiles"]["edges"]
assert len(nodes) == 2
- assert nodes[0]["node"]["createdBy"]["email"] == second_staff_user.email
+ assert nodes[0]["node"]["user"]["email"] == second_staff_user.email
def test_sort_export_files_query_by_created_at_date(
staff_api_client, export_file, permission_manage_products, staff_user
):
- second_export_file = ExportFile.objects.create(created_by=staff_user)
+ second_export_file = ExportFile.objects.create(user=staff_user)
second_export_file.created_at = export_file.created_at - datetime.timedelta(
minutes=10
)
@@ -207,7 +207,7 @@ def test_sort_export_files_query_by_updated_at_date(
)
export_file.save()
- second_export_file = ExportFile.objects.create(created_by=staff_user)
+ second_export_file = ExportFile.objects.create(user=staff_user)
second_export_file.updated_at = export_file.updated_at + datetime.timedelta(
minutes=10
)
diff --git a/saleor/graphql/csv/types.py b/saleor/graphql/csv/types.py
index c388ecf628..b17a6450a7 100644
--- a/saleor/graphql/csv/types.py
+++ b/saleor/graphql/csv/types.py
@@ -53,7 +53,7 @@ class ExportFile(CountableDjangoObjectType):
description = "Represents a job data of exported file."
interfaces = [graphene.relay.Node, Job]
model = models.ExportFile
- only_fields = ["id", "created_by", "url"]
+ only_fields = ["id", "user", "url"]
@staticmethod
def resolve_url(root: models.ExportFile, info):
@@ -63,12 +63,12 @@ class ExportFile(CountableDjangoObjectType):
return info.context.build_absolute_uri(content_file.url)
@staticmethod
- def resolve_created_by(root: models.ExportFile, info):
+ def resolve_user(root: models.ExportFile, info):
requestor = get_user_or_app_from_context(info.context)
- if requestor == root.created_by or requestor.has_perm(
+ if requestor == root.user or requestor.has_perm(
AccountPermissions.MANAGE_USERS
):
- return root.created_by
+ return root.user
raise PermissionDenied()
@staticmethod
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 14f089b4cb..42fa924555 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1776,7 +1776,7 @@ enum ExportEventsEnum {
type ExportFile implements Node & Job {
id: ID!
- createdBy: User!
+ user: User
status: JobStatusEnum!
createdAt: DateTime!
updatedAt: DateTime!
@@ -1799,14 +1799,14 @@ input ExportFileFilterInput {
createdAt: DateTimeRangeInput
updatedAt: DateTimeRangeInput
status: JobStatusEnum
- createdBy: String
+ user: String
}
enum ExportFileSortField {
STATUS
CREATED_AT
UPDATED_AT
- CREATED_BY
+ USER
}
input ExportFileSortingInput {
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 77c7c3f8df..e28d01e8ef 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -2189,7 +2189,7 @@ def allocations(order_list, stock):
@pytest.fixture
def export_file(staff_user):
- job = ExportFile.objects.create(created_by=staff_user)
+ job = ExportFile.objects.create(user=staff_user)
return job
@@ -2198,11 +2198,11 @@ def export_file_list(staff_user):
export_file_list = list(
ExportFile.objects.bulk_create(
[
- ExportFile(created_by=staff_user),
- ExportFile(created_by=staff_user,),
- ExportFile(created_by=staff_user, status=JobStatus.SUCCESS,),
- ExportFile(created_by=staff_user, status=JobStatus.SUCCESS),
- ExportFile(created_by=staff_user, status=JobStatus.FAILED,),
+ ExportFile(user=staff_user),
+ ExportFile(user=staff_user,),
+ ExportFile(user=staff_user, status=JobStatus.SUCCESS,),
+ ExportFile(user=staff_user, status=JobStatus.SUCCESS),
+ ExportFile(user=staff_user, status=JobStatus.FAILED,),
]
)
)
@@ -2241,6 +2241,6 @@ def export_event(export_file):
return ExportEvent.objects.create(
type=ExportEvents.EXPORT_FAILED,
export_file=export_file,
- user=export_file.created_by,
+ user=export_file.user,
parameters={"message": "Example error message"},
)