Allow write custom testrun in scheduler  PRODX-39509

Change-Id: Iccae2b41f52f2cfb83c11c4352347c2d96da7923
diff --git a/testrail_bot/control/celery_tasks/tasks.py b/testrail_bot/control/celery_tasks/tasks.py
index d378827..38bf022 100644
--- a/testrail_bot/control/celery_tasks/tasks.py
+++ b/testrail_bot/control/celery_tasks/tasks.py
@@ -40,7 +40,7 @@
 
 
 @shared_task
-def check_today_testplan():
+def check_today_testplan(*args, **kwargs):
     """
     Finds today testplan
     Creates TestRun with this id
@@ -49,3 +49,15 @@
     :return:
     """
     schedules_pipeline.task_to_check_today_testplan()
+
+
+@shared_task
+def check_specific_testplan(testplan_id, *args, **kwargs):
+    """
+    Finds today testplan
+    Creates TestRun with this id
+    Creates Periodic task to analyze created TestRun
+
+    :return:
+    """
+    schedules_pipeline.task_to_check_testplan(testplan_id)
diff --git a/testrail_bot/control/forms.py b/testrail_bot/control/forms.py
index fc27349..363035b 100644
--- a/testrail_bot/control/forms.py
+++ b/testrail_bot/control/forms.py
@@ -64,4 +64,4 @@
 class PeriodicTaskForm(forms.ModelForm):
     class Meta:
         model = CronPeriodicTask
-        fields = ["id", "enabled", "name", "cron"]
+        fields = ["id", "enabled", "name", "cron", "task_name", "testplan_id_arg"]
diff --git a/testrail_bot/control/migrations/0011_cronperiodictask_task_name_and_more.py b/testrail_bot/control/migrations/0011_cronperiodictask_task_name_and_more.py
new file mode 100644
index 0000000..2901309
--- /dev/null
+++ b/testrail_bot/control/migrations/0011_cronperiodictask_task_name_and_more.py
@@ -0,0 +1,23 @@
+# Generated by Django 4.2.7 on 2024-03-18 19:56
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('control', '0010_alter_testrailtestrun_options'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='cronperiodictask',
+            name='task_name',
+            field=models.CharField(choices=[('control.celery_tasks.tasks.check_today_testplan', 'Check today testplan'), ('control.celery_tasks.tasks.check_specific_testplan', 'Check specific testplan')], default='control.celery_tasks.tasks.check_today_testplan', max_length=300),
+        ),
+        migrations.AddField(
+            model_name='cronperiodictask',
+            name='testplan_id_arg',
+            field=models.CharField(blank=True, max_length=30, null=True),
+        ),
+    ]
diff --git a/testrail_bot/control/models.py b/testrail_bot/control/models.py
index 2994890..02ff78b 100644
--- a/testrail_bot/control/models.py
+++ b/testrail_bot/control/models.py
@@ -131,22 +131,27 @@
                                       auto_now=True)
 
 
-TASK_CHOICES = [
+TASKS = [
         ("control.celery_tasks.tasks.check_today_testplan",
          "Check today testplan",
          []
          ),
-        ("control.celery_tasks.tasks.task_to_check_testplan",
-         "Check testplan",
-         ["testplan_id"]
+        ("control.celery_tasks.tasks.check_specific_testplan",
+         "Check specific testplan",
+         ["testplan_id_arg"]
          ),
     ]
 
+TASK_CHOICES = list(map(lambda x:  x[:-1], TASKS))
+
 
 class CronPeriodicTask(PeriodicTask):
     cron = models.CharField(default="",
                             max_length=300,
                             blank=False)
+    task_name = models.CharField(max_length=300, choices=TASK_CHOICES,
+                                 default=TASK_CHOICES[0][0])
+    testplan_id_arg = models.CharField(max_length=30, blank=True, null=True)
 
     class Meta:
         ordering = ["id"]
diff --git a/testrail_bot/control/templates/control/scheduler.html b/testrail_bot/control/templates/control/scheduler.html
index 6a33562..928fd58 100644
--- a/testrail_bot/control/templates/control/scheduler.html
+++ b/testrail_bot/control/templates/control/scheduler.html
@@ -4,14 +4,14 @@
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
 <script>
 $(document).ready(function(){
-  $("#task-select").change(function(){
+  $("#id_task_name").change(function(){
   $("div#kwargs").empty()
-  var t = $("#task-select").val();
-  {%  for task, text, args in TASK_CHOICES %}
+  var t = $("#id_task_name").val();
+  {%  for task, text, args in TASKS %}
       {% if args %}
         if (t == '{{ task }}') {
           {% for arg in args %}
-            $("div#kwargs").append("<input type='text' name='{{ arg }}'>");
+            $("div#kwargs").append("<input type='text' placeholder='Enter {{ arg }}' name='{{ arg }}'>");
           {% endfor %}
         }
       {% endif %}
@@ -62,12 +62,27 @@
           {% bootstrap_field form.name  size='sm' %}</div>
     <div class="col-xs-8">
           {% bootstrap_field form.cron  size='sm' %}</div>
+    <div class="col-xs-8">
+          {% bootstrap_field form.task_name  size='sm' %}</div>
 
-    <select name="task" id="task-select">
-        <option value="control.celery_tasks.tasks.check_today_testplan">
-          Check today testplan</option>
-    </select>
-    <div id="kwargs"></div>
+
+
+<!--    <select name="task_name" id="task-select">-->
+<!--        <option value="control.celery_tasks.tasks.check_today_testplan">-->
+<!--          Check today testplan</option>-->
+<!--    </select>-->
+    <div id="kwargs">
+    {% if pk %}
+      {%  for task, text, args in TASKS %}
+        {% if 'testplan_id_arg' in args %}
+          <div class="col-xs-8">
+            {% bootstrap_field form.testplan_id_arg size='sm' %}</div>
+        {% endif %}
+      {% endfor %}
+
+    {% endif %}
+
+    </div>
   </form>
 </div>
 {% endblock %}
diff --git a/testrail_bot/control/views.py b/testrail_bot/control/views.py
index 8d5f692..e6655ad 100644
--- a/testrail_bot/control/views.py
+++ b/testrail_bot/control/views.py
@@ -265,12 +265,13 @@
         {
             "form": form,
             "pk": pk,
-            "TASK_CHOICES": models.TASK_CHOICES
+            "TASKS": models.TASKS
         }
     )
 
 
 def save_scheduler(request, pk=None):
+    print(f"{request.POST=}")
     minute, hour, day_of_month, month_of_year, day_of_week = \
         request.POST.get("cron", "* * * * *").split(" ")
     if pk is None:
@@ -285,15 +286,18 @@
             crontab=sch,
             cron=request.POST.get("cron"),
             name=request.POST.get("name"),
-            task="control.celery_tasks.tasks.check_today_testplan",
+            task_name=request.POST.get("task_name"),
             enabled=request.POST.get("enabled") == 'on',
-            # TODO(harhipova): uncomment when implemented tasks with arguments
-            # args=args,
-            # kwargs=kwargs,
+            testplan_id_arg=request.POST.get("testplan_id_arg"),
         )
     else:
         task = models.CronPeriodicTask.objects.get(pk=pk)
+
+    task.task = request.POST.get("task_name")
+    task.args = json.dumps((request.POST.get("testplan_id_arg"),))
+
     form = forms.PeriodicTaskForm(request.POST, instance=task)
+
     CrontabSchedule.objects.filter(id=task.crontab.id).update(
         minute=minute,
         hour=hour,
@@ -301,6 +305,9 @@
         month_of_year=month_of_year,
         day_of_week=day_of_week
     )
+    if not form.is_valid():
+        print(f"{form.errors=}")
+        return
     form.save()
     PeriodicTasks.update_changed()
     return render(
@@ -308,7 +315,7 @@
         {
             "form": form,
             "pk": task.id,
-            "TASK_CHOICES": models.TASK_CHOICES
+            "TASKS": models.TASKS
         }
     )