class ShoppingCartListing( ContainerViewlet ): actions = ContainerViewlet.actions.copy() columns = [ column.SelectionColumn( lambda item: item.item_id, name="selection"), column.GetterColumn( title=_(u"Name"), getter=lineItemURL ), column.GetterColumn( title=_(u"Price"), getter=lineItemPrice ), ] selection_column = columns[0] template = ZopeTwoPageTemplateFile('templates/cart-listing.pt') formatter_factory = CartFormatter def __init__( self, *args, **kw): super( ShoppingCartListing, self ).__init__( *args, **kw ) for column in self.columns: if hasattr(column, 'title'): column.title = utranslate(domain='plonegetpaid', msgid=column.title, context=self.request) def getContainerContext( self ): return self.__parent__.cart def isOrdered( self, *args ): # shopping cart should not be ordered, so override this with False return False
def __init__(self, context, request): super(VersionLogView, self).__init__(context.__parent__, request) # table to display the versions history formatter = date.getLocaleFormatter(self.request, "dateTime", "short") # !+ note this breaks the previous sort-dates-as-strings-hack of # formatting dates, for all locales, as date.strftime("%Y-%m-%d %H:%M") # that, when sorted as a string, gives correct results. self.columns = [ column.SelectionColumn(lambda item: str(item.version_id), name="selection"), column.GetterColumn(title=_(u"version"), getter=lambda i, f: '<a href="%s">%d</a>' % ("%s/versions/obj-%d" % (f.url, i.version_id), i.version_id)), column.GetterColumn(title=_(u"manual"), getter=lambda i, f: i.manual), column.GetterColumn( title=_(u"modified"), getter=lambda i, f: formatter.format(i.change.date_active)), column.GetterColumn(title=_(u"by"), getter=lambda i, f: i.change.user_id), column.GetterColumn(title=_(u"message"), getter=lambda i, f: i.change.description), ] self.selection_column = self.columns[0]
class GDWShoppingCartListing(ShoppingCartListing): """ """ template = ZopeTwoPageTemplateFile('templates/cart-listing.pt') columns = [ column.SelectionColumn(lambda item: item.item_id, name="selection"), column.FieldEditColumn(_(u"Quantity"), 'edit', interfaces.ILineItem['quantity'], lambda item: item.item_id, name="Quantity"), column.GetterColumn(title=_(u"Name"), name="Name", getter=lineItemURL, cell_formatter=formatLinkCell), column.GetterColumn(title=_(u"Price"), name="Price", getter=lineItemPrice), column.GetterColumn(title=_(u"Total"), name="Total", getter=lineItemTotal), ] def update(self): for column in self.columns: if column.name == 'selection': continue if hasattr(column, 'title'): column.title = utranslate(domain='plonegetpaid', msgid=column.name, context=self.request) super(ShoppingCartListing, self).update()
class ShoppingCartListing(ContainerViewlet): actions = ContainerViewlet.actions.copy() columns = [ column.SelectionColumn(lambda item: item.item_id, name="selection"), column.FieldEditColumn(_(u"Quantity"), 'edit', interfaces.ILineItem['quantity'], lambda item: item.item_id), column.GetterColumn(title=_(u"Name"), getter=lineItemURL, cell_formatter=formatLinkCell), column.GetterColumn(title=_(u"Price"), getter=lineItemPrice), column.GetterColumn(title=_(u"Total"), getter=lineItemTotal), ] selection_column = columns[0] quantity_column = columns[1] template = ZopeTwoPageTemplateFile('templates/cart-listing.pt') formatter_factory = CartFormatter def __init__(self, *args, **kw): super(ShoppingCartListing, self).__init__(*args, **kw) for column in self.columns: if hasattr(column, 'title'): column.title = utranslate(domain='plonegetpaid', msgid=column.title, context=self.request) def getContainerContext(self): return self.__parent__.cart def isOrdered(self, *args): # shopping cart should not be ordered, so override this with False return False @form.action(_("Update"), condition="isNotEmpty") def handle_update(self, action, data): try: data = self.quantity_column.input(self.container.values(), self.request) self.form_reset = True self.quantity_column.update(self.container.values(), data) for item_id in self.container: if self.container[item_id].quantity == 0: del self.container[item_id] except: raise self.form_reset = True #reset the form data in the request for i in self.request.form.keys(): self.request.form.pop(i)
class GDWAllItems(AllItems): """ """ columns = [ column.SelectionColumn(lambda item: item.item_id, name="selection"), column.GetterColumn(title=_(u"Item Id"), getter=renderItemId), column.GetterColumn(title=_(u"Name"), getter=renderItemName), column.GetterColumn(title=_(u"Item Code"), getter=renderItemCode), column.GetterColumn(title=_(u"Price"), getter=renderItemCost), column.GetterColumn(title=_(u"Quantity"), getter=AttrColumn("quantity")), column.GetterColumn(title=_(u"Total"), getter=renderItemPrice), column.GetterColumn(title=_(u"Status"), getter=AttrColumn("fulfillment_state")), ]
class OrderContentsComponent( BaseOrderContentsComponent ): """ an item listing used to group items by workflow state and present relevant workflow actions """ # interface.implements( ) # template = ZopeTwoPageTemplateFile('templates/order-item-listing.pt') columns = [ column.SelectionColumn( lambda item: item.item_id, name="selection"), column.GetterColumn( title=_(u"Quantity"), getter=AttrColumn("quantity" ) ), column.GetterColumn( title=_(u"Item Id"), getter=renderItemId, cell_formatter=raw_cell_formatter ), column.GetterColumn( title=_(u"Name"), getter=renderItemName, cell_formatter=raw_cell_formatter ), column.GetterColumn( title=_(u"Price"), getter=renderItemCost ), # column.GetterColumn( title=_(u"Total"), getter=renderItemPrice ), # column.GetterColumn( title=_(u"Status"), getter=AttrColumn("fulfillment_state" ) ), ]
class RelationTableBaseViewlet(viewlet.FormViewlet): domain_model = None # domain model of the other end of this relationship property_name = None # property on context's domain model which denotes the relationship selection_column = column.SelectionColumn(lambda item: str(item.id), name="selection") results = () # any subclass is expected to populate as it will. form_fields = form.Fields() # these as well @property def form_name(self): n = self.property_name.title() return n.replace('_', ' ') @property def prefix(self): return self.property_name def setUpColumns(self): columns = core.setUpColumns(self.domain_model) return columns def setUpFormatter(self): """ setup table listing formatter """ columns = self.setUpColumns() formatter = table.StandaloneFullFormatter(self.context, self.request, self.results, prefix=self.prefix, columns=columns) formatter.cssClasses['table'] = 'yui-dt-table' return formatter def render_listing(self): """ render a listing of all the currently associated objects or search results. """ formatter = self.setUpFormatter() return formatter()
SearchColumns = [ column.GetterColumn(title="Id", getter=idLink), column.GetterColumn(title="First Name", getter=lambda p, f: p.first_name or ''), column.GetterColumn(title="Last Name", getter=lambda p, f: p.last_name or ''), column.GetterColumn(title="Email", getter=lambda p, f: p.email or ''), column.GetterColumn(title="Phone", getter=lambda p, f: p.phone_number or '') ] ListingColumns = list(SearchColumns) SelectionColumn = column.SelectionColumn(lambda item: str(item.person_id), name='selection') ListingColumns.insert(0, SelectionColumn) def searchFields(iface): fields = [] for name, field in schema.getFieldsInOrder(iface): if field.required: field = copy.deepcopy(field) field.required = False fields.append(field) return fields #################################
class OrderContentsComponent(core.ComponentViewlet): """ an item listing used to group items by workflow state and present relevant workflow actions """ interface.implements() template = ZopeTwoPageTemplateFile('templates/order-item-listing.pt') columns = [ column.SelectionColumn(lambda item: item.item_id, name="selection"), column.GetterColumn(title=_(u"Quantity"), getter=AttrColumn("quantity")), column.GetterColumn(title=_(u"Item Id"), getter=renderItemId), column.GetterColumn(title=_(u"Name"), getter=renderItemName, cell_formatter=formatLinkCell), column.GetterColumn(title=_(u"Price"), getter=renderItemCost), column.GetterColumn(title=_(u"Total"), getter=renderItemPrice), column.GetterColumn(title=_(u"Status"), getter=AttrColumn("fulfillment_state")), ] selection_column = columns[0] order = 10 # position in the viewlet stack states = None # tuple of item fulfillment states that we use to get items we display show_finance_states = () # which order finance states we display in show_fulfillment_states = ( ) # which order fulfillment states we display in collection_name = _(u"Contents") def render(self): return self.__of__(self.__parent__).template() def show(self, **kw): if self.show_fulfillment_states and kw['order_fulfillment_state']: return kw[ 'order_fulfillment_state'] in self.show_fulfillment_states if self.show_finance_states and kw['order_finance_state']: return kw['order_finance_state'] in self.show_finance_states if self.states: self.line_items = self.managerItemsByStates(self.states) if not self.line_items: return False return True def update(self): # we need a better way of binding multiple states and associated transitions, such # that we can chain them together. info = IWorkflowInfo(self.line_items[0]) transitions = info.getManualTransitionIds() self.actions = bindTransitions(self, transitions, collection=True) return super(OrderContentsComponent, self).update() def getSelected(self, action, data): selected = self.selection_column.getSelected(self.line_items, self.request) return selected def listing(self): for column in self.columns: if hasattr(column, 'title'): column.title = utranslate(domain='plonegetpaid', msgid=column.title, context=self.request) columns = self.columns formatter = table.StandaloneFullFormatter( self.context, self.request, self.line_items, prefix=self.prefix, visible_column_names=[c.name for c in columns], #sort_on = ( ('name', False) columns=columns) formatter.cssClasses['table'] = 'listing' return formatter() @staticmethod def makeGrouping(*states, **kw): kw.update({'states': states}) return type("Item%sContents" % states[0], (OrderContentsComponent, ), kw)
class Many2ManyEdit(RelationTableBaseViewlet): selection_column = column.SelectionColumn(lambda item: str(item.id), name="selection") state = 'listing' template = ViewPageTemplateFile('templates/relation-edit.pt') def setUpColumns(self): columns = super(Many2ManyEdit, self).setUpColumns() columns.insert(0, self.selection_column) return columns def update(self): # we capture state from the last processed action, so our next action # becomes available. self.state = self.request.get("%s.state" % self.property_name, self.state) # our previous state when we process searches is the add state, in order # to process the form values for the search action, we need to have search # widgets setup prior to the handler invocation, so do that here.. if self.state == 'add': self.form_fields = core.setUpFields(self.domain_model, mode='search') # if we're doing a listing prepopulate state before rendering so we # display empty results status, and for a delete do it eagerly so # we can check our condition if self.state in ('listing', 'delete'): self.results = getattr(self.context, self.property_name) super(Many2ManyEdit, self).update() # if our state changes to listing if not self.results and self.state == 'listing': self.results = getattr(self.context, self.property_name) def condition_search(self, action): return self.state == 'add' @form.action(u"Search", condition='condition_search') def handle_search(self, action, data): """ search the other end point of m2m relationship for rows to associate. """ self.state = 'search' d = core.constructQuery(self.form_fields, self.domain_model, data) context = proxy.removeSecurityProxy(self.context) mapper = orm.class_mapper(self.domain_model) instance_pkey = mapper.primary_key_from_instance pkey = mapper.primary_key[0] query = Session().query(self.domain_model) query = query.filter( rdb.not_( pkey.in_( [ob.id for ob in getattr(context, self.property_name)]))) if not d: self.results = query.all() return self.results = query.filter(*(d.values())).all() def condition_add(self, action): return self.state == 'listing' @form.action(u"Add", condition='condition_add') def handle_add(self, action, data): """ sends user to search form. """ self.state = 'add' session = Session() related_count = session.query(self.domain_model).count() if related_count > 20: self.form_fields = core.setUpFields(self.domain_model, mode='search') else: self.state = 'search' query = session.query(self.domain_model) context = proxy.removeSecurityProxy(self.context) collection = getattr(context, self.property_name) if collection: mapper = orm.class_mapper(self.domain_model) instance_pkey = mapper.primary_key_from_instance pkey = mapper.primary_key[0] query = query.filter( rdb.not_( pkey.in_([ instance_pkey(ob)[0] for ob in getattr(context, self.property_name) ]))) self.results = query.all() def condition_delete(self, action): return self.state == 'listing' and self.results @form.action(u"Delete", condition='condition_delete') def handle_delete(self, action, data): """ delete the selected items from the m2m relationship. """ # reset form state self.state = 'listing' # first we need to dereference the selection column values into # objects to disassociate. values = core.getSelectedObjects(self.selection_column, self.request, self.domain_model) if not values: self.status = "No %s Selected" % ( self.domain_model.__name__.title() + 's') return # remove objects from association association = getattr(self.context, self.property_name) for v in values: if v in association: association.remove(v) # pluralize properly if len(values) > 1: self.status = "%s Removed" % (self.domain_model.__name__.title() + 's') else: self.status = "%s Removed" % self.domain_model.__name__.title() def condition_associate(self, action): return self.state == 'search' @form.action(u"Associate", condition='condition_associate') def handle_associate(self, action, data): """ associate a search result with the context. """ # reset viewlet state to listing of associated objects self.state = 'listing' # first we need to dereference the selection column values into # objects to associate. values = core.getSelectedObjects(self.selection_column, self.request, self.domain_model) # add objects to the association association = proxy.removeSecurityProxy( getattr(self.context, self.property_name)) for v in values: if v not in association: association.append(v) # pluralize properly if len(values) > 1: self.status = "%s Associated" % ( self.domain_model.__name__.title() + 's') else: self.status = "%s Associated" % self.domain_model.__name__.title() # reset selection column self.form_reset = True def condition_cancel(self, action): return self.state != 'listing' @form.action(u"Cancel", condition='condition_cancel') def handle_cancel(self, action, data): """ cancel the current operation and go back to the view """ self.state = 'listing'