def add_tag(request): """Add a tag to a lead. Create the tag if needed""" answer = {} answer["tag_created"] = True # indicate if a tag was reused or created answer["tag_url"] = "" # url on tag answer["tag_name"] = "" # tag name if request.POST["tag"]: tagName = capitalize(request.POST["tag"]) lead = Lead.objects.get(id=int(request.POST["lead_id"])) if tagName in lead.tags.all().values_list("name", flat=True): answer["tag_created"] = False lead.tags.add(tagName) if lead.state not in ("WON", "LOST", "FORGIVEN"): compute_leads_state( relearn=False, leads_id=[ lead.id, ] ) # Update (in background) lead proba state as tag are used in computation compute_lead_similarity() # update lead similarity model in background tag = Tag.objects.filter( name=tagName )[0] # We should have only one, but in case of bad data, just take the first one answer["tag_url"] = urlresolvers.reverse("leads.views.tag", args=[ tag.id, ]) answer["tag_remove_url"] = urlresolvers.reverse( "leads.views.remove_tag", args=[tag.id, lead.id]) answer["tag_name"] = tag.name answer["id"] = tag.id return HttpResponse(json.dumps(answer), content_type="application/json")
def remove_tag(request, tag_id, lead_id): """Remove a tag to a lead""" answer = {} answer["error"] = False answer["id"] = tag_id try: tag = Tag.objects.get(id=tag_id) lead = Lead.objects.get(id=lead_id) lead.tags.remove(tag) if lead.state not in ("WON", "LOST", "FORGIVEN"): compute_leads_state(relearn=False, leads_id=[lead.id, ]) # Update (in background) lead proba state as tag are used in computation compute_lead_similarity() # update lead similarity model in background except (Tag.DoesNotExist, Lead.DoesNotExist): answer["error"] = True return HttpResponse(json.dumps(answer), content_type="application/json")
def remove_tag(request, tag_id, lead_id): """Remove a tag to a lead""" answer = {} answer["error"] = False answer["id"] = tag_id try: tag = Tag.objects.get(id=tag_id) lead = Lead.objects.get(id=lead_id) lead.tags.remove(tag) if lead.state not in ("WON", "LOST", "FORGIVEN"): compute_leads_state(relearn=False, leads_id=[lead.id, ]) # Update (in background) lead proba state as tag are used in computation compute_lead_similarity() # update lead similarity model in background if settings.NEXTCLOUD_TAG_IS_ENABLED: remove_lead_tag(lead.id, tag.id) # Remove the lead tag from the lead files except (Tag.DoesNotExist, Lead.DoesNotExist): answer["error"] = True return HttpResponse(json.dumps(answer), content_type="application/json")
def add_tag(request): """Add a tag to a lead. Create the tag if needed""" answer = {} answer["tag_created"] = True # indicate if a tag was reused or created answer["tag_url"] = "" # url on tag answer["tag_name"] = "" # tag name if request.POST["tag"]: tagName = capitalize(request.POST["tag"]) lead = Lead.objects.get(id=int(request.POST["lead_id"])) if tagName in lead.tags.all().values_list("name", flat=True): answer["tag_created"] = False lead.tags.add(tagName) if lead.state not in ("WON", "LOST", "FORGIVEN"): compute_leads_state(relearn=False, leads_id=[lead.id,]) # Update (in background) lead proba state as tag are used in computation compute_lead_similarity() # update lead similarity model in background if settings.NEXTCLOUD_TAG_IS_ENABLED: tag_leads_files([lead.id]) # Update lead tags from lead files tag = Tag.objects.filter(name=tagName)[0] # We should have only one, but in case of bad data, just take the first one answer["tag_url"] = reverse("leads:tag", args=[tag.id, ]) answer["tag_remove_url"] = reverse("leads:remove_tag", args=[tag.id, lead.id]) answer["tag_name"] = tag.name answer["id"] = tag.id return HttpResponse(json.dumps(answer), content_type="application/json")