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
@@ -0,0 +1,18 @@
# Generated by Django 3.2.13 on 2022-07-05 20:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('atlas', '0045_rename_valid_users_casecollection_valid_cid_users'),
]
operations = [
migrations.AlterField(
model_name='casedetail',
name='sort_order',
field=models.IntegerField(default=1000),
),
]
+1 -1
View File
@@ -739,7 +739,7 @@ class CaseDetail(models.Model):
case = models.ForeignKey(Case, on_delete=models.CASCADE)
collection = models.ForeignKey(CaseCollection, on_delete=models.CASCADE)
sort_order = models.IntegerField()
sort_order = models.IntegerField(default=1000)
class Meta:
ordering = ("sort_order",)
@@ -111,6 +111,11 @@
{% endfor %}
</ul>
<b>Collections:</b>
<button
hx-get="{% url 'atlas:case_collection_form' case.pk %}"
hx-target="#collection-form">
Add collection</button>
<div id="collection-form"></div>
<ul>
{% for p in case.casecollection_set.all %}
<li>{{p.name}}</li>
+2
View File
@@ -119,6 +119,8 @@ urlpatterns = [
# path("unchecked/", views.unchecked_list, name="unchecked_list"),
# path("verified/<int:pk>/", views.verified_detail, name="verified_detail"),
path("case/<int:pk>/", views.case_detail, name="case_detail"),
#path("case/<int:pk>/collection-form", views.AddCollectionToCaseView.as_view(), name="case_collection_form"),
path("case/<int:case_id>/collection-form", views.add_collection_to_case_form, name="case_collection_form"),
path("case/<int:pk>/clone", views.AtlasClone.as_view(), name="case_clone"),
# path("verified/", views.verified, name="verified"),
# path("all_questions/", views.all_questions, name="all_questions"),
+35 -1
View File
@@ -18,7 +18,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMix
from django.views.generic.detail import DetailView
from generic.mixins import SuperuserRequiredMixin
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.edit import CreateView, UpdateView, DeleteView, FormView
from django.views.generic import ListView
from django.views.decorators.csrf import csrf_exempt
@@ -30,6 +30,7 @@ from django.http import HttpResponseRedirect, HttpResponse
from generic.models import CidUser
from .forms import (
AddCollectionToCaseForm,
CaseCollectionCaseFormSet,
CaseCollectionForm,
CaseForm,
@@ -714,6 +715,39 @@ class AtlasUpdate(
else:
return super().form_invalid(form)
def add_collection_to_case_form(request, case_id):
if request.method == 'POST':
form = AddCollectionToCaseForm(request.POST, user=request.user)
if form.is_valid():
case = get_object_or_404(Case, pk=case_id)
case.casecollection_set.add(*form.cleaned_data["casecollection"])
#do_something_with_form_data(form.cleaned_data)
return HttpResponse("Done")
return render(request, 'web/htmx_contact_form_confirm.html')
else:
form = AddCollectionToCaseForm(user=request.user)
return render(request, 'generic_form.html', {
'form': form,
'target_url': reverse("atlas:case_collection_form", args=(case_id,))
})
#class AddCollectionToCaseView(FormView):
# form_class = AddCollectionToCaseForm
# template_name = "generic_form.html"
#
# def form_valid(self, form):
# form.save()
# return super(AddCollectionToCaseView, self).form_valid(form)
#
# def get_form_kwargs(self):
# kwargs = super(AddCollectionToCaseView, self).get_form_kwargs()
# kwargs.update({"user": self.request.user})
# return kwargs
#
# def get_success_url(self) -> str:
# pk = self.kwargs["pk"]
# return reverse("atlas:case_detail", kwargs={"pk": pk})
class AtlasClone(AtlasCreateBase, CreateView):
"""Clones a existing atlas"""