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)
def update_lead(request, pk): lead_record = Lead.objects.filter(pk=pk).first() template_name = "create_lead.html" users = [] if request.user.role == 'ADMIN' or request.user.is_superuser: users = User.objects.filter(is_active=True).order_by('email') else: users = User.objects.filter(role='ADMIN').order_by('email') status = request.GET.get('status', None) initial = {} if status and status == "Qualified": error = "This field is required." lead_record.status = "Qualified" initial.update({ "status": status, "lead": lead_record.id}) error = "" form = LeadForm(instance=lead_record, initial=initial, assigned_to=users) if request.POST: form = LeadForm(request.POST, request.FILES, instance=lead_record, initial=initial, assigned_to=users) if request.POST.get('status') == "Qualified": form.fields['account_name'].required = True form.fields['email'].required = True else: form.fields['account_name'].required = False form.fields['email'].required = False if form.is_valid(): assigned_to_ids = lead_record.assigned_to.all().values_list( 'id', flat=True) lead_obj = form.save(commit=False) lead_obj.save() previous_assigned_to_users = list(lead_obj.assigned_to.all().values_list('id', flat=True)) lead_obj.tags.clear() all_members_list = [] if request.POST.get('tags', ''): tags = request.POST.get("tags") splitted_tags = tags.split(",") for t in splitted_tags: tag = Tags.objects.filter(name=t) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t) lead_obj.tags.add(tag) if request.POST.getlist('assigned_to', []): if request.POST.get('status') != "Qualified": current_site = get_current_site(request) assigned_form_users = form.cleaned_data.get( 'assigned_to').values_list('id', flat=True) all_members_list = list( set(list(assigned_form_users)) - set(list(assigned_to_ids))) # current_site = get_current_site(request) # recipients = all_members_list # send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain, # protocol=request.scheme) # if all_members_list: # for assigned_to_user in all_members_list: # user = get_object_or_404(User, pk=assigned_to_user) # mail_subject = 'Assigned to lead.' # message = render_to_string( # 'assigned_to/leads_assigned.html', { # 'user': user, # 'domain': current_site.domain, # 'protocol': request.scheme, # 'lead': lead_obj # }) # email = EmailMessage( # mail_subject, message, to=[user.email]) # email.content_subtype = "html" # email.send() lead_obj.assigned_to.clear() lead_obj.assigned_to.add(*request.POST.getlist('assigned_to')) else: lead_obj.assigned_to.clear() if request.POST.getlist('teams', []): user_ids = Teams.objects.filter(id__in=request.POST.getlist('teams')).values_list('users', flat=True) assinged_to_users_ids = lead_obj.assigned_to.all().values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: lead_obj.assigned_to.add(user_id) if request.POST.getlist('teams', []): lead_obj.teams.clear() lead_obj.teams.add(*request.POST.getlist('teams')) else: lead_obj.teams.clear() current_site = get_current_site(request) assigned_to_list = list(lead_obj.assigned_to.all().values_list('id', flat=True)) recipients = list(set(assigned_to_list) - set(previous_assigned_to_users)) send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme) # update_leads_cache.delay() if request.FILES.get('lead_attachment'): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( 'lead_attachment').name attachment.lead = lead_obj attachment.attachment = request.FILES.get('lead_attachment') attachment.save() if request.POST.get('status') == "Qualified": account_object = Account.objects.create( created_by=request.user, name=lead_obj.account_name, email=lead_obj.email, phone=lead_obj.phone, description=request.POST.get('description'), website=request.POST.get('website'), lead=lead_obj ) account_object.billing_address_line = lead_obj.address_line account_object.billing_street = lead_obj.street account_object.billing_city = lead_obj.city account_object.billing_state = lead_obj.state account_object.billing_postcode = lead_obj.postcode account_object.billing_country = lead_obj.country for tag in lead_obj.tags.all(): account_object.tags.add(tag) if request.POST.getlist('assigned_to', []): # account_object.assigned_to.add(*request.POST.getlist('assigned_to')) assigned_to_list = request.POST.getlist('assigned_to') current_site = get_current_site(request) recipients = assigned_to_list send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme) # current_site = get_current_site(request) # for assigned_to_user in assigned_to_list: # user = get_object_or_404(User, pk=assigned_to_user) # mail_subject = 'Assigned to account.' # message = render_to_string( # 'assigned_to/account_assigned.html', { # 'user': user, # 'domain': current_site.domain, # 'protocol': request.scheme, # 'account': account_object # }) # email = EmailMessage( # mail_subject, message, to=[user.email]) # email.content_subtype = "html" # email.send() for comment in lead_obj.leads_comments.all(): comment.account = account_object comment.save() account_object.save() status = request.GET.get('status', None) success_url = reverse('leads:list') if status: success_url = reverse('accounts:list') return JsonResponse({'error': False, 'success_url': success_url}) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context["lead_obj"] = lead_record user_assgn_list = [ assigned_to.id for assigned_to in lead_record.get_assigned_users_not_in_teams] if request.user == lead_record.created_by: user_assgn_list.append(request.user.id) if request.user.role != "ADMIN" and not request.user.is_superuser: if request.user.id not in user_assgn_list: raise PermissionDenied team_ids = [user.id for user in lead_record.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["lead_form"] = form context["accounts"] = Account.objects.filter(status="open") context["users"] = users context["users_excluding_team"] = users_excluding_team context["countries"] = COUNTRIES context["status"] = LEAD_STATUS context["source"] = LEAD_SOURCE context["error"] = error context["teams"] = Teams.objects.all() context["assignedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i] return render(request, template_name, context)
def create_lead(request): template_name = "create_lead.html" users = [] if request.user.role == 'ADMIN' or request.user.is_superuser: users = User.objects.filter(is_active=True).order_by('email') elif request.user.google.all(): users = [] else: users = User.objects.filter(role='ADMIN').order_by('email') form = LeadForm(assigned_to=users) if request.POST: form = LeadForm(request.POST, request.FILES, assigned_to=users) if form.is_valid(): lead_obj = form.save(commit=False) lead_obj.created_by = request.user lead_obj.save() l = Lead.objects.values_list('id',flat=True).latest('id') print("Lead:",l) if request.POST.get('tags', ''): tags = request.POST.get("tags") splitted_tags = tags.split(",") for t in splitted_tags: tag = Tags.objects.filter(name=t) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t) lead_obj.tags.add(tag) if request.POST.getlist('assigned_to', []): lead_obj.assigned_to.add(*request.POST.getlist('assigned_to')) assigned_to_list = request.POST.getlist('assigned_to') for i in assigned_to_list: print("I:",i) print(User.objects.values_list('username',flat=True).get(id=i)) # V_user = get_object_or_404(User,pk=i) v_user=User.objects.values_list('username',flat=True).get(id=i) uname = request.user.id notifi=LeadNotification.objects.create(leadid=l,userid=i) # current_site = get_current_site(request) # recipients = assigned_to_list # send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain, # protocol=request.scheme) # for assigned_to_user in assigned_to_list: # user = get_object_or_404(User, pk=assigned_to_user) # mail_subject = 'Assigned to lead.' # message = render_to_string( # 'assigned_to/leads_assigned.html', { # 'user': user, # 'domain': current_site.domain, # 'protocol': request.scheme, # 'lead': lead_obj # }) # email = EmailMessage( # mail_subject, message, to=[user.email]) # email.content_subtype = "html" # email.send() if request.POST.getlist('teams', []): user_ids = Teams.objects.filter(id__in=request.POST.getlist('teams')).values_list('users', flat=True) assinged_to_users_ids = lead_obj.assigned_to.all().values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: lead_obj.assigned_to.add(user_id) if request.POST.getlist('teams', []): lead_obj.teams.add(*request.POST.getlist('teams')) current_site = get_current_site(request) recipients = list(lead_obj.assigned_to.all().values_list('id', flat=True)) send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme) if request.FILES.get('lead_attachment'): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( 'lead_attachment').name attachment.lead = lead_obj attachment.attachment = request.FILES.get('lead_attachment') attachment.save() if request.POST.get('status') == "qualified": account_object = Account.objects.create( created_by=request.user, name=lead_obj.account_name, email=lead_obj.email, phone=lead_obj.phone, description=request.POST.get('description'), website=request.POST.get('website'), ) account_object.billing_address_line = lead_obj.address_line account_object.billing_street = lead_obj.street account_object.billing_city = lead_obj.city account_object.billing_state = lead_obj.state account_object.billing_postcode = lead_obj.postcode account_object.billing_country = lead_obj.country for tag in lead_obj.tags.all(): account_object.tags.add(tag) if request.POST.getlist('assigned_to', []): # account_object.assigned_to.add(*request.POST.getlist('assigned_to')) assigned_to_list = request.POST.getlist('assigned_to') current_site = get_current_site(request) recipients = assigned_to_list send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme) # for assigned_to_user in assigned_to_list: # user = get_object_or_404(User, pk=assigned_to_user) # mail_subject = 'Assigned to account.' # message = render_to_string( # 'assigned_to/account_assigned.html', { # 'user': user, # 'domain': current_site.domain, # 'protocol': request.scheme, # 'account': account_object # }) # email = EmailMessage( # mail_subject, message, to=[user.email]) # email.content_subtype = "html" # email.send() account_object.save() # update_leads_cache.delay() success_url = reverse('leads:list') if request.POST.get("savenewform"): success_url = reverse("leads:add_lead") return JsonResponse({'error': False, 'success_url': success_url}) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context["lead_form"] = form context["accounts"] = Account.objects.filter(status="open") context["users"] = users context["countries"] = COUNTRIES context["status"] = LEAD_STATUS context["source"] = LEAD_SOURCE context["teams"] = Teams.objects.all() context["assignedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i] return render(request, template_name, context)
def create_lead(request): template_name = "create_lead.html" users = [] if request.user.role == 'ADMIN' or request.user.is_superuser: users = User.objects.filter(is_active=True).order_by('email') elif request.user.google.all(): users = [] else: users = User.objects.filter(role='ADMIN').order_by('email') form = LeadForm(assigned_to=users) if request.POST: print(request.POST) cat_id = Category.objects.get(pk=request.POST.get('category')) subc_id = SubCategory.objects.get(pk=request.POST.get('sub_category')) item_id = ItemList.objects.get(pk=request.POST.get('item_list')) form = LeadForm(request.POST, request.FILES, assigned_to=users,sub_category=subc_id,item_list=item_id) if form.is_valid(): lead_obj = form.save(commit=False) lead_obj.created_by = request.user lead_obj.save() if request.POST.get('tags', ''): tags = request.POST.get("tags") splitted_tags = tags.split(",") for t in splitted_tags: tag = Tags.objects.filter(name=t) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t) lead_obj.tags.add(tag) if request.POST.getlist('assigned_to', []): lead_obj.assigned_to.add(*request.POST.getlist('assigned_to')) assigned_to_list = request.POST.getlist('assigned_to') if request.POST.getlist('teams', []): user_ids = Teams.objects.filter(id__in=request.POST.getlist('teams')).values_list('users', flat=True) assinged_to_users_ids = lead_obj.assigned_to.all().values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: lead_obj.assigned_to.add(user_id) if request.POST.getlist('teams', []): lead_obj.teams.add(*request.POST.getlist('teams')) if request.POST.getlist('products',[]): lead_obj.products.add(*request.POST.getlist('products')) current_site = get_current_site(request) recipients = list(lead_obj.assigned_to.all().values_list('id', flat=True)) send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme) if request.FILES.get('lead_attachment'): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( 'lead_attachment').name attachment.lead = lead_obj attachment.attachment = request.FILES.get('lead_attachment') attachment.save() if request.POST.get('status') == "converted": account_object = Account.objects.create( created_by=request.user, name=lead_obj.account_name, email=lead_obj.email, phone=lead_obj.phone, description=request.POST.get('description'), website=request.POST.get('website'), ) account_object.billing_address_line = lead_obj.address_line account_object.billing_street = lead_obj.street account_object.billing_city = lead_obj.city account_object.billing_state = lead_obj.state account_object.billing_postcode = lead_obj.postcode account_object.billing_country = lead_obj.country for tag in lead_obj.tags.all(): account_object.tags.add(tag) if request.POST.getlist('assigned_to', []): # account_object.assigned_to.add(*request.POST.getlist('assigned_to')) assigned_to_list = request.POST.getlist('assigned_to') current_site = get_current_site(request) recipients = assigned_to_list send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme) account_object.save() # update_leads_cache.delay() success_url = reverse('leads:list') if request.POST.get("savenewform"): success_url = reverse("leads:add_lead") return JsonResponse({'error': False, 'success_url': success_url}) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context["lead_form"] = form context["accounts"] = Account.objects.filter(status="open") context["users"] = users context["countries"] = COUNTRIES context["status"] = LEAD_STATUS context["source"] = LEAD_SOURCE context["teams"] = Teams.objects.all() context['products'] = Product.objects.all() context["assignedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i] return render(request, template_name, context)
def create_lead(request): template_name = "create_lead.html" users = User.objects.filter(is_active=True).order_by('email') form = LeadForm(assigned_to=users) if request.POST: form = LeadForm(request.POST, request.FILES, assigned_to=users) if form.is_valid(): lead_obj = form.save(commit=False) lead_obj.created_by = request.user lead_obj.save() if request.POST.get('tags', ''): tags = request.POST.get("tags") splitted_tags = tags.split(",") for t in splitted_tags: tag = Tags.objects.filter(name=t) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t) lead_obj.tags.add(tag) if request.POST.getlist('assigned_to', []): lead_obj.assigned_to.add(*request.POST.getlist('assigned_to')) assigned_to_list = request.POST.getlist('assigned_to') current_site = get_current_site(request) for assigned_to_user in assigned_to_list: user = get_object_or_404(User, pk=assigned_to_user) mail_subject = 'Assigned to lead.' message = render_to_string( 'assigned_to/leads_assigned.html', { 'user': user, 'domain': current_site.domain, 'protocol': request.scheme, 'lead': lead_obj }) email = EmailMessage(mail_subject, message, to=[user.email]) email.content_subtype = "html" email.send() if request.FILES.get('lead_attachment'): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( 'lead_attachment').name attachment.lead = lead_obj attachment.attachment = request.FILES.get('lead_attachment') attachment.save() if request.POST.get('status') == "converted": account_object = Account.objects.create( created_by=request.user, name=lead_obj.account_name, email=lead_obj.email, phone=lead_obj.phone, description=request.POST.get('description'), website=request.POST.get('website'), ) account_object.billing_address_line = lead_obj.address_line account_object.billing_street = lead_obj.street account_object.billing_city = lead_obj.city account_object.billing_state = lead_obj.state account_object.billing_postcode = lead_obj.postcode account_object.billing_country = lead_obj.country for tag in lead_obj.tags.all(): account_object.tags.add(tag) if request.POST.getlist('assigned_to', []): # account_object.assigned_to.add(*request.POST.getlist('assigned_to')) assigned_to_list = request.POST.getlist('assigned_to') current_site = get_current_site(request) for assigned_to_user in assigned_to_list: user = get_object_or_404(User, pk=assigned_to_user) mail_subject = 'Assigned to account.' message = render_to_string( 'assigned_to/account_assigned.html', { 'user': user, 'domain': current_site.domain, 'protocol': request.scheme, 'account': account_object }) email = EmailMessage(mail_subject, message, to=[user.email]) email.content_subtype = "html" email.send() account_object.save() success_url = reverse('leads:list') if request.POST.get("savenewform"): success_url = reverse("leads:add_lead") return JsonResponse({'error': False, 'success_url': success_url}) else: return JsonResponse({'error': True, 'errors': form.errors}) else: context = {} context["lead_form"] = form context["accounts"] = Account.objects.filter(status="open") context["users"] = users context["countries"] = COUNTRIES context["status"] = LEAD_STATUS context["source"] = LEAD_SOURCE context["assignedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i ] return render(request, template_name, context)
def form_valid(self, form): assigned_to_ids = self.get_object().assigned_to.all().values_list( 'id', flat=True) lead_obj = form.save(commit=False) lead_obj.save() lead_obj.tags.clear() all_members_list = [] if self.request.POST.get('tags', ''): tags = self.request.POST.get("tags") splitted_tags = tags.split(",") for t in splitted_tags: tag = Tags.objects.filter(name=t) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t) lead_obj.tags.add(tag) if self.request.POST.getlist('assigned_to', []): if self.request.POST.get('status') != "converted": current_site = get_current_site(self.request) assigned_form_users = form.cleaned_data.get( 'assigned_to').values_list('id', flat=True) all_members_list = list( set(list(assigned_form_users)) - set(list(assigned_to_ids))) if len(all_members_list): for assigned_to_user in all_members_list: user = get_object_or_404(User, pk=assigned_to_user) mail_subject = 'Assigned to lead.' message = render_to_string( 'assigned_to/leads_assigned.html', { 'user': user, 'domain': current_site.domain, 'protocol': self.request.scheme, 'lead': lead_obj }) email = EmailMessage(mail_subject, message, to=[user.email]) email.content_subtype = "html" email.send() lead_obj.assigned_to.clear() lead_obj.assigned_to.add(*self.request.POST.getlist('assigned_to')) else: lead_obj.assigned_to.clear() if self.request.FILES.get('lead_attachment'): attachment = Attachments() attachment.created_by = self.request.user attachment.file_name = self.request.FILES.get( 'lead_attachment').name attachment.lead = lead_obj attachment.attachment = self.request.FILES.get('lead_attachment') attachment.save() if self.request.POST.get('status') == "converted": account_object = Account.objects.create( created_by=self.request.user, name=lead_obj.account_name, email=lead_obj.email, phone=lead_obj.phone, description=self.request.POST.get('description'), website=self.request.POST.get('website')) account_object.billing_address_line = lead_obj.address_line account_object.billing_street = lead_obj.street account_object.billing_city = lead_obj.city account_object.billing_state = lead_obj.state account_object.billing_postcode = lead_obj.postcode account_object.billing_country = lead_obj.country for tag in lead_obj.tags.all(): account_object.tags.add(tag) if self.request.POST.getlist('assigned_to', []): account_object.assigned_to.add( *self.request.POST.getlist('assigned_to')) assigned_to_list = self.request.POST.getlist('assigned_to') current_site = get_current_site(self.request) for assigned_to_user in assigned_to_list: user = get_object_or_404(User, pk=assigned_to_user) mail_subject = 'Assigned to account.' message = render_to_string( 'assigned_to/account_assigned.html', { 'user': user, 'domain': current_site.domain, 'protocol': self.request.scheme, 'account': account_object }) email = EmailMessage(mail_subject, message, to=[user.email]) email.content_subtype = "html" email.send() account_object.save() status = self.request.GET.get('status', None) if status: return redirect('accounts:list') return redirect('leads:list')
def put(self, request, pk, **kwargs): params = ( self.request.query_params if len(self.request.data) == 0 else self.request.data ) data = {} self.lead_obj = self.get_object(pk) if self.lead_obj.company != request.company: return Response( {"error": True, "errors": "User company doesnot match with header...."} ) serializer = LeadCreateSerializer( data=params, instance=self.lead_obj, request_obj=request, ) if serializer.is_valid(): lead_obj = serializer.save() previous_assigned_to_users = list( lead_obj.assigned_to.all().values_list("id", flat=True) ) lead_obj.tags.clear() if params.get("tags"): tags = json.loads(params.get("tags")) # for t in tags: # tag,_ = Tags.objects.get_or_create(name=t) # lead_obj.tags.add(tag) for t in tags: tag = Tags.objects.filter(slug=t.lower()) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t) lead_obj.tags.add(tag) current_site = get_current_site(request) assigned_to_list = list( lead_obj.assigned_to.all().values_list("id", flat=True) ) recipients = list(set(assigned_to_list) - set(previous_assigned_to_users)) send_email_to_assigned_user.delay( recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme, ) if request.FILES.get("lead_attachment"): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get("lead_attachment").name attachment.lead = lead_obj attachment.attachment = request.FILES.get("lead_attachment") attachment.save() if self.request.user.role == "ADMIN": lead_obj.teams.clear() if params.get("teams"): teams = json.loads(params.get("teams")) for team in teams: teams_ids = Teams.objects.filter( id=team, company=request.company ) if teams_ids: lead_obj.teams.add(team) else: lead_obj.delete() data["team"] = "Please enter valid Team" return Response({"error": True, "errors": data}) else: lead_obj.teams.clear() lead_obj.assigned_to.clear() if params.get("assigned_to"): assinged_to_users_ids = json.loads(params.get("assigned_to")) for user_id in assinged_to_users_ids: user = User.objects.filter(id=user_id, company=request.company) if user: lead_obj.assigned_to.add(user_id) else: lead_obj.delete() data["assigned_to"] = "Please enter valid User" return Response({"error": True, "errors": data}) else: lead_obj.assigned_to.clear() if params.get("status") == "converted": account_object = Account.objects.create( created_by=request.user, name=lead_obj.account_name, email=lead_obj.email, phone=lead_obj.phone, description=params.get("description"), website=params.get("website"), lead=lead_obj, company=request.company, ) account_object.billing_address_line = lead_obj.address_line account_object.billing_street = lead_obj.street account_object.billing_city = lead_obj.city account_object.billing_state = lead_obj.state account_object.billing_postcode = lead_obj.postcode account_object.billing_country = lead_obj.country comments = Comment.objects.filter(lead=self.lead_obj) if comments: for comment in comments: comment.account_id = account_object.id attachments = Attachments.objects.filter(lead=self.lead_obj) if attachments: for attachment in attachments: attachment.account_id = account_object.id for tag in lead_obj.tags.all(): account_object.tags.add(tag) if params.get("assigned_to"): # account_object.assigned_to.add(*params.getlist('assigned_to')) assigned_to_list = json.loads(params.get("assigned_to")) current_site = get_current_site(request) recipients = assigned_to_list send_email_to_assigned_user.delay( recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme, ) for comment in lead_obj.leads_comments.all(): comment.account = account_object comment.save() account_object.save() return Response( { "error": False, "message": "Lead Converted to Account Successfully", }, status=status.HTTP_200_OK, ) return Response( {"error": False, "message": "Lead updated Successfully"}, status=status.HTTP_200_OK, ) return Response( {"error": True, "errors": serializer.errors}, status=status.HTTP_400_BAD_REQUEST, )
def put(self, request, pk, **kwargs): params = ( self.request.query_params if len(self.request.data) == 0 else self.request.data ) self.lead_obj = self.get_object(pk) if self.lead_obj.company != request.company: return Response({'error': True}, status=status.HTTP_403_FORBIDDEN) serializer = LeadCreateSerializer( data=params, instance=self.lead_obj, request_obj=request, ) if serializer.is_valid(): lead_obj = serializer.save() previous_assigned_to_users = list( lead_obj.assigned_to.all().values_list("id", flat=True) ) lead_obj.tags.clear() if params.get("tags", ""): tags = params.get("tags") splitted_tags = tags.split(",") for t in splitted_tags: tag = Tags.objects.filter(name=t) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t) lead_obj.tags.add(tag) if params.getlist("assigned_to", []): lead_obj.assigned_to.clear() lead_obj.assigned_to.add(*params.getlist("assigned_to")) else: lead_obj.assigned_to.clear() if params.getlist("teams", []): user_ids = Teams.objects.filter( id__in=params.getlist("teams") ).values_list("users", flat=True) assinged_to_users_ids = lead_obj.assigned_to.all().values_list( "id", flat=True ) for user_id in user_ids: if user_id not in assinged_to_users_ids: lead_obj.assigned_to.add(user_id) if params.getlist("teams", []): lead_obj.teams.clear() lead_obj.teams.add(*params.getlist("teams")) else: lead_obj.teams.clear() current_site = get_current_site(request) assigned_to_list = list( lead_obj.assigned_to.all().values_list("id", flat=True) ) recipients = list(set(assigned_to_list) - set(previous_assigned_to_users)) send_email_to_assigned_user.delay( recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme, ) # update_leads_cache.delay() if request.FILES.get("lead_attachment"): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( "lead_attachment").name attachment.lead = lead_obj attachment.attachment = request.FILES.get("lead_attachment") attachment.save() if params.get("status") == "converted": account_object = Account.objects.create( created_by=request.user, name=lead_obj.account_name, email=lead_obj.email, phone=lead_obj.phone, description=params.get("description"), website=params.get("website"), lead=lead_obj, company=request.company, ) account_object.billing_address_line = lead_obj.address_line account_object.billing_street = lead_obj.street account_object.billing_city = lead_obj.city account_object.billing_state = lead_obj.state account_object.billing_postcode = lead_obj.postcode account_object.billing_country = lead_obj.country for tag in lead_obj.tags.all(): account_object.tags.add(tag) if params.getlist("assigned_to", []): # account_object.assigned_to.add(*params.getlist('assigned_to')) assigned_to_list = params.getlist("assigned_to") current_site = get_current_site(request) recipients = assigned_to_list send_email_to_assigned_user.delay( recipients, lead_obj.id, domain=current_site.domain, protocol=request.scheme, ) for comment in lead_obj.leads_comments.all(): comment.account = account_object comment.save() account_object.save() return Response({'error': False, 'message': 'Lead updated Successfully'}, status=status.HTTP_200_OK) return Response({'error': True, 'errors': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, **kwargs): params = ( self.request.query_params if len(self.request.data) == 0 else self.request.data ) self.lead_obj = self.get_object(pk) if self.lead_obj.org != request.org: return Response( {"error": True, "errors": "User company does not match with header...."}, status=status.HTTP_403_FORBIDDEN ) serializer = LeadCreateSerializer( data=params, instance=self.lead_obj, request_obj=request, ) if serializer.is_valid(): lead_obj = serializer.save() previous_assigned_to_users = list( lead_obj.assigned_to.all().values_list("id", flat=True) ) lead_obj.tags.clear() if params.get("tags"): tags = json.loads(params.get("tags")) # for t in tags: # tag,_ = Tags.objects.get_or_create(name=t) # lead_obj.tags.add(tag) for t in tags: tag = Tags.objects.filter(slug=t.lower()) if tag.exists(): tag = tag[0] else: tag = Tags.objects.create(name=t) lead_obj.tags.add(tag) assigned_to_list = list( lead_obj.assigned_to.all().values_list("id", flat=True) ) recipients = list(set(assigned_to_list) - set(previous_assigned_to_users)) send_email_to_assigned_user.delay( recipients, lead_obj.id, ) if request.FILES.get("lead_attachment"): attachment = Attachments() attachment.created_by = request.profile attachment.file_name = request.FILES.get( "lead_attachment").name attachment.lead = lead_obj attachment.attachment = request.FILES.get("lead_attachment") attachment.save() lead_obj.contacts.clear() if params.get("contacts"): obj_contact = Contact.objects.filter( id=params.get("contacts"), org=request.org) lead_obj.contacts.add(obj_contact) lead_obj.teams.clear() if params.get("teams"): teams_list = json.loads(params.get("teams")) teams = Teams.objects.filter( id__in=teams_list, org=request.org) lead_obj.teams.add(*teams) lead_obj.assigned_to.clear() if params.get("assigned_to"): assinged_to_list = json.loads( params.get("assigned_to")) profiles = Profile.objects.filter( id__in=assinged_to_list, org=request.org) lead_obj.assigned_to.add(*profiles) if params.get("status") == "converted": account_object = Account.objects.create( created_by=request.profile, name=lead_obj.account_name, email=lead_obj.email, phone=lead_obj.phone, description=params.get("description"), website=params.get("website"), lead=lead_obj, org=request.org ) account_object.billing_address_line = lead_obj.address_line account_object.billing_street = lead_obj.street account_object.billing_city = lead_obj.city account_object.billing_state = lead_obj.state account_object.billing_postcode = lead_obj.postcode account_object.billing_country = lead_obj.country comments = Comment.objects.filter(lead=self.lead_obj) if comments.exists(): for comment in comments: comment.account_id = account_object.id attachments = Attachments.objects.filter(lead=self.lead_obj) if attachments.exists(): for attachment in attachments: attachment.account_id = account_object.id for tag in lead_obj.tags.all(): account_object.tags.add(tag) if params.get("assigned_to"): # account_object.assigned_to.add(*params.getlist('assigned_to')) assigned_to_list = json.loads(params.get("assigned_to")) recipients = assigned_to_list send_email_to_assigned_user.delay( recipients, lead_obj.id, ) for comment in lead_obj.leads_comments.all(): comment.account = account_object comment.save() account_object.save() return Response( { "error": False, "message": "Lead Converted to Account Successfully", }, status=status.HTTP_200_OK, ) return Response( {"error": False, "message": "Lead updated Successfully"}, status=status.HTTP_200_OK, ) return Response( {"error": True, "errors": serializer.errors}, status=status.HTTP_400_BAD_REQUEST, )