1

I have managed to get roles working with this code but!

class Roled(object):
    def is_accessible(self):
        roles_accepted = getattr(self, 'roles_accepted', None)
        if flask_login.current_user.is_authenticated:
            user = CustomUser.objects.get(email=flask_login.current_user.id)
            for role in user.roles:
                for permission in role.permissions:
                    if permission == "admin":
                        return flask_login.current_user.is_authenticated

    def inaccessible_callback(self, name, **kwargs):
        # redirect to login page if user doesn't have access
        return redirect(url_for('login', next=request.url))


class AdminView(Roled, ModelView):
    def __init__(self, *args, **kwargs):
        self.roles_accepted = kwargs.pop('roles_accepted', list())
        super(AdminView, self).__init__(*args, **kwargs)


class TranslationView(Roled, ModelView):
    column_filters = ['eng']
    column_searchable_list = ('eng','geo', 'name')

Now I have a problem adding roles_accapted to each add_view when adding flask admin views as it was in this question

pjcunningham
  • 7,676
  • 1
  • 36
  • 49
Beqa Bukhradze
  • 409
  • 4
  • 16

1 Answers1

1

Your TranslationView should inherit from the AdminView class. i.e.

class TranslationView(AdminView):
    column_filters = ['eng']
    column_searchable_list = ('eng','geo', 'name')
Tryph
  • 5,946
  • 28
  • 49
pjcunningham
  • 7,676
  • 1
  • 36
  • 49
  • Ok. I understand that but how to pass roles_accepted to AdminView ? this does not work `admin.add_view(AdminUserView(model=User, session=db.session, category="Accounts", name="Users", endpoint="users_admin", roles_accepted=["admin"]))` May it is because I was not inheriting TranslationView from AdminView ? – Beqa Bukhradze Sep 24 '18 at 12:54
  • @BeqaBukhradze - taken to chat. – pjcunningham Sep 24 '18 at 12:59
  • sorry did not get it ? – Beqa Bukhradze Sep 24 '18 at 13:20
  • @BeqaBukhradze - go to [https://chat.stackoverflow.com/rooms/180667/flask-admin-roles-accepted](https://chat.stackoverflow.com/rooms/180667/flask-admin-roles-accepted) – pjcunningham Sep 24 '18 at 14:06