def createDepedenciesOfCompany(self, user, newCompany): # CompanyProfileSerializer().create_default_company_profile(newCompany.id, user, newCompany.name) chartOfNewUser = ChartSerializer().create_default_chart(newCompany) newMember = { "chart": chartOfNewUser.id, "user": user, "company": newCompany.id, } newMemberPosition = MembersSerializer(data = newMember) newMemberPosition.is_valid(raise_exception=True) newMemberPosition.create(newMember) newSec = { 'name': _("main office"), 'dakheli_letters_format': 'yy./.xxxx', 'sadereh_letters_format': 'yy./.1./.xxxx', 'varede_letters_format': 'yy./.2./.xxxx', 'dakheli_last_id': 0, 'sadere_last_id': 0, 'varede_last_id': 0, 'company': newCompany.id } newSec = SecretariatSerializer(data=newSec) newSec.is_valid(raise_exception=True) newSec = newSec.create(newSec.validated_data) newSecPerm = { "chart": chartOfNewUser.id, "secretariat": newSec.id, "permission": "111", "default": True, } newSecPerm = SecretariatSerializerPermission(data = newSecPerm) newSecPerm.is_valid(raise_exception=True) newSecPerm = newSecPerm.create(newSecPerm.validated_data)
def ForceOut(self, request, *args, **kwargs): companyInstance = Company.objects.get( id=self.kwargs["companyID_id"], owner_user=request.user.id ) mySqlPositionInstance = Position.objects.get( id = request.data["positionID"] ) updating = { "user": None } memSerial = MembersSerializer(instance=mySqlPositionInstance, data=updating, partial=True) memSerial.is_valid(raise_exception=True) memSerial.update(instance=mySqlPositionInstance, validated_data=memSerial.validated_data) return Response({})
def RemoveFromInbox(self, request, *args, **kwargs): companyInstance = Company.objects.get( id=self.kwargs["companyID_id"], owner_user=request.user.id ) mySqlPositionInstance = Position.objects.get( chart=request.data["chartID"], company=companyInstance.id, user=request.data["userID"], ) updating = { "chart": None, "company":None } serial = MembersSerializer(instance=mySqlPositionInstance, data=updating, partial=True) serial.is_valid(raise_exception=True) serial.update(mySqlPositionInstance, updating) return Response({})
def DoInvite(self, request, *args, **kwargs): currentUserInstance = request.user # checking if current is user is CEO or reciever invitationInstance = self.queryset.get(id=request.data['invitationID']) chartInstance = Chart.objects.get(id=invitationInstance.chart) companyInstance = Company.objects.get(id=invitationInstance.company) recieverProfileInstance = invitationInstance.receiver senderProfileInstance = invitationInstance.sender recieverUserInstance = MyUser.objects.get(id=recieverProfileInstance.userID) senderUserInstance = MyUser.objects.get(id=senderProfileInstance.userID) isCurrentUserApproveAsReciever = recieverUserInstance == currentUserInstance isCurrentUserApproveAsCEO = senderUserInstance == currentUserInstance if not isCurrentUserApproveAsCEO and not isCurrentUserApproveAsReciever: return Response({"message": _("You are not allowed to perform this operation")}, status=status.HTTP_401_UNAUTHORIZED) # partial update is here # it means we have to update an position if invitationInstance.isEmpty: positionInstance = Position.objects.get(id=invitationInstance.positionID) updated = { "user": recieverUserInstance.id, "post_date": datetime.now() } posSerializer = MembersSerializer(instance=positionInstance, data=updated, partial=True) posSerializer.is_valid(raise_exception=True) posSerializer.update(instance=positionInstance, validated_data=posSerializer.validated_data) invitationInstance.delete() return Response({}, status=status.HTTP_200_OK) newMember = { "company": companyInstance.id, "chart": chartInstance.id, "user": recieverUserInstance.id } posSerializer = MembersSerializer(data=newMember) posSerializer.is_valid(raise_exception=True) posSerializer.create(posSerializer.validated_data) invitationInstance.delete() return Response({}, status=status.HTTP_200_OK)
def UpdatePost(self, request, *args, **kwargs): companyInstance = Company.objects.get( id=self.kwargs["companyID_id"], owner_user=request.user.id ) userInstance = MyUser.objects.get(id=request.data['UserID']) positionInstance = Position.objects.filter( company=companyInstance, user=userInstance, ) if positionInstance.count() == 0: chart = Chart.objects.get(id=request.data["NewPositionID"]) newPosition = { "chart": chart, "user": userInstance, "company": companyInstance, "post_date": datetime.now() } created = MembersSerializer().create(newPosition) return Response(created.data) positionInstance = Position.objects.get( company=companyInstance, user=userInstance, ) newChartInstance = Chart.objects.get( owner=companyInstance, id=request.data['NewPositionID'] ) positionInstance.chart = newChartInstance updatingPos = MembersSerializer(instance=positionInstance, data={ "chart": newChartInstance.id, "user": userInstance.id, "company": companyInstance.id, "post_date": datetime.now() }, partial=True) if updatingPos.is_valid(): updatingPos.save() return Response({})