def get(self, request, format=None): accounts = Account.objects.filter(status="open", company=request.company) contacts = Contact.objects.filter(company=request.company) leads = Lead.objects.filter(company=request.company).exclude( Q(status="converted") | Q(status="closed")) opportunities = Opportunity.objects.filter(company=request.company) if self.request.user.role == "ADMIN" or self.request.user.is_superuser: pass else: accounts = accounts.filter( Q(assigned_to__id__in=[self.request.user.id]) | Q(created_by=self.request.user.id)) contacts = contacts.filter( Q(assigned_to__id__in=[self.request.user.id]) | Q(created_by=self.request.user.id)) leads = leads.filter( Q(assigned_to__id__in=[self.request.user.id]) | Q(created_by=self.request.user.id)).exclude(status="closed") opportunities = opportunities.filter( Q(assigned_to__id__in=[self.request.user.id]) | Q(created_by=self.request.user.id)) context = {} context["accounts_count"] = accounts.count() context["contacts_count"] = contacts.count() context["leads_count"] = leads.count() context["opportunities_count"] = opportunities.count() context["accounts"] = AccountSerializer(accounts, many=True).data context["contacts"] = ContactSerializer(contacts, many=True).data context["leads"] = LeadSerializer(leads, many=True).data context["opportunities"] = OpportunitySerializer(opportunities, many=True).data return Response(context, status=status.HTTP_200_OK)
def get_context_data(self, **kwargs): params = (self.request.query_params if len(self.request.data) == 0 else self.request.data) queryset = self.model.objects.filter( org=self.request.org).order_by('-id') accounts = Account.objects.filter(org=self.request.org) contacts = Contact.objects.filter(org=self.request.org) if self.request.profile.role != "ADMIN" and not self.request.user.is_superuser: queryset = queryset.filter( Q(created_by=self.request.profile) | Q(assigned_to=self.request.profile)).distinct() accounts = accounts.filter( Q(created_by=self.request.profile) | Q(assigned_to=self.request.profile)).distinct() contacts = contacts.filter( Q(created_by=self.request.profile) | Q(assigned_to=self.request.profile)).distinct() if params: if params.get("name"): queryset = queryset.filter(name__icontains=params.get("name")) if params.get("account"): queryset = queryset.filter(account=params.get("account")) if params.get("stage"): queryset = queryset.filter(stage__contains=params.get("stage")) if params.get("lead_source"): queryset = queryset.filter( lead_source__contains=params.get("lead_source")) if params.get("tags"): queryset = queryset.filter( tags__in=json.loads(params.get("tags"))).distinct() context = {} results_opportunities = self.paginate_queryset(queryset.distinct(), self.request, view=self) opportunities = OpportunitySerializer(results_opportunities, many=True).data if results_opportunities: offset = queryset.filter( id__gte=results_opportunities[-1].id).count() if offset == queryset.count(): offset = None else: offset = 0 context.update({ "opportunities_count": self.count, "offset": offset, }) context["opportunities"] = opportunities context["accounts_list"] = AccountSerializer(accounts, many=True).data context["contacts_list"] = ContactSerializer(contacts, many=True).data context['tags'] = TagsSerailizer(Tags.objects.filter(), many=True).data context["stage"] = STAGES context["lead_source"] = SOURCES context["currency"] = CURRENCY_CODES return context
def post(self, request, pk, **kwargs): params = (self.request.query_params if len(self.request.data) == 0 else self.request.data) context = {} self.opportunity_obj = Opportunity.objects.get(pk=pk) if self.opportunity_obj.company != request.company: return Response({ "error": True, "errors": "User company doesnot match with header...." }) comment_serializer = CommentSerializer(data=params) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if not ( (self.request.user == self.opportunity_obj.created_by) or (self.request.user in self.opportunity_obj.assigned_to.all())): return Response( { "error": True, "errors": "You don't have Permission to perform this action", }, status=status.HTTP_401_UNAUTHORIZED, ) if comment_serializer.is_valid(): if params.get("comment"): comment_serializer.save( opportunity_id=self.opportunity_obj.id, commented_by_id=self.request.user.id, ) if self.request.FILES.get("opportunity_attachment"): attachment = Attachments() attachment.created_by = self.request.user attachment.file_name = self.request.FILES.get( "opportunity_attachment").name attachment.opportunity = self.opportunity_obj attachment.attachment = self.request.FILES.get( "opportunity_attachment") attachment.save() comments = Comment.objects.filter( opportunity=self.opportunity_obj).order_by("-id") attachments = Attachments.objects.filter( opportunity=self.opportunity_obj).order_by("-id") context.update({ "opportunity_obj": OpportunitySerializer(self.opportunity_obj).data, "attachments": AttachmentsSerializer(attachments, many=True).data, "comments": CommentSerializer(comments, many=True).data, }) return Response(context)
def get(self, request, pk, format=None): profile_obj = self.get_object(pk) if (self.request.profile.role != "ADMIN" and not self.request.profile.is_admin and self.request.profile.id != profile_obj.id): return Response( { "error": True, "errors": "Permission Denied" }, status=status.HTTP_403_FORBIDDEN, ) if profile_obj.org != request.org: return Response( { "error": True, "errors": "User company doesnot match with header...." }, status=status.HTTP_403_FORBIDDEN, ) assigned_data = Profile.objects.filter(org=request.org, is_active=True).values( 'id', 'user__first_name') context = {} context["profile_obj"] = ProfileSerializer(profile_obj).data opportunity_list = Opportunity.objects.filter(assigned_to=profile_obj) context["opportunity_list"] = OpportunitySerializer(opportunity_list, many=True).data contacts = Contact.objects.filter(assigned_to=profile_obj) context["contacts"] = ContactSerializer(contacts, many=True).data cases = Case.objects.filter(assigned_to=profile_obj) context["cases"] = CaseSerializer(cases, many=True).data context["assigned_data"] = assigned_data comments = profile_obj.user_comments.all() context["comments"] = CommentSerializer(comments, many=True).data context["countries"] = COUNTRIES return Response( { "error": False, "data": context }, status=status.HTTP_200_OK, )
def get(self, request, pk, format=None): user_obj = self.get_object(pk) users_data = [] for each in User.objects.all(): assigned_dict = {} assigned_dict["id"] = each.id assigned_dict["name"] = each.username users_data.append(assigned_dict) context = {} context["user_obj"] = UserSerializer(user_obj).data opportunity_list = Opportunity.objects.filter(assigned_to=user_obj) context["opportunity_list"] = OpportunitySerializer(opportunity_list, many=True).data contacts = Contact.objects.filter(assigned_to=user_obj) context["contacts"] = ContactSerializer(contacts, many=True).data cases = Case.objects.filter(assigned_to=user_obj) context["cases"] = CaseSerializer(cases, many=True).data context["assigned_data"] = users_data comments = user_obj.user_comments.all() context["comments"] = CommentSerializer(comments, many=True).data return Response({"error": False, "data": context})
def get(self, request, pk, format=None): user_obj = self.get_object(pk) if (self.request.user.role != "ADMIN" and not self.request.user.is_superuser and self.request.user.id != user_obj.id): return Response( { "error": True, "errors": "Permission Denied" }, status=status.HTTP_403_FORBIDDEN, ) users_data = [] for each in User.objects.all(): assigned_dict = {} assigned_dict["id"] = each.id assigned_dict["name"] = each.username users_data.append(assigned_dict) context = {} context["user_obj"] = UserSerializer(user_obj).data opportunity_list = Opportunity.objects.filter(assigned_to=user_obj) context["opportunity_list"] = OpportunitySerializer(opportunity_list, many=True).data contacts = Contact.objects.filter(assigned_to=user_obj) context["contacts"] = ContactSerializer(contacts, many=True).data cases = Case.objects.filter(assigned_to=user_obj) context["cases"] = CaseSerializer(cases, many=True).data context["assigned_data"] = users_data comments = user_obj.user_comments.all() context["comments"] = CommentSerializer(comments, many=True).data return Response( { "error": False, "data": context }, status=status.HTTP_200_OK, )
def get(self, request, pk, format=None): self.opportunity = self.get_object(pk=pk) context = {} context["opportunity_obj"] = OpportunitySerializer( self.opportunity).data if self.opportunity.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.opportunity.created_by) or (self.request.profile in self.opportunity.assigned_to.all())): return Response( { "error": True, "errors": "You don't have Permission to perform this action", }, status=status.HTTP_403_FORBIDDEN, ) comment_permission = False if (self.request.profile == self.opportunity.created_by or self.request.user.is_superuser or self.request.profile.role == "ADMIN"): comment_permission = True if self.request.user.is_superuser or self.request.profile.role == "ADMIN": users_mention = list( Profile.objects.filter( is_active=True, org=self.request.org).values("user__username")) elif self.request.profile != self.opportunity.created_by: if self.opportunity.created_by: users_mention = [{ "username": self.opportunity.created_by.user.username }] else: users_mention = [] else: users_mention = [] context.update({ "comments": CommentSerializer(self.opportunity.opportunity_comments.all(), many=True).data, "attachments": AttachmentsSerializer( self.opportunity.opportunity_attachment.all(), many=True).data, "contacts": ContactSerializer(self.opportunity.contacts.all(), many=True).data, "users": ProfileSerializer( Profile.objects.filter( is_active=True, org=self.request.org).order_by("user__email"), many=True, ).data, "stage": STAGES, "lead_source": SOURCES, "currency": CURRENCY_CODES, "comment_permission": comment_permission, "users_mention": users_mention, }) return Response(context)
def get(self, request, pk, format=None): context = {} context["account_obj"] = AccountSerializer(self.account).data if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if not ((self.request.user == self.account.created_by) or (self.request.user in self.account.assigned_to.all())): raise PermissionDenied comment_permission = (True if (self.request.user == self.account.created_by or self.request.user.is_superuser or self.request.user.role == "ADMIN") else False) 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.account.created_by: if self.account.created_by: users_mention = [{ "username": self.account.created_by.username }] else: users_mention = [] else: users_mention = [] context.update({ "comments": CommentSerializer(self.account.accounts_comments.all(), many=True).data, "attachments": AttachmentsSerializer(self.account.account_attachment.all(), many=True).data, "opportunity_list": OpportunitySerializer( Opportunity.objects.filter(account=self.account), many=True).data, "contacts": ContactSerializer(self.account.contacts.all(), many=True).data, "users": UserSerializer( User.objects.filter( is_active=True, company=self.request.company, ).order_by("email"), many=True, ).data, "cases": CaseSerializer(Case.objects.filter(account=self.account), many=True).data, "stages": STAGES, "sources": SOURCES, "countries": COUNTRIES, "currencies": CURRENCY_CODES, "case_types": CASE_TYPE, "case_priority": PRIORITY_CHOICE, "case_status": STATUS_CHOICE, "comment_permission": comment_permission, "tasks": TaskSerializer(self.account.accounts_tasks.all(), many=True).data, "invoices": InvoiceSerailizer(self.account.accounts_invoices.all(), many=True).data, "emails": EmailSerailizer(self.account.sent_email.all(), many=True).data, "users_mention": users_mention, }) return Response(context)
def get(self, request, pk, format=None): self.account = self.get_object(pk=pk) if self.account.org != request.org: return Response( { "error": True, "errors": "User company doesnot match with header...." }, status=status.HTTP_404_NOT_FOUND, ) context = {} context["account_obj"] = AccountSerializer(self.account).data if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin: if not ((self.request.profile == self.account.created_by) or (self.request.profile in self.account.assigned_to.all())): return Response( { "error": True, "errors": "You do not have Permission to perform this action", }, status=status.HTTP_403_FORBIDDEN, ) comment_permission = False if (self.request.profile == self.account.created_by or self.request.profile.is_admin or self.request.profile.role == "ADMIN"): comment_permission = True if self.request.profile.is_admin or self.request.profile.role == "ADMIN": users_mention = list( Profile.objects.filter( is_active=True, org=self.request.org).values("user__username")) elif self.request.profile != self.account.created_by: if self.account.created_by: users_mention = [{ "username": self.account.created_by.user.username }] else: users_mention = [] else: users_mention = [] context.update({ "attachments": AttachmentsSerializer(self.account.account_attachment.all(), many=True).data, "comments": CommentSerializer(self.account.accounts_comments.all(), many=True).data, "contacts": ContactSerializer(self.account.contacts.all(), many=True).data, "opportunity_list": OpportunitySerializer( Opportunity.objects.filter(account=self.account), many=True).data, "users": ProfileSerializer( Profile.objects.filter( is_active=True, org=self.request.org).order_by("user__email"), many=True, ).data, "cases": CaseSerializer(self.account.accounts_cases.all(), many=True).data, "stages": STAGES, "sources": SOURCES, "countries": COUNTRIES, "currencies": CURRENCY_CODES, "case_types": CASE_TYPE, "case_priority": PRIORITY_CHOICE, "case_status": STATUS_CHOICE, "comment_permission": comment_permission, "tasks": TaskSerializer(self.account.accounts_tasks.all(), many=True).data, "invoices": InvoiceSerailizer(self.account.accounts_invoices.all(), many=True).data, "emails": EmailSerializer(self.account.sent_email.all(), many=True).data, "users_mention": users_mention, }) return Response(context)
def get_context_data(self, **kwargs): params = ( self.request.query_params if len(self.request.data) == 0 else self.request.data ) queryset = self.model.objects.filter(company=self.request.company) accounts = Account.objects.filter(company=self.request.company) contacts = Contact.objects.filter(company=self.request.company) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: queryset = queryset.filter( Q(created_by=self.request.user) | Q(assigned_to=self.request.user) ).distinct() accounts = accounts.filter( Q(created_by=self.request.user) | Q(assigned_to=self.request.user) ).distinct() contacts = contacts.filter( Q(created_by=self.request.user) | Q(assigned_to=self.request.user) ).distinct() if params: request_post = params if request_post.get("name"): queryset = queryset.filter( name__icontains=request_post.get("name") ) if request_post.get("account"): queryset = queryset.filter( account=request_post.get("account") ) if request_post.get("stage"): queryset = queryset.filter( stage__contains=request_post.get("stage") ) if request_post.get("lead_source"): queryset = queryset.filter( lead_source__contains=request_post.get("lead_source") ) if request_post.get("tags"): queryset = queryset.filter( tags__in=json.loads(request_post.get("tags")) ).distinct() context = {} search = False if ( params.get("name") or params.get("account") or params.get("stage") or params.get("lead_source") or params.get("tags") ): search = True context["search"] = search opportunities = OpportunitySerializer( queryset, many=True ).data context["opportunities"] = opportunities context["users"] = UserSerializer( User.objects.filter(is_active=True, company=self.request.company).order_by("email"), many=True ).data tag_ids = list(set(Opportunity.objects.filter( company=self.request.company).values_list("tags", flat=True))) context["tags"] = TagsSerializer( Tags.objects.filter(id__in=tag_ids), many=True ).data context["accounts_list"] = AccountSerializer( accounts, many=True ).data context["contacts_list"] = ContactSerializer( contacts, many=True ).data if self.request.user == "ADMIN": context["teams_list"] = TeamsSerializer( Teams.objects.filter(company=self.request.company), many=True ).data context["stage"] = STAGES context["lead_source"] = SOURCES context["currency"] = CURRENCY_CODES return context