summaryrefslogtreecommitdiff
path: root/journal/api
diff options
context:
space:
mode:
authorMark Powers <mark@marks.kitchen>2024-07-14 15:30:27 -0500
committerMark Powers <mark@marks.kitchen>2024-07-14 15:30:27 -0500
commitad232020d57d2c77dfd5400dc4a8290b484c8ba2 (patch)
tree3e2121666986c67fe554a1e9046682b7370d412e /journal/api
Initial commitmain
Diffstat (limited to 'journal/api')
-rw-r--r--journal/api/__init__.py0
-rw-r--r--journal/api/admin.py22
-rw-r--r--journal/api/apps.py6
-rw-r--r--journal/api/migrations/0001_initial.py39
-rw-r--r--journal/api/migrations/0002_source_title_alter_entry_created.py24
-rw-r--r--journal/api/migrations/__init__.py0
-rw-r--r--journal/api/models.py26
-rw-r--r--journal/api/serializers.py23
-rw-r--r--journal/api/tests.py3
-rw-r--r--journal/api/views.py41
10 files changed, 184 insertions, 0 deletions
diff --git a/journal/api/__init__.py b/journal/api/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/journal/api/__init__.py
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
--- /dev/null
+++ b/journal/api/migrations/__init__.py
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"]