def get_worksheet_uuid_or_create(base_worksheet_uuid, worksheet_spec): """ Return the uuid of the specified worksheet if it exists. If not, create a new worksheet if the specified worksheet is home_worksheet or dashboard. Otherwise, throw an error. """ try: return canonicalize.get_worksheet_uuid( local.model, request.user, base_worksheet_uuid, worksheet_spec ) except NotFoundError: # A bit hacky, duplicates a bit of canonicalize if (worksheet_spec == '' or worksheet_spec == HOME_WORKSHEET) and request.user: return new_worksheet(spec_util.home_worksheet(request.user.user_name)) elif spec_util.is_dashboard(worksheet_spec): return new_worksheet(worksheet_spec) else: raise
def ensure_unused_worksheet_name(name): """ Ensure worksheet names are unique. Note: for simplicity, we are ensuring uniqueness across the system, even on worksheet names that the user may not have access to. """ # If trying to set the name to a home worksheet, then it better be # user's home worksheet. if (spec_util.is_home_worksheet(name) and spec_util.home_worksheet(request.user.user_name) != name): raise UsageError( 'Cannot create %s because this is potentially the home worksheet of another user' % name) try: canonicalize.get_worksheet_uuid(local.model, request.user, None, name) raise UsageError('Worksheet with name %s already exists' % name) except NotFoundError: pass # all good!
def get_worksheet_uuid_or_create(base_worksheet_uuid, worksheet_spec): """ Return the uuid of the specified worksheet if it exists. If not, create a new worksheet if the specified worksheet is home_worksheet or dashboard. Otherwise, throw an error. """ try: return canonicalize.get_worksheet_uuid( local.model, request.user, base_worksheet_uuid, worksheet_spec ) except NotFoundError: # A bit hacky, duplicates a bit of canonicalize if (worksheet_spec == '' or worksheet_spec == HOME_WORKSHEET) and request.user: return new_worksheet(spec_util.home_worksheet(request.user.user_name)) elif spec_util.is_dashboard(worksheet_spec): return new_worksheet(worksheet_spec) else: raise
def ensure_unused_worksheet_name(name): """ Ensure worksheet names are unique. Note: for simplicity, we are ensuring uniqueness across the system, even on worksheet names that the user may not have access to. """ # If trying to set the name to a home worksheet, then it better be # user's home worksheet. if ( spec_util.is_home_worksheet(name) and spec_util.home_worksheet(request.user.user_name) != name ): raise UsageError( 'Cannot create %s because this is potentially the home worksheet of another user' % name ) try: canonicalize.get_worksheet_uuid(local.model, request.user, None, name) raise UsageError('Worksheet with name %s already exists' % name) except NotFoundError: pass # all good!
def get_worksheet_uuid(model, user, base_worksheet_uuid, worksheet_spec): """ Resolve a string worksheet_spec to a unique worksheet uuid. If base_worksheet_uuid specified, then try to resolve worksheet_spec in the context of base_worksheet_uuid. """ worksheet_spec = worksheet_spec.strip() if (worksheet_spec == '' or worksheet_spec == HOME_WORKSHEET) and user: worksheet_spec = spec_util.home_worksheet(user.user_name) if not worksheet_spec: raise UsageError('Tried to expand empty worksheet_spec!') if spec_util.UUID_REGEX.match(worksheet_spec): return worksheet_spec if spec_util.UUID_PREFIX_REGEX.match(worksheet_spec): worksheets = model.batch_get_worksheets( fetch_items=False, uuid=LikeQuery(worksheet_spec + '%'), base_worksheet_uuid=base_worksheet_uuid, ) message = "uuid starting with '%s'" % (worksheet_spec,) else: spec_util.check_name(worksheet_spec) worksheets = model.batch_get_worksheets( fetch_items=False, name=worksheet_spec, base_worksheet_uuid=base_worksheet_uuid ) message = "name '%s'" % (worksheet_spec,) if not worksheets: raise NotFoundError('No worksheet found with %s' % (message,)) if len(worksheets) > 1: raise UsageError( 'Found multiple worksheets with %s:%s' % (message, ''.join('\n %s' % (worksheet,) for worksheet in worksheets)) ) return worksheets[0].uuid
def home_worksheet(self, username): return spec_util.home_worksheet(username)
def home_worksheet(self, username): return spec_util.home_worksheet(username)