Skip to content

Commit

Permalink
Fix new internal user cache
Browse files Browse the repository at this point in the history
The `get_internal_user` function was broken:

- on first use when the key for the internal user pk is not in the cache => the get_or_create would not work because there is no user with ID 0 (get fails) and the unique username is already in use (create fails).
  • Loading branch information
marcantoinedupre committed May 7, 2024
1 parent c022ee4 commit 28f8687
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ CHANGELOG
8.8.0+dev (XXXX-XX-XX)
-----------------------

**Hotfix**

- Fix new internal user cache


8.8.0 (2024-04-10)
-----------------------
Expand Down
29 changes: 15 additions & 14 deletions mapentity/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,26 @@


def get_internal_user():

print("HELLO WORLD")

User = get_user_model()
cache_key = sha256(app_settings['INTERNAL_USER'].encode()).hexdigest()

id = 0

if cache_key in cache:
id = int(cache.get(cache_key))

internal_user, created = User.objects.get_or_create(
id=int(id),
defaults={
'username': app_settings['INTERNAL_USER'],
'password': '',
'is_active': True
}
)
if created:
cache.set(cache_key, internal_user.pk)
return internal_user
foo_user = User.objects.get(pk=id)
else:
foo_user, created = User.objects.get_or_create(
username=app_settings['INTERNAL_USER'],
defaults={
'password': '',
'is_active': True
}
)
if created:
cache.set(cache_key, foo_user.pk)
return foo_user


class AutoLoginMiddleware:
Expand Down

0 comments on commit 28f8687

Please sign in to comment.