def _add_tiddler_to_feed(self, feed, tiddler): do_revisions = self.environ.get('tiddlyweb.query', {}).get( 'depth', [None])[0] if not do_revisions: if binary_tiddler(tiddler): # XXX: ought to be enclosures? if tiddler.type.startswith('image/'): description = ('\n<img src="%s" />\n' % tiddler_url(self.environ, tiddler)) else: description = ('\n<a href="%s">%s</a>\n' % (tiddler_url(self.environ, tiddler), tiddler.title)) elif (renderable(tiddler, self.environ)): try: description = render_wikitext(tiddler, self.environ) except KeyError: description = 'Tiddler cannot be rendered.' else: description = '<pre>' + tiddler.text + '</pre>' self._add_item(feed, tiddler, tiddler_url(self.environ, tiddler), tiddler.title, description) else: self._process_tiddler_revisions(feed, tiddler, tiddler_url(self.environ, tiddler), do_revisions)
def _binary_tiddler(self, tiddler): """ Make the content for a tiddler that has non-wikitext content. Base64 encode if the stuff is small enough for browsers to handle. """ b64text = b64encode(tiddler.text) if b64text < 32 * 1024: return 'data:$s;base64,%s' % (tiddler.type, b64text) else: if tiddler.type.startswith('image'): return '\n<html><img src="%s" /></html>\n' % tiddler_url(self.environ, tiddler) else: return '\n<html><a href="%s">%s</a></html>\n' % (tiddler_url(self.environ, tiddler), tiddler.title)
def _binary_tiddler(self, tiddler): """ Make the content for a tiddler that has non-wikitext content. """ limit = self.environ['tiddlyweb.config'].get( 'tiddlywebwiki.binary_limit', 0) if limit and len(tiddler.text) > limit: if tiddler.type.startswith('image/'): return ('\n<html><img src="%s" /></html>\n' % tiddler_url(self.environ, tiddler)) else: return ('\n<html><a href="%s">%s</a></html>\n' % (tiddler_url(self.environ, tiddler), tiddler.title)) else: return b64encode(tiddler.text)
def _binary_tiddler(self, tiddler): """ Make the content for a tiddler that has non-text content. """ limit = self.environ['tiddlyweb.config'].get( 'tiddlywebwiki.binary_limit', 0) if (limit == -1 or (limit and len(tiddler.text) > limit)): if tiddler.type.startswith('image/'): return ('\n<html><img src="%s" /></html>\n' % tiddler_url(self.environ, tiddler)) else: return ('\n<html><a href="%s">%s</a></html>\n' % (tiddler_url(self.environ, tiddler), tiddler.title)) else: return b64encode(tiddler.text)
def _post_tiddler_revisions(environ, start_response, tiddler): """ We have a list of revisions, put them in a new place. """ content_type = environ['tiddlyweb.type'] if content_type != 'application/json': raise HTTP415('application/json required') # we need a matching etag in order to be able to do # this operation. This will raise exception if there # isn't a valid etag. _require_valid_etag_for_write(environ, tiddler) bag = Bag(tiddler.bag) # both create and write required for this action _check_bag_constraint(environ, bag, 'create') _check_bag_constraint(environ, bag, 'write') length = environ['CONTENT_LENGTH'] content = environ['wsgi.input'].read(int(length)) _store_tiddler_revisions(environ, content, tiddler) response = [('Location', web.tiddler_url(environ, tiddler))] start_response("204 No Content", response) return []
def _send_tiddler_revisions(environ, start_response, tiddler): """ Push the list of tiddler revisions out the network. """ store = environ['tiddlyweb.store'] title = 'Revisions of Tiddler %s' % tiddler.title title = environ['tiddlyweb.query'].get('title', [title])[0] container = 'recipes' if tiddler.recipe else 'bags' if environ['tiddlyweb.filters']: tiddlers = Tiddlers(title=title) else: tiddlers = Tiddlers(title=title, store=store) tiddlers.is_revisions = True tiddlers.link = '%s/revisions' % web.tiddler_url(environ, tiddler, container=container, full=False) recipe = tiddler.recipe try: for revision in store.list_tiddler_revisions(tiddler): tmp_tiddler = Tiddler(title=tiddler.title, bag=tiddler.bag) tmp_tiddler.revision = revision if recipe: tmp_tiddler.recipe = recipe tiddlers.add(tmp_tiddler) except NoTiddlerError, exc: # If a tiddler is not present in the store. raise HTTP404('tiddler %s not found, %s' % (tiddler.title, exc))
def tiddler_as(self, tiddler): self.environ['tiddlyweb.links'].append( '<link rel="alternate" ' 'type="application/atom+xml" ' 'title="Atom" href="%s" />' % '%s.atom' % tiddler_url(self.environ, tiddler)) return HTMLSerialization.tiddler_as(self, tiddler)
def get_tiddler_edit(environ, start_response): usersign = environ['tiddlyweb.usersign'] store = environ['tiddlyweb.store'] title = environ['tiddlyweb.query'].get('title', [''])[0] bag_name = environ['tiddlyweb.query'].get('bag', [''])[0] username = usersign['name'] if not 'MEMBER' in usersign['roles']: raise HTTP404('bad edit') if not title and not bag_name: tiddler = get_profile(store, usersign, username) page_title = 'Edit Profile' return_url = '%s/home' % server_base_url(environ) elif not title: tiddler = Tiddler('', bag_name) page_title = 'Edit New Tiddler' return_url = '' else: tiddler = Tiddler(title, bag_name) page_title = 'Edit %s' % title return_url = tiddler_url(environ, tiddler) try: tiddler = store.get(tiddler) except StoreError: pass bag = Bag(tiddler.bag) bag = store.get(bag) bag.policy.allows(usersign, 'write') data = {} data['tiddler'] = tiddler data['return_url'] = return_url data['title'] = page_title return send_template(environ, 'profile_edit.html', data)
def get_tiddler_edit(environ, start_response): usersign = environ["tiddlyweb.usersign"] store = environ["tiddlyweb.store"] title = environ["tiddlyweb.query"].get("title", [""])[0] bag_name = environ["tiddlyweb.query"].get("bag", [""])[0] username = usersign["name"] if not "MEMBER" in usersign["roles"]: raise HTTP404("bad edit") if not title and not bag_name: tiddler = get_profile(store, usersign, username) page_title = "Edit Profile" return_url = "%s/home" % server_base_url(environ) elif not title: tiddler = Tiddler("", bag_name) page_title = "Edit New Tiddler" return_url = "" else: tiddler = Tiddler(title, bag_name) page_title = "Edit %s" % title return_url = tiddler_url(environ, tiddler) try: tiddler = store.get(tiddler) except StoreError: pass bag = Bag(tiddler.bag) bag = store.get(bag) bag.policy.allows(usersign, "write") data = {} data["tiddler"] = tiddler data["return_url"] = return_url data["title"] = page_title return send_template(environ, "profile_edit.html", data)
def _send_tiddler_revisions(environ, start_response, tiddler): """ Push the list of tiddler revisions out the network. """ store = environ['tiddlyweb.store'] title = 'Revisions of Tiddler %s' % tiddler.title title = environ['tiddlyweb.query'].get('title', [title])[0] container = 'recipes' if tiddler.recipe else 'bags' if environ['tiddlyweb.filters']: tiddlers = Tiddlers(title=title) else: tiddlers = Tiddlers(title=title, store=store) tiddlers.is_revisions = True tiddlers.link = '%s/revisions' % web.tiddler_url( environ, tiddler, container=container, full=False) recipe = tiddler.recipe try: for revision in store.list_tiddler_revisions(tiddler): tmp_tiddler = Tiddler(title=tiddler.title, bag=tiddler.bag) tmp_tiddler.revision = revision if recipe: tmp_tiddler.recipe = recipe tiddlers.add(tmp_tiddler) except NoTiddlerError, exc: # If a tiddler is not present in the store. raise HTTP404('tiddler %s not found, %s' % (tiddler.title, exc))
def tiddler_as(self, tiddler): """ Transform the provided tiddler into an HTML representation of the tiddler packaged in a DIV. Render the content using the render_wikitext subsystem. Links to the tiddler in the wiki are provided. """ user = self.environ['tiddlyweb.usersign'] store = self.environ['tiddlyweb.store'] if tiddler.recipe: list_link = '/recipes/%s/tiddlers' % encode_name(tiddler.recipe) list_title = 'Tiddlers in Recipe %s' % tiddler.recipe else: list_link = '/bags/%s/tiddlers' % encode_name(tiddler.bag) list_title = 'Tiddlers in Bag %s' % tiddler.bag try: store.get(Bag(tiddler.bag)).policy.allows(user, 'manage') container_policy = True except PermissionsError: container_policy = False space_link = self._space_link(tiddler) html = render_wikitext(tiddler, self.environ) return send_template(self.environ, 'tiddler.html', { 'title': '%s' % tiddler.title, 'tags': tiddler.tags, 'fields': tiddler.fields, 'html': html, 'list_link': list_link, 'list_title': list_title, 'space_link': space_link, 'tiddler': tiddler, 'container_policy': container_policy, 'tiddler_url': tiddler_url(self.environ, tiddler)})
def _tiddler_dict(self, tiddler, fat=False, render=False): """ Select fields from a tiddler to create a dictonary. """ unwanted_keys = ['text', 'store'] wanted_keys = [ attribute for attribute in tiddler.slots if attribute not in unwanted_keys ] wanted_info = {} for attribute in wanted_keys: wanted_info[attribute] = getattr(tiddler, attribute, None) wanted_info['permissions'] = self._tiddler_permissions(tiddler) wanted_info['uri'] = tiddler_url(self.environ, tiddler) if fat: if tiddler.text: if binary_tiddler(tiddler): wanted_info['text'] = b64encode(tiddler.text) else: wanted_info['text'] = tiddler.text else: wanted_info['text'] = '' if render and renderable(tiddler, self.environ): wanted_info['render'] = render_wikitext(tiddler, self.environ) return wanted_info
def _post_tiddler_revisions(environ, start_response, tiddler): """ We have a list of revisions, put them in a new place. """ length, content_type = content_length_and_type(environ) if content_type not in CHRONICLE_TYPES: raise HTTP415('application/vnd.tiddlyweb+json required') # we need a matching etag in order to be able to do # this operation. This will raise exception if there # isn't a valid etag. _require_valid_etag_for_write(environ, tiddler) bag = Bag(tiddler.bag) # both create and write required for this action check_bag_constraint(environ, bag, 'create') check_bag_constraint(environ, bag, 'write') content = environ['wsgi.input'].read(int(length)) _store_tiddler_revisions(environ, content, tiddler) response = [('Location', tiddler_url(environ, tiddler))] start_response("204 No Content", response) return []
def _tiddlers_links(self, tiddlers, tiddler): """ Establish the links to use with a tiddlers collection. If the collection is a search or revisions we need to do some special work, otherwise just look at the last tiddler in the collection to determine the container. """ if tiddlers.is_search: return {} if tiddlers.is_revisions: links = {} links['self'] = '%s/%s/revisions' % ( self._tiddlers_collection_uri(), encode_name(tiddler.title)) links['tiddlyweb:tiddler'] = tiddler_url(self.environ, tiddler, full=True) return links links = {} if tiddler: links = self._tiddlers_self(tiddler) tiddler_link = self._tiddlers_collection_uri() if tiddler_link: links['self'] = tiddler_link links['tiddlyweb:tiddler'] = tiddler_link + '/{tiddler}' return links
def _binary_tiddler(self, tiddler): """ Make the content for a tiddler that has non-wikitext content. """ # XXX This is memory inefficient for large tiddlers. # But by this time we are _already_ inefficient. limit = self.environ['tiddlyweb.config'].get( 'tiddlywebwiki.binary_limit', 0) if limit and len(tiddler.text) > limit: if tiddler.type.startswith('image/'): return ('\n<html><img src="%s" /></html>\n' % tiddler_url(self.environ, tiddler)) else: return ('\n<html><a href="%s">%s</a></html>\n' % (tiddler_url(self.environ, tiddler), tiddler.title)) else: return b64encode(tiddler.text)
def tiddler_as(self, tiddler): feed = AtomFeed( title=u'%s' % tiddler.title, link=tiddler_url(self.environ, tiddler), language=u'en', description=u'tiddler %s' % tiddler.title) self._add_tiddler_to_feed(feed, tiddler) return feed.writeString('utf-8')
def tiddler_as(self, tiddler): """ Take the single tiddler provided and inject it into a TiddlyWiki. """ tiddlers = Tiddlers(title=tiddler.title, bag=tiddler.bag, recipe=tiddler.recipe) tiddlers.add(tiddler) tiddlers.link = tiddler_url(self.environ, tiddler, full=False) # Join with '' to return a string not generator. return ''.join(self._put_tiddlers_in_tiddlywiki(tiddlers))
def _tiddler_in_container_info(self, tiddler): """ Get the info for a non-revision tiddler in a list. """ if tiddler.recipe: base = 'recipes' else: base = 'bags' return '<li><a href="%s">%s</a></li>' % ( tiddler_url(self.environ, tiddler, container=base, full=False), tiddler.title.replace(' ', ' ', 1))
def _revision_links(self, tiddler): """ The links to provide with a single revision. """ tiddler_link = tiddler_url(self.environ, tiddler, full=True) return [ Link('latest-version', tiddler_link), Link('tiddlyweb:tiddler', tiddler_link), Link('collection', tiddler_link + '/revisions'), Link('tiddlyweb:revisions', tiddler_link + '/revisions') ]
def _tiddler_in_container_info(self, tiddler): """ Get the info for a non-revision tiddler in a list. """ if tiddler.recipe: base = 'recipes' else: base = 'bags' return '<li><a href="%s">%s</a></li>' % (tiddler_url( self.environ, tiddler, container=base, full=False), tiddler.title.replace(' ', ' ', 1))
def _tiddler_revision_info(self, tiddler): """ Get the individual revision info for listing revisions. """ if tiddler.recipe: base = 'recipes' else: base = 'bags' return ( '<li><a href="%s/revisions/%s">%s:%s</a></li>' % (tiddler_url(self.environ, tiddler, container=base, full=False), tiddler.revision, tiddler.title, tiddler.revision))
def test_tiddler_url(): tiddler = Tiddler('foobar') tiddler.bag = 'zoom' url = tiddler_url(environ, tiddler) assert url == 'http://our_test_domain:8001/bags/zoom/tiddlers/foobar' tiddler.recipe = 'car' url = tiddler_url(environ, tiddler, container='recipes') assert url == 'http://our_test_domain:8001/recipes/car/tiddlers/foobar' url = tiddler_url(environ, tiddler, container='recipes', full=False) assert url == '/recipes/car/tiddlers/foobar' config['server_prefix'] = '/sleep' url = tiddler_url(environ, tiddler, container='recipes', full=False) assert url == '/sleep/recipes/car/tiddlers/foobar' url = tiddler_url(environ, tiddler) assert url == 'http://our_test_domain:8001/sleep/bags/zoom/tiddlers/foobar' tiddler.fields['_canonical_uri'] = 'http://example.com' url = tiddler_url(environ, tiddler) assert url == 'http://our_test_domain:8001/sleep/bags/zoom/tiddlers/foobar'
def test_tiddler_url(): tiddler = Tiddler('foobar') tiddler.bag = 'zoom' url = tiddler_url(environ, tiddler) assert url == 'http://our_test_domain:8001/bags/zoom/tiddlers/foobar' tiddler.recipe = 'car' url = tiddler_url(environ, tiddler, container='recipes') assert url == 'http://our_test_domain:8001/recipes/car/tiddlers/foobar' url = tiddler_url(environ, tiddler, container='recipes', full=False) assert url == '/recipes/car/tiddlers/foobar' config['server_prefix'] = '/sleep' url = tiddler_url(environ, tiddler, container='recipes', full=False) assert url == '/sleep/recipes/car/tiddlers/foobar' url = tiddler_url(environ, tiddler) assert url == 'http://our_test_domain:8001/sleep/bags/zoom/tiddlers/foobar' tiddler.fields['_canonical_uri'] = 'http://example.com' url = tiddler_url(environ, tiddler) # we decided url is always local #assert url == 'http://example.com' assert url == 'http://our_test_domain:8001/sleep/bags/zoom/tiddlers/foobar'
def tiddler_as(self, tiddler): """ Transform the provided tiddler into an HTML representation of the tiddler packaged in a DIV. Render the content using the render_wikitext subsystem. Links to the tiddler in the wiki are provided. """ user = self.environ['tiddlyweb.usersign'] store = self.environ['tiddlyweb.store'] if tiddler.recipe: list_link = '/recipes/%s/tiddlers' % encode_name(tiddler.recipe) list_title = 'Tiddlers in Recipe %s' % tiddler.recipe else: list_link = '/bags/%s/tiddlers' % encode_name(tiddler.bag) list_title = 'Tiddlers in Bag %s' % tiddler.bag try: store.get(Bag(tiddler.bag)).policy.allows(user, 'manage') container_policy = True except PermissionsError: container_policy = False if not self.environ['tiddlyweb.space_settings'].get('index', None): space_link = self._space_link(tiddler) else: space_link = '' try: modifier_link = space_uri(self.environ, tiddler.modifier) except AttributeError: modifier_link = "" try: creator_link = space_uri(self.environ, tiddler.creator) except AttributeError: creator_link = "" html = render_wikitext(tiddler, self.environ) return send_template( self.environ, 'tiddler.html', { 'meta_keywords': ', '.join(tiddler.tags), 'meta_description': tiddler.title, 'title': '%s' % tiddler.title, 'tags': tiddler.tags, 'modifier_link': modifier_link, 'creator_link': creator_link, 'fields': tiddler.fields, 'html': html, 'list_link': list_link, 'list_title': list_title, 'space_link': space_link, 'tiddler': tiddler, 'container_policy': container_policy, 'tiddler_url': tiddler_url(self.environ, tiddler) })
def _tiddler_revision_info(self, tiddler): """ Get the individual revision info for listing revisions. """ if tiddler.recipe: base = 'recipes' else: base = 'bags' return ('<li><a href="%s/revisions/%s">%s:%s</a></li>' % ( tiddler_url(self.environ, tiddler, container=base, full=False), tiddler.revision, tiddler.title, tiddler.revision))
def tiddler_as(self, tiddler): """ Transform the provided tiddler into an HTML representation of the tiddler packaged in a DIV. Render the content using the render_wikitext subsystem. Links to the tiddler in the wiki are provided. """ user = self.environ['tiddlyweb.usersign'] store = self.environ['tiddlyweb.store'] if tiddler.recipe: list_link = '/recipes/%s/tiddlers' % encode_name(tiddler.recipe) list_title = 'Tiddlers in Recipe %s' % tiddler.recipe else: list_link = '/bags/%s/tiddlers' % encode_name(tiddler.bag) list_title = 'Tiddlers in Bag %s' % tiddler.bag try: store.get(Bag(tiddler.bag)).policy.allows(user, 'manage') container_policy = True except PermissionsError: container_policy = False if not self.environ['tiddlyweb.space_settings'].get('index', None): space_link = self._space_link(tiddler) else: space_link = '' try: modifier_link = space_uri(self.environ, tiddler.modifier) except AttributeError: modifier_link = "" try: creator_link = space_uri(self.environ, tiddler.creator) except AttributeError: creator_link = "" html = render_wikitext(tiddler, self.environ) return send_template(self.environ, 'tiddler.html', { 'meta_keywords': ', '.join(tiddler.tags), 'meta_description': tiddler.title, 'title': '%s' % tiddler.title, 'tags': tiddler.tags, 'modifier_link': modifier_link, 'creator_link': creator_link, 'fields': tiddler.fields, 'html': html, 'list_link': list_link, 'list_title': list_title, 'space_link': space_link, 'tiddler': tiddler, 'container_policy': container_policy, 'tiddler_url': tiddler_url(self.environ, tiddler)})
def _tiddler_links(self, tiddler): """ The links to provide with a single tiddler. """ links = [] tiddler_link = tiddler_url(self.environ, tiddler, full=True) collection_link = self._tiddlers_links(Tiddlers(), tiddler)['self'] links.append(Link('tiddlyweb:tiddlers', collection_link)) links.append(Link('collection', collection_link)) links.append(Link('tiddlyweb:bag', bag_url(self.environ, Bag(tiddler.bag), full=True))) if tiddler.recipe: links.append(Link('tiddlyweb:recipe', recipe_url(self.environ, Recipe(tiddler.recipe), full=True))) links.append(Link('self', tiddler_link)) return links
def create(environ, start_response): store = environ['tiddlyweb.store'] username = environ['tiddlyweb.usersign']['name'] query = environ['tiddlyweb.query'] title = query['title'][0] text = query['text'][0] tiddler = Tiddler(title, 'common') tiddler.text = text tiddler.modifier = username store.put(tiddler) start_response('303 Found', [('Location', tiddler_url(environ, tiddler))]) return []
def create(environ, start_response): store = environ['tiddlyweb.store'] username = environ['tiddlyweb.usersign']['name'] query = environ['tiddlyweb.query'] title = query['title'][0] text = query['text'][0] tiddler = Tiddler(title, 'common') tiddler.text = text tiddler.modifier = username store.put(tiddler) start_response('303 Found', [ ('Location', tiddler_url(environ, tiddler))]) return []
def _put_tiddler(environ, start_response, tiddler): """ The guts of putting a tiddler into the store. There's a fair bit of special handling done here depending on whether the tiddler already exists or not. """ store = environ['tiddlyweb.store'] try: bag = Bag(tiddler.bag) _check_and_validate_tiddler(environ, bag, tiddler) user = environ['tiddlyweb.usersign']['name'] if not user == 'GUEST': tiddler.modifier = user tiddler.modified = current_timestring() try: check_bag_constraint(environ, bag, 'accept') except (PermissionsError) as exc: _validate_tiddler_content(environ, tiddler) store.put(tiddler) except NoBagError as exc: raise HTTP409("Unable to put tiddler, %s. There is no bag named: " "%s (%s). Create the bag." % (tiddler.title, tiddler.bag, exc)) except NoTiddlerError as exc: raise HTTP404('Unable to put tiddler, %s. %s' % (tiddler.title, exc)) except TypeError as exc: raise HTTP409('Unable to put badly formed tiddler, %s:%s. %s' % (tiddler.bag, tiddler.title, exc)) etag = ('ETag', tiddler_etag(environ, tiddler)) response = [('Location', tiddler_url(environ, tiddler))] if etag: response.append(etag) start_response("204 No Content", response) return []
def faq(environ, start_response): store = environ['tiddlyweb.store'] bag = Bag(FAQ_BAG) bag = store.get(bag) tiddlers = [store.get(tiddler) for tiddler in bag.list_tiddlers()] categorical = {} for tiddler in tiddlers: tiddler.fields['url'] = tiddler_url(environ, tiddler) for tag in tiddler.tags: try: categorical[tag].append(tiddler) except KeyError: categorical[tag] = [tiddler] try: requested_category = environ['wsgiorg.routing_args'][1]['category'] all_tags = [requested_category] except KeyError: all_tags = categorical.keys() template = template_env.get_template('faqtop.html') return template.generate(categories=all_tags, info=categorical)
def _put_tiddler(environ, start_response, tiddler): """ The guts of putting a tiddler into the store. There's a fair bit of special handling done here depending on whether the tiddler already exists or not. """ store = environ['tiddlyweb.store'] try: bag = Bag(tiddler.bag) _check_and_validate_tiddler(environ, bag, tiddler) user = environ['tiddlyweb.usersign']['name'] tiddler.modifier = user tiddler.modified = current_timestring() try: check_bag_constraint(environ, bag, 'accept') except (PermissionsError): _validate_tiddler_content(environ, tiddler) store.put(tiddler) except NoBagError as exc: raise HTTP409("Unable to put tiddler, %s. There is no bag named: " "%s (%s). Create the bag." % (tiddler.title, tiddler.bag, exc)) except NoTiddlerError as exc: raise HTTP404('Unable to put tiddler, %s. %s' % (tiddler.title, exc)) except TypeError as exc: raise HTTP409('Unable to put badly formed tiddler, %s:%s. %s' % (tiddler.bag, tiddler.title, exc)) etag = ('ETag', tiddler_etag(environ, tiddler)) response = [('Location', tiddler_url(environ, tiddler))] if etag: response.append(etag) start_response("204 No Content", response) return []
def _embedded_tiddlers(self, tiddlers): """ Calculate the embedded tiddlers, return them, a embed rel and if appropriate a single tiddler holding bag and/or recipe information. """ hal_entities = [] tiddler = None embed_name = 'tiddlyweb:tiddler' for tiddler in tiddlers: links = Links() tiddler_link = tiddler_url(self.environ, tiddler, full=True) if tiddlers.is_revisions: tiddler_link += '/revisions/%s' % encode_name( unicode(tiddler.revision)) embed_name = 'tiddlyweb:revision' links.add(Link('self', tiddler_link)) hal_entity = HalDocument(links, data=self._tiddler_dict(tiddler)) hal_entities.append(hal_entity.structure) return (hal_entities, embed_name, tiddler)
def _tiddler_dict(self, tiddler, fat=False, render=False): """ Select fields from a tiddler to create a dictonary. """ unwanted_keys = ['text', 'store'] wanted_keys = [attribute for attribute in tiddler.slots if attribute not in unwanted_keys] wanted_info = {} for attribute in wanted_keys: wanted_info[attribute] = getattr(tiddler, attribute, None) wanted_info['permissions'] = self._tiddler_permissions(tiddler) wanted_info['uri'] = tiddler_url(self.environ, tiddler) if fat: if binary_tiddler(tiddler): wanted_info['text'] = b64encode(tiddler.text) else: wanted_info['text'] = tiddler.text if render and renderable(tiddler, self.environ): wanted_info['render'] = render_wikitext(tiddler, self.environ) return wanted_info
def _send_tiddler_revisions(environ, start_response, tiddler): """ Push the list of tiddler revisions out the network. """ store = environ['tiddlyweb.store'] title = 'Revisions of Tiddler %s' % tiddler.title title = environ['tiddlyweb.query'].get('title', [title])[0] container = 'recipes' if tiddler.recipe else 'bags' if environ['tiddlyweb.filters']: tiddlers = Tiddlers(title=title) else: tiddlers = Tiddlers(title=title, store=store) tiddlers.is_revisions = True tiddlers.link = '%s/revisions' % tiddler_url(environ, tiddler, container=container, full=False) # Set the container on the tiddlers. Since tiddler.recipe # defaults to None, we're "safe" here. tiddlers.recipe = tiddler.recipe tiddlers.bag = tiddler.bag try: for revision in store.list_tiddler_revisions(tiddler): tmp_tiddler = Tiddler(title=tiddler.title, bag=tiddler.bag) tmp_tiddler.revision = revision tmp_tiddler.recipe = tiddler.recipe tiddlers.add(tmp_tiddler) except NoTiddlerError as exc: # If a tiddler is not present in the store. raise HTTP404('tiddler %s not found, %s' % (tiddler.title, exc)) except NoBagError as exc: raise HTTP404('tiddler %s not found, bag %s does not exist, %s' % (tiddler.title, tiddler.bag, exc)) except StoreMethodNotImplemented: raise HTTP400('no revision support') return send_tiddlers(environ, start_response, tiddlers=tiddlers)
def faq(environ, start_response): store = environ['tiddlyweb.store'] bag = Bag(FAQ_BAG) bag = store.get(bag) tiddlers = [store.get(tiddler) for tiddler in bag.list_tiddlers()] categorical = {} for tiddler in tiddlers: tiddler.fields['url'] = tiddler_url(environ, tiddler) for tag in tiddler.tags: try: categorical[tag].append(tiddler) except KeyError: categorical[tag] = [tiddler] try: requested_category = environ['wsgiorg.routing_args'][1]['category'] all_tags = [requested_category] except KeyError: all_tags = categorical.keys() template = template_env.get_template('faqtop.html') return template.generate( categories=all_tags, info=categorical )
def closet(environ, start_response): """ Read file input and write it to special storage. """ store = environ['tiddlyweb.store'] usersign = environ['tiddlyweb.usersign'] bag_name = get_route_value(environ, 'bag_name') redir = environ['tiddlyweb.query'].get('redir', [False])[0] target_name = environ['tiddlyweb.query'].get('name', [None])[0] bag = store.get(Bag(bag_name)) bag.policy.allows(usersign, 'create') bag.policy.allows(usersign, 'write') files = environ['tiddlyweb.input_files'] if not files: raise HTTP400('missing file input') tiddlers = [] for input_file in files: if target_name is None: target_name = input_file.filename if pseudo_binary(input_file.type): tiddler = _regular_tiddler(environ, bag_name, input_file, target_name) else: tiddler = _binary_tiddler(environ, bag_name, input_file, target_name) tiddlers.append(tiddler) response_code = '303 See Other' if redir else '204 No Content' start_response(response_code, [('Location', tiddler_url(environ, tiddlers[-1]))]) return []
def closet(environ, start_response): """ Read file input and write it to special storage. """ store = environ['tiddlyweb.store'] usersign = environ['tiddlyweb.usersign'] bag_name = get_route_value(environ, 'bag_name') redir = environ['tiddlyweb.query'].get('redir', [False])[0] target_name = environ['tiddlyweb.query'].get('name', [None])[0] bag = store.get(Bag(bag_name)) bag.policy.allows(usersign, 'create') bag.policy.allows(usersign, 'write') files = environ['tiddlyweb.input_files'] if not files: raise HTTP400('missing file input') tiddlers = [] for input_file in files: if target_name is None: target_name = input_file.filename if pseudo_binary(input_file.type): tiddler = _regular_tiddler(environ, bag_name, input_file, target_name) else: tiddler = _binary_tiddler(environ, bag_name, input_file, target_name) tiddlers.append(tiddler) response_code = '303 See Other' if redir else '204 No Content' start_response(response_code, [ ('Location', tiddler_url(environ, tiddlers[-1]))]) return []
except NoBagError, exc: raise HTTP409("Unable to put tiddler, %s. There is no bag named: " \ "%s (%s). Create the bag." % (tiddler.title, tiddler.bag, exc)) except NoTiddlerError, exc: raise HTTP404('Unable to put tiddler, %s. %s' % (tiddler.title, exc)) etag = ('Etag', _tiddler_etag(environ, tiddler)) if etag: response = [etag] if redirect: response.append(('Location', str(redirect[0]))) start_response('303 See Other', response) else: response.append(('Location', web.tiddler_url(environ, tiddler))) start_response("204 No Content", response) return [] class Serialization(SerializationInterface): def as_tiddler(self, tiddler, form): """ turn a form input into a tiddler """ if 'file' in form and getattr(form['file'], 'file', None): my_file = form['file'] if not my_file.file: raise TiddlerFormatError tiddler.type = my_file.type tiddler.text = my_file.file.read() if 'tags' in form:
except NoRecipeError, exc: raise HTTP404('unable to edit %s, recipe %s not found: %s' % (tiddler.title, recipe_name, exc)) except NoBagError, exc: raise HTTP404('unable to edit %s: %s' % (tiddler.title, exc)) bag = Bag(tiddler.bag) try: tiddler = store.get(tiddler) bag = store.get(bag) except (NoTiddlerError, NoBagError), exc: raise HTTP404('tiddler %s not found: %s' % (tiddler.title, exc)) bag.policy.allows(usersign, 'write') cancel_url = tiddler_url(environ, tiddler) server_prefix = environ['tiddlyweb.config']['server_prefix'] environ['tiddlyweb.title'] = 'Edit %s' % tiddler.title start_response('200 OK', [('Content-Type', 'text/html')]) return """ <form action="%s/formeditor" method="POST"> <textarea name="text" rows="10" cols="50"> %s </textarea> <input type="hidden" name="title" value="%s"> <input type="hidden" name="bag" value="%s"> <input type="submit"> <a href="%s">Cancel</a> </form>
try: _check_bag_constraint(environ, bag, 'accept') except (PermissionsError), exc: _validate_tiddler_content(environ, tiddler) store.put(tiddler) except NoBagError, exc: raise HTTP409("Unable to put tiddler, %s. There is no bag named: " \ "%s (%s). Create the bag." % (tiddler.title, tiddler.bag, exc)) except NoTiddlerError, exc: raise HTTP404('Unable to put tiddler, %s. %s' % (tiddler.title, exc)) etag = ('Etag', web.tiddler_etag(environ, tiddler)) response = [('Location', web.tiddler_url(environ, tiddler))] if etag: response.append(etag) start_response("204 No Content", response) return [] def _validate_tiddler_content(environ, tiddler): """ Unless tiddler is valid raise a 409 with the reason why things to check are presumably tags and title, but we don't want to worry about that here, we want to dispatch elsewhere. """ try: validate_tiddler(tiddler, environ)
def post_tiddler_edit(environ, start_response): usersign = environ['tiddlyweb.usersign'] text = environ['tiddlyweb.query'].get('text', [''])[0] title = environ['tiddlyweb.query'].get('title', [''])[0] bag = environ['tiddlyweb.query'].get('bag', [''])[0] return_url = environ['tiddlyweb.query'].get('return_url', [''])[0] store = environ['tiddlyweb.store'] tiddler = Tiddler(title, bag) tiddler.text = text tiddler.modifier = usersign['name'] bag = Bag(bag) try: bag = store.get(bag) except NoBagError, exc: raise HTTP404('tiddler %s not found: %s' % (tiddler.title, exc)) bag.policy.allows(usersign, 'write') store.put(tiddler) if not return_url: return_url = tiddler_url(environ, tiddler) raise HTTP303(return_url) def _decode_name(name): return urllib.unquote(name).decode('utf-8')
output_bag = Bag('output', tmpbag=True) output_bag.add_tiddler(tiddler) def create_dynamic_recipe(environ,recipeDefinition): recipe = Recipe('myRecipe') recipe.set_recipe(recipeDefinition) recipe.store = environ['tiddlyweb.store'] return recipe def add_magic_tiddler(bag, title, text): tiddler = Tiddler(title, 'tmp') tiddler.text = text tiddler.tags = ['excludeLists'] bag.add_tiddler(tiddler) add_magic_tiddler(output_bag, 'MainMenu', '[[Back to TiddlyWeb|%s]]' % tiddler_url(environ, tiddler)) add_magic_tiddler(output_bag, 'DefaultTiddlers', '[[%s]]' % tiddler_name) add_magic_tiddler(output_bag, 'SiteTitle', 'Editor for %s' % tiddler_name) add_magic_tiddler(output_bag, 'SiteSubtitle', '') add_magic_tiddler(output_bag, 'SideBarOptions', '') try: rule =environ['tiddlyweb.config']['tiddlyeditor_recipe'] recipe = create_dynamic_recipe(environ,rule) tiddlers = control.get_tiddlers_from_recipe(recipe) for tid in tiddlers: output_bag.add_tiddler(tid) except KeyError: for required_tiddler in environ['tiddlyweb.config'].get('tiddlyeditor_tiddlers', []): r_tiddler = Tiddler(required_tiddler[1], required_tiddler[0]) r_tiddler = store.get(r_tiddler)
def _get_entry_base(self, tiddler): if tiddler.recipe: url = tiddler_url(self.environ, tiddler, container='recipes') else: url = tiddler_url(self.environ, tiddler) return url.rsplit('/', 1)[0] + '/'
raise HTTP404('tiddler %s not found: %s' % (tiddler.title, exc)) bag.policy.allows(usersign, 'write') output_bag = Bag('output', tmpbag=True) output_bag.add_tiddler(tiddler) def add_magic_tiddler(bag, title, text): tiddler = Tiddler(title, 'tmp') tiddler.text = text tiddler.tags = ['excludeLists'] bag.add_tiddler(tiddler) add_magic_tiddler( output_bag, 'MainMenu', '[[Back to TiddlyWeb|%s]]' % tiddler_url(environ, tiddler)) add_magic_tiddler(output_bag, 'DefaultTiddlers', '[[%s]]' % tiddler_name) add_magic_tiddler(output_bag, 'SiteTitle', 'Editor for %s' % tiddler_name) add_magic_tiddler(output_bag, 'SiteSubtitle', '') add_magic_tiddler(output_bag, 'SideBarOptions', '') for required_tiddler in environ['tiddlyweb.config'].get( 'tiddlyeditor_tiddlers', []): r_tiddler = Tiddler(required_tiddler[1], required_tiddler[0]) r_tiddler = store.get(r_tiddler) if 'excludeLists' not in r_tiddler.tags: r_tiddler.tags.append('excludeLists') output_bag.add_tiddler(r_tiddler) environ['tiddlyweb.type'] = 'text/x-tiddlywiki' return send_tiddlers(environ, start_response, output_bag)