Skip to content

Commit ffc0067

Browse files
authored
Merge pull request #36 from DataManagementLab/#18_Frontend/Base_Allow_to_duplicate_courses
frontend/base allow to duplicate courses
2 parents 64d58f7 + fd6d288 commit ffc0067

5 files changed

Lines changed: 119 additions & 5 deletions

File tree

frontend/locale/de_DE/LC_MESSAGES/django.po

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ msgstr ""
8282
" "
8383

8484
#: frontend/templates/frontend/content/detail.html:83
85+
#: frontend/templates/frontend/course/create.html:30
86+
#: frontend/templates/frontend/course/duplicate.html:30
87+
#: frontend/templates/frontend/profile/profile_edit.html:19
88+
msgid "Cancel"
89+
msgstr "Abbrechen"
90+
91+
#: frontend/templates/frontend/content/detail.html:86
8592
msgid "Confirm"
8693
msgstr "Bestätigen"
8794

@@ -114,6 +121,23 @@ msgstr "Vorherige"
114121
msgid "Create a new course"
115122
msgstr "Neuen Kurs anlegen"
116123

124+
#: frontend/templates/frontend/course/create.html:29
125+
#: frontend/templates/frontend/course/duplicate.html:29
126+
msgid "Create"
127+
msgstr "Erzeugen"
128+
129+
#: frontend/templates/frontend/course/duplicate.html:11
130+
#: frontend/templates/frontend/course/duplicate.html:19
131+
#: frontend/templates/frontend/course/view.html:25
132+
msgid "Duplicate Course"
133+
msgstr "Kurs duplizieren"
134+
135+
#: frontend/templates/frontend/course/view.html:36
136+
#: frontend/templates/frontend/course/view.html:27
137+
msgid "Delete Course"
138+
msgstr "Kurs löschen"
139+
140+
#: frontend/templates/frontend/course/view.html:63
117141
#: frontend/templates/frontend/course/dropdown_topic.html:10
118142
msgid "Add Content"
119143
msgstr "Inhalte hinzufügen"
@@ -126,10 +150,6 @@ msgstr "Kurs bearbeiten"
126150
msgid "Edit Course"
127151
msgstr "Kurs hinzufügen"
128152

129-
#: frontend/templates/frontend/course/view.html:36
130-
msgid "Delete Course"
131-
msgstr "Kurs löschen"
132-
133153
#: frontend/templates/frontend/course/view.html:76
134154
msgid "No topics yet"
135155
msgstr "Bisher keine Themen"
@@ -267,6 +287,18 @@ msgstr "Kommentar erfolgreich gelöscht."
267287
msgid "Successfully edited Comment."
268288
msgstr "Kommentar erfolgreich bearbeitet."
269289

290+
#: frontend/views/course.py:27
291+
msgid ""
292+
"Course '{cleaned_data['title']}' successfully created. All settings and "
293+
"contents of the course '{original_course.title}' were copied."
294+
msgstr ""
295+
"Kurs '{cleaned_data['title']}' erfolgreich erstellt. Alle Einstellungen und "
296+
"Inhalte des Kurses '{original_course.title}' wurden kopiert."
297+
298+
#: frontend/views/course.py:62
299+
msgid "Course '{cleaned_data['title']}' successfully created"
300+
msgstr "Kurs '{cleaned_data['title']}' erfolgreich angelegt"
301+
270302
#: frontend/views/content.py:28
271303
msgid "Content '{cleaned_data['type']}' successfully added"
272304
msgstr "Inhalt '{cleaned_data['type']}' erfolgreich hinzugefügt"
@@ -286,3 +318,6 @@ msgstr "Kurs '{cleaned_data['title']}' erfolgreich bearbeitet"
286318
#: frontend/views/profile.py:25
287319
msgid "Profile updated"
288320
msgstr "Profil aktualisiert"
321+
322+
#~ msgid "Course '{original_course.title}' successfully duplicated."
323+
#~ msgstr "Kurs '{cleaned_data['title']}' erfolgreich dupliziert"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{% extends 'frontend/base_logged_in.html' %}
2+
3+
{# Load the tag library #}
4+
{% load bootstrap4 %}
5+
{% load i18n %}
6+
{% load static %}
7+
{% load fontawesome_5 %}
8+
9+
{# Load CSS and JavaScript #}
10+
11+
{% block title %}{% trans "Duplicate Course" %} - Collab Coursebook{% endblock %}
12+
13+
{% block imports %}
14+
<link rel="stylesheet" href="{% static 'vendor/chosen-js/chosen.css' %}">
15+
<link rel="stylesheet" href="{% static 'css/bootstrap-chosen.css' %}">
16+
{% endblock %}
17+
18+
{% block content %}
19+
<h1>{% trans "Duplicate Course" %}</h1>
20+
21+
<form method="post" enctype=multipart/form-data>
22+
{% csrf_token %}
23+
24+
{% bootstrap_form form %}
25+
26+
{{ form.media }}
27+
<div>
28+
29+
<button type="submit" class="btn btn-primary float-right">{% fa5_icon 'plus-circle' 'fas' %} {% trans "Create" %}</button>
30+
<a href="{% url 'frontend:dashboard' %}" class="btn btn-danger">{% fa5_icon 'times' 'fas' %} {% trans "Cancel" %}</a>
31+
</div>
32+
</form>
33+
{% endblock %}
34+
35+
{% block bottom_script %}
36+
<script src="{% static 'vendor/chosen-js/chosen.jquery.js' %}"></script>
37+
<script>
38+
$(function () {
39+
$('.chosen-select').chosen();
40+
});
41+
</script>
42+
{% endblock %}

frontend/templates/frontend/course/view.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{% if user.is_authenticated %}
2929
<a class="dropdown-item" href="{# url 'favourite_content' course.id topic.id content.id ?onDetailPage=True#}">
3030
{% if course in user.profile.stared_courses.all %}{% fa5_icon 'bookmark' 'fas' %} Unsave {% else %}{% fa5_icon 'bookmark' 'far' %} Save{% endif %}</a>
31-
31+
<a href="{% url 'frontend:course-duplicate' pk=course.pk %}" class="btn btn-warning">{% fa5_icon "copy" "fas" %} {% trans "Duplicate Course" %}</a>
3232
<div class="dropdown-divider"></div>
3333

3434
{% if user|check_edit_course_permission:course %}

frontend/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
name='courses-sort'),
1919
path('', views.CourseListView.as_view(), name='courses'),
2020
path('<int:pk>/', include([
21+
path('duplicate/', views.course.DuplicateCourseView.as_view(), name='course-duplicate'),
2122
path('', views.CourseView.as_view(), name='course'),
2223
path('edit/', views.course.EditCourseView.as_view(), name='course-edit'),
2324
path('delete/', views.CourseDeleteView.as_view(), name='course-delete'),

frontend/views/course.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,42 @@
1313
from frontend.forms import AddAndEditCourseForm, FilterAndSortForm
1414

1515

16+
class DuplicateCourseView(SuccessMessageMixin, LoginRequiredMixin, CreateView):
17+
"""
18+
Duplicate a course
19+
"""
20+
model = Course
21+
template_name = 'frontend/course/duplicate.html'
22+
form_class = AddAndEditCourseForm
23+
success_url = reverse_lazy('frontend:dashboard')
24+
25+
def get_success_message(self, cleaned_data):
26+
original_course = Course.objects.get(pk=self.get_object().id)
27+
return _(f"Course '{cleaned_data['title']}' successfully created. All settings and contents of the course '{original_course.title}' were copied.")
28+
29+
def get_initial(self):
30+
course_to_duplicate = Course.objects.get(pk=self.get_object().id)
31+
data = course_to_duplicate.__dict__
32+
# set data not included in the dict
33+
data['owners'] = get_user(self.request)
34+
data['image'] = course_to_duplicate.image
35+
# this data has the wrong key
36+
data['category'] = course_to_duplicate.category
37+
data['period'] = course_to_duplicate.period
38+
return data
39+
40+
def form_valid(self, form):
41+
duplicated_course = form.save()
42+
original_course = Course.objects.get(pk=self.get_object().id)
43+
course_structure_entries = CourseStructureEntry.objects.filter(course=original_course)
44+
# duplicate course structure entries
45+
for entry in course_structure_entries:
46+
entry.pk = None
47+
entry.course = duplicated_course
48+
entry.save()
49+
return super().form_valid(form)
50+
51+
1652
class AddCourseView(SuccessMessageMixin, LoginRequiredMixin, CreateView): # pylint: disable=too-many-ancestors
1753
"""
1854
Adds a new course to the database

0 commit comments

Comments
 (0)