Add logic for editing default phone/email in base master view

and refactor customer, vendor views to use it
This commit is contained in:
Lance Edgar 2018-02-22 12:27:08 -06:00
parent 630ffe0cf8
commit f2a60f683c
3 changed files with 82 additions and 35 deletions

View file

@ -209,32 +209,10 @@ class CustomersView(MasterView):
f.set_renderer('groups', self.render_groups) f.set_renderer('groups', self.render_groups)
f.set_readonly('groups') f.set_readonly('groups')
# TODO: something like this should be supported for default_email, default_phone def objectify(self, form, data):
# def after_edit(self, customer): customer = super(CustomersView, self).objectify(form, data)
# customer = self.objectify_contact(customer, data)
# if not self.is_readonly(): return customer
# address = self._deserialize()
# contact = self.parent.model
# if contact.emails:
# if address:
# email = contact.emails[0]
# email.address = address
# else:
# contact.emails.pop(0)
# elif address:
# email = contact.add_email_address(address)
#
# if not self.is_readonly():
# number = self._deserialize()
# contact = self.parent.model
# if contact.phones:
# if number:
# phone = contact.phones[0]
# phone.number = number
# else:
# contact.phones.pop(0)
# elif number:
# phone = contact.add_phone_number(number)
def render_default_email(self, customer, field): def render_default_email(self, customer, field):
if customer.emails: if customer.emails:

View file

@ -2233,6 +2233,32 @@ class MasterView(View):
obj = form.schema.objectify(data, context=form.model_instance) obj = form.schema.objectify(data, context=form.model_instance)
return obj return obj
def objectify_contact(self, contact, data):
if 'default_email' in data:
address = data['default_email']
if contact.emails:
if address:
email = contact.emails[0]
email.address = address
else:
contact.emails.pop(0)
elif address:
contact.add_email_address(address)
if 'default_phone' in data:
number = data['default_phone']
if contact.phones:
if number:
phone = contact.phones[0]
phone.number = number
else:
contact.phones.pop(0)
elif number:
contact.add_phone_number(number)
return contact
def save_form(self, form): def save_form(self, form):
form.save() form.save()

View file

@ -42,6 +42,12 @@ class VendorsView(MasterView):
model_class = model.Vendor model_class = model.Vendor
has_versions = True has_versions = True
labels = {
'id': "ID",
'default_phone': "Phone Number",
'default_email': "Default Email",
}
grid_columns = [ grid_columns = [
'id', 'id',
'name', 'name',
@ -56,8 +62,9 @@ class VendorsView(MasterView):
'special_discount', 'special_discount',
'lead_time_days', 'lead_time_days',
'order_interval_days', 'order_interval_days',
'phone', 'default_phone',
'email', 'default_email',
'orders_email',
'contact', 'contact',
] ]
@ -68,7 +75,6 @@ class VendorsView(MasterView):
g.filters['name'].default_verb = 'contains' g.filters['name'].default_verb = 'contains'
g.set_sort_defaults('name') g.set_sort_defaults('name')
g.set_label('id', "ID")
g.set_label('phone', "Phone Number") g.set_label('phone', "Phone Number")
g.set_label('email', "Email Address") g.set_label('email', "Email Address")
@ -77,22 +83,59 @@ class VendorsView(MasterView):
def configure_form(self, f): def configure_form(self, f):
super(VendorsView, self).configure_form(f) super(VendorsView, self).configure_form(f)
vendor = f.model_instance
f.set_label('id', "ID")
f.set_label('lead_time_days', "Lead Time in Days") f.set_label('lead_time_days', "Lead Time in Days")
f.set_label('order_interval', "Order Interval in Days") f.set_label('order_interval', "Order Interval in Days")
f.set_readonly('phone') # default_phone
f.set_label('phone', "Phone Number") f.set_renderer('default_phone', self.render_default_phone)
if not self.creating and vendor.phones:
f.set_default('default_phone', vendor.phones[0].number)
f.set_readonly('email') # default_email
f.set_label('email', "Email Address") f.set_renderer('default_email', self.render_default_email)
if not self.creating and vendor.emails:
f.set_default('default_email', vendor.emails[0].address)
# orders_email
f.set_renderer('orders_email', self.render_orders_email)
if not self.creating and vendor.emails:
f.set_default('orders_email', vendor.get_email_address(type_='Orders') or '')
f.set_readonly('contact') f.set_readonly('contact')
f.set_renderer('contact', self.render_contact) f.set_renderer('contact', self.render_contact)
def objectify(self, form, data):
vendor = super(VendorsView, self).objectify(form, data)
vendor = self.objectify_contact(vendor, data)
if 'orders_email' in data:
address = data['orders_email']
email = vendor.get_email(type_='Orders')
if address:
if email:
if email.address != address:
email.address = address
else:
vendor.add_email_address(address, type='Orders')
elif email:
vendor.emails.remove(email)
return vendor
def render_default_email(self, vendor, field):
if vendor.emails:
return vendor.emails[0].address
def render_orders_email(self, vendor, field):
return vendor.get_email_address(type_='Orders')
def render_default_phone(self, vendor, field):
if vendor.phones:
return vendor.phones[0].number
def render_contact(self, vendor, field): def render_contact(self, vendor, field):
person = vendor.contact person = vendor.contact
if not person: if not person: