# Path: instance/models.py
# class WriteItInstance(models.Model):
#     """WriteItInstance: Entity that groups messages and people
#     for usability purposes. E.g. 'Candidates running for president'"""
#     name = models.CharField(max_length=255)
#     description = models.CharField(max_length=512, blank=True)
#     slug = AutoSlugField(populate_from='name', unique=True)
#     persons = models.ManyToManyField(Person,
#         related_name='writeit_instances',
#         through='Membership')
#     owner = models.ForeignKey(User, related_name="writeitinstances")
# 
#     def add_person(self, person):
#         Membership.objects.get_or_create(writeitinstance=self, person=person)
# 
#     @property
#     def persons_with_contacts(self):
#         return self.persons.filter(contact__writeitinstance=self, contact__isnull=False).distinct()
# 
#     def relate_with_persons_from_popit_api_instance(self, popit_api_instance):
#         try:
#             popit_api_instance.fetch_all_from_api(writeitinstance=self)
#         except ConnectionError, e:
#             self.do_something_with_a_vanished_popit_api_instance(popit_api_instance)
#             e.message = _('We could not connect with the URL')
#             return (False, e)
#         except Exception, e:
#             self.do_something_with_a_vanished_popit_api_instance(popit_api_instance)
#             return (False, e)
#         persons = Person.objects.filter(api_instance=popit_api_instance)
#         for person in persons:
#             # There could be several memberships created.
#             memberships = Membership.objects.filter(writeitinstance=self, person=person)
#             if memberships.count() == 0:
#                 Membership.objects.create(writeitinstance=self, person=person)
#             if memberships.count() > 1:
#                 membership = memberships[0]
#                 memberships.exclude(id=membership.id).delete()
# 
#         return (True, None)
# 
#     def do_something_with_a_vanished_popit_api_instance(self, popit_api_instance):
#         pass
# 
#     def _load_persons_from_a_popit_api(self, popit_api_instance):
#         success_relating_people, error = self.relate_with_persons_from_popit_api_instance(popit_api_instance)
#         record = WriteitInstancePopitInstanceRecord.objects.get(
#             writeitinstance=self,
#             popitapiinstance=popit_api_instance
#             )
#         if success_relating_people:
#             record.set_status('success')
#         else:
#             if isinstance(error, ConnectionError):
#                 record.set_status('error', _('We could not connect with the URL'))
#             else:
#                 record.set_status('error', error.message)
#         return (success_relating_people, error)
# 
#     def load_persons_from_a_popit_api(self, popit_url):
#         '''This is an async wrapper for getting people from the api'''
#         popit_api_instance, created = PopitApiInstance.objects.get_or_create(url=popit_url)
#         record, created = WriteitInstancePopitInstanceRecord.objects.get_or_create(
#             writeitinstance=self,
#             popitapiinstance=popit_api_instance
#             )
#         if not created:
#             record.updated = datetime.datetime.today()
#             record.save()
#         record.set_status('inprogress')
#         from nuntium.tasks import pull_from_popit
#         return pull_from_popit.delay(self, popit_api_instance)
# 
#     def get_absolute_url(self):
#         return reverse('instance_detail', subdomain=self.slug)
# 
#     @property
#     def pulling_from_popit_status(self):
#         records = WriteitInstancePopitInstanceRecord.objects.filter(writeitinstance=self)
#         result = {'nothing': 0, 'inprogress': 0, 'success': 0, 'error': 0}
#         for record in records:
#             result[record.status] += 1
#         return result
# 
#     @property
#     def can_create_messages(self):
#         if self.config.allow_messages_using_form and \
#                 self.contacts.exists():
#             return True
# 
#         return False
# 
#     def __unicode__(self):
#         return self.name


from global_test_case import GlobalTestCase as TestCase
from subdomains.utils import reverse
from django.utils.translation import activate
from instance.models import WriteItInstance
from django.contrib.auth.models import User
from django.test.client import Client
        activate('en')
        instance1 = WriteItInstance.objects.get(id=1)
        url = reverse("home")
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)
        self.assertTrue(response.context['writeitinstances'])
        self.assertEquals(response.context['writeitinstances'].count(), 2)
        self.assertEquals(response.context['writeitinstances'][0], instance1)

    def test_list_instances_view(self):
        '''All instances are displayed in home'''
        for instance in WriteItInstance.objects.all():
            instance.config.testing_mode = False
            instance.config.save()
        url = reverse('instance_list')
        self.assertTrue(url)
        c = Client()
        response = c.get(url)
        self.assertEquals(response.status_code, 200)
        self.assertIn('object_list', response.context)
        self.assertIsInstance(response.context['object_list'][0], WriteItInstance)
        self.assertEquals(len(response.context['object_list']), WriteItInstance.objects.count())

        self.assertTemplateUsed(response, 'nuntium/template_list.html')

    def test_testing_mode_instances_not_displayed(self):
        '''Test Mode instances are not displayed if a user is not logged in'''
        fiera = User.objects.create_user(username='fiera',
            password="feroz",
            email="fiera@ciudadanointeligente.org")

# Path: instance/models.py
# class WriteItInstance(models.Model):
#     """WriteItInstance: Entity that groups messages and people
#     for usability purposes. E.g. 'Candidates running for president'"""
#     name = models.CharField(max_length=255)
#     description = models.CharField(max_length=512, blank=True)
#     slug = AutoSlugField(populate_from='name', unique=True)
#     persons = models.ManyToManyField(Person,
#         related_name='writeit_instances',
#         through='Membership')
#     owner = models.ForeignKey(User, related_name="writeitinstances")
# 
#     def add_person(self, person):
#         Membership.objects.get_or_create(writeitinstance=self, person=person)
# 
#     @property
#     def persons_with_contacts(self):
#         return self.persons.filter(contact__writeitinstance=self, contact__isnull=False).distinct()
# 
#     def relate_with_persons_from_popit_api_instance(self, popit_api_instance):
#         try:
#             popit_api_instance.fetch_all_from_api(writeitinstance=self)
#         except ConnectionError, e:
#             self.do_something_with_a_vanished_popit_api_instance(popit_api_instance)
#             e.message = _('We could not connect with the URL')
#             return (False, e)
#         except Exception, e:
#             self.do_something_with_a_vanished_popit_api_instance(popit_api_instance)
#             return (False, e)
#         persons = Person.objects.filter(api_instance=popit_api_instance)
#         for person in persons:
#             # There could be several memberships created.
#             memberships = Membership.objects.filter(writeitinstance=self, person=person)
#             if memberships.count() == 0:
#                 Membership.objects.create(writeitinstance=self, person=person)
#             if memberships.count() > 1:
#                 membership = memberships[0]
#                 memberships.exclude(id=membership.id).delete()
# 
#         return (True, None)
# 
#     def do_something_with_a_vanished_popit_api_instance(self, popit_api_instance):
#         pass
# 
#     def _load_persons_from_a_popit_api(self, popit_api_instance):
#         success_relating_people, error = self.relate_with_persons_from_popit_api_instance(popit_api_instance)
#         record = WriteitInstancePopitInstanceRecord.objects.get(
#             writeitinstance=self,
#             popitapiinstance=popit_api_instance
#             )
#         if success_relating_people:
#             record.set_status('success')
#         else:
#             if isinstance(error, ConnectionError):
#                 record.set_status('error', _('We could not connect with the URL'))
#             else:
#                 record.set_status('error', error.message)
#         return (success_relating_people, error)
# 
#     def load_persons_from_a_popit_api(self, popit_url):
#         '''This is an async wrapper for getting people from the api'''
#         popit_api_instance, created = PopitApiInstance.objects.get_or_create(url=popit_url)
#         record, created = WriteitInstancePopitInstanceRecord.objects.get_or_create(
#             writeitinstance=self,
#             popitapiinstance=popit_api_instance
#             )
#         if not created:
#             record.updated = datetime.datetime.today()
#             record.save()
#         record.set_status('inprogress')
#         from nuntium.tasks import pull_from_popit
#         return pull_from_popit.delay(self, popit_api_instance)
# 
#     def get_absolute_url(self):
#         return reverse('instance_detail', subdomain=self.slug)
# 
#     @property
#     def pulling_from_popit_status(self):
#         records = WriteitInstancePopitInstanceRecord.objects.filter(writeitinstance=self)
#         result = {'nothing': 0, 'inprogress': 0, 'success': 0, 'error': 0}
#         for record in records:
#             result[record.status] += 1
#         return result
# 
#     @property
#     def can_create_messages(self):
#         if self.config.allow_messages_using_form and \
#                 self.contacts.exists():
#             return True
# 
#         return False
# 
#     def __unicode__(self):
#         return self.name


# Path: nuntium/tests/home_view_tests.py
from global_test_case import GlobalTestCase as TestCase
from subdomains.utils import reverse
from django.utils.translation import activate
from instance.models import WriteItInstance
from django.contrib.auth.models import User
from django.test.client import Client

        activate('en')
        instance1 = WriteItInstance.objects.get(id=1)
        url = reverse("home")
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)
        self.assertTrue(response.context['writeitinstances'])
        self.assertEquals(response.context['writeitinstances'].count(), 2)
        self.assertEquals(response.context['writeitinstances'][0], instance1)

    def test_list_instances_view(self):
        '''All instances are displayed in home'''
        for instance in WriteItInstance.objects.all():
            instance.config.testing_mode = False
            instance.config.save()
        url = reverse('instance_list')
        self.assertTrue(url)
        c = Client()
        response = c.get(url)
        self.assertEquals(response.status_code, 200)
        self.assertIn('object_list', response.context)
        self.assertIsInstance(response.context['object_list'][0], WriteItInstance)
        self.assertEquals(len(response.context['object_list']), WriteItInstance.objects.count())

        self.assertTemplateUsed(response, 'nuntium/template_list.html')

    def test_testing_mode_instances_not_displayed(self):
        '''Test Mode instances are not displayed if a user is not logged in'''
        fiera = User.objects.create_user(username='fiera',
            password="feroz",
            email="fiera@ciudadanointeligente.org")

        w = WriteItInstance.objects.create(name='test_mode_instance', owner=fiera)