def put(self, request, pk, format=None): params = request.query_params if len( request.data) == 0 else request.data obj = self.get_object(pk) if (request.user.role == "ADMIN" or request.user.is_superuser or request.user == obj.commented_by): serializer = LeadCommentSerializer(obj, data=params) if params.get("comment"): if serializer.is_valid(): serializer.save() return Response( { "error": False, "message": "Comment Submitted" }, status=status.HTTP_200_OK, ) return Response( { "error": True, "errors": serializer.errors }, status=status.HTTP_400_BAD_REQUEST, ) else: return Response( { "error": True, "errors": "You don't have permission to perform this action", }, status=status.HTTP_403_FORBIDDEN, )
def put(self, request, pk, format=None): params = request.query_params if len( request.data) == 0 else request.data obj = self.get_object(pk) if ( request.user.role == "ADMIN" or request.user.is_superuser or request.user == obj.commented_by ): serializer = LeadCommentSerializer( obj, data=params) if params.get("comment"): if serializer.is_valid(): serializer.save() return Response( {"error": False, "message":"Comment Submitted"}, status=status.HTTP_200_OK, ) return Response( {"error": True, "errors": serializer.errors}, status=status.HTTP_400_BAD_REQUEST, ) else: return Response({ "error": True, "errors": "User company doesnot match with header...."} )
def post(self, request, pk, **kwargs): params = ( self.request.query_params if len(self.request.data) == 0 else self.request.data ) context = {} self.lead_obj = Lead.objects.get(pk=pk) if self.lead_obj.org != request.org: return Response( {"error": True, "errors": "User company doesnot match with header...."}, status=status.HTTP_403_FORBIDDEN ) if self.request.profile.role != "ADMIN" and not self.request.user.is_superuser: if not ( (self.request.profile == self.lead_obj.created_by) or (self.request.profile in self.lead_obj.assigned_to.all()) ): return Response( { "error": True, "errors": "You do not have Permission to perform this action", }, status=status.HTTP_403_FORBIDDEN, ) comment_serializer = CommentSerializer(data=params) if comment_serializer.is_valid(): if params.get("comment"): comment_serializer.save( lead_id=self.lead_obj.id, commented_by_id=self.request.profile.id, ) if self.request.FILES.get("lead_attachment"): attachment = Attachments() attachment.created_by = self.request.profile attachment.file_name = self.request.FILES.get( "lead_attachment").name attachment.lead = self.lead_obj attachment.attachment = self.request.FILES.get( "lead_attachment") attachment.save() comments = Comment.objects.filter( lead__id=self.lead_obj.id).order_by("-id") attachments = Attachments.objects.filter(lead__id=self.lead_obj.id).order_by( "-id" ) context.update( { "lead_obj": LeadSerializer(self.lead_obj).data, "attachments": AttachmentsSerializer(attachments, many=True).data, "comments": LeadCommentSerializer(comments, many=True).data, } ) return Response(context)
class LeadSerializer(serializers.ModelSerializer): assigned_to = UserSerializer(read_only=True, many=True) created_by = UserSerializer() tags = TagsSerializer(read_only=True, many=True) lead_attachment = AttachmentsSerializer(read_only=True, many=True) teams = TeamsSerializer(read_only=True, many=True) company = CompanySerializer() lead_comments = LeadCommentSerializer(read_only=True, many=True) class Meta: model = Lead # fields = ‘__all__’ fields = ( "id", "title", "first_name", "last_name", "phone", "email", "status", "source", "address_line", "street", "city", "state", "postcode", "country", "website", "description", "lead_attachment", "lead_comments", "assigned_to", "account_name", "opportunity_amount", "created_by", "created_on", "is_active", "enquery_type", "tags", "created_from_site", "teams", "company", )
class LeadSerializer(serializers.ModelSerializer): assigned_to = ProfileSerializer(read_only=True, many=True) created_by = ProfileSerializer() country = serializers.SerializerMethodField() tags = TagsSerializer(read_only=True, many=True) lead_attachment = AttachmentsSerializer(read_only=True, many=True) teams = TeamsSerializer(read_only=True, many=True) lead_comments = LeadCommentSerializer(read_only=True, many=True) def get_country(self, obj): return obj.get_country_display() class Meta: model = Lead # fields = ‘__all__’ fields = ("id", "title", "first_name", "last_name", "phone", "email", "status", "source", "address_line", "street", "city", "state", "postcode", "country", "website", "description", "lead_attachment", "lead_comments", "assigned_to", "account_name", "opportunity_amount", "created_by", "created_on", "is_active", "enquiry_type", "tags", "created_from_site", "teams", "skype_ID", "industry", "company")
def get_context_data(self, **kwargs): params = ( self.request.query_params if len(self.request.data) == 0 else self.request.data ) context = {} user_assgn_list = [ assigned_to.id for assigned_to in self.lead_obj.assigned_to.all() ] if self.request.user == self.lead_obj.created_by: user_assgn_list.append(self.request.user.id) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if self.request.user.id not in user_assgn_list: return Response( { "error": True, "errors": "You do not have Permission to perform this action", } ) comments = Comment.objects.filter(lead=self.lead_obj).order_by("-id") attachments = Attachments.objects.filter(lead=self.lead_obj).order_by("-id") assigned_data = [] for each in self.lead_obj.assigned_to.all(): assigned_dict = {} assigned_dict["id"] = each.id assigned_dict["name"] = each.email assigned_data.append(assigned_dict) if self.request.user.is_superuser or self.request.user.role == "ADMIN": users_mention = list( User.objects.filter( is_active=True, company=self.request.company ).values("username") ) elif self.request.user != self.lead_obj.created_by: users_mention = [{"username": self.lead_obj.created_by.username}] else: users_mention = list(self.lead_obj.assigned_to.all().values("username")) if self.request.user.role == "ADMIN" or self.request.user.is_superuser: users = User.objects.filter( is_active=True, company=self.request.company ).order_by("email") else: users = User.objects.filter( role="ADMIN", company=self.request.company ).order_by("email") user_assgn_list = [ assigned_to.id for assigned_to in self.lead_obj.get_assigned_users_not_in_teams ] if self.request.user == self.lead_obj.created_by: user_assgn_list.append(self.request.user.id) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if self.request.user.id not in user_assgn_list: return Response( { "error": True, "errors": "You do not have Permission to perform this action", } ) team_ids = [user.id for user in self.lead_obj.get_team_users] all_user_ids = [user.id for user in users] users_excluding_team_id = set(all_user_ids) - set(team_ids) users_excluding_team = User.objects.filter(id__in=users_excluding_team_id) context.update( { "lead_obj": LeadSerializer(self.lead_obj).data, "attachments": AttachmentsSerializer(attachments, many=True).data, "comments": LeadCommentSerializer(comments, many=True).data, "users_mention": users_mention, "assigned_data": assigned_data, } ) context["users"] = UserSerializer(users, many=True).data context["users_excluding_team"] = UserSerializer( users_excluding_team, many=True ).data context["source"] = LEAD_SOURCE context["status"] = LEAD_STATUS context["teams"] = TeamsSerializer( Teams.objects.filter(company=self.request.company), many=True ).data context["countries"] = COUNTRIES return context