ZEXP Z¯((U OFS.FolderqUFolderqtqNt.}q(U examples.cssq(U ^q(U OFS.ImageqUFileqttQUidq UExamplesq U Navigationq (U _q (hUFolderq ttQU_objectsq(}q(U meta_typeqU Page Templateqh U index_htmlqu}q(hUFileqh hu}q(hUFolderqh U Navigationqu}q(U meta_typeqUFolderqUidqU GuestBookqu}q(U meta_typeqUFolderqUidq U FileLibraryq!u}q"(U meta_typeq#UFolderq$Uidq%U ShoppingCartq&utU__ac_local_roles__q'}q(U_ownerq)(]q*U acl_usersq+aUadminq,th(U \q-(U'Products.PageTemplates.ZopePageTemplateq.UZopePageTemplateq/ttQh!(U ]q0(hUFolderq1ttQUtitleq2UExample Applicationsq3h&(U [q4(hUFolderq5ttQh(U `q6(hUFolderq7ttQu. ^;((U OFS.ImageqUFileqtqNt.}q(UsizeqMSUdataqTSH1, H2, H3, H4, H5, H6 { font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; color: #6699CC; } body, .normal { background: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; } .small{ background: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9pt; } .grey{ background: #DDDDDD; } .white{ background: #FFFFFF; } .bold{ font-weight:bold; } .normal{ font-weight:normal; } .message{ background: #FFFFAA; border-width: thin; border-style: solid; padding: 10pt; font-weight:bold; } qU_EtagSupport__etagqU ts05767609.14q U__name__q U examples.cssq Utitleq UU content_typeq Utext/cssqU preconditionqUU__ac_local_roles__q}qUamosq]qUOwnerqasu. _»((U OFS.FolderqUFolderqtqNt.}q(UidqU NavigationqU__ac_local_roles__q}qUamosq ]q UOwnerq asUBirdsq (U pq (hUFolderqttQU_objectsq(}q(U meta_typeqUScript (Python)qhUbreadcrumbs.pyqu}q(hUFolderqhUMammalsqu}q(hhhh u}q(hhhUReptilesqu}q(U meta_typeqUScript (Python)qUidqU siblings.pyqu}q(hU Page Templateq hU index_htmlq!uth(U mq"(hUFolderq#ttQh(U rq$(U#Products.PythonScripts.PythonScriptq%U PythonScriptq&ttQU index_htmlq'(U qq((U'Products.PageTemplates.ZopePageTemplateq)UZopePageTemplateq*ttQh(U oq+(hUFolderq,ttQU siblings.pyq-(U nq.(h%U PythonScriptq/ttQUtitleq0UAutomatic Navigation Devicesq1u. \((U'Products.PageTemplates.ZopePageTemplateqUZopePageTemplateqtqNt.}q(UexpandqKUidqU index_htmlqU__ac_local_roles__q}q U _bind_namesq (cShared.DC.Scripts.Bindings NameAssignments q o}q U_asgnsq }qU name_subpathqUtraverse_subpathqssbU_textqTÐ The document title or folder title or the example title

The title

Welcome to Zope. We've collected some example applications that demonstrate simple Zope features. You can examine how these applications work by looking at them in the management interface. If you'd like to contribute an example application to add to this collection, write to us. Enjoy!

You can delete the Examples folder if you wish. You can also reload the examples by importing the import/Examples.zexp file.

qU content_typeqU text/htmlqUtitleqUu. ]’((U OFS.FolderqUFolderqtqNt.}q(UidqU FileLibraryqU__ac_local_roles__q}qUamosq ]q UOwnerq asU_objectsq (}q (U meta_typeqU Page TemplateqhU index_htmlqu}q(hUFolderqhUFilesqu}q(hUScript (Python)qhUgetFilesqu}q(hhhUgetLinkqu}q(hhhUgetSizequ}q(hhhUaddFilequtUgetLinkq(U iq(U#Products.PythonScripts.PythonScriptqU PythonScriptq ttQUgetSizeq!(U jq"(hU PythonScriptq#ttQU index_htmlq$(U lq%(U'Products.PageTemplates.ZopePageTemplateq&UZopePageTemplateq'ttQh(U kq((hU PythonScriptq)ttQUtitleq*U File Libraryq+h(U hq,(hU PythonScriptq-ttQh(U gq.(hUFolderq/ttQu. [¬((U OFS.FolderqUFolderqtqNt.}q(UaddItemsq(U fq(U#Products.PythonScripts.PythonScriptqU PythonScriptqttQUidq U ShoppingCartq U__ac_local_roles__q }q Uamosq ]qUOwnerqasU_objectsq(}q(U meta_typeqUScript (Python)qh U currentItemsqu}q(hhh UgetItemqu}q(hhh UgetTotalqu}q(hU Page Templateqh U index_htmlqu}q(U meta_typeqUScript (Python)qUidqU deleteItemsq u}q!(hhhhuth(U cq"(hU PythonScriptq#ttQU index_htmlq$(U eq%(U'Products.PageTemplates.ZopePageTemplateq&UZopePageTemplateq'ttQh(U dq((hU PythonScriptq)ttQUtitleq*U Shopping Cartq+h (U bq,(hU PythonScriptq-ttQh(U aq.(hU PythonScriptq/ttQu. `ž((U OFS.FolderqUFolderqtqNt.}q(UidqU GuestBookqU__ac_local_roles__q}qUamosq ]q UOwnerq asU_objectsq (}q (U meta_typeqU Page TemplateqhU index_htmlqu}q(hUFolderqhUEntriesqu}q(hhhU addEntry.htmlqu}q(U meta_typeqUScript (Python)qUidqUaddEntryqu}q(hhhU formatEntryqu}q(hhhU getEntriesquth(U vq(hUFolderq ttQU index_htmlq!(U wq"(U'Products.PageTemplates.ZopePageTemplateq#UZopePageTemplateq$ttQh(U xq%(h#UZopePageTemplateq&ttQUtitleq'U Guest Bookq(h(U tq)(U#Products.PythonScripts.PythonScriptq*U PythonScriptq+ttQh(U sq,(h*U PythonScriptq-ttQh(U uq.(h*U PythonScriptq/ttQu. p((U OFS.FolderqUFolderqtqNt.}q(UidqUBirdsqU__ac_local_roles__q}qUamosq ]q UOwnerq asUPigeonsq (U q (hUFolderqtqtqQU_objectsq(}q(U meta_typeqUFolderqhUCrowsqu}q(hhhh utUtitleqUh(U ~q(hUFolderqtqtqQu. mr((U OFS.FolderqUFolderqtqNt.}q(UtitleqUUidqUReptilesqU__ac_local_roles__q}q Uamosq ]q UOwnerq asu. r œ((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_argcountq KU co_varnamesq (U _getitem_q U_getitemq U _getattr_q U_getattrqUProducts.PythonScripts.standardqU html_quoteqUlinksqUcontextqUparentqtubU_paramsqUUidqUbreadcrumbs.pyqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq }q!(Uname_containerq"U containerq#U name_subpathq$Utraverse_subpathq%U name_m_selfq&Uscriptq'U name_contextq(Ucontextq)usbU func_defaultsq*NU_codeq+Tðcsd„ZdS(s° This script creates breadcrumb style navigation links. It walks the REQUEST.PARENTS list of parents and creates a hyperlink for each parent. It stops at the Examples folder. c sñt}t}ddkl} g} x¢|||tdƒdƒdd…ƒd rt}| |dƒdd |||d ƒƒƒ|||d ƒƒƒfƒ ||d ƒƒd joPnq\W|d d ƒ|ƒSdS(s° This script creates breadcrumb style navigation links. It walks the REQUEST.PARENTS list of parents and creates a hyperlink for each parent. It stops at the Examples folder. (s html_quotesREQUESTsPARENTSiNisinserts%ss absolute_urlsgetIdsExampless/sjoin( s _getitem_s_getitems _getattr_s_getattrsProducts.PythonScripts.standards html_quoteslinksscontextsparent( s _getitem_s_getitems _getattr_s_getattrsProducts.PythonScripts.standards html_quoteslinksscontextsparent((sScript (Python)sbreadcrumbs.pys 95 N(sbreadcrumbs.py(sbreadcrumbs.py((sScript (Python)ssq,U_bodyq-Tñ""" This script creates breadcrumb style navigation links. It walks the REQUEST.PARENTS list of parents and creates a hyperlink for each parent. It stops at the Examples folder. """ from Products.PythonScripts.standard import html_quote links=[] for parent in context.REQUEST.PARENTS[1:]: links.insert(0, """%s""" % \ (html_quote(parent.absolute_url()), html_quote(parent.getId()))) if parent.getId() == "Examples": break return "/".join(links) q.U Script_magicq/KUwarningsq0)Uerrorsq1)U Python_magicq2U*ë q3u. q q((U'Products.PageTemplates.ZopePageTemplateqUZopePageTemplateqtqNt.}q(UexpandqKUidqU index_htmlqU__ac_local_roles__q}q Uamosq ]q UOwnerq asU_textq T% The document title or container title

breadcrumbs navigation

The title

This example shows how to create simple automatic navigation devices. Take a look at the Navigation folder to see how it was built. The automatically created links on this page allow you to traverse a collection of folders and sub-folders.

Current Folder: Folder ID

Parent Links

The links you see across the top of the page are called breadcrumbs. They show you where you are in the site and let you navigate up the site tree. They are created with a Python-based script, breadcrumbs.py.

Child Links

These automatic links allow you to navigate deeper into the site. These links are created with a Python expression in a Page Template. You could also use a Python-based Script to create the links.

You could customize these links to only show certain sub-folders, rather than listing all sub-folders.

There are no sub-folders here.

Sibling Links

This navigation device allows you to go to sibling folders. A sibling folder is one that is in the same parent as the current folder. The list of siblings is generated by the siblings.py script.

There are no sibling folders here.

qU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}qU name_subpathqUtraverse_subpathqssbUtitleqUU content_typeqU text/htmlqu. o:((U OFS.FolderqUFolderqtqNt.}q(UidqUMammalsqU__ac_local_roles__q}qUamosq ]q UOwnerq asUPrimatesq (U }q (hUFolderqtqtqQU_objectsq(}q(U meta_typeqUFolderqhh u}q(hhhUBatsqu}q(hhhUWhalesquth(U |q(hUFolderqtqtqQUtitleqUh(U {q(hUFolderqtq tq!Qu. ne((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (U _getattr_q U_getattrq Ucontextq Ufoldersq tU co_argcountqKubU_paramsqUUidqU siblings.pyqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}q(Uname_containerqU containerqU name_subpathqUtraverse_subpathq U name_m_selfq!Uscriptq"U name_contextq#Ucontextq$usbU func_defaultsq%NU_codeq&T3csd„ZdS(s/ This script returns a list of sibling folders csRt}d||tdƒdƒdƒ}||dƒtƒ|SdS(s/ This script returns a list of sibling folders s aq_parents objectValuessFoldersremoveN(s _getattr_s_getattrscontextsfolders(s _getattr_s_getattrscontextsfolders((sScript (Python)s siblings.pys $N(s siblings.py(s siblings.py((sScript (Python)ssq'U_bodyq(UÏ""" This script returns a list of sibling folders """ # get my parent's children folders=context.aq_parent.objectValues('Folder') # remove me from the list of children folders.remove(context) return folders q)U Script_magicq*KUwarningsq+)Uerrorsq,)U Python_magicq-U*ë q.u. iT((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (Unew_sortq Usortq Ureverseq U _getattr_q U_getattrqUcontextqUurlqU new_reverseqtU co_argcountqKubU_paramsqUnew_sort, sort, reverseqUidqUgetLinkqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq }q!(Uname_containerq"U containerq#U name_subpathq$Utraverse_subpathq%U name_contextq&Ucontextq'U name_m_selfq(Uscriptq)usbU func_defaultsq*NU_codeq+TÆcsd„ZdS(sA Returns a URL which gives sorting commands in the query string. c sjt}d||tdƒdƒ}||jo d}n | } d|||fSdS(sA Returns a URL which gives sorting commands in the query string. sREQUESTsURL0is%s?sort=%s&reverse:int=%sN(s _getattr_s_getattrscontextsurlsnew_sortssorts new_reversesreverse(snew_sortssortsreverses _getattr_s_getattrscontextsurls new_reverse((sScript (Python)sgetLinks   N(sgetLink(sgetLink((sScript (Python)ssq,U_bodyq-Uì""" Returns a URL which gives sorting commands in the query string. """ url=context.REQUEST.URL0 if new_sort != sort: new_reverse=0 else: new_reverse=not reverse return "%s?sort=%s&reverse:int=%s" % (url, new_sort, new_reverse) q.U Script_magicq/KUwarningsq0)Uerrorsq1)U Python_magicq2U*ë q3u. j(((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (Ubytesq Umegsq UktU co_argcountq KubU_paramsq UbytesqUidqUgetSizeqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}q(Uname_containerqU containerqU name_subpathqUtraverse_subpathqU name_contextq Ucontextq!U name_m_selfq"Uscriptq#usbU func_defaultsq$NU_codeq%Tcsd„ZdS(s. Format a number of bytes as bytes, K, or MB. csqd|d}|djod|Sn|d}|djo d|Sn d|SdS(s. Format a number of bytes as bytes, K, or MB. f 1048576.0is%.2f MBf1024.0s%d Ks%d bytesN(sbytessmegssk(sbytessmegssk((sScript (Python)sgetSizes  N(sgetSize(sgetSize((sScript (Python)ssq&U_bodyq'UÀ""" Format a number of bytes as bytes, K, or MB. """ megs=bytes / 1048576.0 if megs > 1: return "%.2f MB" % megs k=bytes / 1024.0 if k > 1: return "%d K" % k return "%d bytes" % bytes q(U Script_magicq)KUwarningsq*)Uerrorsq+)U Python_magicq,U*ë q-u. l‹((U'Products.PageTemplates.ZopePageTemplateqUZopePageTemplateqtqNt.}q(UexpandqKUidqU index_htmlqU__ac_local_roles__q}q Uamosq ]q UOwnerq asU _bind_namesq (cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}qU name_subpathqUtraverse_subpathqssbU_textqT? The document title or folder title or the example title

The title

This example shows how to create a simple file library. It demonstrates batching and sorting. You can click on the column names to sort the files. If you click twice on a column then the files are sorted in reverse order. If you upload more than eight files, then the results will be split up over more than one page. This is called batching.

The files are located in the Files folder. The getFiles script returns a sorted list of files. The getLink and getSize scripts do small formatting tasks to build hyperlinks and format file sizes. The batching is done in the index_html template. The template also makes use of the test function to assign different CSS classes to elements depending on conditions. This is how the sorted column name is bolded and alternate table rows are made gray.

Files

message goes here

Files 1 to 10 of 17.
< previous 10 next 10 >

ID Type Size Last Modified
example.zip application/x-zip-compressed 12 K Nov 14, 2001 9:31 am

There are no files currently available.

Upload a File

You can add a file to the library by uploading it from your local computer. The addFile script accepts the file upload and creates a new file object in the Files folder. It then returns this page with a confirmation message. For demonstration purposes, only 10 files of no larger than 2Kbytes may be uploaded.

Adding files is protected by the "Add Documents, Images and Files" permission. If you want anonymous users to upload files, you could visit the "Security" tab of the addFile script, and give it the "Manager" proxy role.


qU content_typeqU text/htmlqUtitleqUu. k ¯((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_argcountq KU co_varnamesq (Ufileq U _getitem_q U_getitemq U _getattr_qU_getattrqUProducts.PythonScripts.standardqU url_quoteqU ExceptionqU LimitExceededqUhasattrqUsizeqUlenqU containerqUmessageqUcontextqtubU_paramsqUfileqUidqUaddFileqU__ac_local_roles__q}qUamosq ]q!UOwnerq"asU _bind_namesq#(cShared.DC.Scripts.Bindings NameAssignments q$oq%}q&U_asgnsq'}q((Uname_containerq)U containerq*U name_subpathq+Utraverse_subpathq,U name_contextq-Ucontextq.U name_m_selfq/Uscriptq0usbU func_defaultsq1NU_codeq2TÊcsd„ZdS(s Adds a file to the library. cs³t}t}ddkl}dtfd„ƒY} t|dƒo t|dƒ o dd‚n| |dƒdd ƒ ||dƒƒ} ||dƒdƒt ||t d ƒd ƒd ƒƒd jod| djoD||||t d ƒdƒdƒdƒddddd|ƒn |d‚n |d‚d||dƒ} |||tdƒdƒdƒd|t dƒƒ|| ƒfƒSdS( s Adds a file to the library. (s url_quotes LimitExceededcs dRS(s$raised when file limits are exceeded((((sScript (Python)s LimitExceededssseekstells Bad RequestsInvalid file specifiediisFiless objectValuessFilei ismanage_addProductsOFSPsmanage_addFilesidsstitlesfiles(Uploaded file must be less than 2Kbytes.s%limit on number of files (5) exceededs File '%s' uploaded successfully.sfilenamesREQUESTsRESPONSEsredirects %s?message=%ss absolute_urlN(s _getitem_s_getitems _getattr_s_getattrsProducts.PythonScripts.standards url_quotes Exceptions LimitExceededshasattrsfilessizeslens containersmessagescontext(sfiles _getitem_s_getitems _getattr_s_getattrsProducts.PythonScripts.standards url_quotes Exceptions LimitExceededshasattrssizeslens containersmessagescontext((sScript (Python)saddFiles$.8 N(saddFile(saddFile((sScript (Python)ssq3U _proxy_rolesq4)U_bodyq5TÖ""" Adds a file to the library. """ from Products.PythonScripts.standard import url_quote class LimitExceeded(Exception): """raised when file limits are exceeded""" # create up to 10 files of 2k apiece if not (hasattr(file, 'seek') and hasattr(file, 'tell')): raise "Bad Request", "Invalid file specified" file.seek(0,2) size=file.tell() file.seek(0) # rewind if len(container.Files.objectValues('File')) < 10: if size < 2048: container.Files.manage_addProduct['OFSP'].manage_addFile(id='', title='', file=file) else: raise LimitExceeded, "Uploaded file must be less than 2Kbytes." else: raise LimitExceeded, "limit on number of files (5) exceeded" # create a success message message="File '%s' uploaded successfully." % file.filename # redirect to main page - this is necessary to make all the URLs # on the main page work correctly. return context.REQUEST.RESPONSE.redirect("%s?message=%s" % (container.absolute_url(), url_quote(message))) q6U Script_magicq7KUwarningsq8)Uerrorsq9)U Python_magicq:U*ë q;u. h ?((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (Usortq Ureverseq U _getattr_q U_getattrq U containerqUfilesqUargsqUsequenceqtU co_argcountqKubU_paramsqUsort='id', reverse=0qUidqUgetFilesqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq }q!(Uname_containerq"U containerq#U name_subpathq$Utraverse_subpathq%U name_contextq&Ucontextq'U name_m_selfq(Uscriptq)usbU func_defaultsq*(Uidq+Ktq,U_codeq-T‚csddd„ZdS(s; Returns a list of files sorted by id, date, type or size. sidic s—t}d||tdƒdƒdƒ}f}|djo7 | o dff}n dddff}nú |djoC| od fdff}nd ddfdff}n§|d joC| od fdff}nd ddfdff}nT|d joC| od ddfdff}nd fdff}n|tdƒ||ƒSdS(s; Returns a list of files sorted by id, date, type or size. sFiless objectValuessFilesidsgetIdscmpsdescsdatesbobobase_modification_timestypesgetContentTypessizesgetSizessortN(s _getattr_s_getattrs containersfilessargsssortsreversessequence(ssortsreverses _getattr_s_getattrs containersfilessargsssequence((sScript (Python)sgetFiless( !     N(sgetFiles(sgetFiles((sScript (Python)ssq.U_bodyq/T""" Returns a list of files sorted by id, date, type or size. """ files=container.Files.objectValues('File') args=() # build arguments for sequence.sort if sort=='id': if not reverse: args=(('getId',),) else: args=(('getId', 'cmp', 'desc'),) elif sort=='date': if not reverse: args=(('bobobase_modification_time',), ('getId',)) else: args=(('bobobase_modification_time', 'cmp', 'desc'), ('getId',)) elif sort=='type': if not reverse: args=(('getContentType',), ('getId',)) else: args=(('getContentType', 'cmp', 'desc'), ('getId',)) elif sort=='size': if not reverse: args=(('getSize', 'cmp', 'desc'), ('getId',)) else: args=(('getSize',), ('getId',)) return sequence.sort(files, args) q0U Script_magicq1KUwarningsq2)Uerrorsq3)U Python_magicq4U*ë q5u. g((U OFS.FolderqUFolderqtqNt.}q(UidqUFilesqU__ac_local_roles__q}qUamosq ]q UOwnerq asU example.htmlq (U zq (U OFS.ImageqUFileqtqtqQU_objectsq(}q(U meta_typeqUFileqUidqU example.txtqu}q(hhhh utUtitleqUh(U yq(hUFileqttqQu. f .((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_argcountq KU co_varnamesq (Uordersq UREQUESTq U_write_q U_writeqU _getitem_qU_getitemqU _getattr_qU_getattrqUcontextqUsessionqUitemsqUappendqU$append0qU containerqUdU valid_itemsqUorderqUhasattrqUintqUquantityqUNoneqtubU_paramsqUorders, REQUEST=Noneq Uidq!UaddItemsq"U__ac_local_roles__q#}q$Uamosq%]q&UOwnerq'asU _bind_namesq((cShared.DC.Scripts.Bindings NameAssignments q)oq*}q+U_asgnsq,}q-(Uname_containerq.U containerq/U name_subpathq0Utraverse_subpathq1U name_contextq2Ucontextq3U name_m_selfq4Uscriptq5usbU func_defaultsq6(Ntq7U_codeq8TÁcstd„ZdS(sf Adds one or more items to the shopping cart. Each order is a record with an id and a quantity field. cs§t}t}t}d||tdƒdƒ} || dƒdhƒ}  gi } |t dƒƒd r}| ||dƒƒqq~ } xÊ|d r½}t|dƒo&||dƒot||dƒƒpd}|djo#t|dƒo||dƒ|jo?|| dƒ||dƒdƒ||| ƒ||dƒsq9U_bodyq:T@""" Adds one or more items to the shopping cart. Each order is a record with an id and a quantity field. """ session=context.REQUEST.SESSION # get the items, items is a dictionary that maps item ids to quantities items=session.get('items',{}) # get the valid item names for form validation valid_items=[d['id'] for d in container.getItem()] for order in orders: quantity=hasattr(order, 'quantity') and order.quantity and \ int(order.quantity) or 0 if quantity != 0 and hasattr(order, 'id') and order.id in valid_items: items[order.id]=items.get(order.id, 0) + quantity # save items back to session (this is necessary # since items is a mutable non-persistent object) session['items']=items # if called from the web return a response page if REQUEST is not None: return container.index_html(REQUEST) q;U Script_magicq)U Python_magicq?U*ë q@u. cp((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (Uidq U_write_q U_writeq U _getattr_q U_getattrqUitemsqUNoneqUitemqUresultsqtU co_argcountqKubU_paramsqUid=NoneqUidqUgetItemqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}q U_asgnsq!}q"(Uname_containerq#U containerq$U name_subpathq%Utraverse_subpathq&U name_m_selfq'Uscriptq(U name_contextq)Ucontextq*usbU func_defaultsq+(Ntq,U_codeq-T~cstd„ZdS(sù Returns an item given its id. If no id is provided, returns all available items. In a real shopping cart application, this script would probably consult a database of some sort. Each item is a dictionary with an id, title, description, and price. c sTt}t}dhhdd<dd<dd<d<hdd<d d<d d<d <hd d<d d<dd<d<}|tj oQ||dƒ|ƒ}|tjo tSn|||ƒd<|Sng}xN||dƒƒdr5\}} |||ƒd<|!|dƒ|ƒq W"|SdS(sù Returns an item given its id. If no id is provided, returns all available items. In a real shopping cart application, this script would probably consult a database of some sort. Each item is a dictionary with an id, title, description, and price. s Econo FeederstitlesëThese feeders are made in Taiwan from a light-weight metal with plastic ends, and have an anti-spill edge to prevent the birds from kicking the feed out of the tray. They have a wooden rollbar to prevent the birds from sitting on them.s descriptionf7.95sprices510-115s Hopper Feeders7Need to go out of town for a few days, and no one can feed your pigeons? Don't worry, we now have the virtually spillproof hopper feeder. Made from birch plywood it holds from 30 to 35 pounds of grain. Pigeons can get at the feed through holes in the plexiglass cover, but will not be able to kick out any feed.f55.0s510-122sDust Free Bullet WaterersA favorite with many fanciers, this drinker has a dust cover over each individual drinking hole, to keep out the dirt and dust.f16.0s510-007sgetsidsitemsisappendN( s_write_s_writes _getattr_s_getattrsitemssidsNonesitemsresults( sids_write_s_writes _getattr_s_getattrsitemssNonesitemsresults((sScript (Python)sgetItemsu    'N(sNonesgetItem(sNonesgetItem((sScript (Python)ssq.U_bodyq/TZ""" Returns an item given its id. If no id is provided, returns all available items. In a real shopping cart application, this script would probably consult a database of some sort. Each item is a dictionary with an id, title, description, and price. """ items={ '510-115' : {'title' : 'Econo Feeder', 'description' : 'These feeders are made in Taiwan from a light-weight metal with plastic ends, and have an anti-spill edge to prevent the birds from kicking the feed out of the tray. They have a wooden rollbar to prevent the birds from sitting on them.', 'price' : 7.95}, '510-122' : {'title' : 'Hopper Feeder', 'description' : 'Need to go out of town for a few days, and no one can feed your pigeons? Don\'t worry, we now have the virtually spillproof hopper feeder. Made from birch plywood it holds from 30 to 35 pounds of grain. Pigeons can get at the feed through holes in the plexiglass cover, but will not be able to kick out any feed.', 'price' : 55.0}, '510-007' : {'title' : 'Dust Free Bullet Waterer', 'description' : 'A favorite with many fanciers, this drinker has a dust cover over each individual drinking hole, to keep out the dirt and dust.', 'price' : 16.0}, } # return requested item if id is not None: item=items.get(id) if item is None: # no item by that id return None # add an id field to the record item['id']=id return item # return all items results=[] for id, item in items.items(): item['id']=id results.append(item) return results q0U Script_magicq1KUwarningsq2)Uerrorsq3)U Python_magicq4U*ë q5u. e((U'Products.PageTemplates.ZopePageTemplateqUZopePageTemplateqtqNt.}q(UexpandqKUidqU index_htmlqU__ac_local_roles__q}q Uamosq ]q UOwnerq asU_textq TÉ The document title or folder title or the example title

The title

This example shows how to use sessions. Notice that Zope remembers the contents of your shopping cart until you close your browser.

The contents of the cart are stored in a session. The session variable items keeps track of the item ids and quantities of items in your shopping cart. The currentItems script uses this information to retrieve records describing the items in the shopping card. The deleteItems script removes items from the shopping cart.

Your Shopping Cart

Item Price Quantity
Item $10.00 2
Total
$25.00

There are currently no items in your shopping cart.

Add Items

Item Description Price Quantity
Item Description $10.00

The addItems script adds items to your shopping cart. The above form uses records to build an order list of to pass to the script. Each item in the order list has a id and a quantity field.

qU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}qU name_subpathqUtraverse_subpathqssbUtitleqUU content_typeqU text/htmlqu. dÂ((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_argcountq KU co_varnamesq (U_write_q U_writeq U _getattr_q U_getattrqUcontextqUsessionqU quantitiesqUitemsqUidqUquantityqU containerqUitemqUNoneqtubU_paramsqUUidqU currentItemsqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq (cShared.DC.Scripts.Bindings NameAssignments q!oq"}q#U_asgnsq$}q%(Uname_containerq&U containerq'U name_subpathq(Utraverse_subpathq)U name_contextq*Ucontextq+U name_m_selfq,Uscriptq-usbU func_defaultsq.NU_codeq/Ttcsd„ZdS(sŽ Returns a list of items that are in the shopping cart. Each item has a 'quantity' field as well as the normal 'id' and 'description' fields. c sàt}t}d||tdƒdƒ}||dƒdhƒ}g}xz||dƒƒdra\}}  |t dƒ|ƒ}  | t j o- | || ƒd<| |dƒ| ƒnqmW |Sd S( sŽ Returns a list of items that are in the shopping cart. Each item has a 'quantity' field as well as the normal 'id' and 'description' fields. sREQUESTsSESSIONsgetsitemsisgetItemsquantitysappendN( s_write_s_writes _getattr_s_getattrscontextssessions quantitiessitemssidsquantitys containersitemsNone( s_write_s_writes _getattr_s_getattrscontextssessions quantitiessitemssidsquantitys containersitemsNone((sScript (Python)s currentItemss 'N(s currentItems(s currentItems((sScript (Python)ssq0U_bodyq1TŒ""" Returns a list of items that are in the shopping cart. Each item has a 'quantity' field as well as the normal 'id' and 'description' fields. """ session=context.REQUEST.SESSION quantities=session.get('items', {}) items=[] for id, quantity in quantities.items(): item=container.getItem(id) if item is not None: item['quantity']=quantity items.append(item) return items q2U Script_magicq3KUwarningsq4)Uerrorsq5)U Python_magicq6U*ë q7u. bæ((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (Uidsq UREQUESTq U_write_q U_writeq U _getattr_qU_getattrqUcontextqUsessionqUitemsqUidqUNoneqU containerqtU co_argcountqKubU_paramsqUids, REQUEST=NoneqUidqU deleteItemsqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq (cShared.DC.Scripts.Bindings NameAssignments q!oq"}q#U_asgnsq$}q%(Uname_containerq&U containerq'U name_subpathq(Utraverse_subpathq)U name_contextq*Ucontextq+U name_m_selfq,Uscriptq-usbU func_defaultsq.(Ntq/U_codeq0Tcstd„ZdS(s2 Remove one or more items from the shopping cart. c sÐt}t}d||tdƒdƒ}||dƒdhƒ} x=|d r0} | |dƒ| ƒo||ƒ| =nqXW|||ƒd<|t j o|t dƒ|ƒSndS( s2 Remove one or more items from the shopping cart. sREQUESTsSESSIONsgetsitemsishas_keys index_htmlN( s_write_s_writes _getattr_s_getattrscontextssessionsitemssidssidsREQUESTsNones container( sidssREQUESTs_write_s_writes _getattr_s_getattrscontextssessionsitemssidsNones container((sScript (Python)s deleteItemss+N(sNones deleteItems(sNones deleteItems((sScript (Python)ssq1U_bodyq2T""" Remove one or more items from the shopping cart. """ session=context.REQUEST.SESSION # get the items items=session.get('items',{}) # remove the items for id in ids: if items.has_key(id): del items[id] # save changes back to the session session['items']=items # if this script is called from the web return a response page if REQUEST is not None: return container.index_html(REQUEST) q3U Script_magicq4KUwarningsq5)Uerrorsq6)U Python_magicq7U*ë q8u. a9((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (U _getitem_q U_getitemq U _getattr_q U_getattrq U containerqUitemsqUtotalqUitemqtU co_argcountqKubU_paramsqUUidqUgetTotalqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}q (Uname_containerq!U containerq"U name_subpathq#Utraverse_subpathq$U name_contextq%Ucontextq&U name_m_selfq'Uscriptq(usbU func_defaultsq)NU_codeq*T:csd„ZdS(Ncsqt}t}|tdƒƒ}d}x6|dr)}|||dƒ||dƒ7}q6W|SdS(Ns currentItemsf0.0ispricesquantity(s _getitem_s_getitems _getattr_s_getattrs containersitemsstotalsitem(s _getitem_s_getitems _getattr_s_getattrs containersitemsstotalsitem((sScript (Python)sgetTotals! '(sgetTotal(sgetTotal((sScript (Python)ssq+U_bodyq,Uuitems=container.currentItems() total=0.0 for item in items: total+=item['price'] * item['quantity'] return total q-U Script_magicq.KUwarningsq/)Uerrorsq0)U Python_magicq1U*ë q2u. vÖ((U OFS.FolderqUFolderqtqNt.}q(UtitleqUUentry_0q(U €q(U OFS.ImageqUFileq tq tq QUidq UEntriesq U__ac_local_roles__q}qUamosq]qUOwnerqasU_objectsq(}q(U meta_typeqUFileqUidqhutu. w 9((U'Products.PageTemplates.ZopePageTemplateqUZopePageTemplateqtqNt.}q(UexpandqKUidqU index_htmlqU__ac_local_roles__q}q Uamosq ]q UOwnerq asU_textq Tí The document title or folder title or the example title

The title

This example shows how to create a simple guest book.

The guest book stores its entries as file objects in the Entries folder. This page displays the entries by looping over a sorted list of entries and displaying each one. The list of entries is created by the getEntries script. The formatEntry script formats each guest book entry. To remove an entry, remove its file from the Entries folder. An alternate version of this application could use ZSQL Methods to store guest book entries in a relational database.

The addEntry.html template provides a form that allows you to sign the guest book. The form is processed by the addEntry script. The addEntry script stores the entry as a file. Adding files is protected by the "Add Documents, Images and Files" permission; if you want anonymous users to sign the guest book, you could visit the 'Security' tab of the addEntry script, and give it the "Manager" proxy role.

Sign the guest book

Entries

By Guest Name
2001/12/12
Entry contents

There are no guest book entries yet.

qU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}qU name_subpathqUtraverse_subpathqssbUtitleqUU content_typeqU text/htmlqu. x¹((U'Products.PageTemplates.ZopePageTemplateqUZopePageTemplateqtqNt.}q(UexpandqKUidqU addEntry.htmlqU__ac_local_roles__q}q Uamosq ]q UOwnerq asU_textq TU The document title or folder title or the example title

The document title

You can sign the guest book by filling out this form. You cannot include HTML in your comments, however, Zope will preserve your linebreaks.

Your Name
Comments
qU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}qU name_subpathqUtraverse_subpathqssbUtitleqUSign the Guest BookqU content_typeqU text/htmlqu. t;((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (U _getattr_q U_getattrq U containerq Ufilesq UsequenceqtU co_argcountqKubU_paramsqUUidqU getEntriesqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}q(Uname_containerqU containerqU name_subpathq Utraverse_subpathq!U name_contextq"Ucontextq#U name_m_selfq$Uscriptq%usbU func_defaultsq&NU_codeq'T°csd„ZdS(sJ Returns a list of entries (file objects) in order from newest to oldest. csZt}d||tdƒdƒdƒ}|tdƒ|dddffƒSdS( sJ Returns a list of entries (file objects) in order from newest to oldest. sEntriess objectValuessFilessortsbobobase_modification_timescmpsdescN(s _getattr_s_getattrs containersfilesssequence(s _getattr_s_getattrs containersfilesssequence((sScript (Python)s getEntriess !N(s getEntries(s getEntries((sScript (Python)ssq(U_bodyq)T""" Returns a list of entries (file objects) in order from newest to oldest. """ files=container.Entries.objectValues('File') # sort the files based on modification time, with the most recent files first return sequence.sort(files, (('bobobase_modification_time', 'cmp', 'desc'),)) q*U Script_magicq+KUwarningsq,)Uerrorsq-)U Python_magicq.U*ë q/u. sË((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_varnamesq (Uentryq UProducts.PythonScripts.standardq U html_quoteq U newline_to_brq tU co_argcountqKubU_paramsqUentryqUidqU formatEntryqU__ac_local_roles__q}qUamosq]qUOwnerqasU _bind_namesq(cShared.DC.Scripts.Bindings NameAssignments qoq}qU_asgnsq}q(Uname_containerqU containerqU name_contextq Ucontextq!U name_subpathq"Utraverse_subpathq#Uname_nsq$U_U name_m_selfq%Uscriptq&usbU func_defaultsq'NU_codeq(TÉcsd„ZdS(Ncs-dkl}l}|||ƒƒSdS(N(s html_quotes newline_to_br(sProducts.PythonScripts.standards html_quotes newline_to_brsentry(sentrysProducts.PythonScripts.standards html_quotes newline_to_br((sScript (Python)s formatEntrys(s formatEntry(s formatEntry((sScript (Python)ssq)U_bodyq*Unfrom Products.PythonScripts.standard import html_quote, newline_to_br return newline_to_br(html_quote(entry)) q+U Script_magicq,KUwarningsq-)Uerrorsq.)U Python_magicq/U*ë q0u. u W((U#Products.PythonScripts.PythonScriptqU PythonScriptqtqNt.}q(U func_codeq(cShared.DC.Scripts.Signature FuncCode qoq}q(U co_argcountq KU co_varnamesq (Ucommentsq U guest_nameq UREQUESTq U _getitem_qU_getitemqU _getattr_qU_getattrqU ExceptionqU LimitExceededqU containerqUentries_folderqUlenqUnUidqUgetattrqUentryqUNoneqtubU_paramsqU.comments, guest_name='Anonymous', REQUEST=NoneqUidqUaddEntryqU__ac_local_roles__q}q Uamosq!]q"UOwnerq#asU _bind_namesq$(cShared.DC.Scripts.Bindings NameAssignments q%oq&}q'U_asgnsq(}q)(Uname_containerq*U containerq+U name_subpathq,Utraverse_subpathq-U name_contextq.Ucontextq/U name_m_selfq0Uscriptq1usbU func_defaultsq2(U Anonymousq3Ntq4U_codeq5TÕcsdtd„ZdS(s Create a guest book entry. s Anonymouscs:t}t}ddtfd„ƒY}|tdƒ}  t|| dƒƒƒ}  | djo |d‚n d| } |||| dƒd ƒd ƒ| d d d dd||dd…ƒƒt | | ƒ}||dƒd||dd…ƒdƒ|tj o|tdƒ|ƒSndS(s Create a guest book entry. s LimitExceededcs dRS(s*raised when too many entries are submitted((((sScript (Python)s LimitExceededssEntriess objectIdsis'limit on number of entries (5) exceededsentry_%dsmanage_addProductsOFSPsmanage_addFilestitless content_types text/plainsfileNi€smanage_addPropertys guest_namei sstrings index_html(s _getitem_s_getitems _getattr_s_getattrs Exceptions LimitExceededs containersentries_folderslensnsidscommentssgetattrsentrys guest_namesREQUESTsNone(scommentss guest_namesREQUESTs _getitem_s_getitems _getattr_s_getattrs Exceptions LimitExceededs containersentries_folderslensnsidsgetattrsentrysNone((sScript (Python)saddEntrys@(N(sNonesaddEntry(sNonesaddEntry((sScript (Python)ssq6Uerrorsq7)U_bodyq8T<""" Create a guest book entry. """ class LimitExceeded(Exception): """raised when too many entries are submitted""" entries_folder=container.Entries # create up-to 5 unique entry ids, a real app might not impose such a # small limit, but we do because this is just an example after all n=len(entries_folder.objectIds()) if n > 4: raise LimitExceeded, "limit on number of entries (5) exceeded" id='entry_%d' % n # create the entry, limit body to 128 bytes entries_folder.manage_addProduct['OFSP'].manage_addFile(id, title="", content_type="text/plain", file=comments[:128]) # add a guest_name string property, limited to 32 bytes entry=getattr(entries_folder, id) entry.manage_addProperty('guest_name', guest_name[:32], 'string') # return a response page if REQUEST is not None: return container.index_html(REQUEST) q9U Script_magicq:KUwarningsq;)U _proxy_rolesq<)U Python_magicq=U*ë q>u. q((U OFS.FolderqUFolderqtqNt.}q(UtitleqUUidqUPigeonsqU__ac_local_roles__q}q Uamosq ]q UOwnerq asu. ~o((U OFS.FolderqUFolderqtqNt.}q(UtitleqUUidqUCrowsqU__ac_local_roles__q}q Uamosq ]q UOwnerq asu. }Ð((U OFS.FolderqUFolderqtqNt.}q(UtitleqUUidqUPrimatesqUMonkeysq(U q (hUFolderq tq tq QU__ac_local_roles__q }qUamosq]qUOwnerqasU_objectsq(}q(U meta_typeqUFolderqUidqhutu. |p((U OFS.FolderqUFolderqtqNt.}q(UtitleqUUidqUWhalesqU__ac_local_roles__q}q Uamosq ]q UOwnerq asu. {n((U OFS.FolderqUFolderqtqNt.}q(UtitleqUUidqUBatsqU__ac_local_roles__q}q Uamosq ]q UOwnerq asu. z³((U OFS.ImageqUFileqtqNt.}q(UsizeqKÎUdataqUÎ Example

Example

This is an example HTML file.

qU_EtagSupport__etagqU ts56412592.16q U__name__q U example.htmlq Utitleq UU content_typeq U text/htmlqU preconditionqUU__ac_local_roles__q}qUamosq]qUOwnerqasu. y((U OFS.ImageqUFileqtqNt.}q(UsizeqKUdataqUThis is an example text file.qU_EtagSupport__etagqU ts56412603.29q U__name__q U example.txtq Utitleq UU content_typeq U text/plainqU preconditionqUU__ac_local_roles__q}qUamosq]qUOwnerqasu. €’((U OFS.ImageqUFileqtqNt.}q(UsizeqMMUdataqTMThis is an example comment. It can contain multiple lines of text. Note that you cannot include HTML in your comments, since the HTML is quoted. This guest book entry is stored in a file named entry_0 in the Entries folder. To edit the entry, simple edit that file. You can also delete an entry by deleting its file.qU_EtagSupport__etagqU ts05745452.64q U__name__q Uentry_0q U _propertiesq (}q (UidqUtitleqUtypeqUstringqu}q(hU content_typeqhhu}q(hU guest_nameqhhuthU text/plainqhUU preconditionqUU__ac_local_roles__q}qUamosq]qUOwnerqashUAmosqu. q((U OFS.FolderqUFolderqtqNt.}q(UtitleqUUidqUMonkeysqU__ac_local_roles__q}q Uamosq ]q UOwnerq asu.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ