コード例 #1
0
ファイル: views.py プロジェクト: d9pouces/AutoPlanner
def multiply_task(request, task_pk):
    obj = get_object_or_404(Task.query(request), pk=task_pk)
    # noinspection PyProtectedMember
    model_admin = admin.site._registry[Organization]
    assert isinstance(model_admin, OrganizationAdmin)
    # noinspection PyProtectedMember
    opts = model_admin.model._meta
    if request.method == 'POST':
        form = MultiplyTaskForm(request.POST)
        if form.is_valid():
            until_src = form.cleaned_data['until']
            every_src = form.cleaned_data['every']
            assert isinstance(until_src, datetime.datetime)
            limit = datetime.datetime(year=until_src.year, month=until_src.month, day=until_src.day,
                                      hour=23, minute=59, second=59, tzinfo=obj.start_time.tzinfo)
            to_create = []
            increment = datetime.timedelta(days=every_src)
            start_time = obj.start_time + increment
            end_time = obj.end_time + increment
            matcher = re.match(r'^(.*)\s+\((\d+)\)', obj.name)
            if matcher:
                new_name = matcher.group(1)
                name_index = int(matcher.group(2)) + 1
            else:
                new_name = obj.name
                name_index = 2
            current_category_pks = [x.pk for x in obj.categories.all()]
            while start_time < limit:
                new_task = Task(organization_id=obj.organization_id, name='%s (%d)' % (new_name, name_index),
                                start_time=start_time, end_time=end_time)
                to_create.append(new_task)
                start_time += increment
                end_time += increment
                name_index += 1
            if to_create:
                if current_category_pks:
                    cls = Task.categories.through
                    all_categories_to_create = []
                    for new_task in to_create:
                        new_task.save()
                        all_categories_to_create += [cls(task_id=new_task.pk, category_id=category_pk)
                                                     for category_pk in current_category_pks]
                    cls.objects.bulk_create(all_categories_to_create)
                else:
                    Task.objects.bulk_create(to_create)
            if len(to_create) > 1:
                messages.info(request, _('%(count)d tasks have been created.') % {'count': len(to_create)})
            elif to_create:
                messages.info(request, _('A task has been created.'))
            new_url = reverse('admin:%s_%s_change' % (opts.app_label, opts.model_name),
                              args=(quote(obj.organization_id), ), current_app=model_admin.admin_site.name)
            return HttpResponseRedirect(new_url)
    else:
        form = MultiplyTaskForm(initial={'source_task': obj})
    template_values = get_template_values(request, organization_pk=obj.organization_id)
    template_values['obj'] = obj
    template_values['form'] = form
    return render_to_response('autoplanner/multiply_task.html', template_values, RequestContext(request))
コード例 #2
0
 def test_infeasible(self):
     org = self.get_organization()
     category_1 = org.category_set.get(name='C1')
     assert isinstance(category_1, Category)
     category_1.balancing_mode = Category.BALANCE_NUMBER
     category_1.balancing_tolerance = 0
     category_1.save()
     task_1 = Task(organization=org, name='E1', start_time=self.get_time(0), end_time=self.get_time(1),
                   category=category_1)
     task_1.save()
     task_2 = Task(organization=org, name='E2', start_time=self.get_time(1), end_time=self.get_time(2),
                   category=category_1)
     task_2.save()
     task_3 = Task(organization=org, name='E3', start_time=self.get_time(2), end_time=self.get_time(3),
                   category=category_1)
     task_3.save()
     task_4 = Task(organization=org, name='E4', start_time=self.get_time(3), end_time=self.get_time(4),
                   category=category_1)
     task_4.save()
     s = Scheduler(org)
     result_list = s.solve(verbose=False)
     result_dict = s.result_by_agent(result_list)
     self.assertEqual({}, result_dict)
     category_1.balancing_tolerance = 1
     category_1.save()
     s = Scheduler(org)
     result_list = s.solve(verbose=False)
     result_dict = s.result_by_agent(result_list)
     self.assertEqual({1: {1, 2}, 2: {3}, 3: {4}}, result_dict)
コード例 #3
0
    def test_single(self):
        org = self.get_organization()
        category_1 = org.category_set.get(name='C1')
        task_1 = Task(organization=org, name='E1', start_time=self.get_time(0), end_time=self.get_time(1),
                      category=category_1)
        task_1.save()
        # s = Scheduler(org)
        # for c in s.constraints():
        # print(c)

        org = self.get_organization()
        category_1 = org.category_set.get(name='C1')
        category_1.balancing_mode = Category.BALANCE_NUMBER
        category_1.save()
        task_1 = Task(organization=org, name='E1', start_time=self.get_time(0), end_time=self.get_time(1),
                      category=category_1)
        task_1.save()
        s = Scheduler(org)
        result_list = s.solve(verbose=False)
        result_dict = s.result_by_agent(result_list)
        self.assertEqual({4: {2}}, result_dict)
コード例 #4
0
    def test_overlap(self):
        org = self.get_organization()
        category_1 = org.category_set.get(name='C1')
        Agent.objects.filter(organization=org, name__in=['A2', 'A3']).delete()
        task_1 = Task(organization=org, name='E1', start_time=self.get_time(10), end_time=self.get_time(20),
                      category=category_1)
        task_1.save()
        task_2 = Task(organization=org, name='E2', start_time=self.get_time(15), end_time=self.get_time(23),
                      category=category_1)
        task_2.save()
        s = Scheduler(org)
        result_list = s.solve(verbose=False)
        result_dict = s.result_by_agent(result_list)
        self.assertEqual({}, result_dict)

        task_2.start_time = self.get_time(21)
        task_2.save()
        s = Scheduler(org)
        result_list = s.solve(verbose=False)
        result_dict = s.result_by_agent(result_list)
        self.assertEqual({1: {1, 2}}, result_dict)

        task_2.start_time = self.get_time(20)
        task_2.save()
        s = Scheduler(org)
        result_list = s.solve(verbose=False)
        result_dict = s.result_by_agent(result_list)
        self.assertEqual({1: {1, 2}}, result_dict)

        task_2.start_time = self.get_time(5)
        task_2.end_time = self.get_time(10)
        task_2.save()
        s = Scheduler(org)
        result_list = s.solve(verbose=False)
        result_dict = s.result_by_agent(result_list)
        self.assertEqual({1: {1, 2}}, result_dict)

        task_2.start_time = self.get_time(5)
        task_2.end_time = self.get_time(11)
        task_2.save()
        s = Scheduler(org)
        result_list = s.solve(verbose=False)
        result_dict = s.result_by_agent(result_list)
        self.assertEqual({}, result_dict)

        task_2.start_time = self.get_time(5)
        task_2.end_time = self.get_time(23)
        task_2.save()
        s = Scheduler(org)
        result_list = s.solve(verbose=False)
        result_dict = s.result_by_agent(result_list)
        self.assertEqual({}, result_dict)

        task_2.start_time = self.get_time(12)
        task_2.end_time = self.get_time(17)
        task_2.save()
        s = Scheduler(org)
        result_list = s.solve(verbose=False)
        result_dict = s.result_by_agent(result_list)
        self.assertEqual({}, result_dict)