Skip to content

Commit

Permalink
refactor: cleanup some views (#2011)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed May 29, 2024
1 parent 7e4e44e commit 5888411
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 53 deletions.
20 changes: 20 additions & 0 deletions questions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from core.tables import DEFAULT_ATTRS, DEFAULT_TEMPLATE, ChoiceColumn, ImageColumn, RichTextEllipsisColumn
from questions.models import Question
from quizs.models import Quiz, QuizQuestion


QUESTION_FIELD_SEQUENCE = [
Expand Down Expand Up @@ -46,3 +47,22 @@ def __init__(self, *args, **kwargs):
self.base_columns[field_name] = tables.DateTimeColumn(format="d F Y")
# attrs={"td": {"title": lambda record: getattr(record, field_name)}})
super().__init__(*args, **kwargs)


QUIZ_QUESTION_FIELD_SEQUENCE = ["quiz_id", "quiz_name"]


class QuestionQuizTable(tables.Table):
quiz_id = tables.Column(
verbose_name=Quiz._meta.verbose_name,
accessor="quiz.id",
linkify=lambda record: record.quiz.get_absolute_url(),
)
quiz_name = RichTextEllipsisColumn(accessor="quiz.name", attrs={"td": {"title": lambda record: record.quiz.name}})

class Meta:
model = QuizQuestion
fields = QUIZ_QUESTION_FIELD_SEQUENCE
sequence = QUIZ_QUESTION_FIELD_SEQUENCE
template_name = DEFAULT_TEMPLATE
attrs = DEFAULT_ATTRS
2 changes: 1 addition & 1 deletion templates/admin/contributor_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">
<div class="alert alert-primary" role="alert">
📨 Un <strong>e-mail de bienvenue</strong> sera envoyé automatiquement à l'utilisateur, pour le guider dans ses premiers pas de contributeurs.
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/contributions/detail_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">
{% if comment.question %}
<div class="alert alert-primary" role="alert">
{% blocktrans %}Comment linked to the question:{% endblocktrans %} <a href="{% url 'questions:detail_view' comment.question.id %}" target="_blank">{{ comment.question }}</a>
Expand Down
31 changes: 21 additions & 10 deletions templates/contributions/detail_reply_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load i18n django_bootstrap5 user_can_edit %}

{% block comment_detail_content %}
<!-- Initial comment -->
<div class="row">
<div class="col-12">
<div class="card">
Expand Down Expand Up @@ -29,9 +30,10 @@
<br />
<br />

<div class="row">
<div class="col-12">
{% for reply in comment.replies.all %}
<!-- Existing notes & replies -->
{% for reply in comment.replies.all %}
<div class="row">
<div class="col-12">
<div class="card {% if reply.type == 'REPLY' %}border-success{% else %}border-warning{% endif %}">
<div class="card-header">
<div class="row">
Expand Down Expand Up @@ -63,26 +65,35 @@
{% endif %}
</div>
</div>
<br />
{% endfor %}
</div>
</div>

<br />
{% endfor %}

<!-- Reply form -->
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<strong>{% translate "Reply to the initial comment, or add a note" %}</strong>
</div>
<div class="card-body">
<div class="alert alert-warning" role="status">
{% blocktrans %}<strong>A note?</strong> It would help keep track of how the initial comment is processed{% endblocktrans %}.
<br />
{% blocktrans %}<strong>A reply?</strong> Only one reply possible. For now this content is only visible to contributors, but we will eventually publish comments with their respective replies (to display more information under a question){% endblocktrans %}.
</div>
<form id="comment_reply_create_form" method="POST" action="">
{% csrf_token %}

<div class="row">
<div class="col-md-8">
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 order-first order-md-last">
<div class="alert alert-warning" role="status">
{% blocktrans %}<strong>A note?</strong> It would help keep track of how the initial comment is processed{% endblocktrans %}.
<br />
{% blocktrans %}<strong>A reply?</strong> Only one reply possible. For now this content is only visible to contributors, but we will eventually publish comments with their respective replies (to display more information under a question){% endblocktrans %}.
</div>
</div>
</div>

<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion templates/profile/info_card_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">

</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/profile/info_card_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">

</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/profile/info_card_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>
{% endif %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">
<div class="alert alert-primary" role="alert">
ℹ️ {% blocktrans %}Some information from your author card will be displayed alongside your quiz introduction{% endblocktrans %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/questions/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">
<div class="alert alert-primary" role="alert">
🖼️ {% blocktrans %}You can add an <strong>image</strong> at the end of the form{% endblocktrans %}&nbsp;👇
</div>
Expand Down
7 changes: 6 additions & 1 deletion templates/questions/detail_comments.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{% extends "questions/detail_base.html" %}
{% load render_table from django_tables2 %}
{% load i18n %}

{% block question_detail_content %}
<div class="row">
<div class="col-12">
{% render_table table %}
{% if question_contributions.count %}
{% render_table table %}
{% else %}
<div class="alert alert-warning" role="status">0 {% translate "Comments" %}</div>
{% endif %}
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion templates/questions/detail_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">
{% if question.author == user %}
<div class="alert alert-primary" role="alert">
ℹ️ {% blocktrans %}You are the <strong>author</strong> of this question{% endblocktrans %}.
Expand Down
26 changes: 6 additions & 20 deletions templates/questions/detail_quizs.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
{% extends "questions/detail_base.html" %}
{% load render_table from django_tables2 %}
{% load i18n %}

{% block question_detail_content %}
<div class="row">
<div class="col-12">
<div class="table-container">
<table class="table-responsive table-striped table-bordered border-primary font-size-small">
<thead>
<tr>
<th>ID</th>
<th>{% translate "Quiz" %}</th>
</tr>
</thead>
<tbody>
{% for quiz_question in quiz_questions %}
<tr>
<td>
<a href="{% url 'quizs:detail_view' quiz_question.quiz.id %}">{{ quiz_question.quiz.id }}</a>
</td>
<td>{{ quiz_question.quiz.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% if question_quizs.count %}
{% render_table table %}
{% else %}
<div class="alert alert-warning" role="status">0 {% translate "Quizs" %}</div>
{% endif %}
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion templates/quizs/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">
<div class="alert alert-primary" role="alert">
🖼️ {% blocktrans %}You can add an <strong>image</strong> at the end of the form{% endblocktrans %}&nbsp;👇
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/quizs/detail_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{% bootstrap_form form alert_error_type="all" %}
</div>
<!-- Sidebar -->
<div class="col-md-4 d-none d-md-block">
<div class="col-md-4 order-first order-md-last">
{% if user in quiz.authors.all %}
<div class="alert alert-primary" role="alert">
ℹ️ {% blocktrans %}You are the <strong>author</strong> of this quiz{% endblocktrans %}.
Expand Down
6 changes: 5 additions & 1 deletion templates/quizs/detail_questions.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
{% block quiz_detail_content %}
<div class="row">
<div class="col-12">
{% render_table table %}
{% if quiz_questions.count %}
{% render_table table %}
{% else %}
<div class="alert alert-warning" role="status">0 {% translate "Questions" %}</div>
{% endif %}
</div>
</div>

Expand Down
20 changes: 12 additions & 8 deletions www/questions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from questions.filters import QuestionFilter
from questions.forms import QUESTION_FORM_FIELDS, QuestionCreateForm, QuestionEditForm
from questions.models import Question
from questions.tables import QuestionTable
from questions.tables import QuestionQuizTable, QuestionTable
from quizs.models import QuizQuestion
from stats.models import QuestionAggStat
from users import constants as user_constants
Expand Down Expand Up @@ -124,19 +124,23 @@ def get_success_url(self):
return reverse_lazy("questions:detail_view", args=[self.kwargs.get("pk")])


class QuestionDetailQuizListView(ContributorUserRequiredMixin, ListView):
class QuestionDetailQuizListView(ContributorUserRequiredMixin, SingleTableMixin, ListView):
model = QuizQuestion
template_name = "questions/detail_quizs.html"
context_object_name = "quiz_questions"
context_object_name = "question_quizs"
table_class = QuestionQuizTable

def get_queryset(self):
qs = super().get_queryset()
qs = qs.filter(question__id=self.kwargs.get("pk"))
return qs
def get(self, request, *args, **kwargs):
self.question = Question.objects.get(id=self.kwargs.get("pk"))
return super().get(request, *args, **kwargs)

def get_table_data(self):
return self.question.quizquestion_set.all()

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["question"] = Question.objects.get(id=self.kwargs.get("pk"))
context["question"] = self.question
context["question_quizs"] = self.question.quizquestion_set.all()
return context


Expand Down
7 changes: 3 additions & 4 deletions www/quizs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ def get(self, request, *args, **kwargs):
self.quiz = Quiz.objects.get(id=self.kwargs.get("pk"))
return super().get(request, *args, **kwargs)

def get_table_data(self):
return self.quiz.quizquestion_set.all()

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["quiz"] = self.quiz
Expand All @@ -153,10 +156,6 @@ def get_context_data(self, **kwargs):
context["quiz_question_formset"] = QuizQuestionFormSet(instance=self.quiz)
return context

def get_table_data(self):
return self.quiz.quizquestion_set.all()
# return self.get_context_data()["quiz"].quizquestion_set.all()

def post(self, request, *args, **kwargs):
self.quiz = Quiz.objects.get(id=self.kwargs.get("pk"))
quiz_question_formset = QuizQuestionFormSet(self.request.POST, instance=self.quiz)
Expand Down

0 comments on commit 5888411

Please sign in to comment.