coleifer / flask-peewee

flask integration for peewee, including admin, authentication, rest api and more
http://flask-peewee.readthedocs.org/
MIT License
778 stars 179 forks source link

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

Open kramer65 opened 10 years ago

kramer65 commented 10 years ago

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 commented 10 years ago

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!

coleifer commented 10 years ago

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!

kramer65 commented 10 years ago

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..

coleifer commented 10 years ago

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

admin = Admin(app, auth, prefix='/my-awesome-peewee-admin)
kramer65 commented 10 years ago

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

coleifer commented 10 years ago

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

dommert commented 10 years ago

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.