Skip to content

Commit

Permalink
Merge pull request #826 from intuitem/feat/attach-folder-to-assessment
Browse files Browse the repository at this point in the history
Feat/attach folder to assessment
  • Loading branch information
Mohamed-Hacene authored Sep 13, 2024
2 parents 4376f55 + 4c5877b commit d3c69ef
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Generated by Django 5.1.1 on 2024-09-12 15:00

import django.db.models.deletion
import iam.models
from django.db import migrations, models


def set_assessment_folder(apps, schema_editor):
RiskAssessment = apps.get_model("core", "RiskAssessment")
ComplianceAssessment = apps.get_model("core", "ComplianceAssessment")
for risk_assessment in RiskAssessment.objects.all():
risk_assessment.folder = risk_assessment.project.folder
risk_assessment.save()
for compliance_assessment in ComplianceAssessment.objects.all():
compliance_assessment.folder = compliance_assessment.project.folder
compliance_assessment.save()


class Migration(migrations.Migration):
dependencies = [
("core", "0024_appliedcontrol_owner"),
("iam", "0006_alter_role_folder_alter_roleassignment_folder_and_more"),
]

operations = [
migrations.AddField(
model_name="complianceassessment",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AddField(
model_name="riskassessment",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="appliedcontrol",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="asset",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="evidence",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="framework",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="loadedlibrary",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="project",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="referencecontrol",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="requirementassessment",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="requirementmappingset",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="requirementnode",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="riskacceptance",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="riskmatrix",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="storedlibrary",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="threat",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.RunPython(set_assessment_folder),
]
9 changes: 7 additions & 2 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from django.utils.html import format_html
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _
from iam.models import FolderMixin, PublishInRootFolderMixin
from iam.models import Folder, FolderMixin, PublishInRootFolderMixin
from library.helpers import update_translations, update_translations_in_object
from structlog import get_logger

Expand Down Expand Up @@ -1364,7 +1364,7 @@ def save(self, *args, **kwargs):
########################### Secondary objects #########################


class Assessment(NameDescriptionMixin, ETADueDateMixin):
class Assessment(NameDescriptionMixin, ETADueDateMixin, FolderMixin):
class Status(models.TextChoices):
PLANNED = "planned", _("Planned")
IN_PROGRESS = "in_progress", _("In progress")
Expand Down Expand Up @@ -1409,6 +1409,11 @@ class Status(models.TextChoices):
class Meta:
abstract = True

def save(self, *args, **kwargs) -> None:
if not self.folder or self.folder == Folder.get_root_folder():
self.folder = self.project.folder
return super().save(*args, **kwargs)


class RiskAssessment(Assessment):
risk_matrix = models.ForeignKey(
Expand Down
2 changes: 2 additions & 0 deletions backend/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class Meta:
class RiskAssessmentReadSerializer(AssessmentReadSerializer):
str = serializers.CharField(source="__str__")
project = FieldsRelatedField(["id", "folder"])
folder = FieldsRelatedField()
risk_scenarios = FieldsRelatedField(many=True)
risk_scenarios_count = serializers.IntegerField(source="risk_scenarios.count")
risk_matrix = FieldsRelatedField()
Expand Down Expand Up @@ -535,6 +536,7 @@ class Meta:

class ComplianceAssessmentReadSerializer(AssessmentReadSerializer):
project = FieldsRelatedField(["id", "folder"])
folder = FieldsRelatedField()
framework = FieldsRelatedField(
["id", "min_score", "max_score", "implementation_groups_definition", "ref_id"]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.1.1 on 2024-09-12 15:00

import django.db.models.deletion
import iam.models
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("global_settings", "0001_initial"),
("iam", "0006_alter_role_folder_alter_roleassignment_folder_and_more"),
]

operations = [
migrations.AlterField(
model_name="globalsettings",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 5.1.1 on 2024-09-12 15:00

import django.db.models.deletion
import iam.models
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("iam", "0005_alter_user_managers"),
]

operations = [
migrations.AlterField(
model_name="role",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="roleassignment",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="user",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
migrations.AlterField(
model_name="usergroup",
name="folder",
field=models.ForeignKey(
default=iam.models.Folder.get_root_folder_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(class)s_folder",
to="iam.folder",
),
),
]
15 changes: 10 additions & 5 deletions backend/iam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def get_root_folder() -> Self:
"""class function for general use"""
return _get_root_folder()

@staticmethod
def get_root_folder_id() -> uuid.UUID:
"""class function for general use"""
try:
return uuid.UUID(_get_root_folder().id)
except:
return _get_root_folder()

class ContentType(models.TextChoices):
"""content type for a folder"""

Expand Down Expand Up @@ -173,7 +181,7 @@ class FolderMixin(models.Model):
Folder,
on_delete=models.CASCADE,
related_name="%(class)s_folder",
default=Folder.get_root_folder,
default=Folder.get_root_folder_id,
)

class Meta:
Expand Down Expand Up @@ -382,10 +390,7 @@ def get_full_name(self) -> str:

def get_short_name(self) -> str:
"""get user's short name (i.e. first_name or email before @))"""
try:
return self.first_name if self.first_name else self.email.split("@")[0]
except:
return ""
return self.first_name if self.first_name else self.email.split("@")[0]

def mailing(self, email_template_name, subject, pk=False):
"""
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib/utils/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export const URL_MODEL_MAP: ModelMap = {
verboseName: 'Risk assessment',
verboseNamePlural: 'Risk assessments',
foreignKeyFields: [
{ field: 'folder', urlModel: 'folders', urlParams: 'content_type=DO' },
{ field: 'project', urlModel: 'projects' },
{ field: 'authors', urlModel: 'users' },
{ field: 'reviewers', urlModel: 'users' },
Expand Down Expand Up @@ -387,6 +388,7 @@ export const URL_MODEL_MAP: ModelMap = {
verboseName: 'Compliance assessment',
verboseNamePlural: 'Compliance assessments',
foreignKeyFields: [
{ field: 'folder', urlModel: 'folders', urlParams: 'content_type=DO' },
{ field: 'project', urlModel: 'projects' },
{ field: 'framework', urlModel: 'frameworks' },
{ field: 'authors', urlModel: 'users' },
Expand Down

0 comments on commit d3c69ef

Please sign in to comment.