diff --git a/courses/migrations/0009_auto_20160908_1625.py b/courses/migrations/0009_auto_20160908_1625.py new file mode 100644 index 0000000..2998551 --- /dev/null +++ b/courses/migrations/0009_auto_20160908_1625.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-08 19:25 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('courses', '0008_auto_20160908_1332'), + ] + + operations = [ + migrations.AlterModelOptions( + name='subject', + options={'ordering': ('create_date',), 'verbose_name': 'Subject', 'verbose_name_plural': 'Subjects'}, + ), + migrations.AlterModelOptions( + name='topic', + options={'ordering': ('create_date',), 'verbose_name': 'Topic', 'verbose_name_plural': 'Topics'}, + ), + ] diff --git a/courses/templates/subject/create.html b/courses/templates/subject/create.html new file mode 100644 index 0000000..45163ba --- /dev/null +++ b/courses/templates/subject/create.html @@ -0,0 +1,25 @@ +{% extends 'subject/index.html' %} + +{% load static i18n permission_tags widget_tweaks %} + +{% block content %} + +
+
+
+ {% csrf_token %} + {% for field in form %} +
+ + + {% render_field field class='form-control' placeholder=field.label%} + {{ field.help_text }} +
+ {% endfor %} +
+ +
+
+
+
+{% endblock content %} diff --git a/courses/templates/subject/delete.html b/courses/templates/subject/delete.html new file mode 100644 index 0000000..cdb45f6 --- /dev/null +++ b/courses/templates/subject/delete.html @@ -0,0 +1,18 @@ +{% extends 'subject/index.html' %} + +{% load static i18n permission_tags widget_tweaks %} + +{% block content %} + +
+
+
+ {% csrf_token %} +

{% trans 'Are you sure you want to delete the category' %} "{{subject}}"?

+ + {% trans 'No' %} +
+
+
+ +{% endblock content %} diff --git a/courses/templates/subject/index.html b/courses/templates/subject/index.html index a39a3ef..1d0fd59 100644 --- a/courses/templates/subject/index.html +++ b/courses/templates/subject/index.html @@ -24,14 +24,31 @@ {{subject}} {% endfor %} - + + {% if user|has_role:'system_admin' or user|has_role:'professor' %} + {% trans "Create Subject" %} + {% endif %} {% endblock %} {% block content %}
-

{% trans "Presentation Subject" %}

+
+
+

{% trans "Presentation Subject" %}

+
+
+ {% if user|has_role:'system_admin' or user in subject.professors %} + {% trans "edit" %} + {% endif %} +
+
+ {% if user|has_role:'system_admin' or user in subject.professors %} + {% trans "delete" %} + {% endif %} +
+

diff --git a/courses/templates/subject/update.html b/courses/templates/subject/update.html new file mode 100644 index 0000000..68ed185 --- /dev/null +++ b/courses/templates/subject/update.html @@ -0,0 +1,25 @@ +{% extends 'subject/index.html' %} + +{% load static i18n permission_tags widget_tweaks %} + +{% block content %} + +

+
+
+ {% csrf_token %} + {% for field in form %} +
+ + + {% render_field field class='form-control' placeholder=field.label%} + {{ field.help_text }} +
+ {% endfor %} +
+ +
+
+
+
+{% endblock content %} diff --git a/courses/templates/topic/create.html b/courses/templates/topic/create.html index f6c697a..4c93ec7 100644 --- a/courses/templates/topic/create.html +++ b/courses/templates/topic/create.html @@ -13,6 +13,7 @@ {% render_field field class='form-control' placeholder=field.label%} + {{ field.help_text }}
{% endfor %}
diff --git a/courses/templates/topic/update.html b/courses/templates/topic/update.html index 039ff36..c06d981 100644 --- a/courses/templates/topic/update.html +++ b/courses/templates/topic/update.html @@ -13,6 +13,7 @@ {% render_field field class='form-control' placeholder=field.label%} + {{ field.help_text }}
{% endfor %}
diff --git a/courses/urls.py b/courses/urls.py index 95f0176..64210db 100644 --- a/courses/urls.py +++ b/courses/urls.py @@ -14,11 +14,10 @@ urlpatterns = [ url(r'^categories/edit/(?P[\w_-]+)/$', views.UpdateCatView.as_view(), name='update_cat'), url(r'^categories/(?P[\w_-]+)/$', views.ViewCat.as_view(), name='view_cat'), url(r'^categories/delete/(?P[\w_-]+)/$', views.DeleteCatView.as_view(), name='delete_cat'), - url(r'^course/(?P[\w_-]+)/subjects/$', views.SubjectsView.as_view(), name='view_subject'), - url(r'^course/(?P[\w_-]+)/topics/create/$', views.CreateTopicView.as_view(), name='create_topic'), - url(r'^course/(?P[\w_-]+)/topics/update/$', views.UpdateTopicView.as_view(), name='update_topic'), - # url(r'^course/(?P[\w_-]+)/modules/create/$', views.CreateModView.as_view(), name='create_mods'), - # url(r'^course/(?P[\w_-]+)/modules/edit/(?P[\w_-]+)/$', views.UpdateModView.as_view(), name='update_mods'), - # url(r'^course/(?P[\w_-]+)/modules/delete/(?P[\w_-]+)/$', views.DeleteModView.as_view(), name='delete_mods'), - # url(r'^course/(?P[\w_-]+)/subject$', views.ViewSubject.as_view(), name='view_subject'), + url(r'^course/subjects/(?P[\w_-]+)/$', views.SubjectsView.as_view(), name='view_subject'), + url(r'^course/topics/create/(?P[\w_-]+)/$', views.CreateTopicView.as_view(), name='create_topic'), + url(r'^course/topics/update/(?P[\w_-]+)/$', views.UpdateTopicView.as_view(), name='update_topic'), + url(r'^course/subjects/create/(?P[\w_-]+)/$', views.CreateSubjectView.as_view(), name='create_subject'), + url(r'^course/subjects/update/(?P[\w_-]+)/$', views.UpdateSubjectView.as_view(), name='update_subject'), + url(r'^course/subjects/delete/(?P[\w_-]+)/$', views.DeleteSubjectView.as_view(), name='delete_subject'), ] diff --git a/courses/views.py b/courses/views.py index c181c39..a56258e 100644 --- a/courses/views.py +++ b/courses/views.py @@ -8,6 +8,8 @@ from rolepermissions.mixins import HasRoleMixin from django.core.urlresolvers import reverse_lazy from django.utils.translation import ugettext_lazy as _ from slugify import slugify +from rolepermissions.verifications import has_role +from django.db.models import Q from .forms import CourseForm, CategoryForm, SubjectForm,TopicForm from .models import Course, Subject, Category,Topic @@ -197,7 +199,10 @@ class SubjectsView(LoginRequiredMixin, generic.ListView): def get_queryset(self): subject = get_object_or_404(Subject, slug = self.kwargs.get('slug')) course = subject.course - return course.subjects.filter(visible=True) + context = course.subjects.filter(visible=True) + if (self.request.user in subject.professors.all() or has_role(self.request.user,'system_admin')): + context = course.subjects.all() + return context def get_context_data(self, **kwargs): subject = get_object_or_404(Subject, slug = self.kwargs.get('slug')) @@ -223,8 +228,9 @@ class CreateTopicView(LoginRequiredMixin, HasRoleMixin, generic.edit.CreateView) subject = get_object_or_404(Subject, slug = self.kwargs.get('slug')) context['course'] = subject.course context['subject'] = subject - context['subjects'] = subject.course.subjects.filter(visible=True) - + context['subjects'] = subject.course.subjects.filter(Q(visible=True) | Q(professors__in=[self.request.user])) + if (has_role(self.request.user,'system_admin')): + context['subjects'] = subject.course.subjects.all() return context def form_valid(self, form): @@ -237,18 +243,12 @@ class CreateTopicView(LoginRequiredMixin, HasRoleMixin, generic.edit.CreateView) return super(CreateTopicView, self).form_valid(form) - def render_to_response(self, context, **response_kwargs): - messages.success(self.request, _('Module created successfully!')) - - return self.response_class(request=self.request, template=self.get_template_names(), context=context, using=self.template_engine) - class UpdateTopicView(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): allowed_roles = ['professor', 'system_admin','student'] login_url = reverse_lazy("core:home") redirect_field_name = 'next' template_name = 'topic/update.html' - # model = Topic form_class = TopicForm def get_object(self, queryset=None): @@ -262,48 +262,81 @@ class UpdateTopicView(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) context['course'] = topic.subject.course context['subject'] = topic.subject - context['subjects'] = topic.subject.course.subjects.filter(visible=True) + context['subjects'] = topic.subject.course.subjects.filter(Q(visible=True) | Q(professors__in=[self.request.user])) + if (has_role(self.request.user,'system_admin')): + context['subjects'] = topic.subject.course.subjects.all() + return context + +class CreateSubjectView(LoginRequiredMixin, HasRoleMixin, generic.edit.CreateView): + + allowed_roles = ['professor', 'system_admin'] + login_url = reverse_lazy("core:home") + redirect_field_name = 'next' + template_name = 'subject/create.html' + form_class = SubjectForm + + def get_success_url(self): + return reverse_lazy('course:view_subject', kwargs={'slug' : self.object.slug}) + + def get_context_data(self, **kwargs): + context = super(CreateSubjectView, self).get_context_data(**kwargs) + course = get_object_or_404(Course, slug = self.kwargs.get('slug')) + context['course'] = course + context['subjects'] = course.subjects.filter(Q(visible=True) | Q(professors__in=[self.request.user])) + if (has_role(self.request.user,'system_admin')): + context['subjects'] = course.subjects.all() + return context + + def form_valid(self, form): + course = get_object_or_404(Course, slug = self.kwargs.get('slug')) + + self.object = form.save(commit = False) + self.object.course = course + self.object.professor = self.request.user + self.object.save() + return super(CreateSubjectView, self).form_valid(form) + + +class UpdateSubjectView(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): + + allowed_roles = ['professor', 'system_admin'] + login_url = reverse_lazy("core:home") + redirect_field_name = 'next' + template_name = 'subject/update.html' + form_class = SubjectForm + + def get_object(self, queryset=None): + return get_object_or_404(Subject, slug = self.kwargs.get('slug')) + + def get_success_url(self): + return reverse_lazy('course:view_subject', kwargs={'slug' : self.object.slug}) + + def get_context_data(self, **kwargs): + context = super(UpdateSubjectView, self).get_context_data(**kwargs) + context['course'] = self.object.course + context['subject'] = self.object + context['subjects'] = self.object.course.subjects.filter(Q(visible=True) | Q(professors__in=[self.request.user])) + if (has_role(self.request.user,'system_admin')): + context['subjects'] = self.object.course.subjects.all() return context - # def form_valid(self, form): - # self.object = form.save(commit = False) - # self.object.slug = slugify(self.object.name) - # self.object.save() - # - # return super(UpdateModView, self).form_valid(form) - - # def render_to_response(self, context, **response_kwargs): - # messages.success(self.request, _('Module edited successfully!')) - # - # return self.response_class(request=self.request, template=self.get_template_names(), context=context, using=self.template_engine) - -# class DeleteModView(LoginRequiredMixin, HasRoleMixin, generic.DeleteView): -# -# allowed_roles = ['professor', 'system_admin'] -# login_url = reverse_lazy("core:home") -# redirect_field_name = 'next' -# model = Module -# template_name = 'module/delete.html' -# -# def get_success_url(self): -# return reverse_lazy('course:manage_mods', kwargs={'slug' : self.object.course.slug}) -# -# def get_context_data(self, **kwargs): -# course = get_object_or_404(Course, slug = self.kwargs.get('slug_course')) -# context = super(DeleteModView, self).get_context_data(**kwargs) -# context['course'] = course -# -# return context -# -# def render_to_response(self, context, **response_kwargs): -# messages.success(self.request, _('Module deleted successfully!')) -# -# return self.response_class(request=self.request, template=self.get_template_names(), context=context, using=self.template_engine) - -# class ViewSubject(LoginRequiredMixin, generic.DetailView): -# login_url = reverse_lazy("core:home") -# redirect_field_name = 'next' -# model = Course -# template_name = 'subject/index.html' -# context_object_name = 'course' +class DeleteSubjectView(LoginRequiredMixin, HasRoleMixin, generic.DeleteView): + + allowed_roles = ['professor', 'system_admin'] + login_url = reverse_lazy("core:home") + redirect_field_name = 'next' + model = Subject + template_name = 'subject/delete.html' + + def get_context_data(self, **kwargs): + context = super(DeleteSubjectView, self).get_context_data(**kwargs) + context['course'] = self.object.course + context['subject'] = self.object + context['subjects'] = self.object.course.subjects.filter(Q(visible=True) | Q(professors__in=[self.request.user])) + if (has_role(self.request.user,'system_admin')): + context['subjects'] = self.object.course.subjects.all() + return context + + def get_success_url(self): + return reverse_lazy('course:view_subject', kwargs={'slug' : self.object.course.subjects.all()[0].slug}) diff --git a/users/migrations/0012_auto_20160908_1625.py b/users/migrations/0012_auto_20160908_1625.py new file mode 100644 index 0000000..c4b1a99 --- /dev/null +++ b/users/migrations/0012_auto_20160908_1625.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-08 19:25 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import re + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0011_auto_20160908_1108'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='username', + field=models.CharField(help_text='A short name that will be used to identify you in the platform and to access it', max_length=35, unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[\\w.@+-]+$', 32), 'Type a valid username. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .', 'invalid')], verbose_name='Login'), + ), + ] -- libgit2 0.21.2