thumbnail and overview
This commit is contained in:
@@ -231,6 +231,29 @@ class QuestionBase(models.Model, AuthorMixin, QuestionMixin):
|
||||
|
||||
return False
|
||||
|
||||
def get_thumbnail(self, recreate=False, fail_loudly=False):
|
||||
images = self.images.all()
|
||||
|
||||
if len(images) < 1:
|
||||
return "No images", 0
|
||||
|
||||
img = findMiddle(images).image
|
||||
try:
|
||||
thumbnailer = get_thumbnailer(img)
|
||||
if recreate:
|
||||
thumbnailer.delete_thumbnails()
|
||||
thumbnail = thumbnailer["exam-list"]
|
||||
|
||||
except InvalidImageFormatError as e:
|
||||
if fail_loudly:
|
||||
raise e
|
||||
return format_html(
|
||||
'<img title="{}" src="/static/not-found-image.jpg" />', img
|
||||
), len(images)
|
||||
return format_html('<span style="border: 1px solid;"><img src="/media/{}" /></span>', thumbnail), len(images)
|
||||
|
||||
|
||||
|
||||
|
||||
class SeriesImageBase(models.Model):
|
||||
"""
|
||||
|
||||
@@ -265,6 +265,8 @@ def generic_view_urls(generic_views: GenericViewBase):
|
||||
path(
|
||||
"question/<int:pk>/", generic_views.question_detail, name="question_detail"
|
||||
),
|
||||
path("question/<int:pk>/thumbnail/fail", generic_views.question_thumbnail_fail, name="series_thumbnail_fail"),
|
||||
path("question/<int:pk>/thumbnail", generic_views.question_thumbnail, name="series_thumbnail"),
|
||||
path(
|
||||
"question/<int:pk>/user_answers",
|
||||
generic_views.question_user_answers,
|
||||
|
||||
@@ -2639,6 +2639,18 @@ class GenericViewBase:
|
||||
self.cid_user_answer_object.objects.get(pk=id).delete()
|
||||
return JsonResponse({"status": "success"})
|
||||
|
||||
@method_decorator(login_required)
|
||||
def question_thumbnail_fail(self, request, pk):
|
||||
return self.question_thumbnail(request, pk=pk, fail_loudly=True)
|
||||
|
||||
@method_decorator(login_required)
|
||||
def question_thumbnail(self, request, pk, fail_loudly=False):
|
||||
question = get_object_or_404(self.question_object, pk=pk)
|
||||
|
||||
thumbnail, _ = question.get_thumbnail(recreate=True, fail_loudly=fail_loudly)
|
||||
|
||||
return HttpResponse(thumbnail)
|
||||
|
||||
@method_decorator(login_required)
|
||||
def question_detail(self, request, pk):
|
||||
question: QuestionBase = get_object_or_404(self.question_object, pk=pk)
|
||||
|
||||
Reference in New Issue
Block a user