# Path: nuntium/popit_api_instance.py
# class PopitApiInstance(ApiInstance):
#     class Meta:
#         proxy = True
# 
#     def fetch_all_from_api(self, writeitinstance):
#         """
#         Update all the local data from the API. This method actually delegates
#         to the other models.
#         """
#         models = [PopitPerson]
#         for model in models:
#             model.fetch_all_from_api(instance=self, writeitinstance=writeitinstance)


import datetime
from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.db import models
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _
from annoying.fields import AutoOneToOneField
from autoslug import AutoSlugField
from nuntium.popit_api_instance import PopitApiInstance
from popit.models import Person, ApiInstance
from requests.exceptions import ConnectionError
from subdomains.utils import reverse
        from nuntium.tasks import pull_from_popit
    from nuntium.models import (
        NewAnswerNotificationTemplate, ConfirmationTemplate)
        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()


# Path: nuntium/popit_api_instance.py
# class PopitApiInstance(ApiInstance):
#     class Meta:
#         proxy = True
# 
#     def fetch_all_from_api(self, writeitinstance):
#         """
#         Update all the local data from the API. This method actually delegates
#         to the other models.
#         """
#         models = [PopitPerson]
#         for model in models:
#             model.fetch_all_from_api(instance=self, writeitinstance=writeitinstance)


# Path: instance/models.py
import datetime
from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.db import models
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _
from annoying.fields import AutoOneToOneField
from autoslug import AutoSlugField
from nuntium.popit_api_instance import PopitApiInstance
from popit.models import Person, ApiInstance
from requests.exceptions import ConnectionError
from subdomains.utils import reverse
        from nuntium.tasks import pull_from_popit
    from nuntium.models import (
        NewAnswerNotificationTemplate, ConfirmationTemplate)

        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)