Commit 635a5cd4 authored by Борис Ким's avatar Борис Ким

Добавил миксин PermissionRequired для вьюх

parent c97964de
...@@ -12,12 +12,10 @@ ...@@ -12,12 +12,10 @@
<a class="p-2" href="{% url 'webapp:course_edit' course.id %}"> <i class="fas fa-edit"></i> </a> <a class="p-2" href="{% url 'webapp:course_edit' course.id %}"> <i class="fas fa-edit"></i> </a>
<a class="p-2" href="{% url 'webapp:course_delete' course.id %}"> <i class="fas fa-trash-alt"></i> </a> <a class="p-2" href="{% url 'webapp:course_delete' course.id %}"> <i class="fas fa-trash-alt"></i> </a>
</div> </div>
<div class="card-body" style="height: 150px;"> <div class="card-body" style="height: 200px;">
<h5 class="card-title"> {{course.title}} </h5> <a class="card-title" href="{% url 'webapp:course_detail' course.id %}"> <h5>{{course.title}}</h5> </a>
<p class="card-text"> {{course.description}} </p> <p class="card-text"> {{ course.description|truncatewords:30 }} </p>
</div> <p class="card-text"> <b>Teacher:</b> {{course.teacher}} </p>
<div class="card-buttons" style="text-align: center;">
<a class="btn btn-primary p-2" href="{% url 'webapp:course_detail' course.id %}"> See more <i class="fas fa-eye"></i> </a>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
</div> </div>
<div class="card-body text-dark"> <div class="card-body text-dark">
<h5 class="card-title">Topic: {{ class.title }} </h5> <h5 class="card-title">Topic: {{ class.title }} </h5>
<p class="card-text"> Description: {{ class.description|truncatewords:30 }} </p> <p class="card-text"> Description: {{ class.description }} </p>
{{ class.file }} {{ class.file }}
<hr> <hr>
<p> <b>Created at:</b> {{ class.created_at }} - <b>by:</b> {{ course.teacher }}</p> <p> <b>Created at:</b> {{ class.created_at }} - <b>by:</b> {{ course.teacher }}</p>
......
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls.base import reverse, reverse_lazy from django.urls.base import reverse, reverse_lazy
from django.views.generic import CreateView, DeleteView, UpdateView from django.views.generic import CreateView, DeleteView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from webapp.models import Class, Course from webapp.models import Class, Course
from webapp.forms import ClassForm from webapp.forms import ClassForm
class CreateClassView(LoginRequiredMixin ,CreateView): class CreateClassView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
model = Class model = Class
fields = ['title', 'description', 'file'] fields = ['title', 'description', 'file']
template_name = 'courses/create.html' template_name = 'courses/create.html'
permission_required = 'webapp.add_class'
def form_valid(self, form): def form_valid(self, form):
course_pk = self.kwargs.get('pk') course_pk = self.kwargs.get('pk')
......
...@@ -3,17 +3,18 @@ from django.shortcuts import redirect ...@@ -3,17 +3,18 @@ from django.shortcuts import redirect
from django.urls.base import reverse_lazy from django.urls.base import reverse_lazy
from django.views.generic import ListView, CreateView, DetailView, DeleteView from django.views.generic import ListView, CreateView, DetailView, DeleteView
from django.views.generic import UpdateView from django.views.generic import UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from webapp.models import Course from webapp.models import Course
from webapp.forms import CourseForm from webapp.forms import CourseForm
class CourseCreateView(LoginRequiredMixin, CreateView): class CourseCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
template_name = 'courses/create.html' template_name = 'courses/create.html'
form_class = CourseForm form_class = CourseForm
model = Course model = Course
pk_url_kwarg = 'project_pk' pk_url_kwarg = 'project_pk'
permission_required = 'webapp.add_course'
def get_form(self, form_class=None): def get_form(self, form_class=None):
form = super().get_form() form = super().get_form()
...@@ -27,20 +28,23 @@ class CoursesListView(ListView): ...@@ -27,20 +28,23 @@ class CoursesListView(ListView):
paginate_by = 6 paginate_by = 6
class CourseDetailView(LoginRequiredMixin, DetailView): class CourseDetailView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
model = Course model = Course
template_name = 'courses/detail.html' template_name = 'courses/detail.html'
pk_url_kwarg = 'course_pk' pk_url_kwarg = 'course_pk'
permission_required = 'webapp.view_course'
class CourseDeleteView(LoginRequiredMixin, DeleteView): class CourseDeleteView(LoginRequiredMixin, PermissionRequiredMixin, DeleteView):
model = Course model = Course
template_name = 'courses/delete.html' template_name = 'courses/delete.html'
success_url = reverse_lazy('webapp:courses_list') success_url = reverse_lazy('webapp:courses_list')
permission_required = 'webapp.delete_course'
class CourseEditView(LoginRequiredMixin, UpdateView): class CourseEditView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
model = Course model = Course
fields = ['title', 'description', 'start_date'] fields = ['title', 'description', 'start_date']
template_name = 'courses/edit.html' template_name = 'courses/edit.html'
permission_required = 'webapp.change_course'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment