Skip to content

Commit

Permalink
Try adding guest user library
Browse files Browse the repository at this point in the history
  • Loading branch information
partizipation committed Feb 12, 2025
1 parent 3431375 commit bdbd3c8
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
10 changes: 10 additions & 0 deletions adhocracy-plus/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"widget_tweaks",
"rest_framework",
"rest_framework.authtoken",
"guest_user",
"guest_user.contrib.allauth",
# JWT authentication
"rest_framework_simplejwt.token_blacklist",
"django_filters",
Expand Down Expand Up @@ -276,6 +278,7 @@
"rules.permissions.ObjectPermissionBackend",
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
"guest_user.backends.GuestBackend",
)

ACCOUNT_ADAPTER = "apps.users.adapters.AccountAdapter"
Expand Down Expand Up @@ -705,3 +708,10 @@
},
},
}

GUEST_USER = {
'NAME_GENERATOR': 'guest_user.functions.generate_uuid_username', # Default option
}

GUEST_USER_MODEL = "a4_candy_users.CustomGuestUser"
# GUEST_USER_MODEL = "users.CustomGuestUser"
1 change: 1 addition & 0 deletions adhocracy-plus/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
path("documents/", include(wagtaildocs_urls)),
path("accounts/", include("allauth.urls")),
path("account/", include("apps.account.urls")),
path("convert/", include("guest_user.urls")),
path("profile/", include("apps.users.urls")),
path("userdashboard/", include("apps.userdashboard.urls")),
# this needs to be above i18n/ to make the overwrite work
Expand Down
4 changes: 3 additions & 1 deletion apps/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext
from django.views import generic
from rules.contrib.views import LoginRequiredMixin
from rules.contrib.views import PermissionRequiredMixin
from guest_user.decorators import allow_guest_user

from adhocracy4.dashboard import mixins as a4dashboard_mixins
from adhocracy4.dashboard import signals as a4dashboard_signals
Expand Down Expand Up @@ -271,7 +273,7 @@ def form_valid(self, request, *args, **kwargs):
messages.success(self.request, self.success_message % obj.__dict__)
return super().form_valid(request, *args, **kwargs)


@method_decorator(allow_guest_user, name='dispatch')
class ProjectDetailView(
PermissionRequiredMixin,
ProjectModuleDispatchMixin,
Expand Down
6 changes: 6 additions & 0 deletions apps/users/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from django.contrib.auth.signals import user_logged_in
from django.utils.translation import gettext_lazy as _
# from .guests import CustomGuestUser
# import guests
# __all__ = ['CustomGuest']

# default_app_config = "users.apps.UsersConfig"


USERNAME_REGEX = r"^[\w]+[ \w.@+-]*$"
USERNAME_INVALID_MESSAGE = _(
Expand Down
9 changes: 9 additions & 0 deletions apps/users/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@
class Config(AppConfig):
name = "apps.users"
label = "a4_candy_users"


# class UsersConfig(AppConfig):
# name = "apps.users"
# label = "a4_candy_users"

# def ready(self):
# # Import the model here to ensure it's registered
# from .guests import CustomGuestUser
12 changes: 12 additions & 0 deletions apps/users/guests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# # from apps.users.models import User
# from guest_user.models import Guest, GuestManager


# class CustomGuestManager(GuestManager):
# def create_guest_user(self, request=None, username=None):
# # is just User in the example...
# user = Guest.objects.create(username=username, email='guest' + username + '@liqd.net')
# return user

# class CustomGuestUser(Guest):
# objects = CustomGuestManager()
24 changes: 24 additions & 0 deletions apps/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,34 @@
from adhocracy4.projects.enums import Access
from adhocracy4.projects.models import Project
from apps.organisations.models import OrganisationTermsOfUse
# from .guests import CustomGuestUser
# __all__ = ['CustomGuest']

from . import USERNAME_INVALID_MESSAGE
from . import USERNAME_REGEX

from guest_user.models import Guest, GuestManager

# didn't work
# from django.utils.module_loading import import_string

# def get_guest_model():
# Guest = import_string('guest_user.models.Guest')
# return Guest

# def get_guest_manager():
# GuestManager = import_string('guest_user.models.GuestManager')
# return GuestManager

class CustomGuestManager(GuestManager):
def create_guest_user(self, request=None, username=None):
# is just User in the example...
user = CustomGuestUser.objects.create(username=username, email='guest' + username + '@liqd.net')
return user

class CustomGuestUser(Guest):
objects = CustomGuestManager()


class User(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
username = models.CharField(
Expand Down

0 comments on commit bdbd3c8

Please sign in to comment.