# Path: instance/models.py
# class WriteitInstancePopitInstanceRecord(models.Model):
#     STATUS_CHOICES = (
#         ("nothing", _("Not doing anything now")),
#         ("error", _("Error")),
#         ("success", _("Success")),
#         ("waiting", _("Waiting")),
#         ("inprogress", _("In Progress")),
#         )
#     writeitinstance = models.ForeignKey(WriteItInstance)
#     popitapiinstance = models.ForeignKey(ApiInstance)
#     periodicity = models.CharField(
#         max_length="2",
#         choices=PERIODICITY,
#         default='1W',
#         )
#     status = models.CharField(
#         max_length="20",
#         choices=STATUS_CHOICES,
#         default="nothing",
#         )
#     status_explanation = models.TextField(default='')
#     updated = models.DateTimeField(auto_now_add=True)
#     created = models.DateTimeField(auto_now_add=True, editable=False)
# 
#     def __unicode__(self):
#         return "The people from {url} were loaded into {instance}".format(
#             url=self.popitapiinstance.url,
#             instance=self.writeitinstance.__unicode__(),
#             )
# 
#     def set_status(self, status, explanation=''):
#         self.status = status
#         self.status_explanation = explanation
#         self.save()


from django.core.management.base import BaseCommand
from popit.models import ApiInstance
from instance.models import WriteitInstancePopitInstanceRecord
from django.contrib.auth.models import User
import logging

logger = logging.getLogger('main')


class WPBackfillRecords(object):
    @classmethod
    def back_fill_popit_records(cls, writeitinstance):
        persons_in_instance = writeitinstance.persons.all()
        api_instances = ApiInstance.objects.filter(person__in=persons_in_instance).distinct()
        for a in api_instances:
            record, created = WriteitInstancePopitInstanceRecord.objects.get_or_create(
                writeitinstance=writeitinstance,
                popitapiinstance=a)

# Path: instance/models.py
# class WriteitInstancePopitInstanceRecord(models.Model):
#     STATUS_CHOICES = (
#         ("nothing", _("Not doing anything now")),
#         ("error", _("Error")),
#         ("success", _("Success")),
#         ("waiting", _("Waiting")),
#         ("inprogress", _("In Progress")),
#         )
#     writeitinstance = models.ForeignKey(WriteItInstance)
#     popitapiinstance = models.ForeignKey(ApiInstance)
#     periodicity = models.CharField(
#         max_length="2",
#         choices=PERIODICITY,
#         default='1W',
#         )
#     status = models.CharField(
#         max_length="20",
#         choices=STATUS_CHOICES,
#         default="nothing",
#         )
#     status_explanation = models.TextField(default='')
#     updated = models.DateTimeField(auto_now_add=True)
#     created = models.DateTimeField(auto_now_add=True, editable=False)
# 
#     def __unicode__(self):
#         return "The people from {url} were loaded into {instance}".format(
#             url=self.popitapiinstance.url,
#             instance=self.writeitinstance.__unicode__(),
#             )
# 
#     def set_status(self, status, explanation=''):
#         self.status = status
#         self.status_explanation = explanation
#         self.save()


# Path: nuntium/management/commands/back_fill_writeit_popit_records.py
from django.core.management.base import BaseCommand
from popit.models import ApiInstance
from instance.models import WriteitInstancePopitInstanceRecord
from django.contrib.auth.models import User
import logging


logger = logging.getLogger('main')


class WPBackfillRecords(object):
    @classmethod
    def back_fill_popit_records(cls, writeitinstance):
        persons_in_instance = writeitinstance.persons.all()
        api_instances = ApiInstance.objects.filter(person__in=persons_in_instance).distinct()
        for a in api_instances:
            record, created = WriteitInstancePopitInstanceRecord.objects.get_or_create(
                writeitinstance=writeitinstance,
                popitapiinstance=a)

            logger.info(