RecordViewBase

26 views
Skip to first unread message

ozwyzard

unread,
Jan 20, 2012, 11:45:42 PM1/20/12
to sprox
A work-around to the feature limitation described at
http://groups.google.com/group/sprox/browse_thread/thread/5b1a002ec4110e7d
would be as follows (it can be improved upon). See problem statement
embedded in the code (with regards to __headers__ and __add_fields__
in RecordViewBase).

Thanks,
ozwyzard


# *-* coding: utf-8 *-*


from tw.api import Widget
from sprox.recordviewbase import RecordViewBase
from sprox.widgets import *
from sprox.widgetselector import WidgetSelector
import re


def name2label(name):
"""
Yanked from tw.forms, which in turn was yanked from TGFastData.
"""
"""
Convert a column name to a Human Readable name.

Yanked from TGFastData
"""
# Create label from the name:
# 1) Convert _ to spaces
# 2) Convert CamelCase to Camel Case
# 3) Upcase first character of Each Word
# Note: I *think* it would be thread-safe to
# memoize this thing.
return ' '.join([s.capitalize() for s in
re.findall(r'([A-Z][a-z0-9]+|[a-z0-9]+|[A-Z0-9]+)',
name)])



class MyRecordViewWidget(Widget):
#template = "genshi:sprox.widgets.templates.recordViewTable"
#Remove Name & Value headers
template = "genshi:mysite.controllers.my.MyRecordViewTable"
params = ["entity"]



class MyRecordFieldWidget(Widget):
# ORIGINAL sprox code (does not honor the __headers__ field of
RecordView
# template = "genshi:sprox.widgets.tempaltes.recordField"
# params = ["field_name"]
#
# My slightly modified code.
# Pass a new argument called field_name_header for each field and
use it in the template.
template = "genshi:mysite.controllers.my.MyRecordField"
params = ["field_name", "field_name_header"]



class MyRecordViewWidgetSelector(WidgetSelector):
def select(self, field):
return MyRecordFieldWidget

class MyRecordViewBase(RecordViewBase):
'''
Problem Statement:
1. Sprox 0.6.10 did not support __add_fields__ for
RecordViewBase.
The fields in __add_fields__ were assigned a simple Widget
in _do_get_field_widgets()
which did not set the required dictionary keys that are
used in the associated html template.
2. Sprox 0.6.10 did not honor __headers__ field for
RecordViewBase.

Solution:
Implement a child class of RecordViewBase to:
1. Override/Enhance _do_get_field_widgets() to add the
key:value entries to the Widget for
'field_name' & 'field_name_header' for fields belonging to
__add_fields__
2. Override/Enhance _do_get_field_widget_args() to add a new
custom key:value pair to the
Values dict, to store the field_name_header field which is
then used in the html template

'''
__base_widget_type__ = MyRecordViewWidget
__widget_selector_type__ = MyRecordViewWidgetSelector


def _my_custom_get_field_name_header(self, field_name):
if hasattr(self, '__headers__'):
# if __headers__ specified and has the mapping get it
(default to db field_name)
fnh_value = self.__headers__.get(field_name,
name2label(field_name))
else:
# else use (db) field_name
fnh_value = name2label(field_name)
return fnh_value


def _do_get_field_widget_args(self, field_name, field):
args = super(MyRecordViewBase,
self)._do_get_field_widget_args(field_name, field)
args['field_name_header'] =
self._my_custom_get_field_name_header(field_name)
return args


def _do_get_field_widgets(self, fields):
widgets = super(MyRecordViewBase,
self)._do_get_field_widgets(fields)
if hasattr(self, '__add_fields__'):
for field_name in widgets.keys():
if field_name in self.__add_fields__:
field_name_header =
self._my_custom_get_field_name_header(field_name)
field_args = {'field_name':field_name,
'field_name_header': field_name_header}
widgets[field_name] =
MyRecordFieldWidget(field_name, **field_args)
return widgets



===================
File: MyRecordField.html
===================
<tr xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://
genshi.edgewall.org/"
class="${css_class}"
py:attrs='value_of("attrs", "")'
>
<td>
<b>$field_name_header</b>
</td>
<td> ${value[field_name]}
</td>
</tr>



=======================
File: MyRecordViewTable.html
=======================
<table xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://
genshi.edgewall.org/"
id="${id}"
class="${css_class}"
py:attrs='value_of("attrs", "")'
>
<py:for each="child in children">
${child.display(value)}
</py:for>
</table>

Chris Perkins

unread,
Jan 22, 2012, 4:30:28 PM1/22/12
to sp...@googlegroups.com
can you clone sprox-0.7 (http://bitbucket.org/percious/sprox-0.7) and
make your changes at a root level and send me a pull request? I get
what you are doing, but if you'd like it included in the next sprox,
we need to get your code into the root branch, which will be much
easier to do if you send me a pull request. It might also be a good
idea to run the tests and create any tests that would cover your
changes. Hit me up if you need help getting the test suite running.

cheers.
-chris

> --
> You received this message because you are subscribed to the Google Groups "sprox" group.
> To post to this group, send email to sp...@googlegroups.com.
> To unsubscribe from this group, send email to sprox+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sprox?hl=en.
>

ozwyzard

unread,
Jan 26, 2012, 8:52:04 PM1/26/12
to sprox
I will give it a shot. It may take me a few weeks to get to it. I
don't have a bitbucket account setup yet.

Thanks

On Jan 22, 1:30 pm, Chris Perkins <ch...@percious.com> wrote:
> can you clone sprox-0.7  (http://bitbucket.org/percious/sprox-0.7) and
> make your changes at a root level and send me a pull request?  I get
> what you are doing, but if you'd like it included in the next sprox,
> we need to get your code into the root branch, which will be much
> easier to do if you send me a pull request.  It might also be a good
> idea to run the tests and create any tests that would cover your
> changes.  Hit me up if you need help getting the test suite running.
>
> cheers.
> -chris
>
> On Fri, Jan 20, 2012 at 9:45 PM, ozwyzard <ozwyz...@gmail.com> wrote:
> > A work-around to the feature limitation described at
> >http://groups.google.com/group/sprox/browse_thread/thread/5b1a002ec41...

ozwyzard

unread,
Mar 25, 2012, 4:51:51 AM3/25/12
to sp...@googlegroups.com

For what it's worth, I have pushed the custom mods to the following repository:

https://bitbucket.org/wyzard/sprox-0.7/
> > To unsubscribe from this group, send email to sprox+unsubscribe@​googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/​group/sprox?hl=en.
> > To unsubscribe from this group, send email to sprox+unsubscribe@​googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/​group/sprox?hl=en.
Reply all
Reply to author
Forward
0 new messages