Commit 2b3b9646 authored by okhin's avatar okhin 🚴

Close #10 and fix feedback tests

parent 1bf9ca37
Pipeline #878 passed with stages
in 50 seconds
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-13 11:32
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('contact', '0008_auto_20170119_1136'),
('campaign', '0007_auto_20170119_1217'),
]
operations = [
migrations.AlterUniqueTogether(
name='campaigncontact',
unique_together=set([('campaign', 'contact')]),
),
]
......@@ -37,6 +37,9 @@ class CampaignContact(models.Model):
contact = models.ForeignKey(Contact)
weight = models.IntegerField(default=0)
class Meta:
unique_together = (("campaign", "contact"),)
def __str__(self):
args = {'contact': self.contact, 'title': self.campaign.title}
return _('Contact %(contact)s on %(title)s') % args
......
......@@ -17,6 +17,9 @@ class FeedbackViewSet(viewsets.ViewSet):
@csrf_exempt
def create(self, request, campaign_pk=None):
serializer = FeedbackSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
feedback = serializer.create(serializer.validated_data)
return Response(feedback.id)
try:
serializer.is_valid(raise_exception=True)
feedback = serializer.create(serializer.validated_data)
return Response(feedback.id)
except Exception as e:
raise e
......@@ -116,7 +116,7 @@ class Importer(models.Model):
contact.groups = groups
contact.save()
# We want to link the contact to our current campaign
CampaignContact(campaign=self.campaign, contact=contact).save()
campaigncontact, created = CampaignContact.objects.update_or_create(campaign=self.campaign, contact=contact)
inserted += 1
# Updating the last imported item
......
......@@ -178,3 +178,38 @@ class ImporterTest(TestCase):
self.assertEqual(importer.last_imported, 2)
self.assertEqual(len(Group.objects.all()), 4)
self.assertEqual(len(Contact.objects.all()), 2)
def test_uniqueness_insert_10(self):
'''
This is related to isse #10
'''
# First a classic import.
json_file = None
with open('picampaign/importer/data_tests.json', u'rb') as f:
json_file = SimpleUploadedFile('data_tests.json', f.read())
importer = Importer.objects.create(
name='test importer',
category='repr',
campaign = self.campaign,
file=json_file,
url=''
)
self.assertEqual(importer.kind(), 'file')
importer.handle()
importer.refresh_from_db()
# The file import 3 of the 10 meps
self.assertEqual(importer.last_count, 10)
self.assertEqual(importer.last_imported, 3)
self.assertEqual(len(Group.objects.all()), 13)
self.assertEqual(len(Contact.objects.all()), 3)
self.assertEqual(len(CampaignContact.objects.all()), 3)
# If we rerun the same import we must have
# different values
importer.handle()
importer.refresh_from_db()
self.assertEqual(importer.last_count, 10)
self.assertEqual(importer.last_imported, 3) # We still have 3 contact imported
self.assertEqual(len(Group.objects.all()), 13) # Still the same groups
self.assertEqual(len(Contact.objects.all()), 3) # No more contacts though
self.assertEqual(len(CampaignContact.objects.all()), 3) # Or ContactCampaign
......@@ -29,9 +29,3 @@ urlpatterns = [
url(r'^', include(campaign_router.urls)),
url(r'^i18n/', include('django.conf.urls.i18n'))
]
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment