pedagogy: complete CRUD for UV model

This commit is contained in:
Antoine Bartuccio 2019-06-16 00:04:36 +02:00
parent ec33311715
commit ab344ba02f
Signed by: klmp200
GPG Key ID: E7245548C53F904B
4 changed files with 81 additions and 5 deletions

View File

@ -44,8 +44,14 @@ class UV(models.Model):
return user.is_in_group(settings.SITH_GROUP_PEDAGOGY_ADMIN_ID) return user.is_in_group(settings.SITH_GROUP_PEDAGOGY_ADMIN_ID)
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
"""
Only visible by subscribers
"""
return user.is_subscribed return user.is_subscribed
def __str__(self):
return self.code
code = models.CharField( code = models.CharField(
_("code"), _("code"),
max_length=10, max_length=10,

View File

@ -6,7 +6,18 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
{% if can_create_uv(user) %}
<p>
<a href="{{ url('pedagogy:uv_create') }}">{% trans %}Create UV{% endtrans %}</a>
</p>
{% endif %}
{% for uv in object_list %} {% for uv in object_list %}
<p>
{{ uv.code }} {{ uv.code }}
{% if user.is_owner(uv) -%}
<a href="{{ url('pedagogy:uv_update', uv_id=uv.id) }}">{% trans %}Edit{% endtrans %}</a>
<a href="{{ url('pedagogy:uv_delete', uv_id=uv.id) }}">{% trans %}Delete{% endtrans %}</a>
{%- endif -%}
</p>
{% endfor %} {% endfor %}
{% endblock content %} {% endblock content %}

View File

@ -46,6 +46,7 @@ urlpatterns = [
# Administration : Create Update Delete Edit # Administration : Create Update Delete Edit
url(r"^uv/create$", UVCreateView.as_view(), name="uv_create"), url(r"^uv/create$", UVCreateView.as_view(), name="uv_create"),
url(r"^uv/(?P<uv_id>[0-9]+)/delete$", UVDeleteView.as_view(), name="uv_delete"), url(r"^uv/(?P<uv_id>[0-9]+)/delete$", UVDeleteView.as_view(), name="uv_delete"),
url(r"^uv/(?P<uv_id>[0-9]+)/edit$", UVUpdateView.as_view(), name="uv_update"),
url( url(
r"^department/create$", r"^department/create$",
EducationDepartmentCreateView.as_view(), EducationDepartmentCreateView.as_view(),

View File

@ -22,7 +22,15 @@
# #
# #
from django.views.generic import CreateView, DeleteView, DetailView, ListView, FormView from django.views.generic import (
CreateView,
DeleteView,
DetailView,
UpdateView,
ListView,
FormView,
View,
)
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from core.views import ( from core.views import (
@ -36,6 +44,32 @@ from core.views import (
from pedagogy.forms import UVForm from pedagogy.forms import UVForm
from pedagogy.models import UV from pedagogy.models import UV
# Some mixins
class CanCreateUVFunctionMixin(View):
"""
Add the function can_create_uv(user) into the template
"""
@staticmethod
def can_create_uv(user):
"""
Creates a dummy instance of UV and test is_owner
"""
return user.is_owner(UV())
def get_context_data(self, **kwargs):
"""
Pass the function to the template
"""
kwargs = super(CanCreateUVFunctionMixin, self).get_context_data(**kwargs)
kwargs["can_create_uv"] = self.can_create_uv
return kwargs
# Acutal views
class UVDetailFormView(DetailFormView): class UVDetailFormView(DetailFormView):
""" """
@ -54,7 +88,7 @@ class UVCommentDetailView(DetailView):
pass pass
class UVListView(CanViewMixin, ListView): class UVListView(CanViewMixin, CanCreateUVFunctionMixin, ListView):
""" """
UV guide main page UV guide main page
""" """
@ -108,12 +142,36 @@ class UVCreateView(CanCreateMixin, CreateView):
return reverse_lazy("pedagogy:uv_detail", kwargs={"uv_id": self.object.id}) return reverse_lazy("pedagogy:uv_detail", kwargs={"uv_id": self.object.id})
class UVDeleteView(DeleteView): class UVDeleteView(CanEditPropMixin, DeleteView):
""" """
Allow to delete an UV (Privileged) Allow to delete an UV (Privileged)
""" """
pass model = UV
pk_url_kwarg = "uv_id"
template_name = "core/delete_confirm.jinja"
def get_success_url(self):
return reverse_lazy("pedagogy:guide")
class UVUpdateView(CanEditPropMixin, UpdateView):
"""
Allow to edit an UV (Privilegied)
"""
model = UV
form_class = UVForm
pk_url_kwarg = "uv_id"
template_name = "core/edit.jinja"
def get_form_kwargs(self):
kwargs = super(UVUpdateView, self).get_form_kwargs()
kwargs["author_id"] = self.request.user.id
return kwargs
def get_success_url(self):
return reverse_lazy("pedagogy:uv_detail", kwargs={"uv_id": self.object.id})
class EducationDepartmentCreateView(CreateView): class EducationDepartmentCreateView(CreateView):