# Coverage for shopyo_admin.py : 55%

## 29 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 from flask import redirect

2 from flask import request

3 from flask import url_for

4 from flask_admin import AdminIndexView

5 from flask_admin import expose

6 from flask_admin.contrib import sqla as flask_admin_sqla

7 from flask_login import current_user

8

9

10 class DefaultModelView(flask_admin_sqla.ModelView):

11 def __init__(self, *args, **kwargs):

12 super().__init__(*args, **kwargs)

13

14 def is_accessible(self):

15 return current_user.is_authenticated and current_user.is_admin

16

17 def inaccessible_callback(self, name, **kwargs):

18 # redirect to login page if user doesn't have access

19 return redirect(url_for("auth.login", next=request.url))

20

21

22 class MyAdminIndexView(AdminIndexView):

23 def is_accessible(self):

24 return current_user.is_authenticated and current_user.is_admin

25

26 def inaccessible_callback(self, name, **kwargs):

27 # redirect to login page if user doesn't have access

28 return redirect(url_for("auth.login", next=request.url))

29

30 @expose("/")

31 def index(self):

32 if not current_user.is_authenticated and current_user.is_admin:

33 return redirect(url_for("auth.login"))

34 return super().index()

35

36 @expose("/dashboard")

37 def indexs(self):

38 if not current_user.is_authenticated and current_user.is_admin:

39 return redirect(url_for("auth.login"))

40 return super().index()