flask-peewee icon indicating copy to clipboard operation
flask-peewee copied to clipboard

How to .create_table() and insert user for peewee-admin to be useful?

Open kramer65 opened this issue 9 years ago • 7 comments

In response to the issue I opened yesterday here I just now see that peewee-admin is not part of peewee, but rather of flask-peewee.

So in order to not get in the way with peewee-admin, I renamed my own User class to something different, but I now get an error saying no such table: user. I suppose I need to do a .create_table() on the User class which is defined here, but I wouldn't know when and where to run that .create_table(). Furthermore, I suppose I would also need to insert a user in the user table, but for this I also wouldn't know when and where?

Are there any docs that describe this, or would you otherwise explain me here how I could do that? All tips are welcome!

kramer65 avatar Oct 22 '14 06:10 kramer65

Okay, I got it working. I subclassed the Auth and Admin classes and just overrid some methods as follows (VG is the name of my website):

class VGAuth(Auth):
    def authenticate(self, username, password):
        if g.user is not None and g.user.is_authenticated():
            return g.user._get_current_object()
        else:
            return False

    def get_logged_in_user(self):
        if g.user is not None and g.user.is_authenticated():
            return g.user._get_current_object()
        else:
            return False

    def load_user(self):
        g.user = current_user


class VGAdmin(Admin):
    def check_user_permission(self, user):
        return True


adminAuth = VGAuth(app, relDb, user_model=User)
admin = VGAdmin(app, adminAuth)

The result is that I now need to login using LinkedIn, then go to the admin login page, enter any credentials (anything will work, since it only checks if the user already logged in using LinkedIn) and hit enter. Although not ideal, it does work now.. :)

If you think there's anything wrong with this way of doing it, I would be very happy to know! If not; thanks for creating peewee, it's the best Python ORM out there!

kramer65 avatar Oct 22 '14 07:10 kramer65

Thanks for posting your findings, I think your approach looks sane but not knowing your application as well as you do, I'm not sure I can comment on how correct things are. Good luck and glad you're enjoying working with peewee!

coleifer avatar Oct 22 '14 16:10 coleifer

One last question: is there a way to change the admin base url from /admin/ to something different like /my-awesome-peewee-admin. I would like to use the /admin route for the user admin part..

kramer65 avatar Oct 24 '14 14:10 kramer65

I believe when instantiating your Admin you pass in a different "prefix", e.g.

admin = Admin(app, auth, prefix='/my-awesome-peewee-admin)

coleifer avatar Oct 24 '14 15:10 coleifer

That's what I thought as well, but it doesn't seem to work. Also, the default seems to be '/accounts', not '/admin': https://github.com/coleifer/flask-peewee/blob/master/flask_peewee/auth.py#L42

kramer65 avatar Oct 27 '14 08:10 kramer65

Hmm...I'm not sure why that isn't working.

coleifer avatar Oct 27 '14 15:10 coleifer

the prefix tag works just fine. i moved my admin with that syntax. I also used subdomain='name' and then it would become name.domain.com. so im not sure what you are doing wrong. coleifer just did a flask blueprint for /admin so if you need to did deeper read about flask blueprints.

dommert avatar Nov 11 '14 15:11 dommert