def save(self, force_insert=False, force_update=False): self.description = compact_text(self.description) self.administrative_notes = compact_text(self.administrative_notes) if self.deal_id == "": # First, client company code deal_id = str(self.client.organisation.company.code) # Then, year in two digits deal_id += date.today().strftime("%y") # Then, next id available for this prefix other_deal_ids = Lead.objects.filter( deal_id__startswith=deal_id).order_by("deal_id") other_deal_ids = [ item for sublist in other_deal_ids.values_list("deal_id") for item in sublist ] if other_deal_ids: try: deal_id += "%02d" % (int(other_deal_ids[-1][-2:]) + 1) except (IndexError, ValueError): # Start at 1 deal_id += "01" else: deal_id += "01" self.deal_id = deal_id super(Lead, self).save(force_insert, force_update)
def lead(request, lead_id=None): """Lead creation or modification""" lead = None updated_fields = [] blacklist_fields = ["creation_date", "tags"] max_length = 50 old_lead_description = "" try: if lead_id: lead = Lead.objects.get(id=lead_id) old_lead_description = lead.description except Lead.DoesNotExist: pass if request.method == "POST": if lead: form = LeadForm(request.POST, instance=lead) else: form = LeadForm(request.POST) if form.is_valid(): changed_fields = form.changed_data for field_name, field in form.fields.items(): if field_name in changed_fields and field_name not in blacklist_fields: value = form.cleaned_data[field_name] if field_name == "description": if compact_text(value) == old_lead_description: # Don't consider description field as changed if content is the same continue if isinstance(value, (list, QuerySet)): value = ", ".join([unicode(i) for i in value]) else: value = force_text(value) value = value if len( value) <= max_length else value[0:max_length - 3] + '...' updated_fields.append( "%s: %s" % (force_text(field.label or field_name), value)) lead = form.save() postSaveLead(request, lead, updated_fields) return HttpResponseRedirect( urlresolvers.reverse("leads.views.detail", args=[lead.id])) else: if lead: form = LeadForm(instance=lead) # A form that edit current lead else: try: consultant = Consultant.objects.get( trigramme__iexact=request.user.username) form = LeadForm(initial={"responsible": consultant}) # An unbound form except Consultant.DoesNotExist: form = LeadForm() # An unbound form return render(request, "leads/lead.html", { "lead": lead, "form": form, "user": request.user })
def lead(request, lead_id=None): """Lead creation or modification""" lead = None updated_fields = [] state_changed = False blacklist_fields = ["creation_date", "tags"] max_length = 50 old_lead_description = "" try: if lead_id: lead = Lead.objects.get(id=lead_id) old_lead_description = lead.description except Lead.DoesNotExist: pass if request.method == "POST": if lead: form = LeadForm(request.POST, instance=lead) created = False else: form = LeadForm(request.POST) created = True if form.is_valid(): changed_fields = form.changed_data for field_name, field in list(form.fields.items()): if field_name in changed_fields and field_name not in blacklist_fields: if field_name == "state": state_changed = True value = form.cleaned_data[field_name] if field_name == "description": if compact_text(value) == old_lead_description: # Don't consider description field as changed if content is the same continue if isinstance(value, (list, QuerySet)): value = ", ".join([str(i) for i in value]) else: value = force_text(value) value = value if len(value)<=max_length else value[0:max_length-3]+'...' updated_fields.append("%s: %s" % (force_text(field.label or field_name), value)) lead = form.save() postSaveLead(request, lead, updated_fields, created=created, state_changed=state_changed) return HttpResponseRedirect(reverse("leads:detail", args=[lead.id])) else: if lead: form = LeadForm(instance=lead) # A form that edit current lead else: try: consultant = Consultant.objects.get(trigramme__iexact=request.user.username) form = LeadForm(initial={"responsible": consultant}) # An unbound form except Consultant.DoesNotExist: form = LeadForm() # An unbound form return render(request, "leads/lead.html", {"lead": lead, "form": form, "user": request.user})
def save(self, force_insert=False, force_update=False): self.description = compact_text(self.description) self.administrative_notes= compact_text(self.administrative_notes) if self.deal_id == "": # First, client company code deal_id = str(self.client.organisation.company.code) # Then, year in two digits deal_id += date.today().strftime("%y") # Then, next id available for this prefix other_deal_ids = Lead.objects.filter(deal_id__startswith=deal_id).order_by("deal_id") other_deal_ids = [item for sublist in other_deal_ids.values_list("deal_id") for item in sublist] if other_deal_ids: try: deal_id += "%02d" % (int(other_deal_ids[-1][-2:]) + 1) except (IndexError, ValueError): # Start at 1 deal_id += "01" else: deal_id += "01" self.deal_id = deal_id super(Lead, self).save(force_insert, force_update)