# Path: helper/models.py
# class TaskPair(models.Model):
#     name = models.CharField(max_length=255, null=True, blank=True)
#     enabled = models.BooleanField(default=True)
#     cause_agent = models.ForeignKey(AgentConfig,
#                                     related_name='cause_task_pairs')
#     cause_task = models.CharField(max_length=255)
#     cause_options = HStoreField(null=True, blank=True)
#     effect_agent = models.ForeignKey(AgentConfig,
#                                      related_name='effect_task_pairs')
#     effect_task = models.CharField(max_length=255)
#     effect_options = HStoreField(null=True, blank=True)
# 
#     def __str__(self):
#         if self.name:
#             return self.name
#         return '{}:{} -> {}:{}'.format(
#             self.cause_agent, self.cause_task,
#             self.effect_agent, self.effect_task,
#         )
# 
#     def get_absolute_url(self):
#         return reverse('task_pair_detail', kwargs={'pk': self.id})
# 
#     def clean(self):
#         try:
#             self.cause
#         except AttributeError:
#             raise ValidationError('module "{}" has no task "{}"'.format(
#                 self.cause_agent.name + '.task', self.cause_task
#             ))
#         except ImportError:
#             raise ValidationError('module "{}" does not exist'.format(
#                 self.cause_agent.name + '.task'
#             ))
#         try:
#             self.effect
#         except AttributeError:
#             raise ValidationError('module "{}" has no task "{}"'.format(
#                 self.effect_agent.name + '.task', self.effect_task
#             ))
#         except ImportError:
#             raise ValidationError('module "{}" does not exist'.format(
#                 self.effect_agent.name + '.task'
#             ))
# 
#     @property
#     def cause(self):
#         cause = self.cause_agent.agent.cause_tasks[self.cause_task]
#         cause.do_not_call_in_templates = True
#         return cause
# 
#     @property
#     def effect(self):
#         effect = self.effect_agent.agent.effect_tasks[self.effect_task]
#         effect.do_not_call_in_templates = True
#         return effect
# 
#     @property
#     def cause_view(self):
#         return getattr(import_module(self.cause_agent.name + '.views'),
#                        self.cause_task)
# 
#     @property
#     def effect_view(self):
#         return getattr(import_module(self.effect_agent.name + '.views'),
#                        self.effect_task)
# 
#     @property
#     def cause_name(self):
#         return getattr(self.cause, 'label',
#                        self.cause_task.replace('_', ' ').capitalize())
# 
#     @property
#     def effect_name(self):
#         return getattr(self.effect, 'label',
#                        self.effect_task.replace('_', ' ').capitalize())
# 
#     @property
#     def schedule(self):
#         try:
#             return self.cause.every
#         except (AttributeError, ImportError):
#             pass
# 
#     def run(self):
#         cause_options = self.cause_agent.options or {}
#         cause_options.update({k: v for k, v in self.cause_options.items()
#                               if not k.startswith('_')})
#         cause_options['task_pair_id'] = self.id
#         effect_options = self.effect_agent.options or {}
#         effect_options.update({k: v for k, v in self.effect_options.items()
#                                if not k.startswith('_')})
#         effect_options['task_pair_id'] = self.id
# 
#         cause = self.cause.s(**cause_options)
# 
#         if getattr(self.cause, 'dedup_key', None) is not None:
#             effect = dedup_effect_wrapper.s(
#                 dedup_key=self.cause.dedup_key,
#                 task_pair_id=self.id,
#                 effect=self.effect.s(**effect_options),
#             )
#         else:
#             effect = self.effect.s(**effect_options)
# 
#         return chain(cause, dmap.s(effect))()
# 
#     def populate_dedup_events(self):
#         if getattr(self.cause, 'dedup_key', None) is not None:
#             cause_options = self.cause_agent.options or {}
#             cause_options.update({k: v for k, v in self.cause_options.items()
#                                 if not k.startswith('_')})
#             cause_options['task_pair_id'] = self.id
# 
#             cause = self.cause.s(**cause_options)
#             effect = create_dedup_event.s(task_pair_id=self.id,
#                                         dedup_key=self.cause.dedup_key)
#             return chain(cause, dmap.s(effect))()


from django.core.management.base import BaseCommand, CommandError
from helper.models import TaskPair

class Command(BaseCommand):
    help = 'Runs the specified task'

    def add_arguments(self, parser):
        parser.add_argument('task_id', nargs='+', type=int)
        parser.add_argument('--wait', action='store_true')

    def handle(self, *args, **options):
        for task_id in options['task_id']:
            try:

# Path: helper/models.py
# class TaskPair(models.Model):
#     name = models.CharField(max_length=255, null=True, blank=True)
#     enabled = models.BooleanField(default=True)
#     cause_agent = models.ForeignKey(AgentConfig,
#                                     related_name='cause_task_pairs')
#     cause_task = models.CharField(max_length=255)
#     cause_options = HStoreField(null=True, blank=True)
#     effect_agent = models.ForeignKey(AgentConfig,
#                                      related_name='effect_task_pairs')
#     effect_task = models.CharField(max_length=255)
#     effect_options = HStoreField(null=True, blank=True)
# 
#     def __str__(self):
#         if self.name:
#             return self.name
#         return '{}:{} -> {}:{}'.format(
#             self.cause_agent, self.cause_task,
#             self.effect_agent, self.effect_task,
#         )
# 
#     def get_absolute_url(self):
#         return reverse('task_pair_detail', kwargs={'pk': self.id})
# 
#     def clean(self):
#         try:
#             self.cause
#         except AttributeError:
#             raise ValidationError('module "{}" has no task "{}"'.format(
#                 self.cause_agent.name + '.task', self.cause_task
#             ))
#         except ImportError:
#             raise ValidationError('module "{}" does not exist'.format(
#                 self.cause_agent.name + '.task'
#             ))
#         try:
#             self.effect
#         except AttributeError:
#             raise ValidationError('module "{}" has no task "{}"'.format(
#                 self.effect_agent.name + '.task', self.effect_task
#             ))
#         except ImportError:
#             raise ValidationError('module "{}" does not exist'.format(
#                 self.effect_agent.name + '.task'
#             ))
# 
#     @property
#     def cause(self):
#         cause = self.cause_agent.agent.cause_tasks[self.cause_task]
#         cause.do_not_call_in_templates = True
#         return cause
# 
#     @property
#     def effect(self):
#         effect = self.effect_agent.agent.effect_tasks[self.effect_task]
#         effect.do_not_call_in_templates = True
#         return effect
# 
#     @property
#     def cause_view(self):
#         return getattr(import_module(self.cause_agent.name + '.views'),
#                        self.cause_task)
# 
#     @property
#     def effect_view(self):
#         return getattr(import_module(self.effect_agent.name + '.views'),
#                        self.effect_task)
# 
#     @property
#     def cause_name(self):
#         return getattr(self.cause, 'label',
#                        self.cause_task.replace('_', ' ').capitalize())
# 
#     @property
#     def effect_name(self):
#         return getattr(self.effect, 'label',
#                        self.effect_task.replace('_', ' ').capitalize())
# 
#     @property
#     def schedule(self):
#         try:
#             return self.cause.every
#         except (AttributeError, ImportError):
#             pass
# 
#     def run(self):
#         cause_options = self.cause_agent.options or {}
#         cause_options.update({k: v for k, v in self.cause_options.items()
#                               if not k.startswith('_')})
#         cause_options['task_pair_id'] = self.id
#         effect_options = self.effect_agent.options or {}
#         effect_options.update({k: v for k, v in self.effect_options.items()
#                                if not k.startswith('_')})
#         effect_options['task_pair_id'] = self.id
# 
#         cause = self.cause.s(**cause_options)
# 
#         if getattr(self.cause, 'dedup_key', None) is not None:
#             effect = dedup_effect_wrapper.s(
#                 dedup_key=self.cause.dedup_key,
#                 task_pair_id=self.id,
#                 effect=self.effect.s(**effect_options),
#             )
#         else:
#             effect = self.effect.s(**effect_options)
# 
#         return chain(cause, dmap.s(effect))()
# 
#     def populate_dedup_events(self):
#         if getattr(self.cause, 'dedup_key', None) is not None:
#             cause_options = self.cause_agent.options or {}
#             cause_options.update({k: v for k, v in self.cause_options.items()
#                                 if not k.startswith('_')})
#             cause_options['task_pair_id'] = self.id
# 
#             cause = self.cause.s(**cause_options)
#             effect = create_dedup_event.s(task_pair_id=self.id,
#                                         dedup_key=self.cause.dedup_key)
#             return chain(cause, dmap.s(effect))()


# Path: helper/management/commands/run_task.py
from django.core.management.base import BaseCommand, CommandError
from helper.models import TaskPair


class Command(BaseCommand):
    help = 'Runs the specified task'

    def add_arguments(self, parser):
        parser.add_argument('task_id', nargs='+', type=int)
        parser.add_argument('--wait', action='store_true')

    def handle(self, *args, **options):
        for task_id in options['task_id']:
            try:

                task = TaskPair.objects.get(pk=task_id)