This commit is contained in:
Ross
2022-07-05 22:29:44 +01:00
parent 41991f1ebd
commit 74e99d54b1
12 changed files with 210 additions and 70 deletions
+98 -8
View File
@@ -7,9 +7,11 @@ from django.forms import (
ModelChoiceField,
ChoiceField,
CharField,
ValidationError,
modelformset_factory,
)
from django.forms import inlineformset_factory
from django.shortcuts import get_object_or_404
from atlas.models import (
Case,
@@ -52,6 +54,7 @@ class ConditionForm(ModelForm):
),
}
class CaseCollectionForm(ModelForm):
class Meta:
model = CaseCollection
@@ -73,6 +76,7 @@ class CaseCollectionForm(ModelForm):
super(CaseCollectionForm, self).__init__(*args, **kwargs)
class FindingForm(ModelForm):
class Meta:
model = Finding
@@ -204,7 +208,7 @@ class SeriesForm(ModelForm):
instance.save()
self.save_m2m()
#instance.anonymise_images()
# instance.anonymise_images()
return instance
@@ -337,8 +341,6 @@ CaseDifferentialFormSet = inlineformset_factory(
)
class CaseSeriesForm(ModelForm):
def __init__(self, *args, user, **kwargs):
super(CaseSeriesForm, self).__init__(*args, **kwargs)
@@ -354,10 +356,11 @@ class CaseSeriesForm(ModelForm):
# widget=Select(verbose_name="Series", is_stacked=False),
)
class CaseCollectionCaseForm(ModelForm):
def __init__(self, *args, user, **kwargs):
super(CaseCollectionCaseForm, self).__init__(*args, **kwargs)
if not user.groups.filter(name="atlas_editor").exists():
queryset = Case.objects.filter(author__id=user.id)
else:
@@ -369,6 +372,7 @@ class CaseCollectionCaseForm(ModelForm):
# widget=Select(verbose_name="Series", is_stacked=False),
)
SeriesFormSet = inlineformset_factory(
Case,
Series.case.through,
@@ -399,6 +403,7 @@ SeriesImageFormSet = inlineformset_factory(
max_num=2000,
)
class CidReportAnswerForm(ModelForm):
class Meta:
model = CidReportAnswer
@@ -413,13 +418,14 @@ class CidReportAnswerForm(ModelForm):
"answer": Textarea(attrs={"cols": 100, "rows": 5}),
}
#help_texts = {
# help_texts = {
# "answer": "Write your answer in here."
#}
# }
def __init__(self, *args, **kwargs):
super(CidReportAnswerForm, self).__init__(*args, **kwargs)
self.fields['answer'].required = False
self.fields["answer"].required = False
class CidReportAnswerMarkForm(ModelForm):
class Meta:
@@ -437,4 +443,88 @@ class CidReportAnswerMarkForm(ModelForm):
def __init__(self, *args, **kwargs):
super(CidReportAnswerMarkForm, self).__init__(*args, **kwargs)
self.fields['score'].required = False
self.fields["score"].required = False
#class AddCollectionToCaseForm(Form):
#
# def __init__(self, *args, user, **kwargs):
# super(AddCollectionToCaseForm, self).__init__(*args, **kwargs)
# print("INIT", args, kwargs)
#
# if not user.groups.filter(name="atlas_editor").exists():
# queryset = CaseCollection.objects.filter(author__id=user.id)
# else:
# queryset = CaseCollection.objects.all()
#
# self.valid_collections = queryset
# self.case = get_object_or_404(Case, pk=pk)
#
# self.fields["casecollection"] = ModelMultipleChoiceField(
# required=False,
# queryset=queryset,
# # widget=Select(verbose_name="Series", is_stacked=False),
# )
#
# def save(self, *args, **kwargs):
# print("SAVE", args, kwargs)
# instance = super(AddCollectionToCaseForm, self).save(*args, **kwargs)
#
# # Here we save the modified project selection back into the database
# instance.casecollection_set.set(self.cleaned_data['casecollection'])
#
# print(f"{instance=}")
#
# return instance
#
# def clean_casecollection(self):
# collection_set = self.cleaned_data["casecollection"]
#
# print(f"{collection_set=}")
#
# for col in collection_set:
# if col not in self.valid_collections:
# raise ValidationError("Invalid collection")
#
# return collection_set
class AddCollectionToCaseForm(Form):
def __init__(self, *args, user, **kwargs):
super(AddCollectionToCaseForm, self).__init__(*args, **kwargs)
print("INIT", args, kwargs)
if not user.groups.filter(name="atlas_editor").exists():
queryset = CaseCollection.objects.filter(author__id=user.id)
else:
queryset = CaseCollection.objects.all()
self.valid_collections = queryset
self.fields["casecollection"] = ModelMultipleChoiceField(
required=False,
queryset=queryset,
# widget=Select(verbose_name="Series", is_stacked=False),
)
def save(self, *args, **kwargs):
print("SAVE", args, kwargs)
instance = super(AddCollectionToCaseForm, self).save(*args, **kwargs)
# Here we save the modified project selection back into the database
instance.casecollection_set.set(self.cleaned_data['casecollection'])
print(f"{instance=}")
return instance
def clean_casecollection(self):
collection_set = self.cleaned_data["casecollection"]
print(f"{collection_set=}")
for col in collection_set:
if col not in self.valid_collections:
raise ValidationError("Invalid collection")
return collection_set