This commit is contained in:
Ross
2021-02-05 17:12:13 +00:00
parent f1ebc48d9c
commit 1420d74393
9 changed files with 210 additions and 303 deletions
+70 -1
View File
@@ -64,7 +64,7 @@ class AuthorOrCheckerRequiredMixin(object):
obj = super(UpdateView, self).get_object(*args, **kwargs)
if self.request.user.groups.filter(name="long_checker").exists():
return obj
if self.request.user not in obj.author.all():
if self.request.user not in obj.get_author_objects():
raise PermissionDenied() # or Http404
return obj
@@ -100,6 +100,11 @@ def long_detail(request, pk):
def long_series_detail(request, pk):
series = get_object_or_404(LongSeries, pk=pk)
# if request.user not in long.author.all():
# raise PermissionDenied
# logging.debug(long.long_notes.first())
# logging.debug(long.subspecialty.first().name.all())
return render(request, "longs/long_series.html", {"series": series})
@login_required
@@ -262,8 +267,19 @@ class LongSeriesCreate(LoginRequiredMixin, CreateView):
model = LongSeries
form_class = LongSeriesForm
def get_initial(self):
# print(self.request)
if "pk" in self.kwargs:
initial = super().get_initial()
long = get_object_or_404(Long, pk=self.kwargs["pk"])
initial['long'] = long.id
return initial
def get_context_data(self, **kwargs):
context = super(LongSeriesCreate, self).get_context_data(**kwargs)
if self.request.POST:
context["image_formset"] = LongSeriesImageFormSet(
self.request.POST, self.request.FILES
@@ -291,6 +307,46 @@ class LongSeriesCreate(LoginRequiredMixin, CreateView):
else:
return super().form_invalid(form)
class LongSeriesUpdate(LoginRequiredMixin, AuthorOrCheckerRequiredMixin, UpdateView):
model = LongSeries
form_class = LongSeriesForm
# fields = '__all__'
# #fields = [ 'condition' ]
# #initial = {'date_of_death': '05/01/2018'}
# exclude = [ 'created_date', 'published_date' ]
def get_context_data(self, **kwargs):
context = super(LongSeriesUpdate, self).get_context_data(**kwargs)
if self.request.POST:
context["image_formset"] = LongSeriesImageFormSet(
self.request.POST, self.request.FILES, instance=self.object
)
context["image_formset"].full_clean()
else:
context["image_formset"] = LongSeriesImageFormSet(instance=self.object)
return context
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.save()
# Do this in the form.save()
#new_exams = list(form.cleaned_data['exams'].values_list("pk", flat=True))
#self.object.exams.clear()
#self.object.exams.add(*new_exams)
context = self.get_context_data(form=form)
image_formset = context["image_formset"]
# logger.debug(formset.is_valid())
if image_formset.is_valid():
response = super().form_valid(form)
image_formset.instance = self.object
image_formset.save()
return response
else:
return super().form_invalid(form)
class LongCreateBase(LoginRequiredMixin, CreateView):
model = Long
form_class = LongForm
@@ -309,8 +365,16 @@ class LongCreateBase(LoginRequiredMixin, CreateView):
def form_valid(self, form):
self.object = form.save(commit=False)
# Add exam objects
self.object.save()
# Do this in the form.save()
#new_exams = list(form.cleaned_data['exams'].values_list("pk", flat=True))
#self.object.exams.clear()
#self.object.exams.add(*new_exams)
form.instance.author.add(self.request.user.id)
context = self.get_context_data(form=form)
@@ -377,6 +441,11 @@ class LongUpdate(LoginRequiredMixin, AuthorOrCheckerRequiredMixin, UpdateView):
self.object = form.save(commit=False)
self.object.save()
# Do this in the form.save()
#new_exams = list(form.cleaned_data['exams'].values_list("pk", flat=True))
#self.object.exams.clear()
#self.object.exams.add(*new_exams)
form.instance.author.add(self.request.user.id)
context = self.get_context_data(form=form)