Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-Core
Commits
3b6dbb89
Commit
3b6dbb89
authored
5 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Ensure developers can get a school-related object outside a request.
parent
c09c347f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
biscuit/core/util/core_helpers.py
+24
-6
24 additions, 6 deletions
biscuit/core/util/core_helpers.py
with
24 additions
and
6 deletions
biscuit/core/util/core_helpers.py
+
24
−
6
View file @
3b6dbb89
from
importlib
import
import_module
import
pkgutil
from
typing
import
Optional
,
Sequence
from
warnings
import
warn
from
django.apps
import
apps
from
django.conf
import
settings
from
django_global_request.middleware
import
get_request
...
...
@@ -34,14 +38,28 @@ def get_current_school() -> Optional:
request
=
get_request
()
if
request
:
if
not
hasattr
(
request
,
'
user
'
):
return
None
if
not
hasattr
(
request
.
user
,
'
person
'
):
return
None
# We are inside a web request, thus called from a public interface
if
request
.
user
.
person
:
if
hasattr
(
request
,
'
user
'
)
and
hasattr
(
request
.
user
,
'
person
'
):
# Use the same school as that of the logged-in user
return
request
.
user
.
person
.
school
else
:
# Set no school so no data is ever returned
# during an unauthenticated request
return
None
else
:
return
None
# We are called from outside a request (probably shell),
# thus called from a private interface
School
=
apps
.
get_model
(
'
core.School
'
)
if
'
DEFAULT_SCHOOL
'
in
settings
:
# Use school defined in settings
return
School
.
objects
.
get
(
pk
=
settings
.
DEFAULT_SCHOOL
)
else
:
# Use first school
warn
(
'
No school set, using first known school.
'
,
RuntimeWarning
)
return
School
.
objects
.
first
()
# Raise an exception because not finding a school wreaks havoc
raise
RuntimeError
(
'
No school set or found. Check your database.
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment