From 4856531eb7b3f1213efdfc6d35d64cd429bc61b1 Mon Sep 17 00:00:00 2001 From: Ashwin Date: Mon, 4 Feb 2019 14:52:05 +0530 Subject: [PATCH] Implemented tags section in accounts and leads --- .../migrations/0003_auto_20190201_1840.py | 26 ++++++++ accounts/models.py | 12 ++++ accounts/templates/accounts.html | 25 +++++++- accounts/templates/create_account.html | 59 ++++++++++++++++++- accounts/views.py | 26 +++++++- cases/templates/create_cases.html | 2 +- contacts/templates/create_contact.html | 2 +- leads/migrations/0002_lead_tags.py | 19 ++++++ leads/models.py | 2 + leads/templates/create_lead.html | 48 ++++++++++++++- leads/templates/leads.html | 31 ++++++++-- leads/views.py | 33 ++++++++++- opportunity/templates/create_opportunity.html | 2 +- static/css/main.scss | 11 ++++ 14 files changed, 284 insertions(+), 14 deletions(-) create mode 100644 accounts/migrations/0003_auto_20190201_1840.py create mode 100644 leads/migrations/0002_lead_tags.py diff --git a/accounts/migrations/0003_auto_20190201_1840.py b/accounts/migrations/0003_auto_20190201_1840.py new file mode 100644 index 00000000..7add3ac3 --- /dev/null +++ b/accounts/migrations/0003_auto_20190201_1840.py @@ -0,0 +1,26 @@ +# Generated by Django 2.1.5 on 2019-02-01 13:10 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0002_auto_20190128_1237'), + ] + + operations = [ + migrations.CreateModel( + name='Tags', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=20)), + ('slug', models.CharField(blank=True, max_length=20, unique=True)), + ], + ), + migrations.AddField( + model_name='account', + name='tags', + field=models.ManyToManyField(blank=True, to='accounts.Tags'), + ), + ] diff --git a/accounts/models.py b/accounts/models.py index e2e9bd1a..73d27973 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -5,6 +5,17 @@ from django.utils.translation import ugettext_lazy as _ from common.models import User, Address, Team from common.utils import INDCHOICES from phonenumber_field.modelfields import PhoneNumberField +from django.utils.text import slugify + + +class Tags(models.Model): + name = models.CharField(max_length=20) + slug = models.CharField(max_length=20, unique = True, blank=True) + + def save(self, *args, **kwargs): + self.slug = slugify(self.name) + super(Tags, self).save(*args, **kwargs) + class Account(models.Model): @@ -21,6 +32,7 @@ class Account(models.Model): created_by = models.ForeignKey(User, related_name='account_created_by', on_delete=models.CASCADE) created_on = models.DateTimeField(_("Created on"), auto_now_add=True) is_active = models.BooleanField(default=False) + tags = models.ManyToManyField(Tags, blank=True) def __str__(self): return self.name diff --git a/accounts/templates/accounts.html b/accounts/templates/accounts.html index e88e3efa..6aff6076 100644 --- a/accounts/templates/accounts.html +++ b/accounts/templates/accounts.html @@ -50,6 +50,17 @@ +
+
+ + +
+
@@ -76,10 +87,11 @@ {% if accounts_list|length > 0 %} ID - Name + Name City State Industry + Tags Actions {% endif %} @@ -97,6 +109,17 @@ {% if each_account.billing_address.city %}{{ each_account.billing_address.city }} {% else %} Not specified {% endif %} {% if each_account.billing_address.state %}{{ each_account.billing_address.state }} {% else %} Not specified {% endif %} {% if each_account.industry %}{{ each_account.industry }} {% else %} Not specified {% endif %} + + {% with tags=each_account.tags.all %} + {% if tags %} + {% for tag in tags %} + {{ tag.name }} + {% endfor %} + {% else %} + No Tags + {% endif %} + {% endwith %} + {# View #} diff --git a/accounts/templates/create_account.html b/accounts/templates/create_account.html index 8cddd156..8e227ea4 100644 --- a/accounts/templates/create_account.html +++ b/accounts/templates/create_account.html @@ -13,6 +13,12 @@ {% endblock %} {% block content %} + + + + + +
@@ -107,7 +113,14 @@
-
+
+
+ +
+
+
+
+
Copy Address
@@ -131,6 +144,8 @@ {% endblock %} {% block js_block %} + + {% endblock js_block %} diff --git a/accounts/views.py b/accounts/views.py index b9099644..e8f6b83a 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -8,7 +8,7 @@ from django.template.loader import render_to_string from django.views.generic import ( CreateView, UpdateView, DetailView, TemplateView, View, DeleteView) from accounts.forms import AccountForm, AccountCommentForm, AccountAttachmentForm -from accounts.models import Account +from accounts.models import Account, Tags from common.models import User, Address, Team, Comment, Attachments from common.utils import INDCHOICES, COUNTRIES, CURRENCY_CODES, CASE_TYPE, PRIORITY_CHOICE, STATUS_CHOICE from contacts.models import Contact @@ -34,6 +34,8 @@ class AccountsListView(LoginRequiredMixin, TemplateView): city__contains=request_post.get('city'))]) if request_post.get('industry'): queryset = queryset.filter(industry__icontains=request_post.get('industry')) + if request_post.get('tag'): + queryset = queryset.filter(tags__in=request_post.get('tag')) return queryset def get_context_data(self, **kwargs): @@ -41,6 +43,7 @@ class AccountsListView(LoginRequiredMixin, TemplateView): context["accounts_list"] = self.get_queryset() context["industries"] = INDCHOICES context["per_page"] = self.request.POST.get('per_page') + context['tags'] = Tags.objects.all() return context def get(self, request, *args, **kwargs): @@ -103,6 +106,16 @@ class CreateAccountView(LoginRequiredMixin, CreateView): email.send() if self.request.POST.getlist('teams', []): account_object.teams.add(*self.request.POST.getlist('teams')) + 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.lower()) + if tag: + tag = tag[0] + else: + tag = Tags.objects.create(name=t.lower()) + account_object.tags.add(tag) if self.request.POST.get("savenewform"): return redirect("accounts:new_account") else: @@ -242,6 +255,17 @@ class AccountUpdateView(LoginRequiredMixin, UpdateView): account_object.assigned_to.add(*self.request.POST.getlist('assigned_to')) if self.request.POST.getlist('teams', []): account_object.teams.add(*self.request.POST.getlist('teams')) + account_object.tags.clear() + 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.lower()) + if tag: + tag = tag[0] + else: + tag = Tags.objects.create(name=t.lower()) + account_object.tags.add(tag) return redirect("accounts:list") def form_invalid(self, form, billing_form, shipping_form): diff --git a/cases/templates/create_cases.html b/cases/templates/create_cases.html index e1a26159..9467d1fb 100644 --- a/cases/templates/create_cases.html +++ b/cases/templates/create_cases.html @@ -79,7 +79,7 @@
- {{ case_form.account }} + {{case_form.account.errors}}
diff --git a/contacts/templates/create_contact.html b/contacts/templates/create_contact.html index 1a97a5c8..846ee307 100644 --- a/contacts/templates/create_contact.html +++ b/contacts/templates/create_contact.html @@ -41,7 +41,7 @@
- {{ contact_form.account }} + {{ contact_form.account.errors }}
diff --git a/leads/migrations/0002_lead_tags.py b/leads/migrations/0002_lead_tags.py new file mode 100644 index 00000000..df3c7473 --- /dev/null +++ b/leads/migrations/0002_lead_tags.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1.5 on 2019-02-01 13:10 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0003_auto_20190201_1840'), + ('leads', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='lead', + name='tags', + field=models.ManyToManyField(blank=True, to='accounts.Tags'), + ), + ] diff --git a/leads/models.py b/leads/models.py index 326689c0..a941a514 100644 --- a/leads/models.py +++ b/leads/models.py @@ -6,6 +6,7 @@ from accounts.models import Account from common.models import User, Address, Team from common.utils import LEAD_STATUS, LEAD_SOURCE from phonenumber_field.modelfields import PhoneNumberField +from accounts.models import Tags class Lead(models.Model): @@ -34,6 +35,7 @@ class Lead(models.Model): created_on = models.DateTimeField(_("Created on"), auto_now_add=True) is_active = models.BooleanField(default=False) enquery_type = models.CharField(max_length=255, blank=True, null=True) + tags = models.ManyToManyField(Tags, blank=True) class Meta: ordering =['-created_on'] diff --git a/leads/templates/create_lead.html b/leads/templates/create_lead.html index aa9b04a7..069640ab 100644 --- a/leads/templates/create_lead.html +++ b/leads/templates/create_lead.html @@ -13,6 +13,12 @@ {% endblock %} {%block content%} + + + + + +
@@ -132,9 +138,15 @@
- +
+
+ +
+
+
+
-
+
{{ lead_form.description }} @@ -159,6 +171,8 @@ {% endblock %} {% block js_block %} + + {% endblock js_block %} diff --git a/leads/templates/leads.html b/leads/templates/leads.html index ced40fea..8c4d361e 100644 --- a/leads/templates/leads.html +++ b/leads/templates/leads.html @@ -51,6 +51,17 @@
+
+
+ + +
+
@@ -80,10 +91,11 @@ {% if lead_obj|length > 0 %} ID - Name - Status - Assigned User - Created On + Name + Status + Assigned User + Tags + Created On Actions {% endif %} @@ -110,6 +122,17 @@ {% endif %} {% endwith %} + + {% with tags=lead.tags.all %} + {% if tags %} + {% for tag in tags %} + {{ tag.name }}
+ {% endfor %} + {% else %} + No Tags + {% endif %} + {% endwith %} + {{ lead.created_on }} {# #} diff --git a/leads/views.py b/leads/views.py index 946e1810..45c9cfa6 100644 --- a/leads/views.py +++ b/leads/views.py @@ -11,7 +11,7 @@ from django.urls import reverse from django.views.generic import ( CreateView, UpdateView, DetailView, ListView, TemplateView, View) -from accounts.models import Account +from accounts.models import Account, Tags from common.forms import BillingAddressForm from common.models import User, Comment, Team, Attachments from common.utils import LEAD_STATUS, LEAD_SOURCE, COUNTRIES @@ -40,6 +40,8 @@ class LeadListView(LoginRequiredMixin, TemplateView): queryset = queryset.filter(email__icontains=request_post.get('email')) if request_post.get('status'): queryset = queryset.filter(status=request_post.get('status')) + if request_post.get('tag'): + queryset = queryset.filter(tags__in=request_post.get('tag')) return queryset def get_context_data(self, **kwargs): @@ -47,6 +49,7 @@ class LeadListView(LoginRequiredMixin, TemplateView): context["lead_obj"] = self.get_queryset() context["status"] = LEAD_STATUS context["per_page"] = self.request.POST.get('per_page') + context['tags'] = Tags.objects.all() return context def post(self, request, *args, **kwargs): @@ -83,6 +86,16 @@ class CreateLeadView(LoginRequiredMixin, CreateView): lead_obj.address = address_object lead_obj.created_by = self.request.user lead_obj.save() + 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', []): lead_obj.assigned_to.add(*self.request.POST.getlist('assigned_to')) assigned_to_list = self.request.POST.getlist('assigned_to') @@ -105,9 +118,12 @@ class CreateLeadView(LoginRequiredMixin, CreateView): 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') + website=self.request.POST.get('website'), ) account_object.billing_address = address_object + for tag in lead_obj.tags.all(): + account_object.tags.add(tag) + account_object.tags.add(address_object) 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') @@ -244,7 +260,18 @@ class UpdateLeadView(LoginRequiredMixin, UpdateView): lead_obj.address = address_obj lead_obj.save() lead_obj.teams.clear() + 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": @@ -278,6 +305,8 @@ class UpdateLeadView(LoginRequiredMixin, UpdateView): website=self.request.POST.get('website') ) account_object.billing_address = address_obj + 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') diff --git a/opportunity/templates/create_opportunity.html b/opportunity/templates/create_opportunity.html index 23db1994..e5729336 100644 --- a/opportunity/templates/create_opportunity.html +++ b/opportunity/templates/create_opportunity.html @@ -32,7 +32,7 @@
- {{ opportunity_form.account }} + {{opportunity_form.account.errors}}
diff --git a/static/css/main.scss b/static/css/main.scss index e38684c1..8a73e66d 100644 --- a/static/css/main.scss +++ b/static/css/main.scss @@ -806,3 +806,14 @@ padding:0px !important; padding:3px 10px; border-radius: 3px; } + +#list_tag{ + background: #e5e5e5; + color: #000; + font-weight: 500; + padding: 1px 5px; + margin-right: 5px; + font-size: 0.9rem; + display: inline-block; + margin-bottom: 5px; +} \ No newline at end of file