diff --git a/cookbook/forms.py b/cookbook/forms.py index 239b1701..50f88228 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -37,12 +37,9 @@ class UserPreferenceForm(forms.ModelForm): prefix = 'preference' def __init__(self, *args, **kwargs): - if x := kwargs.get('instance', None): - space = x.space - else: - space = kwargs.pop('space') + space = kwargs.pop('space') super().__init__(*args, **kwargs) - self.fields['plan_share'].queryset = User.objects.filter(userpreference__space=space).all() + self.fields['plan_share'].queryset = User.objects.filter(userspace__space=space).all() class Meta: model = UserPreference diff --git a/cookbook/helper/permission_helper.py b/cookbook/helper/permission_helper.py index 13de26bd..d889a747 100644 --- a/cookbook/helper/permission_helper.py +++ b/cookbook/helper/permission_helper.py @@ -166,7 +166,7 @@ class OwnerRequiredMixin(object): try: obj = self.get_object() - if obj.get_space() != request.space: + if not request.user.userspace.filter(space=obj.get_space()).exists(): messages.add_message(request, messages.ERROR, _('You do not have the required permissions to view this page!')) return HttpResponseRedirect(reverse_lazy('index')) diff --git a/cookbook/helper/scope_middleware.py b/cookbook/helper/scope_middleware.py index df63d5df..34490e6c 100644 --- a/cookbook/helper/scope_middleware.py +++ b/cookbook/helper/scope_middleware.py @@ -26,15 +26,18 @@ class ScopeMiddleware: if request.path.startswith(prefix + '/accounts/'): return self.get_response(request) + if request.path.startswith(prefix + '/switch-space/'): + return self.get_response(request) + with scopes_disabled(): if request.user.userspace_set.count() == 0 and not reverse('account_logout') in request.path: - return views.no_space(request) + return views.space_overview(request) # get active user space, if for some reason more than one space is active select first (group permission checks will fail, this is not intended at this point) user_space = request.user.userspace_set.filter(active=True).first() if not user_space: - pass # TODO show space selection page (maybe include in no space page) + return views.space_overview(request) if user_space.groups.count() == 0 and not reverse('account_logout') in request.path: return views.no_groups(request) diff --git a/cookbook/models.py b/cookbook/models.py index 5b8251dd..746414cb 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -343,7 +343,7 @@ class UserPreference(models.Model, PermissionModelMixin): return str(self.user) -class UserSpace(models.Model): +class UserSpace(models.Model, PermissionModelMixin): user = models.ForeignKey(User, on_delete=models.CASCADE) space = models.ForeignKey(Space, on_delete=models.CASCADE) groups = models.ManyToManyField(Group) diff --git a/cookbook/tables.py b/cookbook/tables.py index 2831c39d..be05fb44 100644 --- a/cookbook/tables.py +++ b/cookbook/tables.py @@ -1,3 +1,4 @@ + import django_tables2 as tables from django.utils.html import format_html from django.utils.translation import gettext as _ diff --git a/cookbook/templates/base.html b/cookbook/templates/base.html index 67a6f8c4..820d8dbe 100644 --- a/cookbook/templates/base.html +++ b/cookbook/templates/base.html @@ -304,19 +304,19 @@ {% trans 'Admin' %} {% endif %} - {% if request.user.is_authenticated and request.user.userspace_set.all|length > 1 %} + {% if request.user.is_authenticated %}
{% trans 'To join an existing space either enter your invite token or click on the invite link the space owner send you.' %}
- - - -{% trans 'Start your own recipe space and invite other users to it.' %}
- -{% trans 'Owner' %}: {{ us.space.created_by }} + {% if us.space.created_by != us.user %} +
{% trans 'Leave Space' %} + {% endif %} + +
+{% trans 'To join an existing space either enter your invite token or click on the invite link the space owner send you.' %}
+ + + +{% trans 'Start your own recipe space and invite other users to it.' %}
+ +