Refactor NormalCase model: remove 'added_by' field, add 'author' ManyToManyField; update related views and templates for editing functionality
This commit is contained in:
+50
-2
@@ -347,7 +347,7 @@ class NormalCaseList(LoginRequiredMixin, FilterView):
|
||||
paginate_by = 25
|
||||
|
||||
def get_queryset(self):
|
||||
qs = NormalCase.objects.select_related("case", "examination", "modality", "added_by")
|
||||
qs = NormalCase.objects.select_related("case", "examination", "modality")
|
||||
# Restrict similar to CaseFilter: non-editors only see authored or open_access
|
||||
if not self.request.user.groups.filter(name="atlas_editor").exists():
|
||||
qs = qs.filter((Q(case__author__id=self.request.user.id) | Q(case__open_access=True)))
|
||||
@@ -847,7 +847,7 @@ def create_case_normal(request, pk):
|
||||
logger.debug("NormalCaseForm.cleaned_data: {}", form.cleaned_data)
|
||||
nc = form.save(commit=False)
|
||||
nc.case = case
|
||||
nc.added_by = request.user
|
||||
nc.author.add(request.user)
|
||||
nc.save()
|
||||
|
||||
# Ensure the case reflects the newly created NormalCase when rendering
|
||||
@@ -874,6 +874,54 @@ def create_case_normal(request, pk):
|
||||
html = render_to_string("atlas/partials/_normal_form.html", {"form": form, "case": case}, request=request)
|
||||
return HttpResponse(html, status=400)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(["GET", "POST"])
|
||||
def case_normal_edit(request, pk):
|
||||
"""Edit an existing NormalCase. Accessible to NormalCase authors and atlas editors."""
|
||||
normal = get_object_or_404(NormalCase, pk=pk)
|
||||
|
||||
# Permission: allow NormalCase authors or atlas editors/superusers
|
||||
is_atlas_editor = request.user.is_superuser or request.user.groups.filter(name="atlas_editor").exists()
|
||||
is_normal_author = normal.author.filter(id=request.user.id).exists()
|
||||
if not (is_atlas_editor or is_normal_author):
|
||||
return HttpResponse(status=403)
|
||||
|
||||
from .forms import NormalCaseForm
|
||||
|
||||
if request.method == "POST":
|
||||
form = NormalCaseForm(request.POST, instance=normal)
|
||||
if form.is_valid():
|
||||
nc = form.save(commit=False)
|
||||
nc.save()
|
||||
form.save_m2m()
|
||||
|
||||
# Ensure the case reflects any updates
|
||||
try:
|
||||
if hasattr(nc.case, "normal_case"):
|
||||
delattr(nc.case, "normal_case")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
case = get_case_for_case_detail(nc.case.pk)
|
||||
is_case_author = case.author.filter(id=request.user.id).exists()
|
||||
can_edit = case.check_user_can_edit(request.user)
|
||||
toggle_html = render_to_string(
|
||||
"atlas/partials/_normal_toggle.html",
|
||||
{"case": case, "user": request.user, "is_atlas_editor": is_atlas_editor, "is_case_author": is_case_author, "can_edit": can_edit},
|
||||
request=request,
|
||||
)
|
||||
toggle_html += "<script>var m = bootstrap.Modal.getOrCreateInstance(document.getElementById('normal-modal-{}')); if(m) m.hide();</script>".format(case.pk)
|
||||
return HttpResponse(toggle_html)
|
||||
else:
|
||||
html = render_to_string("atlas/partials/_normal_form.html", {"form": form, "case": normal.case, "normal": normal}, request=request)
|
||||
return HttpResponse(html, status=400)
|
||||
|
||||
# GET - render form in modal
|
||||
form = NormalCaseForm(instance=normal)
|
||||
html = render_to_string("atlas/partials/_normal_form.html", {"form": form, "case": normal.case, "normal": normal}, request=request)
|
||||
return HttpResponse(html)
|
||||
|
||||
@login_required
|
||||
@user_is_author_or_atlas_series_checker_or_atlas_marker_or_open_access
|
||||
def series_thumbnail_fail(request, pk):
|
||||
|
||||
Reference in New Issue
Block a user