def test_bag_etag(): """ Explicitly test bag_etag method (not used by the core code). """ bag1 = Bag('foo') bag1.desc = 'desc' bag2 = Bag('foo') bag2.desc = 'desc' assert bag_etag(environ, bag1) == bag_etag(environ, bag2)
def create_wiki(environ, name, mode='private', username=None, desc='', validate=True): """ Create a wiki with the name, name. For now a wiki is just a bag a policy. """ store = environ['tiddlyweb.store'] if username is None: username = environ['tiddlyweb.usersign']['name'] bag = Bag(name) # We want this get to fail. try: store.get(bag) return False except NoBagError: pass try: bag.policy = WIKI_MODES[mode](username) except KeyError: bag.policy = WIKI_MODES['private'](username) bag.desc = desc if validate: validate_bag(bag, environ) store.put(bag) return bag
def _create_bag(environ): """Take the form input and turn it into a bag.""" query_data = _flatten_form_data(environ['tiddlyweb.query']) logging.debug(query_data) store = environ['tiddlyweb.store'] try: new_bag_name = query_data['bag_name'] if _bag_exists(store, new_bag_name): raise HTTP409('That bag may not be created.') new_bag = Bag(new_bag_name) username = environ['tiddlyweb.usersign']['name'] new_bag.policy.owner = username new_bag.policy.manage = [username] new_bag.desc = query_data.get('bag_desc', '') for policy_type in ('read', 'write', 'create', 'delete'): texted = query_data.get(policy_type + '_text', None) logging.debug('texted: %s' % texted) if texted: new_bag.policy.__setattr__(policy_type, [x.lstrip().rstrip() for x in texted.split(',')]) else: set = query_data[policy_type] new_bag.policy.__setattr__(policy_type, _policy_form_to_entry(username, set)) store.put(new_bag) except KeyError, exc: raise HTTP400('something went wrong processing for: %s' % exc)
def test_validate_bag_desc(): bag = Bag('barney') bag.desc = '<script>alert("foo");</script>' validate_bag(bag) assert bag.desc == '<script>alert("foo");</script>'
def _bag_list(self, tiddlers): if '/feedbag' in self.environ['selector.matches'][0]: representation_link = '%s/feedbag' % (self._server_prefix()) bag = Bag('feedbag') bag.policy.manage = ["NONE"] bag.policy.delete = ["NONE"] bag.desc = 'Recent Public Stuff' else: name = self.environ['wsgiorg.routing_args'][1]['bag_name'] name = urllib.unquote(name) name = name.decode('utf-8') representation_link = '%s/bags/%s/tiddlers' % ( self._server_prefix(), encode_name(name)) bag = self.environ['tiddlyweb.store'].get(Bag(name)) representations = self._tiddler_list_header(representation_link) user_object = get_user_object(self.environ) publicity = '' try: bag.policy.allows(user_object, 'manage') policy = bag.policy publicity = determine_publicity(user_object, policy) except (UserRequiredError, ForbiddenError): policy = None try: bag.policy.allows(user_object, 'delete') delete = True except (UserRequiredError, ForbiddenError): delete = False data = {'title': 'TiddlyHoster Bag %s' % bag.name, 'policy': policy, 'publicity': publicity, 'delete': delete, 'bag': bag, 'tiddlers': tiddlers, 'representations': representations} del self.environ['tiddlyweb.title'] return send_template(self.environ, 'baglist.html', data)
def _create_bag(environ): """Take the form input and turn it into a bag.""" query_data = _flatten_form_data(environ['tiddlyweb.query']) logging.debug(query_data) store = environ['tiddlyweb.store'] try: new_bag_name = query_data['bag_name'] if _bag_exists(store, new_bag_name): raise HTTP409('That bag may not be created.') new_bag = Bag(new_bag_name) username = environ['tiddlyweb.usersign']['name'] new_bag.policy.owner = username new_bag.policy.manage = [username] new_bag.desc = query_data.get('bag_desc', '') for policy_type in ('read', 'write', 'create', 'delete'): texted = query_data.get(policy_type + '_text', None) logging.debug('texted: %s' % texted) if texted: new_bag.policy.__setattr__( policy_type, [x.lstrip().rstrip() for x in texted.split(',')]) else: set = query_data[policy_type] new_bag.policy.__setattr__( policy_type, _policy_form_to_entry(username, set)) store.put(new_bag) except KeyError, exc: raise HTTP400('something went wrong processing for: %s' % exc)
def _ensure_bags(self, environ, username): # for privacy in ["public", "protected", "private"]: for privacy in ["private", "protected"]: only_user = ["admin", username] if privacy=="public": readPerms = [] writePerms = [] elif privacy=="protected": readPerms = [] writePerms = only_user elif privacy=="private": readPerms = only_user writePerms = only_user bag = Bag(privacy+"-"+username) store = environ['tiddlyweb.store'] try: store.get(bag) return except NoBagError: bag.desc = 'tiddlyguv private user bag' bag.policy.owner = username bag.policy.manage = ["admin", username] bag.policy.read = readPerms bag.policy.write = writePerms bag.policy.create = ["admin", username] bag.policy.delete = ["admin", username] store.put(bag)
def test_store_bag(): bag_in = Bag('bagone') bag_in.desc = 'bag description' bag_in.policy.read = ['reader'] bag_in.policy.write = ['writer'] store.put(bag_in) bag_out = store.get(Bag('bagone')) assert bag_out.name == bag_in.name
def test_bag_as_yaml(): bag = Bag('test bag') tiddlers = [Tiddler('tiddler number ' + str(name)) for name in xrange(2)] bag.desc = 'a bag of tiddlers' bag.policy.manage = ['NONE'] bag.policy.delete = ['go away'] serializer.object = bag string = serializer.to_string() assert string.startswith(u'desc: a bag of tiddlers\n') assert u"\n delete:\n - go away\n" in string assert u"\n manage:\n - NONE\n" in string
def test_bag_has_description(): """ Confirm a bag can set and use a description. """ bag = Bag('hasbeen', desc='monkey puzzle') assert bag.name == 'hasbeen' assert bag.desc == 'monkey puzzle' bag.desc = 'collapsing sideburns' assert bag.desc == 'collapsing sideburns'
def test_store_bag(): bag = Bag('bag1') bag.desc = 'bag 1 desc' store.put(bag) # these should be _description, not description etc assert os.path.exists('store/bag1/description') assert os.path.exists('store/bag1/policy') loaded_bag = Bag('bag1') loaded_bag = store.get(loaded_bag) assert loaded_bag.desc == bag.desc
def test_store_bag(): bag = Bag('testone') bag.desc = 'testone' bag.policy.accept.append('cdent') store.put(bag) bag2 = Bag('testone') bag2 = store.get(bag2) assert bag.desc == bag2.desc assert bag.name == bag2.name assert bag.policy.accept == bag2.policy.accept
def test_delete(): bag = Bag('deleteme') bag.desc = 'delete me please' store.put(bag) stored_bag = Bag('deleteme') stored_bag = store.get(stored_bag) assert stored_bag.desc == 'delete me please' deleted_bag = Bag('deleteme') store.delete(deleted_bag) py.test.raises(NoBagError, 'store.get(deleted_bag)') py.test.raises(NoBagError, 'store.delete(deleted_bag)')
def test_delete(): bag = Bag("deleteme") bag.desc = "delete me please" store.put(bag) stored_bag = Bag("deleteme") stored_bag = store.get(stored_bag) assert stored_bag.desc == "delete me please" deleted_bag = Bag("deleteme") store.delete(deleted_bag) py.test.raises(NoBagError, "store.get(deleted_bag)") py.test.raises(NoBagError, "store.delete(deleted_bag)")
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 ensure_bag(bag_name, store, policy_dict={}, description='', owner=None): """ Ensure that bag with name bag_name exists in store. If not, create it with owner, policy and description optionally provided. In either case return the bag object. """ bag = Bag(bag_name) try: bag = store.get(bag) except NoBagError: bag.desc = description if owner: bag.policy.owner = owner bag.policy.manage = [owner] for key in policy_dict: bag.policy.__setattr__(key, policy_dict[key]) store.put(bag) return bag
def _check_bag(name, environ, user): """ Get the user's bag. Create if required. """ store = environ['tiddlyweb.store'] name = '%s-%s' % (user, name) name = name.replace('.', '_') try: bag = Bag(name) bag = store.get(bag) except NoBagError: uni_user = unicode(user) policy = Policy(owner=uni_user, manage=[uni_user], read=[uni_user], write=[uni_user], delete=[uni_user], create=[uni_user]) bag.policy = policy bag.desc = 'Twotes for %s' % uni_user; store.put(bag) return bag
def _bag_list(self, tiddlers): if '/feedbag' in self.environ['selector.matches'][0]: representation_link = '%s/feedbag' % (self._server_prefix()) bag = Bag('feedbag') bag.policy.manage = ["NONE"] bag.policy.delete = ["NONE"] bag.desc = 'Recent Public Stuff' else: name = self.environ['wsgiorg.routing_args'][1]['bag_name'] name = urllib.unquote(name) name = name.decode('utf-8') representation_link = '%s/bags/%s/tiddlers' % ( self._server_prefix(), encode_name(name)) bag = self.environ['tiddlyweb.store'].get(Bag(name)) representations = self._tiddler_list_header(representation_link) user_object = get_user_object(self.environ) publicity = '' try: bag.policy.allows(user_object, 'manage') policy = bag.policy publicity = determine_publicity(user_object, policy) except (UserRequiredError, ForbiddenError): policy = None try: bag.policy.allows(user_object, 'delete') delete = True except (UserRequiredError, ForbiddenError): delete = False data = { 'title': 'TiddlyHoster Bag %s' % bag.name, 'policy': policy, 'publicity': publicity, 'delete': delete, 'bag': bag, 'tiddlers': tiddlers, 'representations': representations } del self.environ['tiddlyweb.title'] return send_template(self.environ, 'baglist.html', data)
def patch_bag(environ, start_response): """ PATCH a bag entity, resetting the description, or resetting one or more policy constraints. Sample JSON looks like this: {"desc": "oh hi", "policy": {"read": ["frank"], "vroom": ["frank"], "write": ["ANY"]}} """ store = environ['tiddlyweb.store'] length, content_type = content_length_and_type(environ) if content_type != 'application/json': raise HTTP415('application/json required') bag_name = get_route_value(environ, 'bag_name') bag = Bag(bag_name) try: bag = store.get(bag) except NoBagError: raise HTTP409('Unable to patch non-existent bag') data = _read_input(environ, length) try: for key, value in data.iteritems(): if key == 'desc': bag.desc = value elif key == 'policy': for constraint, rules in value.iteritems(): if constraint in Policy.attributes: setattr(bag.policy, constraint, rules) except AttributeError, exc: raise HTTP400('Malformed bag info: %s' % exc)
def new_wiki(environ, start_response): username = environ['tiddlyweb.usersign']['name'] if username == 'GUEST': raise UserRequiredError store = environ['tiddlyweb.store'] wikiname = environ['tiddlyweb.query'].get('wikiname', [''])[0] perms = environ['tiddlyweb.query'].get('perms', [''])[0] if wikiname: bag = Bag(wikiname) try: bag = store.get(bag) raise HTTP409('The bag already exists') except NoBagError: bag.desc = 'hello' bag.policy.owner = username bag.policy.manage = [username] if perms == 'closed': bag.policy.read = [username] bag.policy.write = [username] bag.policy.create = [username] bag.policy.delete = [username] if perms == 'authrw': bag.policy.read = ['ANY'] bag.policy.write = ['ANY'] bag.policy.create = ['ANY'] bag.policy.delete = ['ANY'] if perms == 'read': bag.policy.write = [username] bag.policy.create = [username] bag.policy.delete = [username] if perms == 'authw': bag.policy.write = ['ANY'] bag.policy.create = ['ANY'] bag.policy.delete = ['ANY'] store.put(bag) recipe = Recipe(wikiname) try: reicpe = store.get(recipe) raise HTTP409('That recipe already exists') except NoRecipeError: recipe.desc = 'hello' recipe.policy.owner = username recipe.policy.manage = [username] recipe.set_recipe([[TIDDLYWEB_BAG, ''], [bag.name, '']]) store.put(recipe) user = User(username) note = '' try: user = store.get(user) note = user.note if not note: note = '' except NoUserError: pass note += '%s\n' % wikiname user.note = note store.put(user) raise HTTP302('%s/spowt2/%s' % (server_base_url(environ), urllib.quote(username))) else: raise HTTP409('Missing form data')
def new_wiki(environ, start_response): username = environ['tiddlyweb.usersign']['name'] if username == 'GUEST': raise UserRequiredError store = environ['tiddlyweb.store'] length = environ['CONTENT_LENGTH'] content = environ['wsgi.input'].read(int(length)) wikiname = cgi.parse_qs(content).get('wikiname', [''])[0] perms = cgi.parse_qs(content).get('perms', [''])[0] if wikiname: bag = Bag(wikiname) try: bag = store.get(bag) raise HTTP409('The bag already exists') except NoBagError: bag.desc = 'hello' bag.policy.owner = username bag.policy.manage = [username] if perms == 'closed': bag.policy.read = [username] bag.policy.write = [username] bag.policy.create = [username] bag.policy.delete = [username] if perms == 'authrw': bag.policy.read = ['ANY'] bag.policy.write = ['ANY'] bag.policy.create = ['ANY'] bag.policy.delete = ['ANY'] if perms == 'read': bag.policy.write = [username] bag.policy.create = [username] bag.policy.delete = [username] if perms == 'authw': bag.policy.write = ['ANY'] bag.policy.create = ['ANY'] bag.policy.delete = ['ANY'] store.put(bag) recipe = Recipe(wikiname) try: recipe = store.get(recipe) raise HTTP409('That recipe already exists') except NoRecipeError: recipe.desc = 'hello' recipe.policy.owner = username recipe.policy.manage = [username] recipe.set_recipe([[TIDDLYWEB_BAG, ''], [bag.name, '']]) store.put(recipe) user = User(username) note = '' try: user = store.get(user) note = user.note except NoUserError: pass note += '%s\n' % wikiname user.note = note store.put(user) raise HTTP302('%s/spowt/%s' % (server_base_url(environ), urllib.quote(username))) else: raise HTTP409('Missing form data')