misc: extend notes with a "published" flag
authorFrédéric Péters <fpeters@0d.be>
Sun, 29 Dec 2019 18:42:24 +0000 (19:42 +0100)
committerFrédéric Péters <fpeters@0d.be>
Mon, 30 Dec 2019 08:35:50 +0000 (09:35 +0100)
chloro/phyll/migrations/0003_note_published.py [new file with mode: 0644]
chloro/phyll/models.py
chloro/phyll/static/css/style.scss
chloro/phyll/templates/phyll/note_list.html
chloro/phyll/views.py

diff --git a/chloro/phyll/migrations/0003_note_published.py b/chloro/phyll/migrations/0003_note_published.py
new file mode 100644 (file)
index 0000000..7c5f05e
--- /dev/null
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.17 on 2019-12-29 18:39
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('phyll', '0002_auto_20191229_1932'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='note',
+            name='published',
+            field=models.BooleanField(default=True, verbose_name='Published'),
+        ),
+    ]
index 2307389..1f9914b 100644 (file)
@@ -26,6 +26,7 @@ class Note(models.Model):
     slug = models.SlugField(_('Slug'), max_length=150)
     text = RichTextField(_('Text'), blank=True, null=True)
     tags = TaggableManager(_('Tags'), blank=True)
+    published = models.BooleanField(_('Published'), default=True)
     creation_timestamp = models.DateTimeField(auto_now_add=True)
     last_update_timestamp = models.DateTimeField(auto_now=True)
 
index e25a946..3c78e42 100644 (file)
@@ -98,6 +98,9 @@ main {
                list-style: none;
                display: inline-block;
        }
+       li.unpublished {
+               opacity: 0.5;
+       }
        a {
                text-transform: uppercase;
                display: inline-block;
index abc30a0..aedf918 100644 (file)
@@ -8,7 +8,7 @@
 <h2>{{ view.kwargs.tag }}</h2>
 <ul>
 {% for post in object_list %}
-<li><a href="{{ post.get_absolute_url }}">{{ post.title }} <span>{{ post.creation_timestamp|date:"Y/m/d" }}</a></li>
+<li {% if not post.published %}class="unpublished"{% endif %}><a href="{{ post.get_absolute_url }}">{{ post.title }} <span>{{ post.creation_timestamp|date:"Y/m/d" }}</a></li>
 {% endfor %}
 </ul>
 </div>
index f645f39..b8598dc 100644 (file)
@@ -14,6 +14,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from django.core.exceptions import PermissionDenied
 from django.http import Http404
 from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView, TemplateView
 
@@ -24,9 +25,9 @@ class NoteView(DetailView):
     model = Note
 
     def get(self, request, *args, **kwargs):
+        note = self.get_object()
         if kwargs.get('year'):
             # check date does match
-            note = self.get_object()
             creation = self.get_object().creation_timestamp
             if (creation.year, creation.month, creation.day) != (
                 int(kwargs['year']),
@@ -34,17 +35,19 @@ class NoteView(DetailView):
                 int(kwargs['day']),
             ):
                 raise Http404()
+        if not note.published and not request.user.is_staff:
+            raise PermissionDenied()
         return super(NoteView, self).get(request, *args, **kwargs)
 
 
 class NoteEditView(UpdateView):
     model = Note
-    fields = ['title', 'slug', 'text', 'tags']
+    fields = ['title', 'slug', 'text', 'tags', 'published']
 
 
 class NoteAddView(CreateView):
     model = Note
-    fields = ['title', 'slug', 'text', 'tags']
+    fields = ['title', 'slug', 'text', 'tags', 'published']
 
 
 class NoteDeleteView(DeleteView):
@@ -62,4 +65,7 @@ class ListOnTagView(ListView):
     model = Note
 
     def get_queryset(self):
-        return Note.objects.filter(tags__name__in=[self.kwargs['tag']])
+        qs = Note.objects.filter(tags__name__in=[self.kwargs['tag']])
+        if not self.request.user.is_staff:
+            qs = qs.filter(published=True)
+        return qs