change generic views to DetailViews

This commit is contained in:
2022-07-30 18:16:09 +00:00
parent 8fe31a5072
commit 54f9785a8b
4 changed files with 105 additions and 110 deletions

View File

@@ -4,11 +4,13 @@ from . import apps
from . import views from . import views
from .views import ( from .views import (
AttachmentCreateView, AttachmentCreateView,
AttachmentDetailView,
AttachmentUpdateView, AttachmentUpdateView,
EtherpadCreateView, EtherpadCreateView,
FileUploadCreateView, FileUploadCreateView,
TaskCreateView, TaskCreateView,
TopicCreateView, TopicCreateView,
TopicDetailView,
TopicUpdateView, TopicUpdateView,
) )
@@ -22,7 +24,7 @@ urlpatterns = [
TopicCreateView.as_view(), TopicCreateView.as_view(),
name="topic-create", name="topic-create",
), ),
path("<slug:slug>/", views.show_topic, name="topic"), path("<slug:slug>/", TopicDetailView.as_view(), name="topic"),
path( path(
"<slug:slug>/update/", "<slug:slug>/update/",
TopicUpdateView.as_view(), TopicUpdateView.as_view(),
@@ -34,7 +36,11 @@ urlpatterns = [
name="attachment-create", name="attachment-create",
), ),
path("<slug:slug>/create/", TaskCreateView.as_view(), name="task-create"), path("<slug:slug>/create/", TaskCreateView.as_view(), name="task-create"),
path("<slug:topic_slug>/<slug:slug>/", views.show_attachment, name="attachment"), path(
"<slug:topic_slug>/<slug:slug>/",
AttachmentDetailView.as_view(),
name="attachment",
),
path( path(
"<slug:topic_slug>/<slug:slug>/update/", "<slug:topic_slug>/<slug:slug>/update/",
AttachmentUpdateView.as_view(), AttachmentUpdateView.as_view(),

View File

@@ -6,6 +6,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import F, Q from django.db.models import F, Q
from django.http import HttpResponseRedirect, Http404 from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render from django.shortcuts import render
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse_lazy, reverse from django.urls import reverse_lazy, reverse
@@ -45,108 +46,6 @@ def index(request):
return render(request, "intern/index.html", context) return render(request, "intern/index.html", context)
@authenticated_user
def show_topic(request, slug=None):
active_topic = Topic.objects.filter(slug=slug).first()
if not active_topic:
raise Http404("wrong topic")
attachments = Attachment.objects.filter(topic__slug=slug).order_by("title")
tasks = None
if active_topic.task_list:
tasks = deque(
Task.taskmanager.get_tasks(
user=request.user.id,
completed=False,
task_list=active_topic.task_list,
all_tasks=True,
)
)
context = {
"active_topic": active_topic,
"attachments": attachments,
"tasks": tasks,
}
return render(request, "intern/topic.html", context)
@authenticated_user
def show_attachment(request, topic_slug=None, slug=None):
attachment = Attachment.objects.filter(
Q(topic__slug=topic_slug) & Q(slug=slug)
).first()
if not attachment:
raise Http404("wrong attachment")
active_topic = Topic.objects.filter(slug=topic_slug).first()
if request.method == "POST":
if "btn_etherpad" in request.POST:
form = EtherpadForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(request.path)
else:
for elem in list(form.errors.values()):
messages.info(request, "; ".join(elem))
if "btn_fileupload" in request.POST:
form = FileUploadForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect(request.path)
else:
for elem in list(form.errors.values()):
messages.info(request, "; ".join(elem))
initial = {
"attachment": attachment,
}
form_add_etherpad = EtherpadForm(initial=initial)
form_add_file = FileUploadForm(initial=initial)
files = FileUpload.objects.filter(attachment=attachment)
etherpads = Etherpad.objects.filter(attachment=attachment).order_by("-date")
etherpad_list = deque([])
# list of etherpad url-link of any etherpads
for elem in etherpads:
etherpad_list.append(
{
"title": elem.title,
"date": elem.date,
"etherpad_key": get_pad_link(elem.etherpad_key),
}
)
context = {
"form_add_etherpad": form_add_etherpad,
"form_add_file": form_add_file,
"active_topic": active_topic,
"attachment": attachment,
"etherpads": etherpad_list,
"files": files,
}
response = render(request, "intern/attachment.html", context)
try:
response = add_ep_cookie(request, response)
except Exception as e:
logger.info("Etherpad Server doesn't work. Error: %s", e)
return response
class TopicCreateView(LoginRequiredMixin, CreateView): class TopicCreateView(LoginRequiredMixin, CreateView):
model = Topic model = Topic
success_url = reverse_lazy("intern:index") success_url = reverse_lazy("intern:index")
@@ -170,6 +69,49 @@ class TopicCreateView(LoginRequiredMixin, CreateView):
self.slug = self.kwargs.get("topic_group") self.slug = self.kwargs.get("topic_group")
class TopicDetailView(LoginRequiredMixin, DetailView):
model = Topic
template_name = "intern/topic/topic_detail.html"
slug = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
self.slug = self.object.slug
active_topic = Topic.objects.get(id=self.object.id)
attachments = Attachment.objects.filter(topic__slug=self.slug).order_by("title")
print(self.request.user.id)
tasks = None
if active_topic.task_list:
tasks = deque(
Task.taskmanager.get_tasks(
user=None,
assigned_tasks=True,
task_list=active_topic.task_list,
completed=False,
)
)
context = {
"active_topic": active_topic,
"attachments": attachments,
"tasks": tasks,
}
return context
def get_success_url(self):
context = {
"slug": self.slug,
}
return reverse("intern:topic", kwargs=context)
class TopicUpdateView(LoginRequiredMixin, UpdateView): class TopicUpdateView(LoginRequiredMixin, UpdateView):
model = Topic model = Topic
template_name = "intern/topic/topic_update.html" template_name = "intern/topic/topic_update.html"
@@ -234,6 +176,49 @@ class AttachmentCreateView(LoginRequiredMixin, CreateView):
return reverse("intern:topic", kwargs=context) return reverse("intern:topic", kwargs=context)
class AttachmentDetailView(LoginRequiredMixin, DetailView):
model = Attachment
template_name = "intern/attachment/attachment_detail.html"
topic_slug = None
slug = None
def get(self, request, *args, **kwargs):
response = super().get(request, *args, **kwargs)
try:
response = add_ep_cookie(self.request, response)
except Exception as e:
logger.info("Etherpad Server doesn't work. Error: %s", e)
return response
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
self.topic_slug = self.object.topic.slug
self.slug = self.object.slug
context["active_topic"] = Topic.objects.get(slug=self.topic_slug)
attachment = Attachment.objects.get(id=self.object.id)
context["attachment"] = attachment
etherpads = Etherpad.objects.filter(attachment=attachment).order_by("-date")
context["etherpads"] = etherpads
context["files"] = FileUpload.objects.filter(attachment=attachment)
return context
def get_success_url(self):
context = {
"topic_slug": self.topic_slug,
"slug": self.slug,
}
return reverse("intern:attachment", kwargs=context)
class AttachmentUpdateView(LoginRequiredMixin, UpdateView): class AttachmentUpdateView(LoginRequiredMixin, UpdateView):
model = Attachment model = Attachment
template_name = "intern/attachment/attachment_update.html" template_name = "intern/attachment/attachment_update.html"

View File

@@ -34,7 +34,7 @@
</a> </a>
{% for etherpad in etherpads %} {% for etherpad in etherpads %}
<a href="{{ etherpad.etherpad_key }}" class="flex justify-between items-center gap-2"> <a href="{{ etherpad.get_absolute_url }}" class="flex justify-between items-center gap-2">
<h3 class="text-gray-800 dark:text-gray-200">{{ etherpad.title }}</h2> <h3 class="text-gray-800 dark:text-gray-200">{{ etherpad.title }}</h2>
<span class=" text-gray-700 dark:text-gray-300">{{ etherpad.date }}<span class="ml-2 hidden sm:inline-block"><i class="fa-solid fa-angle-right text-gray-400 dark:text-gray-600"></i></span> <span class=" text-gray-700 dark:text-gray-300">{{ etherpad.date }}<span class="ml-2 hidden sm:inline-block"><i class="fa-solid fa-angle-right text-gray-400 dark:text-gray-600"></i></span>
</a> </a>

View File

@@ -13,7 +13,6 @@
<span class="cursor-default">{{ active_topic.title }}</span> <span class="cursor-default">{{ active_topic.title }}</span>
</aside> </aside>
<!-- TODO: description -->
{% if active_topic.description %} {% if active_topic.description %}
<div class="db-page-content-left"> <div class="db-page-content-left">
{{ active_topic.description|safe }} {{ active_topic.description|safe }}
@@ -21,6 +20,15 @@
{% endif %} {% endif %}
<!-- TODO: tasks --> <!-- TODO: tasks -->
{% if tasks %}
<ul class="flex flex-col gap-1 max-w-fit">
{% for task in tasks %}
<li>
<span class="ml-2">{{ task.title|truncatechars:45 }} <a href="{% url 'tasks:task-detail' task.slug %}" class="inline-block text-proprietary dark:text-proprietary-lighter">Link <i class="fa-solid fa-link"></i></a></span>
</li>
{% endfor %}
</ul>
{% endif %}
<ul class="flex flex-col gap-1 max-w-fit"> <ul class="flex flex-col gap-1 max-w-fit">
{% for attachment in attachments %} {% for attachment in attachments %}
@@ -42,8 +50,4 @@
</section> </section>
</div> </div>
</main> </main>
{% endblock %} {% endblock %}