authentication example

64 views
Skip to first unread message

jerry_gzy

unread,
Jun 2, 2012, 1:30:06 PM6/2/12
to nagare...@googlegroups.com
hi, I was looking at the Package security these days, and I am wondering if there is any up-to-date example I can have look? many thanks.

apoirier

unread,
Jun 3, 2012, 3:50:01 PM6/3/12
to Nagare users
Hi,

On 2 juin, 19:30, jerry_gzy <zheyu....@gmail.com> wrote:
> hi, I was looking at the Package security these days, and I am wondering if
> there is any up-to-date example I can have look? many thanks.

The wiki[10-16].py examples describe how to use the `nagare.security`
service:

http://www.nagare.org/trac/browser/examples/nagare/examples/wiki/wiki10.py
...
http://www.nagare.org/trac/browser/examples/nagare/examples/wiki/wiki16.py

Jerry Gao

unread,
Jun 14, 2012, 1:27:53 PM6/14/12
to nagare...@googlegroups.com
hi, Alain

here is what I get when I do 

>nagare-admin create-db ./conf/wiki10.cfg 
Traceback (most recent call last):
  File "/Users/Jerrygao/Web2/ve/bin/nagare-admin", line 8, in <module>
    load_entry_point('nagare==0.4.1', 'console_scripts', 'nagare-admin')()
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/nagare/admin/main.py", line 73, in main
    command.run(parser, options, args)  # Run the command
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/nagare/admin/db.py", line 85, in create
    util.load_object(populate)[0]()
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/session.py", line 449, in __exit__
    self.commit()
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/session.py", line 378, in commit
    self._prepare_impl()
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/session.py", line 362, in _prepare_impl
    self.session.flush()
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/session.py", line 1354, in flush
    self._flush(objects)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/session.py", line 1432, in _flush
    flush_context.execute()
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/unitofwork.py", line 261, in execute
    UOWExecutor().execute(self, tasks)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/unitofwork.py", line 753, in execute
    self.execute_save_steps(trans, task)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/unitofwork.py", line 768, in execute_save_steps
    self.save_objects(trans, task)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/unitofwork.py", line 759, in save_objects
    task.mapper._save_obj(task.polymorphic_tosave_objects, trans)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/orm/mapper.py", line 1428, in _save_obj
    c = connection.execute(statement.values(value_params), params)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/engine/base.py", line 824, in execute
    return Connection.executors[c](self, object, multiparams, params)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/engine/base.py", line 874, in _execute_clauseelement
    return self.__execute_context(context)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/engine/base.py", line 896, in __execute_context
    self._cursor_execute(context.cursor, context.statement, context.parameters[0], context=context)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/engine/base.py", line 950, in _cursor_execute
    self._handle_dbapi_exception(e, statement, parameters, cursor, context)
  File "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7.egg/sqlalchemy/engine/base.py", line 931, in _handle_dbapi_exception
    raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect)
sqlalchemy.exc.IntegrityError: (IntegrityError) column pagename is not unique u'INSERT INTO nagare_examples_wiki_wikidata_pagedata (pagename, data, creator) VALUES (?, ?, ?)' [u'FrontPage', u'Welcome to my *WikiWiki* !', u'admin']

I am not sure what does it mean for the 

 raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect)
sqlalchemy.exc.IntegrityError: (IntegrityError) column pagename is not unique u'INSERT INTO nagare_examples_wiki_wikidata_pagedata (pagename, data, creator) VALUES (?, ?, ?)' [u'FrontPage', u'Welcome to my *WikiWiki* !', u'admin']




--
You received this message because you are subscribed to the Google Groups "Nagare users" group.
To post to this group, send email to nagare...@googlegroups.com.
To unsubscribe from this group, send email to nagare-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nagare-users?hl=en.


Laurent Rahuel

unread,
Jun 14, 2012, 3:00:27 PM6/14/12
to nagare...@googlegroups.com
Hi,

Sounds like you already have a page in your database. Did you run any other sample before ?

Try this command line:

nagare-admin create-db ./conf/wiki10.cfg --drop

The --drop argument will remove all existing tables before trying to add table and contents.

Regards,

Laurent

Jerry Gao

unread,
Jun 15, 2012, 1:56:22 PM6/15/12
to nagare...@googlegroups.com
Hi, 

you are right, I tried 

nagare-admin serve ./conf/wiki16.cfg 

and it works, but what's the difference between it and 

nagare-admin serve wiki  ?

apoirier

unread,
Jun 15, 2012, 2:27:37 PM6/15/12
to Nagare users
HI,

On Jun 15, 7:56 pm, Jerry Gao <zheyu....@gmail.com> wrote:
> Hi,
>
> you are right, I tried
>
> nagare-admin serve ./conf/wiki16.cfg
>
> and it works, but what's the difference between it and
>
> nagare-admin serve wiki  ?

`nagare-admin serve wiki` is equal to `nagare-admin serve ./conf/
wiki9.cfg`.
Wiki9 is the more complete example of the Wiki tutorial, without the
security stuff.

The `wiki[10-16].py` are the same example but with several level of
security added.

By launching `nagare-admin serve ./conf/wiki[1-16].cfg`, you can test
step-by-step all the wiki examples.
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/session.py",
> > line 449, in __exit__
> > >     self.commit()
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/session.py",
> > line 378, in commit
> > >     self._prepare_impl()
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/session.py",
> > line 362, in _prepare_impl
> > >     self.session.flush()
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/session.py",
> > line 1354, in flush
> > >     self._flush(objects)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/session.py",
> > line 1432, in _flush
> > >     flush_context.execute()
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/unitofwork.py",
> > line 261, in execute
> > >     UOWExecutor().execute(self, tasks)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/unitofwork.py",
> > line 753, in execute
> > >     self.execute_save_steps(trans, task)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/unitofwork.py",
> > line 768, in execute_save_steps
> > >     self.save_objects(trans, task)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/unitofwork.py",
> > line 759, in save_objects
> > >     task.mapper._save_obj(task.polymorphic_tosave_objects, trans)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/orm/mapper.py",
> > line 1428, in _save_obj
> > >     c = connection.execute(statement.values(value_params), params)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/engine/base.py",
> > line 824, in execute
> > >     return Connection.executors[c](self, object, multiparams, params)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/engine/base.py",
> > line 874, in _execute_clauseelement
> > >     return self.__execute_context(context)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/engine/base.py",
> > line 896, in __execute_context
> > >     self._cursor_execute(context.cursor, context.statement,
> > context.parameters[0], context=context)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/engine/base.py",
> > line 950, in _cursor_execute
> > >     self._handle_dbapi_exception(e, statement, parameters, cursor,
> > context)
> > >   File
> > "/Users/Jerrygao/Web2/ve/lib/python2.7/site-packages/SQLAlchemy-0.5.8-py2.7 .egg/sqlalchemy/engine/base.py",
> > line 931, in _handle_dbapi_exception
> > >     raise exc.DBAPIError.instance(statement, parameters, e,
> > connection_invalidated=is_disconnect)
> > > sqlalchemy.exc.IntegrityError: (IntegrityError) column pagename is not
> > unique u'INSERT INTO nagare_examples_wiki_wikidata_pagedata (pagename,
> > data, creator) VALUES (?, ?, ?)' [u'FrontPage', u'Welcome to my *WikiWiki*
> > !', u'admin']
>
> > > I am not sure what does it mean for the
>
> > >  raise exc.DBAPIError.instance(statement, parameters, e,
> > connection_invalidated=is_disconnect)
> > > sqlalchemy.exc.IntegrityError: (IntegrityError) column pagename is not
> > unique u'INSERT INTO nagare_examples_wiki_wikidata_pagedata (pagename,
> > data, creator) VALUES (?, ?, ?)' [u'FrontPage', u'Welcome to my *WikiWiki*
> > !', u'admin']
>
> > > On 4 June 2012 03:50, apoirier <alain.poiri...@gmail.com> wrote:
> > > Hi,
>
> > > On 2 juin, 19:30, jerry_gzy <zheyu....@gmail.com> wrote:
> > > > hi, I was looking at the Package security these days, and I am
> > wondering if
> > > > there is any up-to-date example I can have look? many thanks.
>
> > > The wiki[10-16].py examples describe how to use the `nagare.security`
> > > service:
>
> >http://www.nagare.org/trac/browser/examples/nagare/examples/wiki/wiki...
> > >  ...
>
> >http://www.nagare.org/trac/browser/examples/nagare/examples/wiki/wiki...

Jerry Gao

unread,
Jul 2, 2012, 6:11:26 AM7/2/12
to nagare...@googlegroups.com
hi, 

Where can I find examples of authentication class using database?

currently I can see from the wiki16

class Authentication(form_auth.Authentication):
    def get_password(self, username):
        return username

    def _create_user(self, username):
        if username is None:
            return None

        if (username == 'john') or (username == 'guest'):
            return User(username, editor_role)

        if username == 'admin':
            return User(username, admin_role)

        return User(username)

which looks like a hardcoded user record, so is there any examples which use database and have user signup function?

crumble

unread,
Jul 6, 2012, 5:56:47 AM7/6/12
to nagare...@googlegroups.com
Hi Jerry,

Actually it is just a matter of using an Entity as your user and change Autentication class accordingly:

class Authentication(form_auth.Authentication):
    def get_password(self, username):
        user = UserData.get_by(username=username)
        return user.password if user is not None else None

    def _create_user(self, username):
        user = UserData.get_by(username=username)

        if user is not None:
            return User(user.username, [r.rolename for r in user.roles])

I joined a modified version of the wiki (wiki17) as an example with matching model and configuration file. Put wiki17.cfg in the same folder as wiki16.cfg and the other 2 files(wiki17.py, wikidata17.py)  in the same folder as wikidata.py.

To run it do not forget to do:

nagare-admin create-db ./conf/wiki17.cfg

and then

nagare-admin serve ./conf/wiki17.cfg

You should be good to go.

cheers,
Crumble

> > > For more options, visit this group at
> >http://groups.google.com/group/nagare-users?hl=en.
>
> > > --
> > > You received this message because you are subscribed to the Google
> > Groups "Nagare users" group.
> > > To post to this group, send email to nagare...@googlegroups.com.
> > > To unsubscribe from this group, send email to

> > > For more options, visit this group at
> >http://groups.google.com/group/nagare-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Nagare users" group.
> > To post to this group, send email to nagare...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > For more options, visit this group at
> >http://groups.google.com/group/nagare-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Nagare users" group.
To post to this group, send email to nagare...@googlegroups.com.
To unsubscribe from this group, send email to nagare-users+unsubscribe@googlegroups.com.
wiki17.cfg
wiki17.py
wikidata17.py
Reply all
Reply to author
Forward
0 new messages