I'm trying to get a link (an HTML anchor tag) to display as the contents 
of a cell in a ToscaWidgets DataGrid widget, and came across the email 
discussion on the topic from a year ago, and have attempted to implement 
its recommendations.

On Sep 26 2006, 6:06 pm, Alberto Valverde <[EMAIL PROTECTED]> wrote:
 > You can define a function to process a field and return any 
 > ElementTree instance you feel like. For example:
 >
 > from elementtree import ElementTree as ET
 >
 > def makeLink(obj):
 >      link= ET.Element('a', href=obj.url)
 >      link.text = obj.title
 >      returnlink
 >
 > Then when you define the grid's fields just place it there:
 >
 > fields = [
 >         ('Date created', 'date_created'),
 >         ('Title', makeLink),
 >         ....
 >         ]
 >
 > Your grid will have links with the object's title pointing to the 
 > object's url.

I've got most of the above-recommended setup working, but instead of a 
link being displayed I get:

<Element a at 21816d0>

How can I get the Element instance to render?  The code I'm using is below.

    def makeProjectLink(self, row): # row is a tuple whose first element 
is a model.Project instance
        if row[0] != 'Totals':
            link = Element('a', href='/project/%s' % row[0].id)
            link.text = row[0].name
            return link
        else: return row[0]

    @expose(template="projects.templates.projects_grid")
    def projects_grid(self):
        '''Displays list of projects in a grid'''
        # TODO: Modify the projects template so it can use a grid
        projectList = []
        # Calculate totals for all projects
        hours = '%.1f' % sum([(work.finish-work.start).seconds/(60*60.0) 
for work in model.WorkDone.select()])
        nonBidReceivable = 0.0
        for project in 
model.Project.select(model.Project.q.bidAmount==0.00):
            nonBidReceivable += self.getNonBidReceivable(project)
        bidReceivable = float(sum([project.bidAmount for project in 
model.Project.select(model.Project.q.bidAmount>0.0)]))
        receivable = nonBidReceivable+bidReceivable
        paid = float(sum([payment.amount for payment in 
model.ClientPayment.select()]))
        balance = receivable-paid
        contractorsPayable = sum([bill.amount for bill in 
model.ContractorBill.select()])
        contractorsPaid = sum([payment.amount for payment in 
model.ContractorPayment.select()])
        contractorBalance = contractorsPayable-contractorsPaid
        purchases = sum([item.cost for item in 
model.ItemPurchased.select()])
        estimatedProfit = 
float(receivable)-float(contractorsPayable)-float(purchases)
        currentProfit = float(paid)-float(contractorsPaid)-float(purchases)
        projectList.append(('Totals',hours,'$%.2f' % receivable,'$%.2f' 
% paid,
                            '$%.2f' % balance,'$%.2f' % contractorsPayable,
                            '$%.2f' % contractorsPaid,'$%.2f' % 
contractorBalance,
                            '$%.2f' % purchases,'$%.2f' % estimatedProfit,
                            '$%.2f' % currentProfit))
        # Calculate totals by project
        projects=model.Project.select(orderBy=model.Project.q.name)
        for project in projects:
            hours = '%.1f' % 
sum([(work.finish-work.start).seconds/(60*60.0) for work in 
project.workDone])
            nonBidReceivable = self.getNonBidReceivable(project)
            bidReceivable = project.bidAmount
            receivable = nonBidReceivable+float(bidReceivable)
            paid = float(sum([payment.amount for payment in 
project.clientPayments]))
            balance = receivable-paid
            contractorsPayable = sum([bill.amount for bill in 
project.contractorBills])
            contractorsPaid = sum([payment.amount for payment in 
project.contractorPayments])
            contractorBalance = contractorsPayable-contractorsPaid
            purchases = sum([item.cost for item in project.itemsPurchased])
            estimatedProfit = 
float(receivable)-float(contractorsPayable)-float(purchases)
            currentProfit = 
float(paid)-float(contractorsPaid)-float(purchases)
            # display('text', name)
            projectList.append((project,
                                hours,'$%.2f' % receivable,'$%.2f' % paid,
                                '$%.2f' % balance,'$%.2f' % 
contractorsPayable,
                                '$%.2f' % contractorsPaid,'$%.2f' % 
contractorBalance,
                                '$%.2f' % purchases,'$%.2f' % 
estimatedProfit,
                                '$%.2f' % currentProfit))
        # Construct DataGrid
        DataGrid = tw.widgets.forms.DataGrid
        Column = tw.widgets.forms.datagrid.Column
        projectGrid = DataGrid(fields=[ ('Project', self.makeProjectLink),
                                        ('Hours', lambda row: row[1]),
                                        ('Receivable', lambda row: row[2]),
                                        ("Rec'd", lambda row: row[3]),
                                        ('Bal. due', lambda row: row[4]),
                                        ("Cont's payable", lambda row: 
row[5]),
                                        ("Cont's paid", lambda row: row[6]),
                                        ("Cont's due", lambda row: row[7]),
                                        ('Purchases', lambda row: row[8]),
                                        ('Est. Profit', lambda row: row[9]),
                                        ('Curr. Profit', lambda row: 
row[10])
                                    ],
                            default = projectList)
        return dict(projectGrid=projectGrid)

The template contains just the following:

${projectGrid.display()}

Tim Black

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to