Loading fotoblog/authentication/admin.py +7 −2 Original line number Diff line number Diff line from django.contrib import admin from authentication.models import User # Register your models No newline at end of file class UserAdmin(admin.ModelAdmin): pass admin.site.register(User, UserAdmin) fotoblog/authentication/migrations/0003_auto_20221115_1802.py 0 → 100644 +45 −0 Original line number Diff line number Diff line # Generated by Django 4.1.3 on 2022-11-15 17:02 from django.db import migrations def create_groups(apps, schema_migration): User = apps.get_model('authentication', 'User') Group = apps.get_model('auth', 'Group') Permission = apps.get_model('auth', 'Permission') add_photo = Permission.objects.get(codename='add_photo') change_photo = Permission.objects.get(codename='change_photo') delete_photo = Permission.objects.get(codename='delete_photo') view_photo = Permission.objects.get(codename='view_photo') creator_permissions = [ add_photo, change_photo, delete_photo, view_photo, ] creators = Group(name='creators') creators.save() creators.permissions.set(creator_permissions) subscribers = Group(name='subscribers') subscribers.save() subscribers.permissions.add(view_photo) for user in User.objects.all(): if user.role == 'CREATOR': creators.user_set.add(user) if user.role == 'SUBSCRIBER': subscribers.user_set.add(user) class Migration(migrations.Migration): dependencies = [ ("authentication", "0002_remove_user_profil_photo_user_profile_photo_and_more"), ] operations = [migrations.RunPython(create_groups)] fotoblog/blog/templates/blog/post_list.html +1 −1 Original line number Diff line number Diff line Loading @@ -15,7 +15,7 @@ <div class="post"> <a href="{% url 'blog-post-view' post.id %}" alt="lien vers l'article"> {{ post.title }} </a> {% if user.is_authenticated %} {% if user.is_authenticated and perms.blog.change_blog %} <a href="{% url 'blog-post-edit' post.id %}" alt="editer l'article"> - Editer </a> {% endif %} Loading fotoblog/blog/templates/blog/post_view.html +3 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,9 @@ {% block title %}Fotoblog - {{ post.title }} {% endblock %} {% block content %} <a href="{% url 'blog-post-list' %}" alt="revenir à la lsite"> Revenir à la liste </a> <h1>{{ post.title }}</h1> {% include "messages_notif.html" %} <div class="grid-container"> Loading fotoblog/blog/views.py +6 −2 Original line number Diff line number Diff line from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from django.http import HttpResponse, HttpResponseNotFound from django.views.generic import View from django.forms import formset_factory Loading Loading @@ -44,7 +44,11 @@ class MediaCreateView(LoginRequiredMixin, View): return redirect('home') return render(request, self.template_name, self.context) class MediaMultipleCreateView(LoginRequiredMixin, View): class MediaMultipleCreateView(LoginRequiredMixin, PermissionRequiredMixin, View): permission_required = 'blog.add_photo' raise_exception = True context = dict() template_name = 'blog/media_multiple_create.html' MediaFormset = formset_factory(MediaForm, extra=5) Loading Loading
fotoblog/authentication/admin.py +7 −2 Original line number Diff line number Diff line from django.contrib import admin from authentication.models import User # Register your models No newline at end of file class UserAdmin(admin.ModelAdmin): pass admin.site.register(User, UserAdmin)
fotoblog/authentication/migrations/0003_auto_20221115_1802.py 0 → 100644 +45 −0 Original line number Diff line number Diff line # Generated by Django 4.1.3 on 2022-11-15 17:02 from django.db import migrations def create_groups(apps, schema_migration): User = apps.get_model('authentication', 'User') Group = apps.get_model('auth', 'Group') Permission = apps.get_model('auth', 'Permission') add_photo = Permission.objects.get(codename='add_photo') change_photo = Permission.objects.get(codename='change_photo') delete_photo = Permission.objects.get(codename='delete_photo') view_photo = Permission.objects.get(codename='view_photo') creator_permissions = [ add_photo, change_photo, delete_photo, view_photo, ] creators = Group(name='creators') creators.save() creators.permissions.set(creator_permissions) subscribers = Group(name='subscribers') subscribers.save() subscribers.permissions.add(view_photo) for user in User.objects.all(): if user.role == 'CREATOR': creators.user_set.add(user) if user.role == 'SUBSCRIBER': subscribers.user_set.add(user) class Migration(migrations.Migration): dependencies = [ ("authentication", "0002_remove_user_profil_photo_user_profile_photo_and_more"), ] operations = [migrations.RunPython(create_groups)]
fotoblog/blog/templates/blog/post_list.html +1 −1 Original line number Diff line number Diff line Loading @@ -15,7 +15,7 @@ <div class="post"> <a href="{% url 'blog-post-view' post.id %}" alt="lien vers l'article"> {{ post.title }} </a> {% if user.is_authenticated %} {% if user.is_authenticated and perms.blog.change_blog %} <a href="{% url 'blog-post-edit' post.id %}" alt="editer l'article"> - Editer </a> {% endif %} Loading
fotoblog/blog/templates/blog/post_view.html +3 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,9 @@ {% block title %}Fotoblog - {{ post.title }} {% endblock %} {% block content %} <a href="{% url 'blog-post-list' %}" alt="revenir à la lsite"> Revenir à la liste </a> <h1>{{ post.title }}</h1> {% include "messages_notif.html" %} <div class="grid-container"> Loading
fotoblog/blog/views.py +6 −2 Original line number Diff line number Diff line from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from django.http import HttpResponse, HttpResponseNotFound from django.views.generic import View from django.forms import formset_factory Loading Loading @@ -44,7 +44,11 @@ class MediaCreateView(LoginRequiredMixin, View): return redirect('home') return render(request, self.template_name, self.context) class MediaMultipleCreateView(LoginRequiredMixin, View): class MediaMultipleCreateView(LoginRequiredMixin, PermissionRequiredMixin, View): permission_required = 'blog.add_photo' raise_exception = True context = dict() template_name = 'blog/media_multiple_create.html' MediaFormset = formset_factory(MediaForm, extra=5) Loading