diff --git a/README.md b/README.md index 7d062f9..e0418e4 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,4 @@ Simple blog management system. ## Todo 4. Find a nice way to display metadata (author, dates, etc) -5. Allow adding pages (pinned articles ?) 6. Add syntax coloration to code blocks diff --git a/articles/admin.py b/articles/admin.py index dca6518..78de714 100644 --- a/articles/admin.py +++ b/articles/admin.py @@ -4,7 +4,7 @@ from django.contrib.admin import register from django.contrib.auth.admin import UserAdmin from django.db import models -from .models import Article, User +from .models import Article, Page, User admin.site.register(User, UserAdmin) @@ -67,3 +67,22 @@ class ArticleAdmin(admin.ModelAdmin): class Media: css = {"all": ("admin_articles.css",)} + + +@register(Page) +class PageAdmin(ArticleAdmin): + list_display = ["position"] + ArticleAdmin.list_display + fieldsets = [ + ( + "Metadata", + { + "fields": ( + ("title", "slug", "position"), + ("author", "status"), + ("published_at", "created_at", "updated_at"), + "views_count", + ) + }, + ), + ("Content", {"fields": ("content",)}), + ] diff --git a/articles/context_processors.py b/articles/context_processors.py new file mode 100644 index 0000000..410ccf8 --- /dev/null +++ b/articles/context_processors.py @@ -0,0 +1,5 @@ +from articles.models import Article, Page + + +def pages(request): + return {"pages": Page.objects.filter(status=Article.PUBLISHED)} diff --git a/articles/migrations/0006_page.py b/articles/migrations/0006_page.py new file mode 100644 index 0000000..81d5fe9 --- /dev/null +++ b/articles/migrations/0006_page.py @@ -0,0 +1,31 @@ +# Generated by Django 3.1 on 2020-08-17 07:00 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("articles", "0005_article_slug"), + ] + + operations = [ + migrations.CreateModel( + name="Page", + fields=[ + ( + "article_ptr", + models.OneToOneField( + auto_created=True, + on_delete=django.db.models.deletion.CASCADE, + parent_link=True, + primary_key=True, + serialize=False, + to="articles.article", + ), + ), + ], + bases=("articles.article",), + ), + ] diff --git a/articles/migrations/0007_auto_20200817_0941.py b/articles/migrations/0007_auto_20200817_0941.py new file mode 100644 index 0000000..3699b39 --- /dev/null +++ b/articles/migrations/0007_auto_20200817_0941.py @@ -0,0 +1,19 @@ +# Generated by Django 3.1 on 2020-08-17 07:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("articles", "0006_page"), + ] + + operations = [ + migrations.AlterModelOptions( + name="page", options={"ordering": ["position", "-published_at"]}, + ), + migrations.AddField( + model_name="page", name="position", field=models.IntegerField(default=0), + ), + ] diff --git a/articles/models.py b/articles/models.py index a4d9f78..cec1bb9 100644 --- a/articles/models.py +++ b/articles/models.py @@ -13,6 +13,11 @@ class User(AbstractUser): pass +class ArticleManager(models.Manager): + def get_queryset(self): + return super().get_queryset().filter(page__isnull=True) + + class Article(models.Model): DRAFT = "draft" PUBLISHED = "published" @@ -30,6 +35,9 @@ class Article(models.Model): views_count = models.IntegerField(default=0) slug = models.SlugField(unique=True) + objects = ArticleManager() + with_pages = models.Manager() + class Meta: ordering = ["-published_at"] @@ -67,7 +75,15 @@ class Article(models.Model): self.status = self.DRAFT self.save() - def save(self, *args, **kwargs): # new + def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(self.title) return super().save(*args, **kwargs) + + +class Page(Article): + objects = models.Manager() + position = models.IntegerField(default=0) + + class Meta: + ordering = ["position", "-published_at"] diff --git a/articles/templates/articles/base.html b/articles/templates/articles/base.html index 1ba0712..4539791 100644 --- a/articles/templates/articles/base.html +++ b/articles/templates/articles/base.html @@ -11,7 +11,15 @@