def _validate_subscription(environ, name, recipe): """ Determine if this space can be subscribed to. We know that the space exists, what we want to determine here is if it has been blacklisted or already been subscribed. """ space = Space(name) if name in environ['tiddlyweb.config'].get('blacklisted_spaces', []): raise HTTP409('Subscription not allowed to space: %s' % name) elif [space.public_bag(), ''] in recipe: raise HTTP409('Space already subscribed: %s' % name) return space
def delete_space_member(environ, start_response): """ Remove a member from a space. If the space does not exist raise 404. If the named member is not in the space, do nothing. """ store = environ['tiddlyweb.store'] space_name = environ['wsgiorg.routing_args'][1]['space_name'] user_name = environ['wsgiorg.routing_args'][1]['user_name'] current_user = environ['tiddlyweb.usersign'] _same_space_required(environ, space_name) try: change_space_member(store, space_name, remove=user_name, current_user=current_user) except (NoBagError, NoRecipeError): raise HTTP404('space %s does not exist' % space_name) except NoUserError: raise HTTP409('attempt to remove non-member user: %s' % user_name) start_response('204 No Content', []) return ['']
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 _do_unsubscriptions(space_name, unsubscriptions, public_recipe_list, private_recipe_list, store): """ Remove unsubscriptions from the space represented by public_recipe_list and private_recipe_list. """ for space in unsubscriptions: if space == space_name: raise HTTP409('Attempt to unsubscribe self') try: unsubscribed_space = Space(space) bag = unsubscribed_space.public_bag() public_recipe_list.remove([bag, ""]) private_recipe_list.remove([bag, ""]) except ValueError, exc: raise HTTP409('Invalid content for unsubscription: %s' % exc)
def add_space_member(environ, start_response): """ Add a member to a space if they are not already a member. If they are already a member, nothing happens. If the username given does not exist, raise 409. If the space does not exist raise 404. """ store = environ['tiddlyweb.store'] space_name = environ['wsgiorg.routing_args'][1]['space_name'] user_name = environ['wsgiorg.routing_args'][1]['user_name'] current_user = environ['tiddlyweb.usersign'] _same_space_required(environ, space_name) try: change_space_member(store, space_name, add=user_name, current_user=current_user) except (NoBagError, NoRecipeError): raise HTTP404('space %s does not exist' % space_name) except NoUserError: raise HTTP409('attempt to add non-existent user: %s' % user_name) start_response('204 No Content', []) return ['']
def _create_recipe(environ): """Take the form input and turn it into a recipe.""" # get bag_names before we flatten because it will be a list bag_names = environ['tiddlyweb.query'].get('bags', []) query_data = _flatten_form_data(environ['tiddlyweb.query']) store = environ['tiddlyweb.store'] try: new_recipe_name = query_data['recipe_name'] if _recipe_exists(store, new_recipe_name): raise HTTP409('That recipe may not be created.') new_recipe = Recipe(new_recipe_name) username = environ['tiddlyweb.usersign']['name'] new_recipe.policy.owner = username new_recipe.policy.manage = [username] new_recipe.desc = query_data.get('recipe_desc', '') privacy = query_data['privacy'] new_recipe.policy.read = _policy_form_to_entry(username, privacy) bag_list = [] if query_data.get('autowiki', 0): bag_list.extend(AUTOWIKI_BAGS) # don't worry about default content bag yet bag_list.extend(bag_names) recipe_list = [[bag_name, ''] for bag_name in bag_list] new_recipe.set_recipe(recipe_list) store.put(new_recipe) except KeyError, exc: raise HTTP400('something went wrong processing for: %s' % exc)
def _validate_recipe(environ, recipe): """ Unless recipe is valid raise a 409 with the reason why. """ try: validate_recipe(recipe, environ) except InvalidBagError, exc: raise HTTP409('Recipe content is invalid: %s' % exc)
def _validate_bag(environ, bag): """ Unless bag is valid raise a 409 with the reason why. """ try: validate_bag(bag, environ) except InvalidBagError, exc: raise HTTP409('Bag content is invalid: %s' % exc)
def _store_tiddler_revisions(environ, content, tiddler): """ Given json revisions in content, store them as a revision history to tiddler. """ try: json_tiddlers = simplejson.loads(content) except ValueError, exc: raise HTTP409('unable to handle json: %s' % exc)
def _validate_space_name(environ, name): """ Determine if space name can be used. We've already checked if the space exists. """ store = environ['tiddlyweb.store'] try: space = Space(name) except ValueError, exc: raise HTTP409(exc)
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) except InvalidTiddlerError, exc: raise HTTP409('Tiddler content is invalid: %s' % exc)
def _get_subscription_info(environ): """ Extract subscription info from the JSON posted to a space. """ try: length = environ['CONTENT_LENGTH'] content = environ['wsgi.input'].read(int(length)) info = simplejson.loads(content) subscriptions = info.get('subscriptions', []) unsubscriptions = info.get('unsubscriptions', []) except (JSONDecodeError, KeyError), exc: raise HTTP409('Invalid content for subscription: %s' % exc)
def _do_subscriptions(environ, subscriptions, public_recipe_list, private_recipe_list, store): """ Add subscriptions to the space represented by public_recipe_list and private_recipe_list. """ for space in subscriptions: try: subscribed_space = _validate_subscription(environ, space, private_recipe_list) except ValueError, exc: raise HTTP409('Invalid space name: %s' % exc) try: subscribed_recipe = store.get( Recipe(subscribed_space.public_recipe())) for bag, filter_string in subscribed_recipe.get_recipe()[2:]: if [bag, filter_string] not in public_recipe_list: public_recipe_list.insert(-1, (bag, filter_string)) if [bag, filter_string] not in private_recipe_list: private_recipe_list.insert(-2, (bag, filter_string)) except NoRecipeError, exc: raise HTTP409('Invalid content for subscription: %s' % exc)
def subscribe_space(environ, start_response): """ Subscribe and/or unsubscribe the spaces named in the JSON content of the request to the space named in the URI. The current user must be a member of the space. Raise 409 if the JSON is no good. Raise 404 if the space does not exist. Raise 409 if a space in the JSON does not exist. """ store = environ['tiddlyweb.store'] space_name = environ['wsgiorg.routing_args'][1]['space_name'] current_user = environ['tiddlyweb.usersign'] try: current_space = Space(space_name) except ValueError, exc: raise HTTP409('Invalid space name: %s' % exc)
extractor. """ content_type = environ['tiddlyweb.type'] if content_type != 'application/json': raise HTTP415('application/json required') length = environ['CONTENT_LENGTH'] content = environ['wsgi.input'].read(int(length)) store = environ['tiddlyweb.store'] try: user_info = simplejson.loads(content) except ValueError, exc: raise HTTP400('Invalid JSON, %s' % exc) try: user = User(user_info['username']) try: store.get(user) raise HTTP409('User exists') except NoUserError: pass # we're carrying on below user.set_password(user_info['password']) except KeyError, exc: raise HTTP400('Missing required data: %s', exc) store.put(user) start_response('201 Created', [('Content-Type', 'text/html; charset=UTF-8')]) return ['Created %s' % user_info['username']]
def _validate_space_name(environ, name): """ Determine if space name can be used. We've already checked if the space exists. """ store = environ['tiddlyweb.store'] try: space = Space(name) except ValueError, exc: raise HTTP409(exc) # This reserved list should/could be built up from multiple # sources. reserved_space_names = environ['tiddlyweb.config'].get( 'socialusers.reserved_names', []) if name in reserved_space_names: raise HTTP409('Invalid space name: %s' % name) try: store.get(Recipe(space.private_recipe())) raise HTTP409('%s already exists as space' % name) except NoRecipeError: pass import tiddlywebplugins.socialusers original_validate_user = tiddlywebplugins.socialusers._validate_user def _validate_user_name(environ, user_info): """ Override socialusers _validate_user.
tiddler.revision = _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), 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): """
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')