• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iplweb / bpp / #820

19 Oct 2025 06:59PM UTC coverage: 65.093% (+5.3%) from 59.791%
#820

push

coveralls-python

MichaƂ Pasternak
Fixes

4215 of 9430 branches covered (44.7%)

Branch coverage included in aggregate %.

27562 of 39388 relevant lines covered (69.98%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.35
src/bpp_setup_wizard/middleware.py
1
from django.contrib.auth import get_user_model
1✔
2
from django.shortcuts import redirect
1✔
3
from django.urls import reverse
1✔
4
from django.utils.deprecation import MiddlewareMixin
1✔
5

6
from bpp.models import Uczelnia
1✔
7

8
BppUser = get_user_model()
1✔
9

10

11
class SetupWizardMiddleware(MiddlewareMixin):
1✔
12
    """
13
    Middleware to redirect to setup wizard when database is empty.
14
    Handles both user setup and uczelnia setup.
15
    """
16

17
    def process_request(self, request):
1✔
18
        # Skip middleware for static files and media files
19
        if request.path.startswith("/static/") or request.path.startswith("/media/"):
1!
20
            return None
×
21

22
        # Skip if we're already on the setup wizard URLs
23
        if request.path.startswith("/setup/"):
1✔
24
            return None
1✔
25

26
        # Skip for admin URLs during setup (in case we need to access admin for debugging)
27
        if request.path.startswith("/admin/") and request.path != "/admin/":
1!
28
            # Allow access to admin if users exist
29
            try:
1✔
30
                if BppUser.objects.exists():
×
31
                    return None
×
32
            except BaseException:
×
33
                # Database might not be migrated yet
34
                return None
×
35

36
        # Skip for migration-related URLs
37
        if any(path in request.path for path in ["migrate", "__debug__"]):
1!
38
            return None
×
39

40
        # Skip for login/logout URLs
41
        if any(path in request.path for path in ["login", "logout", "accounts"]):
1!
42
            return None
×
43

44
        # First check if any users exist
45
        try:
1✔
46
            needs_user_setup = not BppUser.objects.exists()
1✔
47
        except BaseException:
1✔
48
            # If there's an error (e.g., table doesn't exist), don't redirect
49
            # This allows migrations to run properly
50
            return None
×
51

52
        if needs_user_setup:
1✔
53
            # Redirect to user setup wizard
54
            setup_url = reverse("bpp_setup_wizard:setup")
1✔
55
            if request.path != setup_url:
1!
56
                return redirect(setup_url)
1✔
57

58
        # If users exist, check if Uczelnia is configured
59
        try:
1✔
60
            needs_uczelnia_setup = not Uczelnia.objects.exists()
1✔
61
        except BaseException:
1✔
62
            # Table might not exist yet
63
            return None
×
64

65
        if needs_uczelnia_setup:
1!
66
            # Only redirect authenticated superusers to Uczelnia setup
67
            # Check if request has user attribute (set by AuthenticationMiddleware)
68
            if (
1!
69
                hasattr(request, "user")
70
                and request.user.is_authenticated
71
                and request.user.is_superuser
72
            ):
73
                uczelnia_url = reverse("bpp_setup_wizard:uczelnia_setup")
1✔
74
                if request.path != uczelnia_url:
1!
75
                    return redirect(uczelnia_url)
1✔
76

77
        return None
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc