Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add CustomerNote
michaladf33c0de18f
diff --git a/saleor/account/migrations/0018_customernote.py b/saleor/account/migrations/0018_customernote.pynew file mode 100644index 0000000000..70056410ff--- /dev/null+++ b/saleor/account/migrations/0018_customernote.py@@ -0,0 +1,27 @@+# Generated by Django 2.0.3 on 2018-04-05 13:39++from django.conf import settings+from django.db import migrations, models+import django.db.models.deletion+++class Migration(migrations.Migration):++ dependencies = [+ ('core', '0001_initial'),+ ('account', '0017_auto_20180206_0957'),+ ]++ operations = [+ migrations.CreateModel(+ name='CustomerNote',+ fields=[+ ('basenote_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='core.BaseNote')),+ ('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notes', to=settings.AUTH_USER_MODEL)),+ ],+ options={+ 'ordering': ('date',),+ },+ bases=('core.basenote',),+ ),+ ]diff --git a/saleor/account/models.py b/saleor/account/models.pyindex 4334b240be..68c4535b4b 100644--- a/saleor/account/models.py+++ b/saleor/account/models.py@@ -1,3 +1,4 @@+from django.conf import settingsfrom django.contrib.auth.models import (AbstractBaseUser, BaseUserManager, PermissionsMixin)from django.db import models@@ -8,6 +9,7 @@ from django_countries.fields import Country, CountryFieldfrom phonenumber_field.modelfields import PhoneNumberFieldfrom .validators import validate_possible_number+from ..core.models import BaseNoteclass PossiblePhoneNumberField(PhoneNumberField):@@ -133,3 +135,11 @@ class User(PermissionsMixin, AbstractBaseUser):return '%s %s (%s)' % (address.first_name, address.last_name, self.email)return self.email+++class CustomerNote(BaseNote):+ customer = models.ForeignKey(+ settings.AUTH_USER_MODEL, related_name='notes', on_delete=models.CASCADE)++ class Meta:+ ordering = ('date', )diff --git a/saleor/dashboard/customer/forms.py b/saleor/dashboard/customer/forms.pyindex 0229456b77..4984b76ac9 100644--- a/saleor/dashboard/customer/forms.py+++ b/saleor/dashboard/customer/forms.py@@ -1,6 +1,7 @@from django import forms+from django.utils.translation import pgettext_lazy-from ...account.models import User+from ...account.models import User, CustomerNoteclass CustomerForm(forms.ModelForm):@@ -14,3 +15,16 @@ class CustomerForm(forms.ModelForm):class Meta:model = Userfields = ['email', 'is_active']+++class CustomerNoteForm(forms.ModelForm):+ class Meta:+ model = CustomerNote+ fields = ['content', 'is_public']+ widget = {+ 'content': forms.Textarea()}+ labels = {+ 'content': pgettext_lazy('Customer note', 'Note'),+ 'is_public': pgettext_lazy(+ 'Allow customers to see note toggle',+ 'Customer can see this note')}diff --git a/saleor/dashboard/customer/urls.py b/saleor/dashboard/customer/urls.pyindex 8e9f1db1e3..aab8248797 100644--- a/saleor/dashboard/customer/urls.py+++ b/saleor/dashboard/customer/urls.py@@ -8,4 +8,6 @@ urlpatterns = [url(r'^(?P<pk>[0-9]+)/$', views.customer_details, name='customer-details'),url(r'^(?P<pk>[0-9]+)/update/$', views.customer_edit,name='customer-update'),- url(r'^ajax/users/$', views.ajax_users_list, name='ajax-users-list')]+ url(r'^ajax/users/$', views.ajax_users_list, name='ajax-users-list'),+ url(r'^(?P<customer_pk>[0-9]+)/add-note/$',+ views.customer_add_note, name='customer-add-note')]diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.pyindex 5f5f8abbf4..0839a13d1a 100644--- a/saleor/dashboard/customer/views.py+++ b/saleor/dashboard/customer/views.py@@ -4,15 +4,16 @@ from django.contrib.auth.decorators import permission_requiredfrom django.db.models import Qfrom django.http import JsonResponsefrom django.shortcuts import get_object_or_404, redirect+from django.template.context_processors import csrffrom django.template.response import TemplateResponsefrom django.utils.translation import pgettext_lazy-from ...account.models import User+from ...account.models import User, CustomerNotefrom ...core.utils import get_paginator_itemsfrom ..emails import send_set_password_emailfrom ..views import staff_member_requiredfrom .filters import UserFilter-from .forms import CustomerForm+from .forms import CustomerForm, CustomerNoteForm@staff_member_required@@ -44,7 +45,10 @@ def customer_details(request, pk):'default_billing_address', 'default_shipping_address')customer = get_object_or_404(queryset, pk=pk)customer_orders = customer.orders.all()- ctx = {'customer': customer, 'customer_orders': customer_orders}+ notes = customer.notes.all()+ ctx = {+ 'customer': customer, 'customer_orders': customer_orders,+ 'notes': notes}return TemplateResponse(request, 'dashboard/customer/detail.html', ctx)@@ -93,3 +97,24 @@ def ajax_users_list(request):users = [{'id': user.pk, 'text': user.ajax_label} for user in queryset]return JsonResponse({'results': users})+++@staff_member_required+@permission_required('account.edit_user')+def customer_add_note(request, customer_pk):+ customer = get_object_or_404(User, pk=customer_pk)+ print(customer)+ note = CustomerNote(customer=customer, user=request.user)+ form = CustomerNoteForm(request.POST or None, instance=note)+ status = 200+ if form.is_valid():+ form.save()+ msg = pgettext_lazy(+ 'Dashboard message related to an customer', 'Added note')+ messages.success(request, msg)+ elif form.errors:+ status = 400+ ctx = {'customer': customer, 'form': form}+ ctx.update(csrf(request))+ template = 'dashboard/customer/modal/add_note.html'+ return TemplateResponse(request, template, ctx, status=status)diff --git a/templates/dashboard/customer/detail.html b/templates/dashboard/customer/detail.htmlindex 6377d55714..f847494522 100644--- a/templates/dashboard/customer/detail.html+++ b/templates/dashboard/customer/detail.html@@ -168,6 +168,36 @@</div></div></div>+ <div class="col s12 m6 p6 l12">+ <div class="card equal-height-on-med">+ <div class="card-content">+ <span class="card-title black-text">+ {% trans "Customer notes" context "Customer notes card title" %}+ </span>+ {% if notes %}+ <ul class="list">+ {% for note in notes %}+ <li class="list-item list-item--spaced">+ <small class="list-item-author">{{ note.user }} - {{ note.date }}</small>+ <p>{{ note.content }}</p>+ </li>+ {% endfor %}+ </ul>+ {% else %}+ <div>+ {% trans "There are no notes associated with this customer" context "Customer without notes message" %}+ </div>+ {% endif %}+ {% if perms.account.edit_user %}+ <div class="card-action">+ <a data-href="{% url "dashboard:customer-add-note" customer_pk=customer.pk %}" class="modal-trigger-custom btn-flat waves-effect" href="#base-modal">+ {% trans "Add note" context "Customer notes card action" %}+ </a>+ </div>+ {% endif %}+ </div>+ </div>+ </div></div></div></div>diff --git a/templates/dashboard/customer/modal/add_note.html b/templates/dashboard/customer/modal/add_note.htmlnew file mode 100644index 0000000000..7b79ddab9d--- /dev/null+++ b/templates/dashboard/customer/modal/add_note.html@@ -0,0 +1,20 @@+{% extends "dashboard/base_modal.html" %}++{% load i18n %}+{% load materializecss %}++{% block action_url %}+ {% url 'dashboard:customer-add-note' customer_pk=customer.pk %}+{% endblock %}++{% block title %}+ {% trans "Add note" context "Modal add note title" %}+{% endblock %}++{% block content %}+ {{ form|materializecss }}+{% endblock %}++{% block primary_action %}+ {% trans "Save note" context "Modal add note primary action" %}+{% endblock %}