def partial_update(self, request, pk=None): changedContact = request.data.pop('changedContact', False) sendMsgContact = request.data.pop('sendMsgContact', False) sendMsgHead = request.data.pop('sendMsgHead', False) user = self.request.user oldReq = models.Request.objects.get(pk=pk) if changedContact: data = request.data user = None user_model = get_user_model() try: user = user_model.objects.get(username=data['aaieduhr']) except user_model.DoesNotExist: user = user_model.objects.create( username=data['aaieduhr'], first_name=data['first_name'], last_name=data['last_name'], email=data['email'], role=data['role'], aaieduhr=data['aaieduhr'], institution=data['institution'], is_staff=False, is_active=True) oldReq.user = user oldReq.save() serializer = serializers.RequestsListSerializer(oldReq) oldRequest = serializer.data ret = super().partial_update(request, pk) if not changedContact: newRequest = ret.data if settings.MAIL_SEND: notification = Notification(newRequest['id']) # state transitions mail sending if (oldRequest['approved'] == 2 and newRequest['approved'] > 1 ) or (oldRequest['approved'] == 1 and newRequest['approved'] == 2): notification.sendChangedRequestEmail(oldRequest) elif oldRequest['approved'] == -1 and newRequest[ 'approved'] == -1: if user.is_staff: notification.sendFixRequestEmail( sendMsgContact, sendMsgHead) else: notification.sendChangedRequestEmail(oldRequest) elif oldRequest['approved'] == -1 and newRequest[ 'approved'] == 0: notification.sendRejecedRequestEmail( sendMsgContact, sendMsgHead) elif oldRequest['approved'] == -1 and newRequest[ 'approved'] == 1: notification.sendApprovedRequestEmail( sendMsgContact, sendMsgHead) return ret
def generate(self, request): requests = models.Request.objects.filter(approved__gte=1).order_by('-approved_date') serializer = serializers.RequestsListSerializer(requests, many=True) output = '' for req in serializer.data: output += req['vm_fqdn'] + ' ' + req['user']['email'] + ' ' + req['sys_email'] + '<br>' return Response(output)
def approved(self, request): user = request.user if user.is_staff or user.is_superuser: requests = models.Request.objects.filter( approved__gte=1).order_by('-approved_date') serializer = serializers.RequestsListSerializer(requests, many=True) return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(data=None, status=status.HTTP_401_UNAUTHORIZED)
def mine(self, request): user = request.user requests = models.Request.objects.filter(user=user) serializer = serializers.RequestsListSerializer(requests, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def partial_update(self, request, pk=None): user = self.request.user existing_requestdb = models.Request.objects.get(pk=pk) if not user.is_staff and not user.is_superuser: if existing_requestdb.approved in [1, 2, 3]: return Response({'detail': 'Nije moguće mijenjati'}, status=status.HTTP_403_FORBIDDEN) dict_exist_requestdb = models.Request.objects.values().get(pk=pk) for skip_field in [ "head_institution", "approved", "timestamp", "vm_reason", "vm_ip", "vm_admin_remark", "approvedby" ]: request.data[skip_field] = dict_exist_requestdb[skip_field] else: changed_contact = request.data.pop('changedContact', False) sendmsg_contact = request.data.pop('sendMsgContact', False) sendmsg_head = request.data.pop('sendMsgHead', False) if changed_contact: data = request.data user = None user_model = get_user_model() try: user = user_model.objects.get(username=data['aaieduhr']) except user_model.DoesNotExist: user = user_model.objects.create( username=data['aaieduhr'], first_name=data['first_name'], last_name=data['last_name'], email=data['email'], role=data['role'], aaieduhr=data['aaieduhr'], institution=data['institution'], is_staff=False, is_active=True) existing_requestdb.user = user existing_requestdb.save() serializer = serializers.RequestsListSerializer(existing_requestdb, data=request.data) if serializer.is_valid(): old_request = serializer.data response_update = super().partial_update(request, pk) if not changed_contact: new_request = response_update.data if settings.MAIL_SEND: notification = Notification(new_request['id']) # state transitions mail sending if (old_request['approved'] == 2 and new_request['approved'] > 1) or ( old_request['approved'] == 1 and new_request['approved'] == 2): notification.sendChangedRequestEmail(old_request) elif old_request['approved'] == -1 and new_request[ 'approved'] == -1: if user.is_staff: notification.sendFixRequestEmail( sendmsg_contact, sendmsg_head) else: notification.sendChangedRequestEmail(old_request) elif old_request['approved'] == -1 and new_request[ 'approved'] == 0: notification.sendRejecedRequestEmail( sendmsg_contact, sendmsg_head) elif old_request['approved'] == -1 and new_request[ 'approved'] == 1: notification.sendApprovedRequestEmail( sendmsg_contact, sendmsg_head) return response_update else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def handlenew(self, request, pk=None): request = models.Request.objects.get(pk=pk) serializer = serializers.RequestsListSerializer(request) return Response(serializer.data, status=status.HTTP_200_OK)
def __init__(self, requestID): self.sender = settings.ADMIN_MAIL reqData = models.Request.objects.get(pk=requestID) serializer = serializers.RequestsListSerializer(reqData, many=False) self.request = serializer.data
def new(self, request): requests = models.Request.objects.filter( approved=-1).order_by('-request_date') serializer = serializers.RequestsListSerializer(requests, many=True) return Response(serializer.data, status=status.HTTP_200_OK)