diff --git a/atlas/forms.py b/atlas/forms.py index 0f127ac1..19c83027 100755 --- a/atlas/forms.py +++ b/atlas/forms.py @@ -13,6 +13,7 @@ from django.forms import inlineformset_factory from atlas.models import ( Case, + CaseCollection, Differential, Finding, Series, @@ -50,6 +51,26 @@ class ConditionForm(ModelForm): ), } +class CaseCollectionForm(ModelForm): + class Meta: + model = CaseCollection + exclude = ["cases", "author"] + + def __init__(self, *args, **kwargs): + self.user = kwargs.pop( + "user" + ) # To get request.user. Do not use kwargs.pop('user', None) due to potential security hole + if kwargs.get("instance"): + # We get the 'initial' keyword argument or initialize it + # as a dict if it didn't exist. + initial = kwargs.setdefault("initial", {}) + # The widget for a ModelMultipleChoiceField expects + # a list of primary key for the selected data. + # initial["exams"] = [t.pk for t in kwargs["instance"].exams.all()] + + ModelForm.__init__(self, *args, **kwargs) + + super(CaseCollectionForm, self).__init__(*args, **kwargs) class FindingForm(ModelForm): class Meta: @@ -153,7 +174,7 @@ class SeriesForm(ModelForm): else: case_queryset = Case.objects.filter( author__id=self.user.id - ) #| Case.objects.filter(open_access=True) + ) # | Case.objects.filter(open_access=True) self.fields["case"] = ModelMultipleChoiceField( required=False, @@ -226,7 +247,6 @@ class CaseForm(ModelForm): ModelForm.__init__(self, *args, **kwargs) super(CaseForm, self).__init__(*args, **kwargs) - print(1) # self.fields["series"].queryset = @@ -313,25 +333,60 @@ CaseDifferentialFormSet = inlineformset_factory( }, ) + class BaseSeriesFormSet(BaseInlineFormSet): - def __init__(self, *args, **kwargs): - super(BaseSeriesFormSet, self).__init__(*args, **kwargs) + def __init__(self, *args, **kwargs): - self.queryset = Series.case.through.objects.filter(series__author__id=99) + super(BaseSeriesFormSet, self).__init__(*args, **kwargs) - #for form in self.forms: - # form.fields['series'].queryset = series_queryset + self.queryset = Series.case.through.objects.filter(series__author__id=99) + + # for form in self.forms: + # form.fields['series'].queryset = series_queryset + + +class CaseSeriesForm(ModelForm): + def __init__(self, *args, user, **kwargs): + super(CaseSeriesForm, self).__init__(*args, **kwargs) + + self.fields["series"] = ModelChoiceField( + required=False, + queryset=Series.objects.filter(author__id=user.id), + # widget=Select(verbose_name="Series", is_stacked=False), + ) + +class CaseCollectionCaseForm(ModelForm): + def __init__(self, *args, user, **kwargs): + super(CaseCollectionCaseForm, self).__init__(*args, **kwargs) + + self.fields["case"] = ModelChoiceField( + required=False, + queryset=Case.objects.filter(author__id=user.id), + # widget=Select(verbose_name="Series", is_stacked=False), + ) SeriesFormSet = inlineformset_factory( Case, Series.case.through, - formset=BaseSeriesFormSet, + form=CaseSeriesForm, + # formset=BaseSeriesFormSet, exclude=[], can_delete=True, - extra=1, + extra=0, max_num=10, ) +CaseCollectionCaseFormSet = inlineformset_factory( + CaseCollection, + Case.casecollection_set.through, + form=CaseCollectionCaseForm, + # formset=BaseSeriesFormSet, + exclude=[], + can_delete=True, + extra=0, + max_num=50, +) + SeriesImageFormSet = inlineformset_factory( Series, diff --git a/atlas/migrations/0028_casecollection_author.py b/atlas/migrations/0028_casecollection_author.py new file mode 100644 index 00000000..1405097e --- /dev/null +++ b/atlas/migrations/0028_casecollection_author.py @@ -0,0 +1,20 @@ +# Generated by Django 3.2.10 on 2022-03-29 20:53 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('atlas', '0027_auto_20220325_2138'), + ] + + operations = [ + migrations.AddField( + model_name='casecollection', + name='author', + field=models.ManyToManyField(blank=True, help_text='Author of the collection', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/atlas/migrations/0029_casecollection_show_report.py b/atlas/migrations/0029_casecollection_show_report.py new file mode 100644 index 00000000..3c716f14 --- /dev/null +++ b/atlas/migrations/0029_casecollection_show_report.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.10 on 2022-03-29 20:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('atlas', '0028_casecollection_author'), + ] + + operations = [ + migrations.AddField( + model_name='casecollection', + name='show_report', + field=models.BooleanField(default=False, help_text='Show the case report'), + ), + ] diff --git a/atlas/models.py b/atlas/models.py index f4e12a21..9caf3de4 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -572,6 +572,14 @@ class CaseCollection(models.Model): show_title = models.BooleanField(default=False, help_text="Show the title of the cases") show_description = models.BooleanField(default=False, help_text="Show the description of the cases") show_discussion = models.BooleanField(default=False, help_text="Show the case discussion") + show_report = models.BooleanField(default=False, help_text="Show the case report") + + author = models.ManyToManyField( + settings.AUTH_USER_MODEL, + blank=True, + help_text="Author of the collection", + #related_name="atlas_authored_cases", + ) def get_absolute_url(self): return reverse("atlas:collection_detail_view", kwargs={"pk": self.pk}) diff --git a/atlas/templates/atlas/base.html b/atlas/templates/atlas/base.html index 9167abee..a2f7d33e 100755 --- a/atlas/templates/atlas/base.html +++ b/atlas/templates/atlas/base.html @@ -19,8 +19,9 @@ Atlas: {% if request.user.is_authenticated %} Cases / Series / +Collections / Categories / -Create Case / +Create Case / Create Series {% endif %} {% comment %}
diff --git a/atlas/templates/atlas/casecollection_form.html b/atlas/templates/atlas/casecollection_form.html new file mode 100755 index 00000000..08432abd --- /dev/null +++ b/atlas/templates/atlas/casecollection_form.html @@ -0,0 +1,54 @@ +{% extends "atlas/base.html" %} + + +{% block css %} +{% endblock %} +{% block js %} + +{{form.media}} + + + +{% endblock %} +{% block content %} +

Create Collection

+Use this form to create a collection of cases +
+ {% csrf_token %} + + + {{ form.as_table }} +
+

Cases:

+ Add cases here. These can only be added once created (they can also be added to cases on creation). + +
+ {% for form in case_formset %} + + {% endfor %} +
+ {{ case_formset.management_form }} + +
+ +{% endblock %} \ No newline at end of file diff --git a/atlas/templates/atlas/collection_case_view.html b/atlas/templates/atlas/collection_case_view.html index f623e033..50c2da29 100644 --- a/atlas/templates/atlas/collection_case_view.html +++ b/atlas/templates/atlas/collection_case_view.html @@ -43,6 +43,16 @@ {% endif %} +{% if collection.show_report and case.report%} +
+ + Report: + +
+ {{case.report}} +
+
+{% endif %}
{% if previous %} diff --git a/atlas/templates/atlas/collection_detail_view.html b/atlas/templates/atlas/collection_detail_view.html index 0b9f37a9..37d8f668 100644 --- a/atlas/templates/atlas/collection_detail_view.html +++ b/atlas/templates/atlas/collection_detail_view.html @@ -1,6 +1,11 @@ {% extends 'atlas/base.html' %} {% block content %} +{% if request.user.is_authenticated %} +
+ Edit +
+{% endif %}

{{collection.name}}