Skip to content

Commit 2dbcb3a

Browse files
committed
implement coursebook
1 parent ffc0067 commit 2dbcb3a

6 files changed

Lines changed: 68 additions & 3 deletions

File tree

frontend/templates/frontend/content/detail.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@
2727
<a class="dropdown-item"
2828
href="{#{% url 'edit_md_file' course.id topic.id content.id %}#}">{% fa5_icon 'pencil-alt' 'fas' %}
2929
Edit</a>
30-
3130
{% endif %}
31+
32+
{# TODO: include link add/remove#}
33+
{# {% if is_content_in_coursebook user, course, content %}#}
34+
{# <p>TEST</p>#}
35+
{# {% endif %}#}
36+
3237
{% if isCurrentUserOwner or user.is_superuser or content.author == user %}
3338
<a href="#" class="dropdown-item text-danger" data-toggle="modal"
3439
data-target="#deleteContentModal">{% fa5_icon 'trash' 'fas' %} Delete</a>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% load i18n %}
2+
{# Load the tag library #}
3+
{% load bootstrap4 %}
4+
{% load fontawesome_5 %}
5+
{% load cc_frontend_tags %}
6+
7+
<div>
8+
{% with user|get_coursebook:course as topic_contents %}
9+
<h2>Coursebook</h2>
10+
{% if topic_contents|length > 0 %}
11+
{% include "frontend/course/topic_contents.html" %}
12+
{% else %}
13+
<p>Coursebook empty</p>
14+
{% endif %}
15+
{% endwith %}
16+
</div>

frontend/templates/frontend/course/view.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ <h5 style="margin-top: 20px;">
8484
<!-- TODO Add image? -->
8585
</div>
8686

87+
{% include 'frontend/course/coursebook.html' %}
88+
8789
{% for entry in structure %}
8890
<div style="margin-top: 30px;">
8991
{% with forloop.counter as outer_index %}

frontend/templatetags/cc_frontend_tags.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django import template
22
from django.conf import settings
33

4+
from base.models import Favorite
45
from collab_coursebook.settings import ALLOW_PUBLIC_COURSE_EDITING_BY_EVERYONE
56
from content.models import CONTENT_TYPES
67

@@ -115,3 +116,16 @@ def add_content_button(user, course_id, topic_id):
115116
# generate list of tuple (content type, content verbose name) for add content dropdown
116117
content_data = [(content_type, content_model.DESC) for content_type, content_model in CONTENT_TYPES.items()]
117118
return {'user': user, 'course_id': course_id, 'topic_id': topic_id, 'content_data': content_data}
119+
120+
121+
@register.filter
122+
def get_coursebook(user, course):
123+
favourites = Favorite.objects.filter(user=user.profile, course=course)
124+
coursebook = [favourite.content for favourite in favourites]
125+
return coursebook
126+
127+
128+
# TODO: template tag can only have 2 parameters
129+
@register.filter
130+
def is_content_in_coursebook(user, course, content):
131+
return Favorite.objects.filter(user=user, course=course, content=content).count() == 1

frontend/urls.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from content.models import CONTENT_TYPES
44
from frontend import views
55
from frontend.views.search import SearchView
6+
from frontend.views.coursebook import add_to_coursebook, remove_from_coursebook
67

78
app_name = "frontend"
89

@@ -25,8 +26,12 @@
2526
])),
2627
path('<int:course_id>/topic/<int:topic_id>/content/', include([
2728
re_path(r'add/(?P<type>' + '|'.join([key for key in CONTENT_TYPES.keys()]) + ')/$', views.content.AddContentView.as_view(), name='content-add'),
28-
path('<int:content_id>/comment/<int:pk>/delete/', views.DeleteComment.as_view(), name='comment-delete'),
29-
path('<int:content_id>/comment/<int:pk>/edit/', views.EditComment.as_view(), name='comment-edit'),
29+
path('<int:content_id>', include([
30+
path('/comment/<int:pk>/delete/', views.DeleteComment.as_view(), name='comment-delete'),
31+
path('/comment/<int:pk>/edit/', views.EditComment.as_view(), name='comment-edit'),
32+
path('/coursebook/add/', views.coursebook.add_to_coursebook, name='coursebook-add'),
33+
path('/coursebook/remove/', views.coursebook.remove_from_coursebook, name='coursebook-remove'),
34+
])),
3035
path('<pk>/', views.ContentView.as_view(), name='content'),
3136
path('<pk>/read/', views.content.ContentReadingModeView.as_view(), name='content-reading-mode'),
3237
])),

frontend/views/coursebook.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django.http import HttpResponseRedirect
2+
from django.urls import reverse
3+
from base.models import Course, Favorite, Topic, Content
4+
5+
6+
def add_to_coursebook(request, *args, **kwargs):
7+
user = request.user.profile
8+
course = Course.objects.get(pk=kwargs['course_id'])
9+
topic = Topic.objects.get(pk=kwargs['topic_id'])
10+
content = Content.objects.get(pk=kwargs['content_id'])
11+
12+
Favorite.objects.create(content=content, user=user, course=course)
13+
return HttpResponseRedirect(reverse('frontend:content', args=(course.id, topic.id, content.id,)))
14+
15+
16+
def remove_from_coursebook(request, *args, **kwargs):
17+
user = request.user.profile
18+
course = Course.objects.get(pk=kwargs['course_id'])
19+
topic = Topic.objects.get(pk=kwargs['topic_id'])
20+
content = Content.objects.get(pk=kwargs['content_id'])
21+
22+
Favorite.objects.filter(course=course, user=user, content=content).delete()
23+
return HttpResponseRedirect(reverse('frontend:content', args=(course.id, topic.id, content.id,)))

0 commit comments

Comments
 (0)