def opp_create(request): users = User.objects.filter(is_active=True).order_by('email') accounts = Account.objects.all() contacts = Contact.objects.all() form = OpportunityForm(assigned_to=users, account=accounts, contacts=contacts) assignedto_list = request.POST.getlist("assigned_to") teams_list = request.POST.getlist("teams") contacts_list = request.POST.getlist("contacts") teams = Team.objects.all() if request.method == 'POST': form = OpportunityForm(request.POST, assigned_to=users, account=accounts, contacts=contacts) if form.is_valid(): opportunity_obj = form.save(commit=False) opportunity_obj.created_by = request.user if request.POST.get('stage') in ['CLOSED WON', 'CLOSED LOST']: opportunity_obj.closed_by = request.user opportunity_obj.save() opportunity_obj.assigned_to.add(*assignedto_list) opportunity_obj.teams.add(*teams_list) opportunity_obj.contacts.add(*contacts_list) if request.is_ajax(): return JsonResponse({'error': False}) if request.POST.get("savenewform"): return HttpResponseRedirect(reverse("opportunities:save")) else: return HttpResponseRedirect(reverse('opportunities:list')) else: if request.is_ajax(): return JsonResponse({ 'error': True, 'opportunity_errors': form.errors }) return render( request, 'create_opportunity.html', { 'opportunity_form': form, 'accounts': accounts, 'users': users, 'teams': teams, 'stages': STAGES, 'sources': SOURCES, 'currencies': CURRENCY_CODES, 'assignedto_list': [int(user_id) for user_id in assignedto_list], 'teams_list': teams_list, 'contacts_list': contacts_list }) else: return render( request, 'create_opportunity.html', { 'opportunity_form': form, 'accounts': accounts, 'users': users, 'teams': teams, 'stages': STAGES, 'sources': SOURCES, 'currencies': CURRENCY_CODES, 'assignedto_list': assignedto_list, 'teams_list': teams_list, 'contacts_list': contacts_list })
def leads_convert(request, pk): account_form = AccountForm() billing_form = BillingAddressForm() shipping_form = ShippingAddressForm(prefix='ship') lead_objects = Lead.objects.all() lead_obj = Lead.objects.get(id=pk) accounts = Account.objects.all() contact_form = ContactForm() opportunity_form = OpportunityForm() teams = Team.objects.all() if request.method == "POST": if request.POST.get('accountname') == "on": account_form = AccountForm(request.POST) billing_form = BillingAddressForm(request.POST) shipping_form = ShippingAddressForm(request.POST, prefix='ship') if account_form.is_valid() and billing_form.is_valid( ) and shipping_form.is_valid(): billing_object = billing_form.save() shipping_object = shipping_form.save() account_object = account_form.save(commit=False) account_object.billing_address = billing_object account_object.shipping_address = shipping_object account_object.created_by = request.user account_object.save() lead_obj.delete() return HttpResponseRedirect(reverse('leads:list')) else: street1 = request.POST.get('street') city1 = request.POST.get('city') state1 = request.POST.get('state') postcode1 = request.POST.get('postcode') country1 = request.POST.get('country') shipdata = { 'street1': street1, 'city1': city1, 'state1': state1, 'postcode1': postcode1, 'country1': country1 } return render( request, 'checkbox.html', { 'account_form': account_form, 'form1': billing_form, 'form2': shipping_form, 'form5': COUNTRIES, 'stages': STAGES, 'acc_error': account_form.errors, 'shipdata': shipdata, 'sources': LEAD_SOURCE, 'industries': INDCHOICES, 'teams': teams, 'task': lead_objects, 'post': lead_obj, 'accounts': accounts, 'counties': COUNTRIES }) if request.POST.get('contactname') == "on": contact_form = ContactForm(request.POST) address_form = BillingAddressForm(request.POST) if contact_form.is_valid() and address_form.is_valid(): address_obj = address_form.save() contact_obj = contact_form.save(commit=False) contact_obj.address = address_obj contact_obj.created_by = request.user contact_obj.save() return HttpResponseRedirect(reverse('contacts:list')) else: return render( request, 'checkbox.html', { 'post': lead_obj, 'accounts': accounts, 'teams': teams, 'contact_form': contact_form, 'address_form': address_form }) if request.POST.get('opportunityname') == "on": opportunity_form = OpportunityForm(request.POST) if opportunity_form.is_valid(): opportunity_form.save() return HttpResponseRedirect(reverse('opportunities:list')) else: return render( request, 'checkbox.html', { 'post': lead_obj, 'accounts': accounts, 'sources': LEAD_SOURCE, 'teams': teams, 'stages': STAGES, 'opportunity_form': opportunity_form }) else: return render( request, 'checkbox.html', { 'form': account_form, 'form1': billing_form, 'form2': shipping_form, 'form5': COUNTRIES, 'industries': INDCHOICES, 'teams': teams, 'task': lead_objects, 'counties': COUNTRIES, 'post': lead_obj, 'accounts': accounts })
def update_opportunity(request, pk): opportunity_object = Opportunity.objects.filter(pk=pk).first() accounts = Account.objects.filter(status="open") contacts = Contact.objects.all() if request.user.role != "ADMIN" and not request.user.is_superuser: accounts = Account.objects.filter( created_by=request.user) contacts = Contact.objects.filter( Q(assigned_to__in=[request.user]) | Q(created_by=request.user)) 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') kwargs_data = { "assigned_to": users, "account": accounts, "contacts": contacts} form = OpportunityForm(instance=opportunity_object, **kwargs_data) if request.POST: form = form = OpportunityForm( request.POST, request.FILES, instance=opportunity_object, **kwargs_data) if form.is_valid(): assigned_to_ids = opportunity_object.assigned_to.all().values_list( 'id', flat=True) opportunity_obj = form.save(commit=False) if request.POST.get('stage') in ['CLOSED WON', 'CLOSED LOST']: opportunity_obj.closed_by = request.user previous_assigned_to_users = list(opportunity_obj.assigned_to.all().values_list('id', flat=True)) opportunity_obj.save() opportunity_obj.contacts.clear() all_members_list = [] if request.POST.getlist('assigned_to', []): 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, opportunity_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 opportunity.' # message = render_to_string( # 'assigned_to/opportunity_assigned.html', { # 'user': user, # 'domain': current_site.domain, # 'protocol': request.scheme, # 'opportunity': opportunity_obj # }) # email = EmailMessage( # mail_subject, message, to=[user.email]) # email.content_subtype = "html" # email.send() opportunity_obj.assigned_to.clear() opportunity_obj.assigned_to.add( *request.POST.getlist('assigned_to')) else: opportunity_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 = opportunity_obj.assigned_to.all().values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: opportunity_obj.assigned_to.add(user_id) if request.POST.getlist('teams', []): opportunity_obj.teams.clear() opportunity_obj.teams.add(*request.POST.getlist('teams')) else: opportunity_obj.teams.clear() current_site = get_current_site(request) assigned_to_list = list(opportunity_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, opportunity_obj.id, domain=current_site.domain, protocol=request.scheme) if request.POST.getlist('contacts', []): opportunity_obj.contacts.add( *request.POST.getlist('contacts')) opportunity_obj.tags.clear() 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.lower()) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t.lower()) opportunity_obj.tags.add(tag) if request.FILES.get('oppurtunity_attachment'): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( 'oppurtunity_attachment').name attachment.opportunity = opportunity_obj attachment.attachment = request.FILES.get( 'oppurtunity_attachment') attachment.save() success_url = reverse('opportunities:list') if request.POST.get('from_account'): from_account = request.POST.get('from_account') success_url = reverse("accounts:view_account", kwargs={ 'pk': from_account}) return JsonResponse({'error': False, 'success_url': success_url}) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context['TITLE'] = settings.TITLE context["opportunity_obj"] = opportunity_object user_assgn_list = [ assigned_to.id for assigned_to in context["opportunity_obj"].assigned_to.all()] if request.user == context['opportunity_obj'].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 context["opportunity_form"] = form context["accounts"] = accounts if request.GET.get('view_account'): context['account'] = get_object_or_404( Account, id=request.GET.get('view_account')) context["contacts"] = contacts context["users"] = kwargs_data['assigned_to'] context["currencies"] = CURRENCY_CODES context["stages"] = STAGES context["sources"] = SOURCES context["teams"] = Teams.objects.all() context["assignedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i] context["contacts_list"] = [ int(i) for i in request.POST.getlist('contacts', []) if i] return render(request, "create_opportunity.html", context)
def create_opportunity(request): accounts = Account.objects.filter(status="open", company=request.company) contacts = Contact.objects.filter(company=request.company) if request.user.role != "ADMIN" and not request.user.is_superuser: accounts = Account.objects.filter(created_by=request.user, company=request.company) contacts = Contact.objects.filter( Q(assigned_to__in=[request.user]) | Q(created_by=request.user)).filter(company=request.company) users = [] if request.user.role == "ADMIN" or request.user.is_superuser: users = User.objects.filter(is_active=True, company=request.company).order_by("email") elif request.user.google.all(): users = [] else: users = User.objects.filter(role="ADMIN", company=request.company).order_by("email") kwargs_data = { "assigned_to": users, "account": accounts, "contacts": contacts, "request_obj": request, } if request.POST: form = OpportunityForm(request.POST, request.FILES, **kwargs_data) if form.is_valid(): opportunity_obj = form.save(commit=False) opportunity_obj.created_by = request.user opportunity_obj.company = request.company if request.POST.get("stage") in ["CLOSED WON", "CLOSED LOST"]: opportunity_obj.closed_by = request.user opportunity_obj.save() if request.POST.getlist("assigned_to", []): opportunity_obj.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, opportunity_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 opportunity.' # message = render_to_string( # 'assigned_to/opportunity_assigned.html', { # 'user': user, # 'domain': current_site.domain, # 'protocol': request.scheme, # 'opportunity': opportunity_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 = opportunity_obj.assigned_to.all( ).values_list("id", flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: opportunity_obj.assigned_to.add(user_id) if request.POST.getlist("teams", []): opportunity_obj.teams.add(*request.POST.getlist("teams")) current_site = get_current_site(request) recipients = list(opportunity_obj.assigned_to.all().values_list( "id", flat=True)) send_email_to_assigned_user.delay( recipients, opportunity_obj.id, domain=current_site.domain, protocol=request.scheme, ) if request.POST.getlist("contacts", []): opportunity_obj.contacts.add(*request.POST.getlist("contacts")) 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.lower()) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t.lower()) opportunity_obj.tags.add(tag) if request.FILES.get("oppurtunity_attachment"): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( "oppurtunity_attachment").name attachment.opportunity = opportunity_obj attachment.attachment = request.FILES.get( "oppurtunity_attachment") attachment.save() success_url = reverse("opportunities:list") if request.POST.get("savenewform"): success_url = reverse("opportunities:save") if request.POST.get("from_account"): from_account = request.POST.get("from_account") success_url = reverse("accounts:view_account", kwargs={"pk": from_account}) # print(success_url) return JsonResponse({"error": False, "success_url": success_url}) return JsonResponse({"error": True, "errors": form.errors}) context = {} context["opportunity_form"] = OpportunityForm(**kwargs_data) context["accounts"] = accounts if request.GET.get("view_account"): context["account"] = get_object_or_404( Account, id=request.GET.get("view_account")) context["contacts"] = contacts.filter(company=request.company) context["users"] = kwargs_data["assigned_to"] context["currencies"] = CURRENCY_CODES context["stages"] = STAGES context["sources"] = SOURCES context["teams"] = Teams.objects.filter(company=request.company) context["assignedto_list"] = [ int(i) for i in request.POST.getlist("assigned_to", []) if i ] context["contacts_list"] = [ int(i) for i in request.POST.getlist("contacts", []) if i ] return render(request, "create_opportunity.html", context)
def create_opportunity(request): template_name = "create_opportunity.html" users = User.objects.filter(is_active=True).order_by('email') accounts = Account.objects.filter(status="open") contacts = Contact.objects.all() if request.user.role != "ADMIN" and not request.user.is_superuser: accounts = Account.objects.filter( created_by=request.user) contacts = Contact.objects.filter( Q(assigned_to__in=[request.user]) | Q(created_by=request.user)) kwargs_data = {"assigned_to": users, "account": accounts, "contacts": contacts} form = OpportunityForm(**kwargs_data) if request.POST: form = OpportunityForm(request.POST, request.FILES, **kwargs_data) if form.is_valid(): opportunity_obj = form.save(commit=False) opportunity_obj.created_by = request.user if request.POST.get('stage') in ['CLOSED WON', 'CLOSED LOST']: opportunity_obj.closed_by = request.user opportunity_obj.save() if request.POST.getlist('assigned_to', []): opportunity_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 opportunity.' message = render_to_string('assigned_to/opportunity_assigned.html', { 'user': user, 'domain': current_site.domain, 'protocol': request.scheme, 'opportunity': opportunity_obj }) email = EmailMessage(mail_subject, message, to=[user.email]) email.content_subtype = "html" email.send() if request.POST.getlist('contacts', []): opportunity_obj.contacts.add( *request.POST.getlist('contacts')) 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.lower()) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t.lower()) opportunity_obj.tags.add(tag) if request.FILES.get('oppurtunity_attachment'): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( 'oppurtunity_attachment').name attachment.opportunity = opportunity_obj attachment.attachment = request.FILES.get( 'oppurtunity_attachment') attachment.save() success_url = reverse('opportunities:list') if request.POST.get("savenewform"): success_url = reverse("opportunities:save") if request.POST.get('from_account'): from_account = request.POST.get('from_account') success_url = reverse("accounts:view_account", pk=from_account) return JsonResponse({'error': False, 'success_url': success_url}) else: return JsonResponse({'error': True, 'errors': form.errors}) else: context = {} context["opportunity_form"] = form context["accounts"] = accounts if request.GET.get('view_account'): context['account'] = get_object_or_404( Account, id=request.GET.get('view_account')) context["contacts"] = contacts context["users"] = users context["currencies"] = CURRENCY_CODES context["stages"] = STAGES context["sources"] = SOURCES context["assignedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i] context["contacts_list"] = [ int(i) for i in request.POST.getlist('contacts', []) if i] return render(request, template_name, context)
def create_opportunity(request): accounts = Account.objects.filter(status="open") contacts = Contact.objects.all() if request.user.role != "ADMIN" and not request.user.is_superuser: accounts = Account.objects.filter( Q(created_by=request.user) | Q(assigned_to=request.user)) contacts = Contact.objects.filter( Q(assigned_to__in=[request.user]) | Q(created_by=request.user)) 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') kwargs_data = { "assigned_to": users, "account": accounts, "contacts": contacts } cur = Currency.objects.values_list('currency_settings', flat=True).first() print(cur, "************") if request.POST: s = request.POST.get("closed_on") print('ssssssssssss', s) p = datetime.strptime(s, "%d-%m-%Y").strftime("%Y-%m-%d") print('pppppppppp', p) form = OpportunityForm(request.POST, request.FILES, **kwargs_data) if form.is_valid(): opportunity_obj = form.save(commit=False) opportunity_obj.closed_on = p opportunity_obj.created_by = request.user if request.POST.get('stage') in ['CLOSED WON', 'CLOSED LOST']: opportunity_obj.closed_by = request.user opportunity_obj.save() if request.POST.getlist('assigned_to', []): opportunity_obj.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, opportunity_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 opportunity.' # message = render_to_string( # 'assigned_to/opportunity_assigned.html', { # 'user': user, # 'domain': current_site.domain, # 'protocol': request.scheme, # 'opportunity': opportunity_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 = opportunity_obj.assigned_to.all( ).values_list('id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: opportunity_obj.assigned_to.add(user_id) if request.POST.getlist('teams', []): opportunity_obj.teams.add(*request.POST.getlist('teams')) current_site = get_current_site(request) recipients = list(opportunity_obj.assigned_to.all().values_list( 'id', flat=True)) send_email_to_assigned_user.delay(recipients, opportunity_obj.id, domain=current_site.domain, protocol=request.scheme) if request.POST.getlist('contacts', []): opportunity_obj.contacts.add(*request.POST.getlist('contacts')) 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.lower()) if tag: tag = tag[0] else: tag = Tags.objects.create(name=t.lower()) opportunity_obj.tags.add(tag) if request.FILES.get('oppurtunity_attachment'): attachment = Attachments() attachment.created_by = request.user attachment.file_name = request.FILES.get( 'oppurtunity_attachment').name attachment.opportunity = opportunity_obj attachment.attachment = request.FILES.get( 'oppurtunity_attachment') attachment.save() success_url = reverse('opportunities:list') if request.POST.get("savenewform"): success_url = reverse("opportunities:save") if request.POST.get('from_account'): from_account = request.POST.get('from_account') success_url = reverse("accounts:view_account", kwargs={'pk': from_account}) # print(success_url) return JsonResponse({'error': False, 'success_url': success_url}) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context["opportunity_form"] = OpportunityForm(**kwargs_data) context["accounts"] = accounts if request.GET.get('view_account'): context['account'] = get_object_or_404( Account, id=request.GET.get('view_account')) context["contacts"] = contacts context["users"] = kwargs_data['assigned_to'] context["currencies"] = CURRENCY_CODES context["cur"] = cur context["stages"] = STAGES context["sources"] = SOURCES context["teams"] = Teams.objects.all() context["assignedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i ] context["contacts_list"] = [ int(i) for i in request.POST.getlist('contacts', []) if i ] return render(request, "create_opportunity.html", context)