From ad232020d57d2c77dfd5400dc4a8290b484c8ba2 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Sun, 14 Jul 2024 15:30:27 -0500 Subject: Initial commit --- journal/api/__init__.py | 0 journal/api/admin.py | 22 ++++++++++++ journal/api/apps.py | 6 ++++ journal/api/migrations/0001_initial.py | 39 ++++++++++++++++++++ .../0002_source_title_alter_entry_created.py | 24 +++++++++++++ journal/api/migrations/__init__.py | 0 journal/api/models.py | 26 ++++++++++++++ journal/api/serializers.py | 23 ++++++++++++ journal/api/tests.py | 3 ++ journal/api/views.py | 41 ++++++++++++++++++++++ 10 files changed, 184 insertions(+) create mode 100644 journal/api/__init__.py create mode 100644 journal/api/admin.py create mode 100644 journal/api/apps.py create mode 100644 journal/api/migrations/0001_initial.py create mode 100644 journal/api/migrations/0002_source_title_alter_entry_created.py create mode 100644 journal/api/migrations/__init__.py create mode 100644 journal/api/models.py create mode 100644 journal/api/serializers.py create mode 100644 journal/api/tests.py create mode 100644 journal/api/views.py (limited to 'journal/api') diff --git a/journal/api/__init__.py b/journal/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/journal/api/admin.py b/journal/api/admin.py new file mode 100644 index 0000000..8ba4fdd --- /dev/null +++ b/journal/api/admin.py @@ -0,0 +1,22 @@ +from django.contrib import admin + +from . import models + + +class AuthorAdmin(admin.ModelAdmin): + list_display = ["author"] + +admin.site.register(models.Author, AuthorAdmin) + + +class SourceAdmin(admin.ModelAdmin): + list_display = ["title", "author", "url"] + +admin.site.register(models.Source, SourceAdmin) + + +class EntryAdmin(admin.ModelAdmin): + list_display = ["created", "source"] + list_filter = ["source"] + +admin.site.register(models.Entry, EntryAdmin) diff --git a/journal/api/apps.py b/journal/api/apps.py new file mode 100644 index 0000000..e68ac76 --- /dev/null +++ b/journal/api/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + name = 'journal.api' + label = 'journal_api' diff --git a/journal/api/migrations/0001_initial.py b/journal/api/migrations/0001_initial.py new file mode 100644 index 0000000..ad1f787 --- /dev/null +++ b/journal/api/migrations/0001_initial.py @@ -0,0 +1,39 @@ +# Generated by Django 5.0.4 on 2024-04-30 02:08 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Author', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('author', models.CharField(max_length=1024)), + ], + ), + migrations.CreateModel( + name='Source', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('url', models.CharField(max_length=1024)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='journal_api.author')), + ], + ), + migrations.CreateModel( + name='Entry', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created', models.DateTimeField()), + ('html_text', models.TextField()), + ('source', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='entries', to='journal_api.source')), + ], + ), + ] diff --git a/journal/api/migrations/0002_source_title_alter_entry_created.py b/journal/api/migrations/0002_source_title_alter_entry_created.py new file mode 100644 index 0000000..26ae3b1 --- /dev/null +++ b/journal/api/migrations/0002_source_title_alter_entry_created.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.4 on 2024-04-30 02:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('journal_api', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='source', + name='title', + field=models.CharField(default='', max_length=1024), + preserve_default=False, + ), + migrations.AlterField( + model_name='entry', + name='created', + field=models.DateField(), + ), + ] diff --git a/journal/api/migrations/__init__.py b/journal/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/journal/api/models.py b/journal/api/models.py new file mode 100644 index 0000000..fb5dac4 --- /dev/null +++ b/journal/api/models.py @@ -0,0 +1,26 @@ +from django.db import models + +class Author(models.Model): + author = models.CharField(max_length=1024) + + def __str__(self): + return self.author + + +class Source(models.Model): + url = models.CharField(max_length=1024) + title = models.CharField(max_length=1024) + author = models.ForeignKey(Author, on_delete=models.CASCADE) + + def __str__(self): + return f"{self.author} -{self.title}" + + +class Entry(models.Model): + created = models.DateField() + html_text = models.TextField() + source = models.ForeignKey( + Source, on_delete=models.CASCADE, related_name="entries") + + def __str__(self): + return f"{self.source} - {self.created}" diff --git a/journal/api/serializers.py b/journal/api/serializers.py new file mode 100644 index 0000000..ced248b --- /dev/null +++ b/journal/api/serializers.py @@ -0,0 +1,23 @@ +from . import models +from rest_framework import serializers + + +class AuthorSerializer(serializers.ModelSerializer): + class Meta: + exclude = ["id"] + model = models.Author + + +class SourceSerializer(serializers.ModelSerializer): + class Meta: + exclude = ["id"] + model = models.Source + + author = AuthorSerializer() + +class EntrySerializer(serializers.ModelSerializer): + class Meta: + exclude = ["id"] + model = models.Entry + + source = SourceSerializer() diff --git a/journal/api/tests.py b/journal/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/journal/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/journal/api/views.py b/journal/api/views.py new file mode 100644 index 0000000..a88e560 --- /dev/null +++ b/journal/api/views.py @@ -0,0 +1,41 @@ +from rest_framework import viewsets +from rest_framework import filters +import datetime + +from . import serializers +from . import models + + +class CustomDateFilterBackend(filters.BaseFilterBackend): + def filter_queryset(self, request, queryset, view): + if "today" in request.query_params: + today = datetime.date.today() + queryset = queryset.filter(created__month=today.month, created__day=today.day) + day = request.query_params.get("day") + if day: + try: + queryset = queryset.filter(created__day=day) + except ValueError: + pass + month = request.query_params.get("month") + if month: + try: + queryset = queryset.filter(created__month=month) + except ValueError: + pass + year = request.query_params.get("year") + if year: + try: + queryset = queryset.filter(created__year=year) + except ValueError: + pass + return queryset + + +class EntrySerializer(viewsets.ReadOnlyModelViewSet): + queryset = models.Entry.objects.all() + serializer_class = serializers.EntrySerializer + filter_backends = [ + CustomDateFilterBackend, filters.OrderingFilter, filters.SearchFilter] + search_fields = ["html_text"] + ordering_fields = ["created"] -- cgit v1.2.3