From 0d4a93a5bf48257bd995cc8982ebc52405b8390e Mon Sep 17 00:00:00 2001 From: "T. Werner" Date: Thu, 18 Jun 2020 13:40:26 +0200 Subject: [PATCH 1/3] add edit course form and view --- frontend/templates/frontend/course/edit.html | 42 ++++++++++++++++++++ frontend/templates/frontend/course/view.html | 1 + frontend/urls.py | 1 + frontend/views/course.py | 20 +++++++++- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 frontend/templates/frontend/course/edit.html diff --git a/frontend/templates/frontend/course/edit.html b/frontend/templates/frontend/course/edit.html new file mode 100644 index 00000000..66e69c96 --- /dev/null +++ b/frontend/templates/frontend/course/edit.html @@ -0,0 +1,42 @@ +{% extends 'frontend/base_logged_in.html' %} + +{# Load the tag library #} +{% load bootstrap4 %} +{% load i18n %} +{% load static %} +{% load fontawesome_5 %} + +{# Load CSS and JavaScript #} + +{% block title %}{% trans "Edit course" %} - Collab Coursebook{% endblock %} + +{% block imports %} + + +{% endblock %} + +{% block content %} +

Edit Course

+ +
+ {% csrf_token %} + + {% bootstrap_form form %} + + {{ form.media }} +
+ + + {% fa5_icon 'times' 'fas' %} {% trans "Cancel" %} +
+
+{% endblock %} + +{% block bottom_script %} + + +{% endblock %} diff --git a/frontend/templates/frontend/course/view.html b/frontend/templates/frontend/course/view.html index f5de24e8..3eab087c 100644 --- a/frontend/templates/frontend/course/view.html +++ b/frontend/templates/frontend/course/view.html @@ -23,6 +23,7 @@ {% fa5_icon 'bookmark' 'fas' %} {% endif %} {% if user.profile in course.owners.all %} + {% fa5_icon "pencil-alt" "fas" %} {% trans "Edit Course" %} {% fa5_icon "trash" "fas" %} {% trans "Delete Course" %} {% endif %} diff --git a/frontend/urls.py b/frontend/urls.py index f9288b73..180ba4f2 100644 --- a/frontend/urls.py +++ b/frontend/urls.py @@ -17,6 +17,7 @@ path('', views.CourseListView.as_view(), name='courses'), path('/', include([ path('', views.CourseView.as_view(), name='course'), + path('edit/', views.course.EditCourseView.as_view(), name='course-edit'), path('delete/', views.CourseDeleteView.as_view(), name='course-delete'), ])), path('/topic//content/', include([ diff --git a/frontend/views/course.py b/frontend/views/course.py index 20b7e8a1..24a265ac 100644 --- a/frontend/views/course.py +++ b/frontend/views/course.py @@ -3,9 +3,9 @@ from django.contrib.messages.views import SuccessMessageMixin from django.contrib import messages from django.http import HttpResponseRedirect -from django.urls import reverse_lazy +from django.urls import reverse_lazy, reverse from django.views.generic import DetailView -from django.views.generic.edit import FormMixin, CreateView, DeleteView +from django.views.generic.edit import FormMixin, CreateView, DeleteView, UpdateView from django.utils.translation import gettext_lazy as _ from base.models import Course, CourseStructureEntry @@ -36,6 +36,22 @@ def get_initial(self): return initial +class EditCourseView(SuccessMessageMixin, LoginRequiredMixin, UpdateView): + """ + Edit course + """ + model = Course + template_name = 'frontend/course/edit.html' + form_class = AddAndEditCourseForm + + def get_success_url(self): + course_id = self.get_object().id + return reverse('frontend:course', args=(course_id,)) + + def get_success_message(self, cleaned_data): + return _(f"Course '{cleaned_data['title']}' successfully edited") + + class CourseView(DetailView, FormMixin): # pylint: disable=too-many-ancestors """ Displays the course detail page From eb19c51151dc5ccd6f9f80b9c851ed0c956ba132 Mon Sep 17 00:00:00 2001 From: "T. Werner" Date: Mon, 22 Jun 2020 17:05:54 +0200 Subject: [PATCH 2/3] create translation edit course --- frontend/locale/de_DE/LC_MESSAGES/django.po | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/frontend/locale/de_DE/LC_MESSAGES/django.po b/frontend/locale/de_DE/LC_MESSAGES/django.po index 74248d65..c8eb5270 100644 --- a/frontend/locale/de_DE/LC_MESSAGES/django.po +++ b/frontend/locale/de_DE/LC_MESSAGES/django.po @@ -94,16 +94,23 @@ msgid "Create a new course" msgstr "Neuen Kurs anlegen" #: frontend/templates/frontend/course/create.html:29 +#: frontend/templates/frontend/course/edit.html:29 msgid "Create" msgstr "Erzeugen" +#: frontend/templates/frontend/course/edit.html:11 +msgid "Edit course" +msgstr "Kurs bearbeiten" + #: frontend/templates/frontend/course/view.html:26 -#: frontend/templates/frontend/course/view_course.html:36 +msgid "Edit Course" +msgstr "Kurs hinzufügen" + +#: frontend/templates/frontend/course/view.html:27 msgid "Delete Course" msgstr "Kurs löschen" -#: frontend/templates/frontend/course/view.html:62 -#: frontend/templates/frontend/course/view_course.html:58 +#: frontend/templates/frontend/course/view.html:63 msgid "No topics yet" msgstr "Bisher keine Themen" @@ -227,6 +234,12 @@ msgstr "Kommentar erfolgreich bearbeitet." msgid "Course '{cleaned_data['title']}' successfully created" msgstr "Kurs '{cleaned_data['title']}' erfolgreich angelegt" -#: frontend/views/profile.py:26 +#: frontend/views/course.py:52 +#, fuzzy +#| msgid "Course '{cleaned_data['title']}' successfully created" +msgid "Course '{cleaned_data['title']}' successfully edited" +msgstr "Kurs '{cleaned_data['title']}' erfolgreich angelegt" + +#: frontend/views/profile.py:25 msgid "Profile updated" msgstr "Profil aktualisiert" From f077fb3e27eef8ee1413727532f0ad0f4b56ee1a Mon Sep 17 00:00:00 2001 From: "T. Werner" Date: Sun, 28 Jun 2020 19:17:14 +0200 Subject: [PATCH 3/3] implement changes from pull request comment --- collab_coursebook/settings.py | 2 ++ frontend/locale/de_DE/LC_MESSAGES/django.po | 5 ++--- frontend/templates/frontend/course/view.html | 2 +- frontend/templatetags/cc_frontend_tags.py | 8 ++++++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/collab_coursebook/settings.py b/collab_coursebook/settings.py index acea7854..76b6da21 100644 --- a/collab_coursebook/settings.py +++ b/collab_coursebook/settings.py @@ -166,3 +166,5 @@ "impress_text": "", "impress_url": "" } + +ALLOW_PUBLIC_COURSE_EDITING_BY_EVERYONE = True diff --git a/frontend/locale/de_DE/LC_MESSAGES/django.po b/frontend/locale/de_DE/LC_MESSAGES/django.po index c8eb5270..95bae9d7 100644 --- a/frontend/locale/de_DE/LC_MESSAGES/django.po +++ b/frontend/locale/de_DE/LC_MESSAGES/django.po @@ -68,6 +68,7 @@ msgstr "" #: frontend/templates/frontend/content/detail.html:83 #: frontend/templates/frontend/course/create.html:30 +#: frontend/templates/frontend/course/edit.html:30 #: frontend/templates/frontend/profile/profile_edit.html:19 msgid "Cancel" msgstr "Abbrechen" @@ -235,10 +236,8 @@ msgid "Course '{cleaned_data['title']}' successfully created" msgstr "Kurs '{cleaned_data['title']}' erfolgreich angelegt" #: frontend/views/course.py:52 -#, fuzzy -#| msgid "Course '{cleaned_data['title']}' successfully created" msgid "Course '{cleaned_data['title']}' successfully edited" -msgstr "Kurs '{cleaned_data['title']}' erfolgreich angelegt" +msgstr "Kurs '{cleaned_data['title']}' erfolgreich bearbeitet" #: frontend/views/profile.py:25 msgid "Profile updated" diff --git a/frontend/templates/frontend/course/view.html b/frontend/templates/frontend/course/view.html index 3eab087c..3b224235 100644 --- a/frontend/templates/frontend/course/view.html +++ b/frontend/templates/frontend/course/view.html @@ -22,7 +22,7 @@ {% if course in user.profile.stared_courses.all %} {% fa5_icon 'bookmark' 'fas' %} {% endif %} - {% if user.profile in course.owners.all %} + {% if user|check_edit_course_permission:course %} {% fa5_icon "pencil-alt" "fas" %} {% trans "Edit Course" %} {% fa5_icon "trash" "fas" %} {% trans "Delete Course" %} {% endif %} diff --git a/frontend/templatetags/cc_frontend_tags.py b/frontend/templatetags/cc_frontend_tags.py index 7743528a..c12c37ac 100644 --- a/frontend/templatetags/cc_frontend_tags.py +++ b/frontend/templatetags/cc_frontend_tags.py @@ -1,6 +1,7 @@ from django import template from django.conf import settings +from collab_coursebook.settings import ALLOW_PUBLIC_COURSE_EDITING_BY_EVERYONE from content.models import CONTENT_TYPES register = template.Library() @@ -86,3 +87,10 @@ def content_card(type): if type in CONTENT_TYPES.keys(): return f"content/cards/{type}.html" return "content/cards/blank.html" + + +@register.filter +def check_edit_course_permission(user, course): + # either an user is an owner or the course is public and it is allowed to edit public courses + return (user.profile in course.owners.all()) or (not course.restrict_changes + and ALLOW_PUBLIC_COURSE_EDITING_BY_EVERYONE)