Skip to content

Commit

Permalink
keep steps data
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Oct 10, 2023
1 parent 0e59c4c commit f24a8b2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
40 changes: 40 additions & 0 deletions lemarche/tenders/migrations/0058_tenderstepsdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.2.2 on 2023-10-05 14:15

from django.db import migrations, models
import django.utils.timezone
import django_extensions.db.fields
import shortuuid.main


class Migration(migrations.Migration):

dependencies = [
("tenders", "0057_alter_tender_siae_transactioned"),
]

operations = [
migrations.CreateModel(
name="TenderStepsData",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
(
"uuid",
django_extensions.db.fields.ShortUUIDField(
auto_created=True,
blank=True,
db_index=True,
default=shortuuid.main.ShortUUID.uuid,
editable=False,
unique=True,
verbose_name="Identifiant UUID",
),
),
(
"created_at",
models.DateTimeField(default=django.utils.timezone.now, verbose_name="Date de création"),
),
("updated_at", models.DateTimeField(auto_now=True, verbose_name="Date de modification")),
("steps_data", models.JSONField(default=list, editable=False, verbose_name="Données des étapes")),
],
),
]
16 changes: 16 additions & 0 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from django.utils.functional import cached_property
from django.utils.text import slugify
from django_better_admin_arrayfield.models.fields import ArrayField
from django_extensions.db.fields import ShortUUIDField
from shortuuid import uuid

from lemarche.perimeters.models import Perimeter
from lemarche.siaes import constants as siae_constants
Expand Down Expand Up @@ -761,3 +763,17 @@ class Meta:
@cached_property
def perimeters_list_string(self) -> str:
return ", ".join(self.perimeters.values_list("name", flat=True))


class TenderStepsData(models.Model):
created_at = models.DateTimeField(verbose_name="Date de création", default=timezone.now)
updated_at = models.DateTimeField(verbose_name="Date de modification", auto_now=True)
uuid = ShortUUIDField(
verbose_name="Identifiant UUID",
default=uuid,
editable=False,
unique=True,
db_index=True,
auto_created=True,
)
steps_data = models.JSONField(verbose_name="Données des étapes", editable=False, default=list)
24 changes: 23 additions & 1 deletion lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from lemarche.siaes.models import Siae
from lemarche.tenders import constants as tender_constants
from lemarche.tenders.models import Tender, TenderSiae
from lemarche.tenders.models import Tender, TenderSiae, TenderStepsData
from lemarche.users.models import User
from lemarche.utils.data import get_choice
from lemarche.utils.mixins import (
Expand Down Expand Up @@ -148,6 +148,28 @@ def get_context_data(self, form, **kwargs):
context.update({"tender": tender_dict})
return context

def process_step(self, form):
"""
Save step data
"""
data = form.data.copy()
if "csrfmiddlewaretoken" in data:
del data["csrfmiddlewaretoken"]

uuid = self.request.session.get("tender_steps_data_uuid", None)
if uuid:
try:
tender_steps_data = TenderStepsData.objects.get(uuid=uuid)
tender_steps_data.steps_data.append(data)
tender_steps_data.save()
except TenderStepsData.DoesNotExist:
tender_steps_data = TenderStepsData.objects.create(uuid=uuid, steps_data=[data])
else:
tender_steps_data = TenderStepsData.objects.create(steps_data=[data])
self.request.session["tender_steps_data_uuid"] = tender_steps_data.uuid

return form.data

def save_instance_tender(self, tender_dict: dict, form_dict: dict, is_draft: bool):
tender_status = tender_constants.STATUS_DRAFT if is_draft else tender_constants.STATUS_PUBLISHED
tender_published_at = None if is_draft else timezone.now()
Expand Down

0 comments on commit f24a8b2

Please sign in to comment.