def page_guide(self, pages, url): if(url.find('?') == -1): url += '?' else: from cgi import parse_qs from urllib import urlencode stub, query = url.split('?') query = parse_qs(query) if('page' in query): del query['page'] url = '%s?%s&' % (stub, urlencode(query, True)) if(pages.has_previous()): prev = tags.span()[tags.a(href="%spage=%d" % (url, pages.page - 1))['<< Previous']] else: prev = tags.span()['<< Previous'] if(pages.has_next()): next = tags.span()[tags.a(href="%spage=%d" % (url, pages.page + 1))['Next >>']] else: next = tags.span()['Next >>'] return tags.div(_class='page-guide')[[ tags.div()['Items %d – %d of %s shown.' % (pages.start_range, pages.end_range, pages.total_results)], tags.div()[prev, ' | ', next] ]]
def get_element(self, req, style, storable): frm = form.FormNode(self.name) frm.theme = theme.GigkeeperTheme req.store.ensure_factory("note", model_class=note.Note) notes = req.store.load("note", item_id=storable.get_id(), item_table=storable.get_table(), __order_by="type") params = urllib.urlencode( {"__init__[item_id]": storable.get_id(), "__init__[item_table]": storable.get_table()} ) add_note_url = req.get_path(req.prepath, "detail/note/new?") + params if storable.get_id(): frm["add_note"](type="markup", value=tags.a(href=add_note_url)["Add Note"]) else: frm["add_note"](type="label", value="Please save this record before adding notes.") if notes: for n in notes: note_url = req.get_path(req.prepath, "detail/note", n.get_id()) frm["notes"](type="note_list") frm["notes"][n.get_id()](type="note") frm["notes"][n.get_id()]["select"](type="checkbox") frm["notes"][n.get_id()]["title"]( type="label", value=tags.a(href=note_url, _class="note-title")[n.summary] ) frm["notes"][n.get_id()]["created_date"]( type="label", value=n.created_date, attributes=dict(_class="note-date") ) frm["notes"][n.get_id()]["type"]( type="label", value="(%s)" % n.type, prefix=" ", attributes=dict(_class="note-type") ) frm["notes"][n.get_id()]["body"](type="body_text", value=n.summarize(length=50)) return frm
def get_element(self, req, style, storable): """ @see: L{modu.editable.define.definition.get_element()} """ store = storable.get_store() label = None value = None label_col = self.get('flabel', 'title') value_col = 'id' table = getattr(storable, 'item_table', None) if not(table): table = self.get('ftable') item_value = getattr(storable, self.get_column_name(), None) if(table is None or item_value is None): results = None else: # We select * in case the particular item doesn't have a title field foreign_label_query = "SELECT * FROM %s WHERE %s = %%s" % (table, value_col) foreign_label_query = sql.interp(foreign_label_query, [item_value]) results = store.pool.runQuery(foreign_label_query) if(results): value = results[0][value_col] label = results[0].get(label_col, '(label not found)') frm = form.FormNode(self.name) suffix = '' prefix = '' if(style == 'listing'): frm(type='hidden', value=value) if(table and value): label = tags.a(href=req.get_path(req.prepath, 'detail', table, value))[label] frm(type='label', value=label) else: if not(label): label = '(no link available)' frm(type='hidden', value=value) if(table and value): prefix = tags.a(href=req.get_path(req.prepath, 'detail', table, value))[label] else: prefix = label frm(prefix=prefix, suffix=suffix) return frm
def test_linked_labelfield(self): """ Test for L{modu.editable.datatypes.string.LabelField}. ...and any other datatype that could support a link prefix/suffix """ test_itemdef = define.itemdef( # Normally base_path is set by the admin resource __config = dict( base_path = '/admin' ), linked_name = string.LabelField( label = 'Name', link = True, listing = True, attributes = dict(basic_element=False), ) ) req = self.get_request() test_storable = storable.Storable('test') test_storable.linked_name = 'Linked Name' itemdef_form = test_itemdef.get_listing(req, [test_storable]) reference_form = form.FormNode('test-row') reference_form['linked_name'](type='label', label='Name', value='Linked Name', prefix=tags.a(href='http://____store-test-domain____:1234567/app-test/detail/test/0', __no_close=True), suffix='</a>') itemdef_form_html = itemdef_form[0].render(req) reference_form_html = reference_form.render(req) self.failUnlessEqual(itemdef_form_html, reference_form_html, "Didn't get expected form output, got:\n%s\n instead of:\n%s" % (itemdef_form_html, reference_form_html) )
def get_element(self, req, style, storable): frm = form.FormNode(self.name) frm.theme = theme.GigkeeperTheme req.store.ensure_factory('address', model_class=address.Address) addresses = req.store.load('address', item_id=storable.get_id(), item_table=storable.get_table(), __order_by='type') params = { '__init__[item_id]' : storable.get_id(), '__init__[item_table]' : storable.get_table(), } if(storable.get_id()): address_name_callback = self.get('address_name_callback', None) if(callable(address_name_callback)): params['__init__[name]'] = address_name_callback(req, frm, storable) add_address_url = req.get_path(req.prepath, 'detail/address/new?') + urllib.urlencode(params) frm['add_address']( type = 'markup', value = tags.a(href=add_address_url)['Add Address'] ) else: frm['add_address']( type = 'label', value = "Please save this record before adding addresses.", ) if(addresses): for a in addresses: address_url = req.get_path(req.prepath, 'detail/address', a.get_id()) frm['addresses'][a.get_id()]( prefix = '<div>', suffix = '</div>', ) frm['addresses'][a.get_id()]['select']( type = 'checkbox', ) frm['addresses'][a.get_id()]['address']( type = 'label', value = tags.a(href=address_url)[a.as_string()], prefix = ' ', ) return frm
def get_element(self, req, style, storable): frm = form.FormNode(self.name) company_id = storable.get_id() params = { '__init__[company_id]' : company_id } req.store.ensure_factory('contact', model_class=contact.Contact) if(company_id): contacts = req.store.load('contact', company_id=company_id) or [] if not(contacts): frm['contact']( type = 'label', value = "This company has no contacts yet.", ) return frm else: contacts = [] add_contact_url = req.get_path(req.prepath, 'detail/contact/new?') + urllib.urlencode(params) if(company_id): frm['add_contact']( type = 'markup', value = tags.a(href=add_contact_url)['Add Contact'] ) else: frm['add_contact']( type = 'label', value = "This item has no contacts yet.", ) for c in contacts: contact_url = req.get_path(req.prepath, 'detail/contact', c.get_id()) frm['contact'][c.get_id()]( prefix = '<ul>', suffix = '</ul>', ) frm['contact'][c.get_id()]['name']( type = 'label', value = tags.a(href=contact_url)[c.name], prefix = '<li>', suffix = '</li>', ) return frm
def get_element(self, req, style, storable): frm = form.FormNode(self.name) req.store.ensure_factory('url', model_class=url.URL) urls = req.store.load('url', item_id=storable.get_id(), item_table=storable.get_table(), __order_by='type') params = urllib.urlencode({ '__init__[item_id]' : storable.get_id(), '__init__[item_table]' : storable.get_table(), }) add_url_url = req.get_path(req.prepath, 'detail/url/new?') + params if(storable.get_id()): frm['add_url']( type = 'markup', value = tags.a(href=add_url_url)['Add URL'] ) else: frm['add_url']( type = 'label', value = "Please save this record before adding URLs.", ) if(urls): for u in urls: frm['urls'][u.get_id()]( prefix = '<div>', suffix = '</div>', ) frm['urls'][u.get_id()]['select']( type = 'checkbox', ) frm['urls'][u.get_id()]['url']( type = 'label', value = tags.a(href=u.url)[u.url], prefix = ' ', ) frm['urls'][u.get_id()]['type']( type = 'label', value = u.type, prefix = ' ', ) return frm
def get_element(self, req, style, storable): frm = form.FormNode(self.name) project_id = storable.get_id() if not (project_id): frm["release"](type="label", value="You must save this project before adding a new release.") return frm req.store.ensure_factory("release", model_class=release.Release) releases = req.store.load("release", project_id=project_id, __order_by="version_weight DESC") or [] query = { "__init__[project_id]": storable.get_id(), "__init__[license_name]": storable.license_name, "__init__[license_url]": storable.license_url, "__init__[installation_url]": storable.installation_url, "__init__[changelog_url]": storable.changelog_url, } new_release_url = req.get_path(req.prepath, "detail/release/new?" + urllib.urlencode(query)) if not (releases): if style == "listing": frm["release"](type="label", value="(no releases)") else: frm["release"]( type="label", value="This project has no releases yet. " + tags.a(href=new_release_url)["Click here to create one."], ) return frm for r in releases: release_url = req.get_path(req.prepath, "detail/release", r.get_id()) frm["release"][r.get_id()](prefix='<span class="releases">', suffix="</span>") frm["release"][r.get_id()]["version_string"](type="label", value=tags.a(href=release_url)[r.version_string]) if style != "listing": frm["release"]["new"]( type="markup", prefix="<div>", value=tags.a(href=new_release_url)["Create New Release"], suffix="</div>" ) return frm
def get_form_element(self, req, style, storable): """ Get a FormNode element that represents this field. The parent itemdef class will call this function, which will call the get_element() method, and set a few default values. @param req: The current request @type req: L{modu.web.app.Request} @param style: Generate for 'listing', 'search', or 'detail' views. @type style: str @param storable: The Storable instance to fill form data with. @type storable: L{modu.persist.storable.Storable} subclass @return: form element @rtype: L{modu.util.form.FormNode} """ frm = self.get_element(req, style, storable) classes = [self.__class__] while(classes): for cls in classes: if not(hasattr(cls, 'inherited_attributes')): continue for name in cls.inherited_attributes: if(name in self and name not in frm.attributes): frm.attributes[name] = self[name] classes = cls.__bases__ for name, value in self.get('attributes', {}).iteritems(): frm.attributes[name] = value # Since the templates take care of the wrapper, all # elements are defined as basic elements unless set # explicitly by the field definition. if(style != 'search' and 'basic_element' not in frm.attributes): frm(basic_element=True) if(style == 'listing' and self.get('link', False)): href = req.get_path(req.prepath, 'detail', storable.get_table(), storable.get_id()) frm(prefix=tags.a(href=href, __no_close=True), suffix='</a>') if(self.get('required', False)): frm(required=True) if(callable(self.get('form_alter', None))): self['form_alter'](req, style, frm, storable, self) return frm
def default_formatter(self, req, results, storable): """ The default way to format the output of this field. """ query = {'__init__[item_id]':storable.get_id(), '__init__[item_table]':storable.get_table(), } new_media_link = tags.a(href=req.get_path(req.prepath, 'detail/media/new?' + urllib.urlencode(query)))['Add Media'] if(len(results) == 0): return tags.span()[new_media_link] def format_type(item): filesize = media.formatbytes(item.get('filesize', 0)) return '%s %s %s (%s)' % (filesize, item['format'], item['type'], item['quality']) def format_status(item): full_path = os.path.join(req.app.upload_path, item['filename']) status = [] try: finfo = os.stat(full_path) if(finfo.st_size != item['filesize']): status.append("wrong filesize") if(req.postpath[-1] == 'check-md5'): filehash = media.get_checksum(full_path, md5_path=req.app.md5_path) if(filehash != item['md5']): status.append("incorrect hash") except OSError, e: status.append(e.strerror) if(status): return tags.span(_class='error')[str(tags.br()).join(status)] else: return tags.span(_class='message')['OK']
def test_a_stub(self): tag = tags.a(href='http://www.example.com', __no_close=True) expected = '<a href="http://www.example.com">' self.assertEqual(str(tag), expected, "Got '%s' instead of '%s'" % (str(tag), expected))
def error_formatter(req, field, err): return tags.a(href="#form-item-%s" % field)[err]