This commit is contained in:
Ross
2024-10-07 17:46:09 +01:00
parent 7b938291ce
commit 28bfa34367
14 changed files with 95 additions and 8 deletions
+39
View File
@@ -1,5 +1,7 @@
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.views.generic.edit import UpdateView, CreateView
from django.views.generic.detail import DetailView
from .models import Entry, Formats
# Create your views here.
@@ -14,6 +16,10 @@ class EntryUpdateView(UpdateView):
template_name = "oef/entry_update_view.html"
fields = ['modality', 'exam_code', 'exam_name', 'questions', 'comments']
class EntryDetailView(DetailView):
model = Entry
template_name = "oef/entry_detail_view.html"
class FormatsCreateView(CreateView):
model = Formats
template_name_suffix = "_update_form"
@@ -47,3 +53,36 @@ def formats_apply(request, pk):
for entry in entries:
entry_list.append((entry, entry.questions.replace(search_text, "")))
return render(request, "oef/formats_apply.html", {"format": format, "entry_list": entry_list, "entries": entries})
def entry_detail_formats(request, pk):
entry = get_object_or_404(Entry, pk=pk)
formats = Formats.objects.all()
questions = entry.questions
matching_formats = []
for format in formats:
if format.get_format() in questions:
matching_formats.append(format)
matching_formats = sorted(matching_formats, key=lambda x: len(x.get_format()))
return render(request, "oef/entry_detail_formats.html", {"entry": entry, "matching_formats": matching_formats})
def replace_questions(request):
if request.method == "POST":
search = request.POST.get("search")
replace = request.POST.get("replace")
if not search or not replace:
return HttpResponse("Please provide both search and replace values")
entries = Entry.objects.filter(questions__contains=search)
for entry in entries:
entry.questions = entry.questions.replace(search, replace)
entry.save()
return HttpResponse(f"{entries.count()} questions replaced")
else:
return render(request, "oef/replace_questions.html", {})