def get(environ, start_response): """ Perform a search on the store. What search means and what results are returned is dependent on the search implementation (if any) in the chosen store. """ store = environ['tiddlyweb.store'] tiddlers = get_tiddlers(environ) usersign = environ['tiddlyweb.usersign'] tmp_bag = Bag('tmp_bag', tmpbag=True, searchbag=True) bag_readable = {} for tiddler in tiddlers: try: if bag_readable[tiddler.bag]: tmp_bag.add_tiddler(store.get(tiddler)) except KeyError: bag = Bag(tiddler.bag) bag.skinny = True bag = store.get(bag) try: bag.policy.allows(usersign, 'read') tmp_bag.add_tiddler(store.get(tiddler)) bag_readable[tiddler.bag] = True except(ForbiddenError, UserRequiredError): bag_readable[tiddler.bag] = False return send_tiddlers(environ, start_response, tmp_bag)
def post_createbag(environ, start_response): user = get_user_object(environ) store = environ['tiddlyweb.store'] bag_name = environ['tiddlyweb.query'].get('bag', [''])[0] publicity = environ['tiddlyweb.query'].get('publicity', [''])[0] description = environ['tiddlyweb.query'].get('description', [''])[0] if not bag_name: raise HTTP400('missing data') bag = Bag(bag_name) try: bag.skinny = True bag = store.get(bag) raise HTTP400('bag exists') except NoBagError: pass if publicity == 'public': bag = ensure_public_bag( store, user['name'], desc=description, name=bag_name) elif publicity == 'protected': bag = ensure_protected_bag( store, user['name'], desc=description, name=bag_name) else: bag = ensure_private_bag( store, user['name'], desc=description, name=bag_name) # the bag has already been stored raise HTTP303('%s/tiddlers' % bag_url(environ, bag))
def _process_choices(environ, start_response, form): store = environ['tiddlyweb.store'] user = environ['tiddlyweb.usersign'] tmp_bag = form['tmp_bag'][0] bag = form['target_bag'][0] if bag: bag = Bag(bag) try: bag.skinny = True bag = store.get(bag) except NoBagError: return _send_wimport(environ, start_response, 'chosen bag does not exist') else: bag = form['new_bag'][0] bag = _make_bag(environ, bag) try: bag.policy.allows(user, 'write') except (ForbiddenError, UserRequiredError): return _send_wimport(environ, start_response, 'you may not write to that bag') tiddler_titles = form['tiddler'] for title in tiddler_titles: tiddler = Tiddler(title.decode('utf-8', 'ignore'), tmp_bag) tiddler = store.get(tiddler) tiddler.bag = bag.name store.put(tiddler) tmp_bag = Bag(tmp_bag) store.delete(tmp_bag) bagurl = bag_url(environ, bag) + '/tiddlers' raise HTTP302(bagurl)
def profile_listing_tiddlers(): store = Store('text', environ=environ) environ['tiddlyweb.store'] = store bag = Bag('profiler') bag.skinny = True store.get(bag) print 'filter', time() filter_string = 'select=tag:100' filters, leftovers = parse_for_filters(filter_string, environ) tiddlers = control.filter_tiddlers_from_bag(bag, filters) print 'tmp bag', time() tmp_bag = Bag('tmp_bag', tmpbag=True) tmp_bag.add_tiddlers(tiddlers) print 'output', time() print[tiddler.title for tiddler in control.get_tiddlers_from_bag(tmp_bag)] #print 'serializer', time() #serializer = Serializer('wiki', environ) #print 'wikify', time() #output = serializer.list_tiddlers(tmp_bag) print 'done', time()
def _read_bag_perms(environ, tiddler): perms = [] if 'tiddlyweb.usersign' in environ: store = tiddler.store if store: bag = Bag(tiddler.bag) bag.skinny = True bag = store.get(bag) perms = bag.policy.user_perms(environ['tiddlyweb.usersign']) return perms
def test_put_get_bag(): bag = Bag('testone') bag.policy.read = ['cdent'] store.put(bag) read_bag = Bag('testone') read_bag.skinny = True read_bag = store.get(read_bag) assert read_bag.policy.read == ['cdent']
def get_favorited_bags(store, username): favorites = get_favorites(store, username) bags = [] for favorite in favorites: try: bag = Bag(favorite) bag.skinny = True bags.append(store.get(bag)) except NoBagError: pass # don't care if it doesn't exist return bags
def _get_bag(environ, bag_name, skinny=False): """ Get the named bag out of the store. """ bag = Bag(bag_name) if skinny: bag.skinny = True store = environ["tiddlyweb.store"] try: bag = store.get(bag) except NoBagError, exc: raise HTTP404("%s not found, %s" % (bag.name, exc))
def first_time_check(environ, user): username = user["name"] store = environ["tiddlyweb.store"] try: bag = Bag(username) bag.skinny = True store.get(bag) except NoBagError: ensure_public_bag(store, username) ensure_private_bag(store, username) ensure_public_recipe(store, username) ensure_private_recipe(store, username) ensure_user_bag(store, username)
def _tiddler_permissions(self, tiddler): """ Make a list of the permissions the current user has on this tiddler. """ perms = [] bag = Bag(tiddler.bag) if tiddler.store: bag.skinny = True bag = tiddler.store.get(bag) if 'tiddlyweb.usersign' in self.environ: perms = bag.policy.user_perms(self.environ['tiddlyweb.usersign']) return perms
def get(environ, start_response): """ Perform a search on the store. What search means and what results are returned is dependent on the search implementation (if any) in the chosen store. """ try: search_query = environ['tiddlyweb.query']['q'][0] search_query = urllib.unquote(search_query) search_query = unicode(search_query, 'utf-8') except (KeyError, IndexError): raise HTTP400('query string required') filters = environ['tiddlyweb.filters'] store = environ['tiddlyweb.store'] try: tiddlers = store.search(search_query) except StoreMethodNotImplemented: raise HTTP400('Search system not implemented') usersign = environ['tiddlyweb.usersign'] # It's necessary to get the tiddler off the store # in case we are doing wiki or atom outputs of the # search. tmp_bag = Bag('tmp_bag', tmpbag=True, searchbag=True) bag_readable = {} for tiddler in tiddlers: try: if bag_readable[tiddler.bag]: tmp_bag.add_tiddler(store.get(tiddler)) except KeyError: bag = Bag(tiddler.bag) bag.skinny = True bag = store.get(bag) try: bag.policy.allows(usersign, 'read') tmp_bag.add_tiddler(store.get(tiddler)) bag_readable[tiddler.bag] = True except(ForbiddenError, UserRequiredError): bag_readable[tiddler.bag] = False if len(filters): tiddlers = control.filter_tiddlers_from_bag(tmp_bag, filters) tmp_bag = Bag('tmp_bag', tmpbag=True) tmp_bag.add_tiddlers(tiddlers) return send_tiddlers(environ, start_response, tmp_bag)
def _read_bag_perms(environ, tiddler): """ Read the permissions for the bag containing this tiddler. """ perms = [] if 'tiddlyweb.usersign' in environ: store = tiddler.store if store: bag = Bag(tiddler.bag) bag.skinny = True bag = store.get(bag) perms = bag.policy.user_perms( environ['tiddlyweb.usersign']) return perms
def test_get_bag(): bag = Bag('thing') bag.desc = 'stuff' store.put(bag) if hasattr(bag, 'list_tiddlers'): retrieved = Bag('thing') retrieved.skinny = True retrieved = store.get(retrieved) assert retrieved.desc == 'stuff' retrieved = Bag('thing') retrieved = store.get(retrieved) assert retrieved.desc == 'stuff' else: retrieved = Bag('thing') retrieved = store.get(retrieved) assert retrieved.desc == 'stuff'
def put(environ, start_response): """ Put a bag to the server, meaning the description and policy of the bag, if policy allows. """ bag_name = _determine_bag_name(environ) bag_name = web.handle_extension(environ, bag_name) bag = Bag(bag_name) store = environ['tiddlyweb.store'] length = environ['CONTENT_LENGTH'] usersign = environ['tiddlyweb.usersign'] try: bag.skinny = True bag = store.get(bag) bag.policy.allows(usersign, 'manage') try: delattr(bag, 'skinny') except AttributeError: pass except NoBagError: create_policy_check(environ, 'bag', usersign) try: serialize_type = web.get_serialize_type(environ)[0] serializer = Serializer(serialize_type, environ) serializer.object = bag content = environ['wsgi.input'].read(int(length)) serializer.from_string(content.decode('utf-8')) bag.policy.owner = usersign['name'] _validate_bag(environ, bag) store.put(bag) except TypeError: raise HTTP400('Content-type header required') except NoSerializationError: raise HTTP415('Content type not supported: %s' % serialize_type) start_response("204 No Content", [('Location', web.bag_url(environ, bag))]) return []
def post_createrecipe(environ, start_response): user = get_user_object(environ) store = environ['tiddlyweb.store'] recipe_name = environ['tiddlyweb.query'].get('recipe', [''])[0] bag_name = environ['tiddlyweb.query'].get('bag', [''])[0] publicity = environ['tiddlyweb.query'].get('publicity', [''])[0] description = environ['tiddlyweb.query'].get('description', [''])[0] if not bag_name or not recipe_name: raise HTTP400('missing data') recipe = Recipe(recipe_name) bag = Bag(bag_name) try: recipe = store.get(recipe) raise HTTP400('recipe exists') except NoRecipeError: pass try: bag.skinny = True bag = store.get(bag) try: bag.policy.allows(user, 'read') except (UserRequiredError, ForbiddenError): raise HTTP400('bag not readable') except NoBagError: bag.policy.owner = user['name'] for constraint in ['read', 'write', 'create', 'delete', 'manage']: setattr(bag.policy, constraint, [user['name']]) store.put(bag) if publicity == 'private': recipe.policy.read = [user['name']] else: recipe.policy.read = [] recipe.policy.manage = [user['name']] recipe.policy.owner = user['name'] recipe.desc = description recipe.set_recipe([ ('system', ''), (bag.name, ''), ]) store.put(recipe) raise HTTP303('%s/home' % server_base_url(environ))
def post_profile(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', ['/home'])[0] store = environ['tiddlyweb.store'] tiddler = Tiddler(title, bag) tiddler.text = text tiddler.modifier = usersign['name'] bag = Bag(bag) try: bag.skinny = True bag = store.get(bag) except NoBagError, exc: raise HTTP404('tiddler %s not found: %s' % (tiddler.title, exc))
def profile_listing_tiddlers(): store = Store('text', environ=environ) environ['tiddlyweb.store'] = store bag = Bag('profiler') bag.skinny = True store.get(bag) print 'filter', time() filter_string = 'select=tag:100' filters, leftovers = parse_for_filters(filter_string, environ) tiddlers = control.filter_tiddlers_from_bag(bag, filters) print 'tmp bag', time() tmp_bag = Bag('tmp_bag', tmpbag=True) tmp_bag.add_tiddlers(tiddlers) print 'output', time() print [tiddler.title for tiddler in control.get_tiddlers_from_bag(tmp_bag)]
def get_profiler(environ, start_response): usersign = environ['tiddlyweb.usersign'] store = environ['tiddlyweb.store'] username = usersign['name'] if not 'MEMBER' in usersign['roles']: raise HTTP404('bad edit') tiddler = get_profile(store, usersign, username) bag = Bag(tiddler.bag) bag.skinny = True bag = store.get(bag) bag.policy.allows(usersign, 'write') return_url = '%s/home' % server_base_url(environ) data = {} data['tiddler'] = tiddler data['return_url'] = return_url return send_template(environ, 'profile_edit.html', data)
def get_tiddlers_from_recipe(recipe, environ=None): """ Return the list of tiddlers that result from processing the recipe. This list of tiddlers is unique by title with tiddlers later in the recipe taking precedence over those earlier in the recipe. """ template = _recipe_template(environ) store = recipe.store uniquifier = {} for bag, filter_string in recipe.get_recipe(template): if isinstance(bag, basestring): bag = Bag(name=bag) if store: bag.skinny = True bag = store.get(bag) for tiddler in filter_tiddlers_from_bag(bag, filter_string): uniquifier[tiddler.title] = tiddler return uniquifier.values()
def put(environ, start_response): """ Put a bag to the server, meaning the description and policy of the bag, if policy allows. """ bag_name = _determine_bag_name(environ) bag_name = web.handle_extension(environ, bag_name) bag = Bag(bag_name) store = environ["tiddlyweb.store"] length = environ["CONTENT_LENGTH"] usersign = environ["tiddlyweb.usersign"] try: bag.skinny = True bag = store.get(bag) bag.policy.allows(usersign, "manage") delattr(bag, "skinny") except NoBagError: create_policy_check(environ, "bag", usersign) try: serialize_type = web.get_serialize_type(environ)[0] serializer = Serializer(serialize_type, environ) serializer.object = bag content = environ["wsgi.input"].read(int(length)) serializer.from_string(content.decode("utf-8")) bag.policy.owner = usersign["name"] store.put(bag) except NoSerializationError: raise HTTP415("Content type not supported: %s" % serialize_type) start_response("204 No Content", [("Location", web.bag_url(environ, bag))]) return []
def bag_policy(environ, start_response): user = get_user_object(environ) store = environ['tiddlyweb.store'] publicity = environ['tiddlyweb.query'].get('publicity', [''])[0] bag_name = environ['tiddlyweb.query'].get('bag', [''])[0] bag = Bag(bag_name) bag.skinny = True bag = store.get(bag) bag.policy.allows(user, 'manage') if publicity == 'custom': raise HTTP303(bag_url(environ, bag) + '/tiddlers') if publicity == 'public': bag.policy = public_policy(user['name']) elif publicity == 'protected': bag.policy = protected_policy(user['name']) else: bag.policy = private_policy(user['name']) store.put(bag) raise HTTP303(bag_url(environ, bag) + '/tiddlers')
except NoBagError, exc: raise HTTP404("recipe %s lists an unknown bag: %s" % (recipe.name, exc)) tmp_bag = Bag("tmp_bag1", tmpbag=True) # Make an optimization so we are not going # to the database to load the policies of # the same bag over and over. policies = {} for tiddler in tiddlers: bag_name = tiddler.bag try: policies[bag_name].allows(usersign, "read") except KeyError: bag = Bag(tiddler.bag) bag.skinny = True bag = store.get(bag) policy = bag.policy policies[bag_name] = policy policies[bag_name].allows(usersign, "read") tiddler.recipe = recipe.name tmp_bag.add_tiddler(tiddler) return send_tiddlers(environ, start_response, tmp_bag) def list_recipes(environ, start_response): """ Get a list of all recipes the current user can read. """