diff --git a/fet2020/intern/urls.py b/fet2020/intern/urls.py index 97aa7e64..b9d3fcf6 100644 --- a/fet2020/intern/urls.py +++ b/fet2020/intern/urls.py @@ -4,11 +4,13 @@ from . import apps from . import views from .views import ( AttachmentCreateView, + AttachmentDetailView, AttachmentUpdateView, EtherpadCreateView, FileUploadCreateView, TaskCreateView, TopicCreateView, + TopicDetailView, TopicUpdateView, ) @@ -22,7 +24,7 @@ urlpatterns = [ TopicCreateView.as_view(), name="topic-create", ), - path("/", views.show_topic, name="topic"), + path("/", TopicDetailView.as_view(), name="topic"), path( "/update/", TopicUpdateView.as_view(), @@ -34,7 +36,11 @@ urlpatterns = [ name="attachment-create", ), path("/create/", TaskCreateView.as_view(), name="task-create"), - path("//", views.show_attachment, name="attachment"), + path( + "//", + AttachmentDetailView.as_view(), + name="attachment", + ), path( "//update/", AttachmentUpdateView.as_view(), diff --git a/fet2020/intern/views.py b/fet2020/intern/views.py index 20dc8116..c1ee29f5 100644 --- a/fet2020/intern/views.py +++ b/fet2020/intern/views.py @@ -6,6 +6,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import F, Q from django.http import HttpResponseRedirect, Http404 from django.shortcuts import render +from django.views.generic.detail import DetailView from django.views.generic.edit import CreateView, UpdateView from django.urls import reverse_lazy, reverse @@ -45,108 +46,6 @@ def index(request): 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): model = Topic success_url = reverse_lazy("intern:index") @@ -170,6 +69,49 @@ class TopicCreateView(LoginRequiredMixin, CreateView): 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): model = Topic template_name = "intern/topic/topic_update.html" @@ -234,6 +176,49 @@ class AttachmentCreateView(LoginRequiredMixin, CreateView): 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): model = Attachment template_name = "intern/attachment/attachment_update.html" diff --git a/fet2020/templates/intern/attachment.html b/fet2020/templates/intern/attachment/attachment_detail.html similarity index 97% rename from fet2020/templates/intern/attachment.html rename to fet2020/templates/intern/attachment/attachment_detail.html index 991d65c0..6cba1da0 100644 --- a/fet2020/templates/intern/attachment.html +++ b/fet2020/templates/intern/attachment/attachment_detail.html @@ -34,7 +34,7 @@ {% for etherpad in etherpads %} - +

{{ etherpad.title }}

{{ etherpad.date }}
diff --git a/fet2020/templates/intern/topic.html b/fet2020/templates/intern/topic/topic_detail.html similarity index 81% rename from fet2020/templates/intern/topic.html rename to fet2020/templates/intern/topic/topic_detail.html index 4ffa8659..4c26e0dc 100644 --- a/fet2020/templates/intern/topic.html +++ b/fet2020/templates/intern/topic/topic_detail.html @@ -13,7 +13,6 @@ {{ active_topic.title }} - {% if active_topic.description %}
{{ active_topic.description|safe }} @@ -21,6 +20,15 @@ {% endif %} + {% if tasks %} +
    + {% for task in tasks %} +
  • + {{ task.title|truncatechars:45 }} Link +
  • + {% endfor %} +
+ {% endif %}
    {% for attachment in attachments %} @@ -42,8 +50,4 @@
- - - - {% endblock %}