Add ability to select date for test run.

Adds ability to select date for test run to process. Adds direct link to
test that failed to be processed by testrail. Changes created_by from
username to user_id

Related-PROD: PRODX-6452
Change-Id: Ib721dba49bf5c6c8d570e3e676eb4eb072604c67
diff --git a/testrail_bot/control/celery_tasks/api.py b/testrail_bot/control/celery_tasks/api.py
index 2954903..9510b62 100644
--- a/testrail_bot/control/celery_tasks/api.py
+++ b/testrail_bot/control/celery_tasks/api.py
@@ -54,11 +54,3 @@
 
 def add_result(test_id, update_dict):
     api.results.add_result(test_id, **update_dict)
-
-
-def get_user_id(user_name):
-    users = api.users.get_users()
-    users = list(filter(lambda x: x["name"] == user_name, users))
-    if not users:
-        return None
-    return users[0]["id"]
diff --git a/testrail_bot/control/celery_tasks/tasks.py b/testrail_bot/control/celery_tasks/tasks.py
index 4b3d5e9..08308f9 100644
--- a/testrail_bot/control/celery_tasks/tasks.py
+++ b/testrail_bot/control/celery_tasks/tasks.py
@@ -57,7 +57,7 @@
 
 
 @shared_task
-def process_run(run_id, report_id, path):
+def process_run(run_id, report_id, path, run_date):
     report = models.Report.objects.get(pk=report_id)
     with open(path, "w") as f:
         test_run = models.TestRun.objects.get(pk=run_id)
@@ -72,8 +72,8 @@
             finish_report(report)
             return
 
-        created_by_id = api.get_user_id(test_run.created_by)
-        kw = {"limit": 100}
+        created_by_id = test_run.created_by_id
+        kw = {"limit": 100, "created_before": int(run_date)}
         if created_by_id:
             kw["created_by"] = created_by_id
         plans = api.get_plans(project_id, test_run.plan_name, **kw)
@@ -120,6 +120,12 @@
                         "Similarity not found due to similarity:{}%\n".format(
                             round(100.0 * ratio, 2)))
                     f.flush()
+            else:
+                f.write("Automatic test processing failed. Please process "
+                        "test manualy <a href=https://mirantis.testrail.com/"
+                        "index.php?/tests/view/{test_id}>{test_id}"
+                        "</a>\n".format(test_id=test["id"]))
+                f.flush()
         f.write("Test processing finished")
         f.flush()
         finish_report(report)
diff --git a/testrail_bot/control/forms.py b/testrail_bot/control/forms.py
index 1236d6d..e3d47da 100644
--- a/testrail_bot/control/forms.py
+++ b/testrail_bot/control/forms.py
@@ -1,8 +1,9 @@
-from django.forms import ModelForm
+from datetime import date
+from django import forms
 from .models import TestRun
 
 
-class TestRunForm(ModelForm):
+class TestRunForm(forms.ModelForm):
     class Meta:
         model = TestRun
         fields = "__all__"
@@ -10,7 +11,7 @@
             "project_name": "Name of the project",
             "plan_name": "Name of the Test Plan without date",
             "run_name": "Name of the run",
-            "created_by": "Test Run created by",
+            "created_by_id": "If of the user that created Test Run",
             "filter_func": "Custom filter function",
             "ip_filter": "Change ip to x.x.x.x",
             "uuid_filter": "Change uuid to xxxx",
@@ -20,3 +21,8 @@
         help_texts = {
             "filter_func": "Leave blank if not used",
         }
+
+    timestamp = forms.DateField(
+        label="Date of report to be processed", initial=date.today(),
+        widget=forms.SelectDateWidget(years=[date.today().year + i for i in range(-3, 10)]),
+        required=False)
diff --git a/testrail_bot/control/migrations/0014_auto_20200731_1626.py b/testrail_bot/control/migrations/0014_auto_20200731_1626.py
new file mode 100644
index 0000000..cac1a33
--- /dev/null
+++ b/testrail_bot/control/migrations/0014_auto_20200731_1626.py
@@ -0,0 +1,22 @@
+# Generated by Django 3.0.7 on 2020-07-31 16:26
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('control', '0013_auto_20200721_1248'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='testrun',
+            name='created_by',
+        ),
+        migrations.AddField(
+            model_name='testrun',
+            name='created_by_id',
+            field=models.IntegerField(default=-1),
+        ),
+    ]
diff --git a/testrail_bot/control/migrations/0015_auto_20200731_1818.py b/testrail_bot/control/migrations/0015_auto_20200731_1818.py
new file mode 100644
index 0000000..aac64c0
--- /dev/null
+++ b/testrail_bot/control/migrations/0015_auto_20200731_1818.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.0.7 on 2020-07-31 18:18
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('control', '0014_auto_20200731_1626'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='testrun',
+            name='created_by_id',
+            field=models.IntegerField(),
+        ),
+    ]
diff --git a/testrail_bot/control/models.py b/testrail_bot/control/models.py
index b79836a..328ed29 100644
--- a/testrail_bot/control/models.py
+++ b/testrail_bot/control/models.py
@@ -6,7 +6,7 @@
     project_name = models.CharField(max_length=300)
     plan_name = models.CharField(max_length=300)
     run_name = models.CharField(max_length=300)
-    created_by = models.CharField(max_length=300, default="")
+    created_by_id = models.IntegerField()
     filter_func = models.TextField(null=True, blank=True)
     ip_filter = models.BooleanField(default=False)
     uuid_filter = models.BooleanField(default=False)
diff --git a/testrail_bot/control/templates/control/create_run.html b/testrail_bot/control/templates/control/create_run.html
index 9d6ddb1..61eec45 100644
--- a/testrail_bot/control/templates/control/create_run.html
+++ b/testrail_bot/control/templates/control/create_run.html
@@ -8,7 +8,10 @@
       <div class="col-xs-8">{% bootstrap_field form.project_name  size='sm' %}</div>
       <div class="col-xs-8">{% bootstrap_field form.plan_name size='sm' %}</div>
       <div class="col-xs-8">{% bootstrap_field form.run_name  size='sm' %}</div>
-      <div class="col-xs-8">{% bootstrap_field form.created_by  size='sm' %}</div>
+      <div class="col-xs-8">{% bootstrap_field form.created_by_id  size='sm' %}</div>
+      <div class="col-xs-8">
+        User to id list: <ul><li>os-qa-bot = <code>109</code></li></ul>
+      </div>
       <div class="col-xs-8">{% bootstrap_field form.filter_func  size='sm' %}</div>
       <div class="col-md-5">
         {% bootstrap_field form.ip_filter  size='sm'%}
diff --git a/testrail_bot/control/templates/control/help.html b/testrail_bot/control/templates/control/help.html
index d4dbf69..6c63b78 100644
--- a/testrail_bot/control/templates/control/help.html
+++ b/testrail_bot/control/templates/control/help.html
@@ -15,7 +15,7 @@
                 <li>Enter Project name i.e. <code>Mirantis Cloud Platform</code></li>
                 <li>Enter Plan name without date i.e. <code>[MCP2.0]OSCORE</code></li>
                 <li>Enter full name of the run i.e. <code>ussuri-core-ceph-ceph-dvr <[MCP2.0_USSURI]Tempest></code></li>
-                <li>Fill created by column i.e. <code>os-qa-bot</code></li>
+                <li>Fill created_by_id column i.e. <code>109</code></li>
                 <li>(Optional) Enter custom filtration function.
                     If not used it should be empty. If used, the function definition should be like:
                     <pre>
diff --git a/testrail_bot/control/templates/control/update_run.html b/testrail_bot/control/templates/control/update_run.html
index 1893ee5..91c953e 100644
--- a/testrail_bot/control/templates/control/update_run.html
+++ b/testrail_bot/control/templates/control/update_run.html
@@ -8,8 +8,14 @@
       <div class="col-xs-8">{% bootstrap_field form.project_name  size='sm' %}</div>
       <div class="col-xs-8">{% bootstrap_field form.plan_name size='sm' %}</div>
       <div class="col-xs-8">{% bootstrap_field form.run_name  size='sm' %}</div>
-      <div class="col-xs-8">{% bootstrap_field form.created_by  size='sm' %}</div>
+      <div class="col-xs-8">{% bootstrap_field form.created_by_id  size='sm' %}</div>
+      <div class="col-xs-8">
+        User to id list: <ul><li>os-qa-bot = <code>109</code></li></ul>
+      </div>
       <div class="col-xs-8">{% bootstrap_field form.filter_func  size='sm' %}</div>
+      <div class="col-xs-8">
+        {% bootstrap_field form.timestamp size='sm' %}
+      </div>
       <div class="col-md-5">
         {% bootstrap_field form.ip_filter  size='sm'%}
       </div>
diff --git a/testrail_bot/control/views.py b/testrail_bot/control/views.py
index 1e8eaa3..f813f86 100644
--- a/testrail_bot/control/views.py
+++ b/testrail_bot/control/views.py
@@ -1,4 +1,4 @@
-from datetime import datetime
+import datetime
 import json
 import os
 
@@ -63,17 +63,23 @@
 def submit_run(request, run_id):
     run = models.TestRun.objects.get(pk=run_id)
     report_name = "{}-{}".format(
-        run.run_name, datetime.isoformat(datetime.now()))
+        run.run_name, datetime.datetime.isoformat(datetime.datetime.now()))
     path = os.path.join(models.fs.location, report_name)
     with open(path, "w"):
         pass
+    form = forms.TestRunForm(request.POST)
+    run_date = None
+    if form.is_valid():
+        run_date = datetime.datetime.combine(
+            form.cleaned_data["timestamp"] + datetime.timedelta(days=1),
+            datetime.datetime.min.time()).timestamp()
 
     report = models.Report(
         test_run=run,
         report_name=report_name,
         path=path)
     report.save()
-    process_run.delay(run_id, report.id, path)
+    process_run.delay(run_id, report.id, path, run_date)
 
     return redirect("single_report", report.id)