def run_tests(): import shutil try: shutil.rmtree('store') except: pass from tiddlyweb.config import config from tiddlywebplugins.utils import get_store from tiddlyweb.model.bag import Bag from tiddlyweb.model.tiddler import Tiddler from tiddlyweb.control import filter_tiddlers store = get_store(config) bag = Bag('place') store.put(bag) for number in range(5): tiddler = Tiddler('tiddler %s' % number, 'place') tiddler.text = 'text %s' % number for tag in range(number): tiddler.tags.append('tag %s' % tag) store.put(tiddler) tiddlers = list(store.list_bag_tiddlers(bag)) assert len(tiddlers) == 5 environ = {'tiddlyweb.store': store} sorts = list( store.get(tiddler) for tiddler in filter_tiddlers( tiddlers, 'sort=-tags', environ=environ)) assert len(sorts[0].tags) == 4 assert len(sorts[4].tags) == 0 selected = list( store.get(tiddler) for tiddler in filter_tiddlers( tiddlers, 'select=tags:4', environ=environ)) assert len(selected) == 1 assert len(selected[0].tags) == 4 selected = list( store.get(tiddler) for tiddler in filter_tiddlers( tiddlers, 'select=tags:1', environ=environ)) assert len(selected) == 1 assert len(selected[0].tags) == 1 selectsort = list( store.get(tiddler) for tiddler in filter_tiddlers( tiddlers, 'select=tags:>2;sort=-tags', environ=environ)) assert len(selectsort) == 2 assert len(selectsort[0].tags) == 4 assert len(selectsort[1].tags) == 3
def manage_gym(environ, start_response): store = environ['tiddlyweb.store'] gym = get_route_value(environ, 'gym') routes_bag = store.get(Bag('%s_climbs' % gym)) news_bag = Bag('%s_news' % gym) # Bail out if we are not allowed to manage. routes_bag.policy.allows(environ['tiddlyweb.usersign'], 'manage') gym_tiddler = store.get(Tiddler(gym, GYMS_BAG)) try: latest_news = [tiddler for tiddler in filter_tiddlers( store.list_bag_tiddlers(news_bag), 'sort=-modified;limit=1', environ)][0] latest_news = store.get(latest_news) news_html = render_wikitext(latest_news, environ) latest_news.fields['html'] = news_html except IndexError: latest_news = Tiddler('tmp') latest_news.fields['html'] = '<p>No News</p>' routes = _get_gym_routes(environ, gym) return send_template(environ, 'manage_gym.html', { 'title': 'Manage %s' % gym, 'gym_tiddler': gym_tiddler, 'latest_news': latest_news, 'routes': routes})
def get_rellinks(environ, tiddler): """ Create a dict of rellinks for this tiddler in this tank. """ store = environ['tiddlyweb.store'] bag_name = tiddler.bag links = {'index': True} if tiddler.title == INDEX_PAGE: links['index'] = False tiddlers = [filtered_tiddler.title for filtered_tiddler in filter_tiddlers(store.list_bag_tiddlers(Bag(bag_name)), 'sort=modified', environ) if renderable(store.get(filtered_tiddler), environ)] try: this_index = tiddlers.index(tiddler.title) prev_index = this_index - 1 if prev_index >= 0: prev_tiddler = tiddlers[prev_index] else: prev_tiddler = None try: next_tiddler = tiddlers[this_index + 1] except IndexError: next_tiddler = None links['prev'] = prev_tiddler links['next'] = next_tiddler except ValueError: pass return links
def get_identities(environ, start_response): """ Get a list of the identities associated with the named user. That named user must match the current user or the current user must be an admin. """ store = environ['tiddlyweb.store'] username = get_route_value(environ, 'username') usersign = environ['tiddlyweb.usersign']['name'] roles = environ['tiddlyweb.usersign']['roles'] if username != usersign and 'ADMIN' not in roles: raise HTTP403('Bad user for action') identities = [] try: mapped_bag = store.get(Bag('MAPUSER')) tiddlers = store.list_bag_tiddlers(mapped_bag) matched_tiddlers = control.filter_tiddlers( tiddlers, 'select=mapped_user:%s' % username, environ) identities = [tiddler.title for tiddler in matched_tiddlers] except NoBagError: pass start_response('200 OK', [('Content-Type', 'application/json; charset=UTF-8')]) return [simplejson.dumps(identities)]
def view_auth(environ, start_response): """ Get a list of extant keys with an interface to revoke and to create more. """ config = environ['tiddlyweb.config'] store = environ['tiddlyweb.store'] usersign = environ['tiddlyweb.usersign'] username = usersign['name'] bag_name = config.get('oauth.tokens_bag', 'oauth_tokens') our_tokens = [store.get(tiddler) for tiddler in filter_tiddlers(store.list_bag_tiddlers(Bag(bag_name)), 'select=modifier:%s' % username, environ)] template = get_template(environ, AUTH_TEMPLATE) start_response('200 OK', [ ('Content-Type', 'text/html; charset=UTF-8'), ('Cache-Control', 'no-cache')]) return template.generate({ 'socket_link': config.get('socket.link'), 'csrf_token': get_nonce(environ), 'gravatar': gravatar(environ), 'user': username, 'tokens': our_tokens, })
def get_identities(environ, start_response): """ Get a list of the identities associated with the named user. That named user must match the current user or the current user must be an admin. """ store = environ['tiddlyweb.store'] username = get_route_value(environ, 'username') usersign = environ['tiddlyweb.usersign']['name'] roles = environ['tiddlyweb.usersign']['roles'] if username != usersign and 'ADMIN' not in roles: raise HTTP403('Bad user for action') identities = [] try: mapped_bag = store.get(Bag('MAPUSER')) tiddlers = store.list_bag_tiddlers(mapped_bag) matched_tiddlers = control.filter_tiddlers(tiddlers, 'select=mapped_user:%s' % username, environ) identities = [tiddler.title for tiddler in matched_tiddlers] except NoBagError: pass start_response('200 OK', [ ('Content-Type', 'application/json; charset=UTF-8')]) return [simplejson.dumps(identities)]
def recent_changes(environ, start_response): """ List recent changes for the named tank. """ tank_name = get_route_value(environ, 'bag_name') store = environ['tiddlyweb.store'] usersign = environ['tiddlyweb.usersign'] config = environ['tiddlyweb.config'] days = environ['tiddlyweb.query'].get('d', [7])[0] try: bag = store.get(Bag(tank_name)) bag.policy.allows(usersign, 'read') except NoBagError: raise HTTP404('no tank found for %s' % tank_name) tiddlers = filter_tiddlers(store.list_bag_tiddlers(bag), 'select=modified:>%sd;sort=-modified' % days, environ) changes_template = get_template(environ, CHANGES_TEMPLATE) start_response('200 OK', [ ('Content-Type', 'text/html; charset=UTF-8'), ('Cache-Control', 'no-cache')]) return changes_template.generate({ 'socket_link': config.get('socket.link'), 'csrf_token': get_nonce(environ), 'days': days, 'tiddlers': tiddlers, 'bag': bag, 'gravatar': gravatar(environ), 'user': usersign['name'], })
def recent_changes(environ, start_response): """ List recent changes for the named tank. """ tank_name = get_route_value(environ, 'bag_name') store = environ['tiddlyweb.store'] usersign = environ['tiddlyweb.usersign'] days = environ['tiddlyweb.query'].get('d', [7])[0] try: bag = store.get(Bag(tank_name)) bag.policy.allows(usersign, 'read') except NoBagError: raise HTTP404('no tank found for %s' % tank_name) tiddlers = (store.get(tiddler) for tiddler in filter_tiddlers( store.list_bag_tiddlers(bag), 'select=modified:>%sd;sort=-modified' % days, environ)) start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'), ('Cache-Control', 'no-cache')]) return send_template(environ, CHANGES_TEMPLATE, { 'days': days, 'tiddlers': tiddlers, 'bag': bag, })
def admin(environ, start_response): store = environ['tiddlyweb.store'] gyms = (store.get(tiddler) for tiddler in filter_tiddlers( store.list_bag_tiddlers(Bag(GYMS_BAG)), 'sort=title', environ)) return send_template(environ, 'admin.html', { 'gyms': gyms, 'title': 'Admin Page'})
def get_notice(environ): store = environ['tiddlyweb.store'] try: tiddlers = filter_tiddlers(store.list_bag_tiddlers(Bag('notifications')), 'sort=-modified;limit=1', environ=environ) tiddler = store.get(tiddlers.next()) except (StopIteration, StoreError): tiddler = Tiddler('profile') tiddler.text = "''No Current Notifications''.\n" return tiddler
def _show_chooser(environ, tmp_bag, fixed_bag): # refresh the bag object store = environ['tiddlyweb.store'] tmp_bag.skinny = True tmp_bag = store.get(tmp_bag) tiddlers = filter_tiddlers(store.list_bag_tiddlers(tmp_bag), 'sort=title') template = get_template(environ, 'chooser.html') return template.generate(tiddlers=tiddlers, tmp_bag=tmp_bag.name, fixed_bag=fixed_bag, bags=_get_bags(environ))
def get_notice(environ): store = environ['tiddlyweb.store'] try: tiddlers = filter_tiddlers(store.list_bag_tiddlers( Bag('notifications')), 'sort=-modified;limit=1', environ=environ) tiddler = store.get(tiddlers.next()) except (StopIteration, StoreError): tiddler = Tiddler('profile') tiddler.text = "''No Current Notifications''.\n" return tiddler
def profile_listing_tiddlers(): store = Store('text', environ['tiddlyweb.config']['server_store'][1], environ) environ['tiddlyweb.store'] = store bag = Bag('profiler') print 'filter', time() filter_string = 'select=tag:1' filters, leftovers = parse_for_filters(filter_string, environ) tiddlers = control.filter_tiddlers(store.list_bag_tiddlers(bag), filters, environ=environ) print 'output', time() print [tiddler.title for tiddler in tiddlers]
def _get_gym_routes(environ, gym_name): store = environ['tiddlyweb.store'] gym_bag = Bag('%s_climbs' % gym_name) def _get_and_mangle_tiddler(tiddler): store.get(tiddler) try: tiddler.fields['lineNumber'] = int(tiddler.fields['lineNumber']) except ValueError: pass # don't worry return tiddler return [_get_and_mangle_tiddler(tiddler) for tiddler in filter_tiddlers( store.list_bag_tiddlers(gym_bag), 'select=tag:route', environ)]
def test_select(): # cook some known modified strings in the past nowstring = current_timestring() now = int(nowstring) yearago = str(now - 10000000101) dayago = str(now - 1000101) tiddlers = [] tiddler = Tiddler('now') tiddler.modified = nowstring tiddlers.append(tiddler) tiddler = Tiddler('year') tiddler.modified = yearago tiddlers.append(tiddler) tiddler = Tiddler('day') tiddler.modified = dayago tiddlers.append(tiddler) results = list(filter_tiddlers(tiddlers, 'select=modified:<1y')) assert len(results) == 1 assert results[0].title == 'year' results = list(filter_tiddlers(tiddlers, 'select=modified:>1y')) assert len(results) == 2 assert sorted(tiddler.title for tiddler in results) == ['day', 'now'] results = list(filter_tiddlers(tiddlers, 'select=modified:<1d')) assert len(results) == 2 assert sorted(tiddler.title for tiddler in results) == ['day', 'year'] results = list(filter_tiddlers(tiddlers, 'select=modified:>1m')) assert len(results) == 1 assert results[0].title == 'now'
def get_thing_from_bag(environ,start_response): bagName = environ['wsgiorg.routing_args'][1]['bagName'] try: fieldName = environ['wsgiorg.routing_args'][1]['fieldName'] except KeyError: fieldName = "tags" try: limit = int(environ['tiddlyweb.query']['max'][0]) except KeyError: limit = 0 store = environ['tiddlyweb.store'] bag = store.get(Bag(bagName)) tiddlers = control.filter_tiddlers(control.get_tiddlers_from_bag(bag), environ['tiddlyweb.filters']) start_response('200 OK', [ ('Content-Type', 'text/plain; charset=utf-8') ]) return "\n".join(survey_field_values(environ,tiddlers,fieldName,limit))
def view_auth(environ, start_response): """ Get a list of extant keys with an interface to revoke and to create more. """ config = environ["tiddlyweb.config"] store = environ["tiddlyweb.store"] usersign = environ["tiddlyweb.usersign"] username = usersign["name"] bag_name = config.get("oauth.tokens_bag", "oauth_tokens") our_tokens = [ store.get(tiddler) for tiddler in filter_tiddlers(store.list_bag_tiddlers(Bag(bag_name)), "select=modifier:%s" % username, environ) ] start_response("200 OK", [("Content-Type", "text/html; charset=UTF-8"), ("Cache-Control", "no-cache")]) return send_template(environ, AUTH_TEMPLATE, {"tokens": our_tokens})
def recent_changes(tiddler, environ): """ Display recent changes for the wiki. RecentChanges is handled as a SPECIAL_PAGES, described below. Recent changes are simply the 30 most recently modified tiddlers from the recipe. We make a list of those tiddlers and provide them to the changes.html template. """ # XXX to be strict we should do permissions checking # on the bags of all the tiddlers returned. store = environ["tiddlyweb.store"] recipe = _get_recipe(environ) recipe = store.get(Recipe(recipe)) tiddlers = Tiddlers() for tiddler in control.get_tiddlers_from_recipe(recipe, environ): tiddlers.add(tiddler) tiddlers = control.filter_tiddlers(tiddlers, "sort=-modified;limit=30") template = get_template(environ, "changes.html") environ["tiddlyweb.title"] = "Recent Changes" return template.generate(tiddlers=(store.get(tiddler) for tiddler in tiddlers))
def gym_info(environ, start_response): """ Display the info about a single gym for the public. """ store = environ['tiddlyweb.store'] gym = get_route_value(environ, 'gym') news_tiddlers = [] try: gym_tiddler = store.get(Tiddler(gym, GYMS_BAG)) news_bag = Bag('%s_news' % gym) latest_news = [tiddler for tiddler in filter_tiddlers( store.list_bag_tiddlers(news_bag), 'sort=-created;limit=10', environ)] for tiddler in latest_news: tiddler = store.get(tiddler) news_html = render_wikitext(tiddler, environ) tiddler.fields['html'] = news_html news_tiddlers.append(tiddler) except StoreError, exc: raise HTTP404('that gym does not exist: %s' % exc)
def mapping_list(environ, start_response): """ List the mappings for the current user as a JSON dictionary: mapping uri -> mapped uri. Matching is done based on the user field of the tiddlers in the PRIVATEER bag. """ current_user = environ['tiddlyweb.usersign']['name'] store = environ['tiddlyweb.store'] try: bag = Bag(MAPPING_BAG) tiddlers = filter_tiddlers(store.list_bag_tiddlers(bag), 'select=user:%s' % current_user, environ=environ) results = {} for tiddler in tiddlers: tiddler = store.get(tiddler) results[_mapping_uri(environ, tiddler.title)] = tiddler.fields['uri'] except StoreError, exc: raise HTTP404('Unable to list mappings: %s' % exc)
def view_auth(environ, start_response): """ Get a list of extant keys with an interface to revoke and to create more. """ config = environ['tiddlyweb.config'] store = environ['tiddlyweb.store'] usersign = environ['tiddlyweb.usersign'] username = usersign['name'] bag_name = config.get('oauth.tokens_bag', 'oauth_tokens') our_tokens = [ store.get(tiddler) for tiddler in filter_tiddlers(store.list_bag_tiddlers(Bag( bag_name)), 'select=modifier:%s' % username, environ) ] start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'), ('Cache-Control', 'no-cache')]) return send_template(environ, AUTH_TEMPLATE, { 'tokens': our_tokens, })
def profile_listing_tiddlers(): store = Store('text', environ['tiddlyweb.config']['server_store'][1], environ) environ['tiddlyweb.store'] = store bag = Bag('profiler') print 'filter', time() filter_string = 'select=tag:1' filters, leftovers = parse_for_filters(filter_string, environ) tiddlers = control.filter_tiddlers(store.list_bag_tiddlers(bag), filters, environ=environ) print 'output', time() print[tiddler.title for tiddler in tiddlers] #print 'serializer', time() #serializer = Serializer('wiki', environ) #print 'wikify', time() #output = serializer.list_tiddlers(tmp_bag) print 'done', time()
def recent_changes(environ, start_response): """ List recent changes for the named tank. """ tank_name = get_route_value(environ, "bag_name") store = environ["tiddlyweb.store"] usersign = environ["tiddlyweb.usersign"] days = environ["tiddlyweb.query"].get("d", [7])[0] try: bag = store.get(Bag(tank_name)) bag.policy.allows(usersign, "read") except NoBagError: raise HTTP404("no tank found for %s" % tank_name) tiddlers = ( store.get(tiddler) for tiddler in filter_tiddlers( store.list_bag_tiddlers(bag), "select=modified:>%sd;sort=-modified" % days, environ ) ) start_response("200 OK", [("Content-Type", "text/html; charset=UTF-8"), ("Cache-Control", "no-cache")]) return send_template(environ, CHANGES_TEMPLATE, {"days": days, "tiddlers": tiddlers, "bag": bag})
def sort_tiddlers(tiddlers): return filter_tiddlers(tiddlers, 'sort=-sort_field')