From 121e9cc6c56c0437977e83c6c2cb75277fc40b44 2021-10-06 11:29:14 From: Lance Edgar Date: 2021-10-06 11:29:14 Subject: [PATCH] Add `get_contact_emails()` and `should_restrict_contact_info()` for custorder batch --- diff --git a/rattail/batch/custorder.py b/rattail/batch/custorder.py index bfc8ef261805004b51ba3d6a0820b6acd2878ef5..e4a7aeae6beb67796650419735b3b4292f960f9d 100644 --- a/rattail/batch/custorder.py +++ b/rattail/batch/custorder.py @@ -72,6 +72,16 @@ class CustomerOrderBatchHandler(BatchHandler): 'new_order_requires_customer', default=False) + def should_restrict_contact_info(self): + """ + Returns a boolean indicating whether contact info should be + "restricted" - i.e. user can only choose from existing contact + info and cannot override by e.g. entering a new phone number. + """ + return self.config.getbool('rattail.custorders', + 'new_orders.restrict_contact_info', + default=False) + def assign_contact(self, batch, customer=None, person=None, **kwargs): """ Assign the customer and/or person "contact" for the order. @@ -108,6 +118,18 @@ class CustomerOrderBatchHandler(BatchHandler): session = self.app.get_session(batch) session.flush() + def get_contact_display(self, batch): + """ + Should return contact display text for the batch, + i.e. customer name. + """ + customer_required = self.new_order_requires_customer() + + if customer_required: + return six.text_type(batch.customer) + else: + return six.text_type(batch.person) + def get_contact_phones(self, batch): """ Retrieve all phone records on file for the batch contact, to @@ -140,6 +162,45 @@ class CustomerOrderBatchHandler(BatchHandler): 'preferred': phone.preference == 1, } + def get_contact_emails(self, batch): + """ + Retrieve all email records on file for the batch contact, to + be presented as options for user to choose from when making a + new order. + + Note that the default logic will exclude invalid email addresses. + """ + customer_required = self.new_order_requires_customer() + + emails = [] + if customer_required: + if batch.customer: + emails = batch.customer.emails + else: + if batch.person: + emails = batch.person.emails + + # exclude invalid + emails = [email for email in emails + if not email.invalid] + + return [self.normalize_email(email) + for email in emails] + + def normalize_email(self, email): + """ + Normalize the given email record to simple data dict, for + passing around via JSON etc. + """ + return { + 'uuid': email.uuid, + 'type': email.type, + 'address': email.address, + 'invalid': email.invalid, + 'preference': email.preference, + 'preferred': email.preference == 1, + } + def get_contact_notes(self, batch): """ Get extra "contact notes" which should be made visible to the diff --git a/rattail/settings.py b/rattail/settings.py index 0280a8ccd531296bd958a5e7952c8a126a804f28..51f998c74551799d4bcc6a8619fb4633f10d3c81 100644 --- a/rattail/settings.py +++ b/rattail/settings.py @@ -127,6 +127,17 @@ class rattail_custorders_new_order_requires_customer(Setting): name = 'new_order_requires_customer' data_type = bool +class rattail_custorders_new_orders_restrict_contact_info(Setting): + """ + If set, then user can only choose from existing contact info options, + for the customer/order. If *not* set, then user is allowed to enter + new/different contact info. + """ + group = "Customer Orders" + namespace = 'rattail.custorders' + name = 'new_orders.restrict_contact_info' + data_type = bool + ############################## # DataSync