Skip to content

Commit

Permalink
Add "add tag" action for multiple contacts.
Browse files Browse the repository at this point in the history
Pushes towards #44 and #4.
  • Loading branch information
therealphildini committed Apr 30, 2016
1 parent 9c28af0 commit 13dd9b7
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
12 changes: 11 additions & 1 deletion contacts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,15 @@ def __init__(self, *args, **kwargs):
super(MultiContactForm, self).__init__(*args, **kwargs)
for contact_id in contact_ids:
self.fields['contact_%s' % (contact_id,)] = forms.BooleanField(
required=False
required=False,
)

class MultiTagForm(forms.Form):

def __init__(self, *args, **kwargs):
tag_ids = kwargs.pop('tag_ids')
super(MultiTagForm, self).__init__(*args, **kwargs)
for tag_id in tag_ids:
self.fields['tag_%s' % (tag_id,)] = forms.BooleanField(
required=False,
)
5 changes: 3 additions & 2 deletions contacts/templates/contact_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ <h3>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">More <span class="caret"></span></button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Selected... <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><input class="btn btn-default" name="emails" id="emails-submit" type="submit" value="Export emails" /></li>
<li><input class="btn btn-default" name="addresses" id="addresses-submit" type="submit" value="Export addresses" /></li>
<li><input class="btn btn-default" name="merge" id="merge-submit" type="submit" value="Merge selected" /></li>
<li><input class="btn btn-default" name="addtag" id="addtag-submit" type="submit" value="Add tag" /></li>
<li><input class="btn btn-default" name="merge" id="merge-submit" type="submit" value="Merge" /></li>
</ul>
</div>
</h3>
Expand Down
31 changes: 31 additions & 0 deletions contacts/templates/contacts_add_tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends "base.html" %}

{% block content %}
<form action="{{ action }}" method="POST">
{% csrf_token %}
<div class="row">
<div class="col-md-6">
<h2>For contacts...</h2>
<ul>
{% for contact in contacts %}
<li>{{ contact.name }}</li>
{% endfor %}
</ul>
</div>
<div class="col-md-6">
<h2>Add tags...</h2>
<ul>
{% for tag in tags %}
<li>
<label for="id_tag_{{ tag.id }}">
<input id="id_tag_{{ tag.id }}" name="tag_{{ tag.id }}" class="input" type="checkbox">
<span class="label label-default" style="background-color:{{ tag.corrected_color}};">{{ tag }}</span>
</label>
</li>
{% endfor %}
</ul>
</div>
<input id="add_tag" type="submit" value="Add tags" />
</div>
</form>
{% endblock %}
5 changes: 5 additions & 0 deletions contacts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
contact_views.MergeContactsView.as_view(),
name="contacts_merge",
),
url(
r'addtag/',
contact_views.AddTagView.as_view(),
name="contacts_add_tag",
),
]

tag_urls = [
Expand Down
3 changes: 3 additions & 0 deletions contacts/views/contact_list_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def form_valid(self, form, *args, **kwargs):
if form.cleaned_data[contact]:
contact_ids.append(contact.split('_')[1])
self.request.session['selected_contacts'] = json.dumps(contact_ids)
import pdb; pdb.set_trace()
if not contact_ids:
messages.info(self.request, "No contacts selected.")
return HttpResponseRedirect(reverse('contacts-list'))
Expand All @@ -91,6 +92,8 @@ def form_valid(self, form, *args, **kwargs):
return HttpResponseRedirect(reverse('contact_addresses'))
if self.request.POST.get('merge'):
return HttpResponseRedirect(reverse('contacts_merge'))
if self.request.POST.get('addtag'):
return HttpResponseRedirect(reverse('contacts_add_tag'))
return HttpResponseRedirect(self.get_success_url())

def get_queryset(self):
Expand Down
58 changes: 58 additions & 0 deletions contacts/views/contact_views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import csv
import json
from braces.views import LoginRequiredMixin
from django.conf import settings
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import (
get_object_or_404,
Expand Down Expand Up @@ -361,3 +363,59 @@ def post(self, request, *args, **kwargs):
))
return redirect(reverse('contacts-list'))


class AddTagView(LoginRequiredMixin, FormView):

template_name = "contacts_add_tag.html"
form_class = forms.MultiTagForm

def get_success_url(self):
return reverse('contacts-list')

def get_tags(self):
if not hasattr(self, '_tags'):
self._tags = Tag.objects.get_tags_for_user(self.request.user)
return self._tags

def get_context_data(self, *args, **kwargs):
context = super(AddTagView, self).get_context_data(*args, **kwargs)
context['contacts'] = common.get_selected_contacts_from_request(
self.request,
)
context['tags'] = self.get_tags()
return context

def get_form_kwargs(self):
form_kwargs = super(AddTagView, self).get_form_kwargs()
form_kwargs['tag_ids'] = [tag.id for tag in self.get_tags()]
return form_kwargs

def form_valid(self, form):
contact_ids = common.get_selected_contacts_from_request(self.request)
tag_ids = []
for tag in form.cleaned_data:
if form.cleaned_data[tag]:
tag_ids.append(tag.split('_')[1])
contacts = Contact.objects.get_contacts_for_user(self.request.user).filter(id__in=contact_ids)
for contact in contacts:
# Django's many-to-many add can take just ids, which saves us having
# to pull the Tag objects from the DB.
try:
contact.tags.add(*tag_ids)
contact.save()
except IntegrityError:
# If that contact already had the tag, step through tags to add
# the missing ones
for tag_id in tag_ids:
try:
contact.tags.add(tag_id)
except IntegrityError:
pass
LogEntry.objects.create(
contact=contact,
kind='edit',
notes='Added tags',
logged_by=self.request.user,
)
self.request.session['selected_contacts'] = None
return super(AddTagView, self).form_valid(form)

0 comments on commit 13dd9b7

Please sign in to comment.