class TagViewSet(ReadWriteViewSet): queryset = Tag.objects.all() serializer_classes = SerializerClasses(retrieve=SimpleTagSerializer, list=SimpleTagSerializer, create=WriteTagSerializer, update=WriteTagSerializer, destroy=WriteTagSerializer) filter_class = TagFilter
class SystemControlsViewSet(ReadOnlyViewSet): queryset = ElementControl.objects.all() serializer_classes = SerializerClasses( retrieve=DetailedElementControlSerializer, list=SimpleElementControlSerializer) NESTED_ROUTER_PKS = [{'pk': 'systems_pk', 'model_field': 'element.system'}]
class SystemAssessmentViewSet(ReadOnlyViewSet): queryset = SystemAssessmentResult.objects.all() serializer_classes = SerializerClasses( retrieve=DetailedSystemAssessmentResultSerializer, list=SimpleSystemAssessmentResultSerializer) NESTED_ROUTER_PKS = [{'pk': 'systems_pk', 'model_field': 'system'}]
class AppointmentViewSet(ReadWriteViewSet): queryset = Appointment.objects.all() serializer_classes = SerializerClasses( retrieve=SimpleAppointmentSerializer, list=SimpleAppointmentSerializer, create=WriteAppointmentSerializer, update=WriteAppointmentSerializer, destroy=WriteAppointmentSerializer)
class ProposalViewSet(ReadWriteViewSet): queryset = Proposal.objects.all() serializer_classes = SerializerClasses( retrieve=SimpleProposalSerializer, list=SimpleProposalSerializer, create=WriteProposalSerializer, update=WriteProposalSerializer, destroy=WriteProposalSerializer, )
class SystemPoamViewSet(ReadOnlyViewSet): queryset = Poam.objects.all() serializer_classes = SerializerClasses(retrieve=DetailedPoamSerializer, list=SimplePoamSerializer) NESTED_ROUTER_PKS = [{ 'pk': 'systems_pk', 'model_field': 'statement.consumer_element.system' }]
class RequestViewSet(ReadWriteViewSet): queryset = Request.objects.all() serializer_classes = SerializerClasses( retrieve=SimpleRequestSerializer, list=SimpleRequestSerializer, create=WriteRequestSerializer, update=WriteRequestSerializer, destroy=WriteRequestSerializer, )
class ElementWithPermissionsViewSet(ReadWriteViewSet): # NESTED_ROUTER_PKS = [{'pk': 'modules_pk', 'model_field': 'module', 'model': Module}] queryset = Element.objects.all() # ordering = ('definition_order',) serializer_classes = SerializerClasses( retrieve=ElementPermissionSerializer, list=ElementPermissionSerializer, create=UpdateElementPermissionSerializer, update=UpdateElementPermissionSerializer, destroy=UpdateElementPermissionSerializer, assign_role=UpdateElementPermissionSerializer, remove_user=RemoveUserPermissionFromElementSerializer) @action(detail=True, url_path="assign_role", methods=["PUT"]) def assign_role(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) # assign permissions to user on element user_permissions = [] perm_user = {} for key, value in validated_data.items(): perm_user = value['user'] if (value['add']): user_permissions.append('add_element') if (value['change']): user_permissions.append('change_element') if (value['delete']): user_permissions.append('delete_element') if (value['view']): user_permissions.append('view_element') user = User.objects.get(id=perm_user['id']) element.assign_user_permissions(user, user_permissions) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="remove_user", methods=["PUT"]) def remove_user(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) userId = None for key, value in validated_data.items(): userId = value['id'] user = User.objects.get(id=userId) element.remove_all_permissions_from_user(user) serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data)
class PartyViewSet(ReadWriteViewSet): queryset = Party.objects.all() search_fields = ['name'] filter_backends = (filters.SearchFilter, ) serializer_classes = SerializerClasses(retrieve=SimplePartySerializer, list=SimplePartySerializer, create=WritePartySerializer, update=WritePartySerializer, destroy=WritePartySerializer) def search(self, request, keyword): return Q(name__icontains=keyword)
class ModuleQuestionViewSet(ReadWriteViewSet): NESTED_ROUTER_PKS = [{ 'pk': 'modules_pk', 'model_field': 'module', 'model': Module }] queryset = ModuleQuestion.objects.all() ordering = ('definition_order', ) serializer_classes = SerializerClasses( retrieve=SimpleModuleQuestionSerializer, list=SimpleModuleQuestionSerializer, create=CreateModuleQuestionSerializer, update=CreateModuleQuestionSerializer, destroy=CreateModuleQuestionSerializer)
class SystemViewSet(ReadOnlyViewSet): queryset = System.objects.all() serializer_classes = SerializerClasses( retrieve=DetailedSystemSerializer, list=SimpleSystemSerializer, CreateAndSetProposal=SystemCreateAndSetProposalSerializer, retrieveProposals=SystemRetrieveProposalsSerializer, ) @action(detail=True, url_path="CreateAndSetProposal", methods=["POST"]) def CreateAndSetProposal(self, request, **kwargs): system, validated_data = self.validate_serializer_and_get_object( request) newProposal = Proposal.objects.create( user=User.objects.get(id=validated_data['userId']), requested_element=Element.objects.get( id=validated_data['elementId']), criteria_comment=validated_data['criteria_comment'], status=validated_data['status'], ) newProposal.save() system.add_proposals([newProposal.id]) system.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, system) return Response(serializer.data) @action(detail=True, url_path="retrieveProposals", methods=["GET"]) def retrieveProposals(self, request, **kwargs): system, validated_data = self.validate_serializer_and_get_object( request) system.save() serializer_class = self.get_serializer_class('retrieveProposals') serializer = self.get_serializer(serializer_class, system) return Response(serializer.data)
class UsersViewSet(BaseViewSet, CustomSearchModelMixin, CustomUpdateModelMixin, CustomListModelMixin): ROLLUP = { # "staff_count": Q(is_staff=True) } search_fields = ['username'] filter_backends = (filters.SearchFilter, ) queryset = User.objects.all() serializer_classes = SerializerClasses(list=UserProfileRead, update=UserProfileWrite, retrieve=UserProfileRead, profile=UserProfileRead) def search(self, request, keyword): return Q(username__icontains=keyword) @action(methods=[ 'GET', ], detail=False) def profile(self, request, **kwargs): serializer = self.get_serializer_class() data = serializer(request.user).data return Response(data)
class PortfolioViewSet(ReadOnlyViewSet): queryset = Portfolio.objects.all() serializer_classes = SerializerClasses(retrieve=SimplePortfolioSerializer, list=SimplePortfolioSerializer)
class ModuleViewSet(ReadOnlyViewSet): queryset = Module.objects.all() serializer_classes = SerializerClasses(retrieve=ModuleReadSerializer, list=ModuleReadSerializer)
class ElementViewSet(ReadOnlyViewSet): queryset = Element.objects.all() serializer_classes = SerializerClasses( retrieve=DetailedElementSerializer, list=SimpleElementSerializer, tags=WriteElementTagsSerializer, retrieveParties=ElementPartySerializer, appointments=WriteElementAppointPartySerializer, removeAppointments=WriteElementAppointPartySerializer, removeAppointmentsByParty=DeletePartyAppointmentsFromElementSerializer, CreateAndSet=CreateMultipleAppointmentsFromRoleIds, retrieveRequests=ElementRequestsSerializer, setRequest=ElementSetRequestsSerializer, CreateAndSetRequest=ElementCreateAndSetRequestSerializer, ) @action(detail=True, url_path="tags", methods=["PUT"]) def tags(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) element.tags.clear() element.tags.add(*validated_data['tags']) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="retrieveParties", methods=["GET"]) def retrieveParties(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="appointments", methods=["PUT"]) def appointments(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) for key, value in validated_data.items(): element.add_appointments(value) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="removeAppointments", methods=["PUT"]) def removeAppointments(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) for key, value in validated_data.items(): element.remove_appointments(value) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="removeAppointmentsByParty", methods=["PUT"]) def removeAppointmentsByParty(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) for key, value in validated_data.items(): for party in element.appointments.filter(party_id=value): element.remove_appointments([party.id]) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="CreateAndSet", methods=["POST"]) def CreateAndSet(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) createdAppointments = [] # expects this object type: # { # "role_ids": { # "party_id": 3, # "roles": [1,2] # } # } for key, value in validated_data.items(): for val in value['roles']: createExample = Appointment.objects.create( party=Party.objects.get(id=value['party_id']), role=Role.objects.get(id=val), model_name="element", comment="Assigning new role") createExample.save() createdAppointments.append(createExample.id) element.add_appointments(createdAppointments) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="retrieveRequests", methods=["GET"]) def retrieveRequests(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) element.save() serializer_class = self.get_serializer_class('retrieveRequests') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="setRequest", methods=["PUT"]) def setRequest(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) for key, value in validated_data.items(): element.add_requests(value) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data) @action(detail=True, url_path="CreateAndSetRequest", methods=["POST"]) def CreateAndSetRequest(self, request, **kwargs): element, validated_data = self.validate_serializer_and_get_object( request) prop = Proposal.objects.get(id=validated_data['proposalId']) newRequest = Request.objects.create( user=User.objects.get(id=validated_data['userId']), system=System.objects.get(id=validated_data['systemId']), requested_element=element, criteria_comment=validated_data['criteria_comment'], criteria_reject_comment=validated_data['criteria_reject_comment'], status=validated_data['status'], ) newRequest.save() prop.req = newRequest prop.save() element.add_requests([newRequest.id]) element.save() serializer_class = self.get_serializer_class('retrieve') serializer = self.get_serializer(serializer_class, element) return Response(serializer.data)
class OrganizationViewSet(ReadOnlyViewSet): queryset = Organization.objects.all() serializer_classes = SerializerClasses( retrieve=SimpleOrganizationSerializer, list=DetailedOrganizationSerializer)
class SupportViewSet(ReadOnlyViewSet): queryset = Support.objects.all() serializer_classes = SerializerClasses(retrieve=SimpleSupportSerializer, list=SimpleSupportSerializer)