def create_gist(self, apiuser, files, owner=Optional(OAttr('apiuser')), gist_type=Optional(Gist.GIST_PUBLIC), lifetime=Optional(-1), description=Optional('')): try: if isinstance(owner, Optional): owner = apiuser.user_id owner = get_user_or_error(owner) description = Optional.extract(description) gist_type = Optional.extract(gist_type) lifetime = Optional.extract(lifetime) # files: { # 'filename': {'content':'...', 'lexer': null}, # 'filename2': {'content':'...', 'lexer': null} #} gist = GistModel().create(description=description, owner=owner, gist_mapping=files, gist_type=gist_type, lifetime=lifetime) Session().commit() return dict( msg='created new gist', gist=gist.get_api_data() ) except Exception: log.error(traceback.format_exc()) raise JSONRPCError('failed to create gist')
def create_gist(self, apiuser, files, owner=Optional(OAttr('apiuser')), gist_type=Optional(Gist.GIST_PUBLIC), lifetime=Optional(-1), description=Optional('')): try: if isinstance(owner, Optional): owner = apiuser.user_id owner = get_user_or_error(owner) description = Optional.extract(description) gist_type = Optional.extract(gist_type) lifetime = Optional.extract(lifetime) # files: { # 'filename': {'content':'...', 'lexer': null}, # 'filename2': {'content':'...', 'lexer': null} #} gist = GistModel().create(description=description, owner=owner, gist_mapping=files, gist_type=gist_type, lifetime=lifetime) Session().commit() return dict(msg='created new gist', gist=gist.get_api_data()) except Exception: log.error(traceback.format_exc()) raise JSONRPCError('failed to create gist')
def destroy_gists(self, gistid=None): for g in GistModel.cls.get_all(): if gistid: if gistid == g.gist_access_id: GistModel().delete(g) else: GistModel().delete(g) Session().commit()
def test_index_show_all_hidden_from_regular(self, create_gist): self.log_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS) create_gist('gist2', gist_type='private') create_gist('gist3', gist_type='private') create_gist('gist4', gist_type='private') response = self.app.get(url('gists', all=1)) assert len(GistModel.get_all()) == 3 # since we don't have access to private in this view, we # should see nothing for gist in GistModel.get_all(): response.mustcontain(no=['gist: %s' % gist.gist_access_id])
def test_index_show_all(self, create_gist): self.log_user() create_gist('gist1') create_gist('gist2', lifetime=1400) create_gist('gist3', description='gist3-desc') create_gist('gist4', gist_type='private') response = self.app.get(url('gists', all=1)) assert len(GistModel.get_all()) == 4 # and privates for gist in GistModel.get_all(): response.mustcontain('gist: %s' % gist.gist_access_id)
def edit_form(self, gist_id, format='html'): """GET /admin/gists/gist_id/edit: Form to edit an existing item""" # url('edit_gist', gist_id=ID) self._add_gist_to_context(gist_id) owner = c.gist.gist_owner == c.rhodecode_user.user_id if not (h.HasPermissionAny('hg.admin')() or owner): raise HTTPForbidden() try: c.file_last_commit, c.files = GistModel().get_gist_files(gist_id) except VCSError: log.exception("Exception in gist edit") raise HTTPNotFound() if c.gist.gist_expires == -1: expiry = _('never') else: # this cannot use timeago, since it's used in select2 as a value expiry = h.age(h.time_to_datetime(c.gist.gist_expires)) self.__load_defaults(extra_values=('0', _('%(expiry)s - current value') % { 'expiry': expiry })) return render('admin/gists/edit.html')
def create(self): """POST /admin/gists: Create a new item""" # url('gists') self.__load_defaults() gist_form = GistForm([x[0] for x in c.lifetime_values])() try: form_result = gist_form.to_python(dict(request.POST)) #TODO: multiple files support, from the form filename = form_result['filename'] or 'gistfile1.txt' nodes = { filename: { 'content': form_result['content'], 'lexer': form_result['mimetype'] # None is autodetect } } _public = form_result['public'] gist_type = Gist.GIST_PUBLIC if _public else Gist.GIST_PRIVATE gist = GistModel().create(description=form_result['description'], owner=c.rhodecode_user, gist_mapping=nodes, gist_type=gist_type, lifetime=form_result['lifetime']) Session().commit() new_gist_id = gist.gist_access_id except formencode.Invalid, errors: defaults = errors.value return formencode.htmlfill.render(render('admin/gists/new.html'), defaults=defaults, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8")
def test_create_uses_global_config(self, user_util, backend_hg): model = GistModel() owner = user_util.create_user() repo = backend_hg.create_repo() create_repo_patch = mock.patch.object( RepoModel, '_create_filesystem_repo', return_value=repo.scm_instance()) with create_repo_patch as create_repo_mock: gist_mapping = { 'filename.txt': { 'content': 'Test content' } } model.create('Test description', owner, gist_mapping) _, kwargs = create_repo_mock.call_args assert kwargs['use_global_config'] is True
def delete(self, gist_id): """DELETE /admin/gists/gist_id: Delete an existing item""" # Forms posted to this method should contain a hidden field: # <input type="hidden" name="_method" value="DELETE" /> # Or using helpers: # h.form(url('gist', gist_id=ID), # method='delete') # url('gist', gist_id=ID) gist = GistModel().get_gist(gist_id) owner = gist.gist_owner == c.rhodecode_user.user_id if h.HasPermissionAny('hg.admin')() or owner: GistModel().delete(gist) Session().commit() h.flash(_('Deleted gist %s') % gist.gist_access_id, category='success') else: raise HTTPForbidden() return redirect(url('gists'))
def _create_gist(f_name, content='some gist', lifetime=-1, description='gist-desc', gist_type='public', owner=TEST_USER_ADMIN_LOGIN): gist_mapping = {f_name: {'content': content}} user = User.get_by_username(owner) gist = GistModel().create(description, owner=user, gist_mapping=gist_mapping, gist_type=gist_type, lifetime=lifetime) Session().commit() return gist
def delete_gist(request, apiuser, gistid): """ Deletes existing gist :param apiuser: filled automatically from apikey :type apiuser: AuthUser :param gistid: id of gist to delete :type gistid: str Example output: .. code-block:: bash id : <id_given_in_input> result : { "deleted gist ID: <gist_id>", "gist": null } error : null Example error output: .. code-block:: bash id : <id_given_in_input> result : null error : { "failed to delete gist ID:<gist_id>" } """ gist = get_gist_or_error(gistid) if not has_superadmin_permission(apiuser): if gist.gist_owner != apiuser.user_id: raise JSONRPCError('gist `%s` does not exist' % (gistid,)) try: GistModel().delete(gist) Session().commit() return { 'msg': 'deleted gist ID:%s' % (gist.gist_access_id,), 'gist': None } except Exception: log.exception('Error occured during gist deletion') raise JSONRPCError('failed to delete gist ID:%s' % (gist.gist_access_id,))
def create_gist(self, **kwargs): form_data = { 'description': 'new-gist', 'owner': TEST_USER_ADMIN_LOGIN, 'gist_type': GistModel.cls.GIST_PUBLIC, 'lifetime': -1, 'acl_level': Gist.ACL_LEVEL_PUBLIC, 'gist_mapping': {'filename1.txt': {'content': 'hello world'},} } form_data.update(kwargs) gist = GistModel().create( description=form_data['description'], owner=form_data['owner'], gist_mapping=form_data['gist_mapping'], gist_type=form_data['gist_type'], lifetime=form_data['lifetime'], gist_acl_level=form_data['acl_level'] ) Session().commit() return gist
def create(self): """POST /admin/gists: Create a new item""" # url('gists') self.__load_defaults() gist_form = GistForm([x[0] for x in c.lifetime_values], [x[0] for x in c.acl_options])() try: form_result = gist_form.to_python(dict(request.POST)) # TODO: multiple files support, from the form filename = form_result['filename'] or Gist.DEFAULT_FILENAME nodes = { filename: { 'content': form_result['content'], 'lexer': form_result['mimetype'] # None is autodetect } } _public = form_result['public'] gist_type = Gist.GIST_PUBLIC if _public else Gist.GIST_PRIVATE gist_acl_level = form_result.get('acl_level', Gist.ACL_LEVEL_PRIVATE) gist = GistModel().create(description=form_result['description'], owner=c.rhodecode_user.user_id, gist_mapping=nodes, gist_type=gist_type, lifetime=form_result['lifetime'], gist_id=form_result['gistid'], gist_acl_level=gist_acl_level) Session().commit() new_gist_id = gist.gist_access_id except formencode.Invalid as errors: defaults = errors.value return formencode.htmlfill.render(render('admin/gists/new.html'), defaults=defaults, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) except Exception: log.exception("Exception while trying to create a gist") h.flash(_('Error occurred during gist creation'), category='error') return redirect(url('new_gist')) return redirect(url('gist', gist_id=new_gist_id))
def __call__(self, f_name, content='some gist', lifetime=-1, description='gist-desc', gist_type='public', acl_level=Gist.GIST_PUBLIC, owner=TEST_USER_ADMIN_LOGIN): gist_mapping = {f_name: {'content': content}} user = User.get_by_username(owner) gist = GistModel().create(description, owner=user, gist_mapping=gist_mapping, gist_type=gist_type, lifetime=lifetime, gist_acl_level=acl_level) Session().commit() self._gist_ids.append(gist.gist_id) return gist
def show(self, gist_id, revision='tip', format='html', f_path=None): """GET /admin/gists/gist_id: Show a specific item""" # url('gist', gist_id=ID) self._add_gist_to_context(gist_id) c.render = not request.GET.get('no-render', False) try: c.file_last_commit, c.files = GistModel().get_gist_files( gist_id, revision=revision) except VCSError: log.exception("Exception in gist show") raise HTTPNotFound() if format == 'raw': content = '\n\n'.join([ f.content for f in c.files if (f_path is None or f.path == f_path) ]) response.content_type = 'text/plain' return content return render('admin/gists/show.html')
def edit(self, gist_id): self._add_gist_to_context(gist_id) owner = c.gist.gist_owner == c.rhodecode_user.user_id if not (h.HasPermissionAny('hg.admin')() or owner): raise HTTPForbidden() rpost = request.POST nodes = {} _file_data = zip(rpost.getall('org_files'), rpost.getall('files'), rpost.getall('mimetypes'), rpost.getall('contents')) for org_filename, filename, mimetype, content in _file_data: nodes[org_filename] = { 'org_filename': org_filename, 'filename': filename, 'content': content, 'lexer': mimetype, } try: GistModel().update(gist=c.gist, description=rpost['description'], owner=c.gist.owner, gist_mapping=nodes, gist_type=c.gist.gist_type, lifetime=rpost['lifetime'], gist_acl_level=rpost['acl_level']) Session().commit() h.flash(_('Successfully updated gist content'), category='success') except NodeNotChangedError: # raised if nothing was changed in repo itself. We anyway then # store only DB stuff for gist Session().commit() h.flash(_('Successfully updated gist data'), category='success') except Exception: log.exception("Exception in gist edit") h.flash(_('Error occurred during update of gist %s') % gist_id, category='error') return redirect(url('gist', gist_id=gist_id))
def get_gist(request, apiuser, gistid, content=Optional(False)): """ Get the specified gist, based on the gist ID. :param apiuser: This is filled automatically from the |authtoken|. :type apiuser: AuthUser :param gistid: Set the id of the private or public gist :type gistid: str :param content: Return the gist content. Default is false. :type content: Optional(bool) """ gist = get_gist_or_error(gistid) content = Optional.extract(content) if not has_superadmin_permission(apiuser): if gist.gist_owner != apiuser.user_id: raise JSONRPCError('gist `%s` does not exist' % (gistid,)) data = gist.get_api_data() if content: from rhodecode.model.gist import GistModel rev, gist_files = GistModel().get_gist_files(gistid) data['content'] = dict([(x.path, x.content) for x in gist_files]) return data
def show(self, gist_id, format='html', revision='tip', f_path=None): """GET /admin/gists/gist_id: Show a specific item""" # url('gist', gist_id=ID) c.gist = Gist.get_or_404(gist_id) #check if this gist is not expired if c.gist.gist_expires != -1: if time.time() > c.gist.gist_expires: log.error('Gist expired at %s' % (time_to_datetime(c.gist.gist_expires))) raise HTTPNotFound() try: c.file_changeset, c.files = GistModel().get_gist_files(gist_id) except VCSError: log.error(traceback.format_exc()) raise HTTPNotFound() if format == 'raw': content = '\n\n'.join([ f.content for f in c.files if (f_path is None or f.path == f_path) ]) response.content_type = 'text/plain' return content return render('admin/gists/show.html')
def create_gist( request, apiuser, files, owner=Optional(OAttr('apiuser')), gist_type=Optional(Gist.GIST_PUBLIC), lifetime=Optional(-1), acl_level=Optional(Gist.ACL_LEVEL_PUBLIC), description=Optional('')): """ Creates a new Gist. :param apiuser: This is filled automatically from the |authtoken|. :type apiuser: AuthUser :param files: files to be added to the gist. The data structure has to match the following example:: {'filename': {'content':'...', 'lexer': null}, 'filename2': {'content':'...', 'lexer': null}} :type files: dict :param owner: Set the gist owner, defaults to api method caller :type owner: Optional(str or int) :param gist_type: type of gist ``public`` or ``private`` :type gist_type: Optional(str) :param lifetime: time in minutes of gist lifetime :type lifetime: Optional(int) :param acl_level: acl level for this gist, can be ``acl_public`` or ``acl_private`` If the value is set to ``acl_private`` only logged in users are able to access this gist. If not set it defaults to ``acl_public``. :type acl_level: Optional(str) :param description: gist description :type description: Optional(str) Example output: .. code-block:: bash id : <id_given_in_input> result : { "msg": "created new gist", "gist": {} } error : null Example error output: .. code-block:: bash id : <id_given_in_input> result : null error : { "failed to create gist" } """ try: if isinstance(owner, Optional): owner = apiuser.user_id owner = get_user_or_error(owner) description = Optional.extract(description) gist_type = Optional.extract(gist_type) lifetime = Optional.extract(lifetime) acl_level = Optional.extract(acl_level) gist = GistModel().create(description=description, owner=owner, gist_mapping=files, gist_type=gist_type, lifetime=lifetime, gist_acl_level=acl_level) Session().commit() return { 'msg': 'created new gist', 'gist': gist.get_api_data() } except Exception: log.exception('Error occurred during creation of gist') raise JSONRPCError('failed to create gist')
def tearDown(self): for g in Gist.get_all(): GistModel().delete(g) Session().commit()