mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 11:27:09 +00:00
@@ -19,6 +19,16 @@ class OrganisationListView(LoginRequiredMixin, ListView):
|
||||
model = models.Organisation
|
||||
template_name = 'people/organisation/list.html'
|
||||
|
||||
def get_context_data(self,
|
||||
**kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
context['existing_relationships'] = set(
|
||||
self.request.user.person.organisation_relationship_targets.
|
||||
values_list('pk', flat=True))
|
||||
|
||||
return context
|
||||
|
||||
|
||||
class OrganisationDetailView(LoginRequiredMixin, DetailView):
|
||||
"""View displaying details of a :class:`Organisation`."""
|
||||
|
||||
@@ -24,7 +24,8 @@ class RelationshipCreateView(LoginRequiredMixin, RedirectView):
|
||||
|
||||
Redirects to a form containing the :class:`RelationshipQuestion`s.
|
||||
"""
|
||||
def get_redirect_url(self, *args: typing.Any, **kwargs: typing.Any) -> typing.Optional[str]:
|
||||
def get_redirect_url(self, *args: typing.Any,
|
||||
**kwargs: typing.Any) -> typing.Optional[str]:
|
||||
target = models.Person.objects.get(pk=self.kwargs.get('person_pk'))
|
||||
relationship, _ = models.Relationship.objects.get_or_create(
|
||||
source=self.request.user.person, target=target)
|
||||
@@ -96,3 +97,94 @@ class RelationshipUpdateView(permissions.UserIsLinkedPersonMixin, CreateView):
|
||||
answer_set.save()
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class OrganisationRelationshipDetailView(permissions.UserIsLinkedPersonMixin,
|
||||
DetailView):
|
||||
"""View displaying details of an :class:`OrganisationRelationship`."""
|
||||
model = models.OrganisationRelationship
|
||||
template_name = 'people/organisation-relationship/detail.html'
|
||||
related_person_field = 'source'
|
||||
context_object_name = 'relationship'
|
||||
|
||||
|
||||
class OrganisationRelationshipCreateView(LoginRequiredMixin, RedirectView):
|
||||
"""View for creating a :class:`OrganisationRelationship`.
|
||||
|
||||
Redirects to a form containing the :class:`OrganisationRelationshipQuestion`s.
|
||||
"""
|
||||
def get_redirect_url(self, *args: typing.Any,
|
||||
**kwargs: typing.Any) -> typing.Optional[str]:
|
||||
target = models.Organisation.objects.get(
|
||||
pk=self.kwargs.get('organisation_pk'))
|
||||
relationship, _ = models.OrganisationRelationship.objects.get_or_create(
|
||||
source=self.request.user.person, target=target)
|
||||
|
||||
return reverse('people:organisation.relationship.update',
|
||||
kwargs={'relationship_pk': relationship.pk})
|
||||
|
||||
|
||||
class OrganisationRelationshipUpdateView(permissions.UserIsLinkedPersonMixin,
|
||||
CreateView):
|
||||
"""
|
||||
View for updating the details of a Organisationrelationship.
|
||||
|
||||
Creates a new :class:`OrganisationRelationshipAnswerSet` for the :class:`OrganisationRelationship`.
|
||||
Displays / processes a form containing the :class:`OrganisationRelationshipQuestion`s.
|
||||
"""
|
||||
model = models.OrganisationRelationshipAnswerSet
|
||||
template_name = 'people/relationship/update.html'
|
||||
form_class = forms.OrganisationRelationshipAnswerSetForm
|
||||
|
||||
def get_test_person(self) -> models.Person:
|
||||
"""
|
||||
Get the person instance which should be used for access control checks.
|
||||
"""
|
||||
relationship = models.OrganisationRelationship.objects.get(
|
||||
pk=self.kwargs.get('relationship_pk'))
|
||||
|
||||
return relationship.source
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
self.relationship = models.OrganisationRelationship.objects.get(
|
||||
pk=self.kwargs.get('relationship_pk'))
|
||||
self.person = self.relationship.source
|
||||
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.relationship = models.OrganisationRelationship.objects.get(
|
||||
pk=self.kwargs.get('relationship_pk'))
|
||||
self.person = self.relationship.source
|
||||
|
||||
return super().post(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
context['person'] = self.person
|
||||
context['relationship'] = self.relationship
|
||||
|
||||
return context
|
||||
|
||||
def get_initial(self):
|
||||
initial = super().get_initial()
|
||||
|
||||
initial['relationship'] = self.relationship
|
||||
|
||||
return initial
|
||||
|
||||
def form_valid(self, form):
|
||||
"""
|
||||
Mark any previous answer sets as replaced.
|
||||
"""
|
||||
response = super().form_valid(form)
|
||||
now_date = timezone.now().date()
|
||||
|
||||
# Shouldn't be more than one after initial updates after migration
|
||||
for answer_set in self.relationship.answer_sets.exclude(
|
||||
pk=self.object.pk):
|
||||
answer_set.replaced_timestamp = now_date
|
||||
answer_set.save()
|
||||
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user