summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2022-03-29 11:23:37 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:21:00 +0100
commit221599192781585f4c7ca20dffdcf781b14ac3b9 (patch)
tree1aa4a605884b0652235f482545d7418a9c903bc0
parent69c2ef08e8cb3fb4b23a9bf7764386c95954e002 (diff)
downloadIshtar-221599192781585f4c7ca20dffdcf781b14ac3b9.tar.bz2
Ishtar-221599192781585f4c7ca20dffdcf781b14ac3b9.zip
Background tasks: adapt model (refs #5319)
-rw-r--r--ishtar_common/migrations/0218_auto_20220329_1121.py46
-rw-r--r--ishtar_common/models.py49
-rw-r--r--ishtar_common/views.py5
3 files changed, 99 insertions, 1 deletions
diff --git a/ishtar_common/migrations/0218_auto_20220329_1121.py b/ishtar_common/migrations/0218_auto_20220329_1121.py
new file mode 100644
index 000000000..01e5bfbcf
--- /dev/null
+++ b/ishtar_common/migrations/0218_auto_20220329_1121.py
@@ -0,0 +1,46 @@
+# Generated by Django 2.2.24 on 2022-03-29 11:21
+
+import datetime
+import django.contrib.postgres.fields
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('ishtar_common', '0217_auto_20220328_1222'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='userprofile',
+ name='background_tasks',
+ field=models.BooleanField(default=True, help_text='If set to true, each import, export, document generation is set as a background task.', verbose_name='Use background tasks'),
+ ),
+ migrations.AddField(
+ model_name='userprofile',
+ name='background_tasks_send_email',
+ field=models.BooleanField(default=True, verbose_name='Send email when the task is finished'),
+ ),
+ migrations.CreateModel(
+ name='BackgroundTask',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('name', models.TextField(verbose_name='Name')),
+ ('view', models.CharField(choices=[('generate-labels', 'Generate label')], max_length=100, verbose_name='View')),
+ ('args', django.contrib.postgres.fields.ArrayField(base_field=models.TextField(), blank=True, null=True, size=None, verbose_name='Arguments')),
+ ('state', models.CharField(choices=[('S', 'Scheduled'), ('P', 'In progress'), ('F', 'Finished')], default='S', max_length=2, verbose_name='State')),
+ ('creation_date', models.DateTimeField(default=datetime.datetime.now)),
+ ('launch_date', models.DateTimeField(blank=True, null=True)),
+ ('finished_date', models.DateTimeField(blank=True, null=True)),
+ ('result', models.FileField(blank=True, null=True, upload_to='', verbose_name='Result')),
+ ('profile', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ishtar_common.UserProfile')),
+ ],
+ options={
+ 'verbose_name': 'Background task',
+ 'verbose_name_plural': 'Background tasks',
+ 'ordering': ['creation_date'],
+ },
+ ),
+ ]
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index fbab13010..5b9d48357 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -55,7 +55,7 @@ from django.contrib.gis.db import models
from django.contrib.gis.db.models.aggregates import Union
from django.contrib.gis.geos.polygon import Polygon
from django.contrib.gis.geos.collections import MultiPolygon
-from django.contrib.postgres.fields import JSONField
+from django.contrib.postgres.fields import JSONField, ArrayField
from django.contrib.postgres.indexes import GinIndex
from django.contrib.sites.models import Site
from django.core.cache import cache
@@ -3081,6 +3081,14 @@ class UserProfile(models.Model):
show_field_number = models.BooleanField(_("Show field number"), default=False)
auto_pin = models.BooleanField(_("Automatically pin"), default=False)
display_pin_menu = models.BooleanField(_("Display pin menu"), default=False)
+ background_tasks = models.BooleanField(
+ _("Use background tasks"), default=True,
+ help_text=_("If set to true, each import, export, document generation is set "
+ "as a background task.")
+ )
+ background_tasks_send_email = models.BooleanField(
+ _("Send email when the task is finished"), default=True,
+ )
person = models.ForeignKey(
Person,
verbose_name=_("Person"),
@@ -3175,6 +3183,45 @@ def post_save_userprofile(sender, **kwargs):
post_save.connect(post_save_userprofile, sender=UserProfile)
+TASK_STATE = (
+ ("S", _("Scheduled")),
+ ("P", _("In progress")),
+ ("F", _("Finished")),
+)
+
+BACKGROUND_TASKS = {
+ # key: (label, function)
+ "generate-labels": (
+ _("Generate label"), "ishtar_common.views.generate_label_view"
+ ),
+}
+
+
+BACKGROUND_TASKS_LIST = [
+ (k, BACKGROUND_TASKS[k][0]) for k in BACKGROUND_TASKS
+]
+
+
+class BackgroundTask(models.Model):
+ name = models.TextField(_("Name"))
+ view = models.CharField(_("View"), max_length=100, choices=BACKGROUND_TASKS_LIST)
+ args = ArrayField(models.TextField(),
+ verbose_name=_("Arguments"), blank=True, null=True)
+ profile = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
+ state = models.CharField(
+ _("State"), max_length=2, choices=TASK_STATE, default="S"
+ )
+ creation_date = models.DateTimeField(default=datetime.datetime.now)
+ launch_date = models.DateTimeField(null=True, blank=True)
+ finished_date = models.DateTimeField(null=True, blank=True)
+ result = models.FileField(_("Result"), null=True, blank=True)
+
+ class Meta:
+ verbose_name = _("Background task")
+ verbose_name_plural = _("Background tasks")
+ ordering = ["creation_date"]
+
+
class IshtarUser(FullSearch):
SLUG = "ishtaruser"
TABLE_COLS = (
diff --git a/ishtar_common/views.py b/ishtar_common/views.py
index f229ffdbd..50964f462 100644
--- a/ishtar_common/views.py
+++ b/ishtar_common/views.py
@@ -1341,6 +1341,11 @@ class GenerateView(IshtarMixin, LoginRequiredMixin, View):
return response
+# TODO
+def generate_label_view():
+ pass
+
+
class GenerateLabelView(GenerateView):
def get_template(self, template_slug):
try: