Commit 00c4323fbed9053c6bff2fa585951eec2227b8f5
1 parent
415f147d
Exists in
master
and in
5 other branches
Adding subscribe button [Issue: #195]
Showing
4 changed files
with
29 additions
and
4 deletions
Show diff stats
courses/models.py
1 | 1 | from django.utils.translation import ugettext_lazy as _ |
2 | 2 | from django.db import models |
3 | +import datetime | |
3 | 4 | from autoslug.fields import AutoSlugField |
4 | 5 | from users.models import User |
5 | 6 | from core.models import Resource, MimeType |
... | ... | @@ -79,6 +80,11 @@ class Subject(models.Model): |
79 | 80 | def __str__(self): |
80 | 81 | return self.name |
81 | 82 | |
83 | + def show_subscribe(self): | |
84 | + today = datetime.date.today() | |
85 | + | |
86 | + return today < self.init_date | |
87 | + | |
82 | 88 | class Topic(models.Model): |
83 | 89 | |
84 | 90 | name = models.CharField(_('Name'), max_length = 100) | ... | ... |
courses/static/js/course.js
courses/templates/course/view.html
... | ... | @@ -7,6 +7,10 @@ |
7 | 7 | <link rel="stylesheet" href="{% static 'css/course/course.css' %}"> |
8 | 8 | {% endblock style %} |
9 | 9 | |
10 | +{% block javascript %} | |
11 | + <script type="text/javascript" src="{% static 'js/course.js' %}"></script> | |
12 | +{% endblock %} | |
13 | + | |
10 | 14 | {% block breadcrumbs %} |
11 | 15 | |
12 | 16 | {{ block.super }} |
... | ... | @@ -212,6 +216,12 @@ |
212 | 216 | </div> |
213 | 217 | </div> |
214 | 218 | {% endif %} |
219 | + | |
220 | + {% if user|has_role:'student' and not user in subject.students.all and subject.show_subscribe %} | |
221 | + <div class="col-xs-3 col-md-2"> | |
222 | + <a onclick="subscribe($(this), '{% url 'course:subscribe_subject' subject.slug %}' , '{% trans 'Are you sure you want to subscribe to this subject?' %}')" class="btn btn-sm btn-primary btn-raised">{% trans 'Subscribe' %}</a> | |
223 | + </div> | |
224 | + {% endif %} | |
215 | 225 | </div> |
216 | 226 | </div> |
217 | 227 | <div class="panel-body"> |
... | ... | @@ -259,6 +269,11 @@ |
259 | 269 | </div> |
260 | 270 | </div> |
261 | 271 | {% endif %} |
272 | + {% if user|has_role:'student' and not user in subject.students and subject.show_subscribe %} | |
273 | + <div class="col-xs-3 col-md-2"> | |
274 | + <a onclick="subscribe($(this), '{% url 'course:subscribe_subject' subject.slug %}' , '{% trans 'Are you sure you want to subscribe to this subject?' %}')" class="btn btn-sm btn-primary btn-raised">{% trans 'Subscribe' %}</a> | |
275 | + </div> | |
276 | + {% endif %} | |
262 | 277 | </div> |
263 | 278 | </div> |
264 | 279 | <div class="panel-body"> | ... | ... |
courses/views.py
... | ... | @@ -231,7 +231,9 @@ class DeleteView(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.De |
231 | 231 | def subscribe_course(request, slug): |
232 | 232 | course = get_object_or_404(Course, slug = slug) |
233 | 233 | |
234 | - if course.students.add(request.user): | |
234 | + course.students.add(request.user) | |
235 | + | |
236 | + if request.user in course.students.all(): | |
235 | 237 | return JsonResponse({"status": "ok", "message": _("Successfully subscribed to the course!")}) |
236 | 238 | else: |
237 | 239 | return JsonResponse({"status": "erro", "message": _("An error has occured. Could not subscribe to this course, try again later")}) |
... | ... | @@ -552,8 +554,10 @@ class DeleteSubjectView(LoginRequiredMixin, HasRoleMixin, generic.DeleteView): |
552 | 554 | def subscribe_subject(request, slug): |
553 | 555 | subject = get_object_or_404(Subject, slug = slug) |
554 | 556 | |
555 | - if request.user.courses_student.filter(slug = slug).exists(): | |
556 | - if subject.students.add(request.user): | |
557 | + if request.user in subject.course.students.all(): | |
558 | + subject.students.add(request.user) | |
559 | + | |
560 | + if request.user in subject.students.all(): | |
557 | 561 | return JsonResponse({"status": "ok", "message": _("Successfully subscribed to the subject!")}) |
558 | 562 | else: |
559 | 563 | return JsonResponse({"status": "erro", "message": _("An error has occured. Could not subscribe to this subject, try again later")}) | ... | ... |