Skip to content

Commit

Permalink
Add view checks for user belongs to organization
Browse files Browse the repository at this point in the history
  • Loading branch information
pxwxnvermx committed Jun 29, 2023
1 parent 6328f0f commit 9391580
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions commcare_connect/opportunity/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views.generic import CreateView, ListView, UpdateView

from commcare_connect.opportunity.forms import OpportunityChangeForm, OpportunityCreationForm
from commcare_connect.opportunity.models import Opportunity
from commcare_connect.utils.commcarehq_api import get_applications_for_user


@method_decorator(login_required, name="dispatch")
class OpportunityList(ListView):
class OrganizationUserMixin(LoginRequiredMixin, UserPassesTestMixin):
def test_func(self):
return self.request.user.organizations.filter(organization__slug=self.kwargs.get("org_slug")).exists()


class OpportunityList(OrganizationUserMixin, ListView):
model = Opportunity
paginate_by = 10

Expand All @@ -23,8 +25,7 @@ def get_queryset(self):
return Opportunity.objects.filter(organization__slug=self.kwargs["org_slug"])


@method_decorator(login_required, name="dispatch")
class OpportunityCreate(UserPassesTestMixin, CreateView):
class OpportunityCreate(OrganizationUserMixin, CreateView):
template_name = "opportunity/opportunity_create.html"
form_class = OpportunityCreationForm

Expand All @@ -47,8 +48,7 @@ def test_func(self):
return self.request.user.organizations.filter(organization__slug=self.kwargs.get("org_slug")).exists()


@method_decorator(login_required, name="dispatch")
class OpportunityEdit(UpdateView):
class OpportunityEdit(OrganizationUserMixin, UpdateView):
model = Opportunity
template_name = "opportunity/opportunity_create.html"
form_class = OpportunityChangeForm
Expand Down

0 comments on commit 9391580

Please sign in to comment.