I have an app with a form called ‘BookingEnquiries’. The form contains a datagrid with a repeating panel called 'rpEnquiries. The row template has components on it populated from the 'BookingEnquiries’table. The last component is a link called ‘lnkDelete’. Clicking this link should
(a) Delete the linked contact record IF it is linked to no other enquiry.
(b) Delete the enquiry record.
(c) Refresh the repeating panel.
Clicking the delete link publishes a message from the row template code with content = row id of enquiry to delete.
A handler in the main form code gets the row id from message.content then handles the processing as outlined above but weirdly the handler runs twice and the form does not refresh.
Code Samples:
BookingEnquiries code
from ._anvil_designer import BookingEnquiriesTemplate
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from ..common import publisher
class BookingEnquiries(BookingEnquiriesTemplate):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
self.rpEnquiries.items = app_tables.bookingenquiry.search(tables.order_by('eventdate', ascending=False))
publisher.subscribe(channel='Enquiries', subscriber = self, handler=self.HandleDelete)
# Any code you write here will run before the form opens.
@handle("btnNewEnquiry", "click")
def btnNewEnquiry_click(self, **event_args):
"""This method is called when the button is clicked"""
open_form('BookingEnquiryDetail','new',None,None)
@handle("radByEventDate", "clicked")
@handle('radByReceivedDate', 'clicked')
def radByEventDate_clicked(self, **event_args):
if self.radByEventDate.selected:
self.rpEnquiries.items = app_tables.bookingenquiry.search(tables.order_by('eventdate', ascending=False))
else:
self.rpEnquiries.items = app_tables.bookingenquiry.search(tables.order_by('datereceived', ascending=False))
def HandleDelete(self, message):
if message.title == 'Delete Enquiry':
enqid = message.content
alert('Enqiry Row ID = ' + str(enqid))
#Notification('Enqiry Row ID = ' + str(enqid))
enquirytodelete = app_tables.bookingenquiry.get_by_id(enqid)
if enquirytodelete is not None:
contacttomaybedelete = enquirytodelete['contactlink']
contactlinks = 0
eeee = app_tables.bookingenquiry.search()
for ee in eeee:
if ee['contactlink'] == contacttomaybedelete:
contactlinks += 1
Notification('Contact used ' + str(contactlinks) + ' times.').show()
if contactlinks < 2:
Notification('The contact ' + contacttomaybedelete['forename'] + ' ' + contacttomaybedelete['surname']+ ' will be deleted.').show()
contacttomaybedelete.delete()
else:
Notification('The contact ' + contacttomaybedelete['forename'] + ' ' + contacttomaybedelete['surname']+ ' will NOT be deleted.').show()
Notification('The enquiry ' + enquirytodelete['eventtitle'] + ' : ' + enquirytodelete['eventdate'].strftime('%d/%m/%y') + ' will be deleted.').show()
enquirytodelete.delete()
if self.radByEventDate.selected:
self.rpEnquiries.items = app_tables.bookingenquiry.search(tables.order_by('eventdate', ascending=False))
else:
self.rpEnquiries.items = app_tables.bookingenquiry.search(tables.order_by('datereceived', ascending=False))
repeating panel template code
from ._anvil_designer import RowTemplate3Template
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from ...common import publisher
class RowTemplate3(RowTemplate3Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run before the form opens.
@handle("lnkEditEnquiry", "click")
def lnkEditEnquiry_click(self, **event_args):
open_form('BookingEnquiryDetail', 'old', self.item['eventtitle'],self.item['eventdate'])
@handle("lnkDelete", "click")
def lnkDelete_click(self, **event_args):
enqid = self.item.get_id()
alert('Enquiry Row ID = ' + str(enqid))
publisher.publish(channel='Enquiries', title='Delete Enquiry', content=enqid)
Clone link:
Pass word to login to app = 12345678