work on searchability

This commit is contained in:
Christoph J. Scherr 2023-05-31 23:29:46 +02:00
parent e103889291
commit cf70474b4f
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
8 changed files with 161 additions and 23 deletions

View File

@ -0,0 +1,31 @@
# Generated by Django 3.2.19 on 2023-05-31 20:54
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('start', '0002_auto_20230531_2254'),
]
operations = [
migrations.CreateModel(
name='BlogPost',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=50)),
('subtitle', models.CharField(max_length=50)),
('desc', models.CharField(max_length=250, unique=True)),
('body', models.TextField()),
('date', models.DateField(blank=True)),
('slug', models.SlugField()),
('keywords', models.ManyToManyField(to='start.Keyword')),
],
options={
'abstract': False,
},
),
]

View File

@ -1,6 +1,8 @@
from django.db import models from django.db import models
class BlogPost(models.Model): from start.models import AbstractSearchable
class BlogPost(AbstractSearchable):
""" """
Should contain a blogpost Should contain a blogpost
""" """
@ -9,3 +11,4 @@ class BlogPost(models.Model):
desc = models.CharField(max_length=250, unique=True) desc = models.CharField(max_length=250, unique=True)
body = models.TextField() body = models.TextField()
date = models.DateField(blank=True) date = models.DateField(blank=True)
slug = models.SlugField()

View File

@ -3,5 +3,5 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path("", views.IndexView.as_view(), name="BlogIndex"), path("", views.Index.as_view(), name="BlogIndex"),
] ]

View File

@ -1,7 +1,9 @@
from django.shortcuts import render from django.shortcuts import render
from django.views.generic.base import TemplateView from django.views.generic import TemplateView, DetailView, ListView, View
class IndexView(TemplateView): from start.views import SearchableView
class Index(TemplateView, SearchableView):
""" """
The index page of the gawa/blog app. The index page of the gawa/blog app.
@ -11,3 +13,14 @@ class IndexView(TemplateView):
template_name: str = "blog/index.html" template_name: str = "blog/index.html"
class Post(DetailView):
"""
Main page of a blog post
"""
pass
class List(ListView):
"""
Scroll through a list of blog posts
"""
pass

View File

@ -0,0 +1,37 @@
# Generated by Django 3.2.19 on 2023-05-31 20:54
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('start', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Keyword',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.CharField(max_length=40)),
],
),
migrations.CreateModel(
name='StaticSite',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=50)),
('subtitle', models.CharField(max_length=50)),
('desc', models.CharField(max_length=250, unique=True)),
('date', models.DateField(blank=True)),
('keywords', models.ManyToManyField(to='start.Keyword')),
],
options={
'abstract': False,
},
),
migrations.DeleteModel(
name='MainSearchEntry',
),
]

View File

@ -1,16 +1,56 @@
from django.db import models from django.db import models
from django.db.models.options import override
class MainSearchEntry(models.Model): #from .views import SearchableView
# ^^^ raises Circular Import
class Keyword(models.Model):
""" """
This model will be searched for by the searchbox that is in every upper part of the website. this is the model that should contain searchable keywords
The view making use of it is MainSearchView. """
text = models.CharField(max_length=40)
Any model object that I implement as searchable should eventually have an entry here. class AbstractSearchable(models.Model):
"""
Abstract class for any model that should be searchable.
This model will be searched for by the searchbox that is in every upper part of the website.
""" """
title = models.CharField(max_length=50) title = models.CharField(max_length=50)
subtitle = models.CharField(max_length=50) subtitle = models.CharField(max_length=50)
desc = models.CharField(max_length=250, unique=True) desc = models.CharField(max_length=250, unique=True)
# may be empty/blank for some entries # may be empty/blank for some entries
date = models.DateField(blank=True) date = models.DateField(blank=True)
# every searchable object should have some url associated with it. keywords = models.ManyToManyField(Keyword)
link = models.URLField()
class Meta:
"""
AbstractSearchable is an abstract model
"""
abstract = True
@classmethod
def regenerate_all_entries(cls):
"""
regenerate all searchable items
"""
raise NotImplementedError
class StaticSite(AbstractSearchable):
"""
This model represents any static site, such as start:index,
that should show up in search.
Every searchable view should inherit from start.views.SearchableView.
# TODO automate scanning for SearchableView classes
"""
@override
def regenerate_all_entries(cls):
# this does not actually contain all views, only those that should show
# up in the search.
# TODO automate searching for these
#all_views: list[SearchableView] = []
raise NotImplementedError

View File

@ -4,9 +4,9 @@ from . import views
app_name: str = "start" app_name: str = "start"
urlpatterns = [ urlpatterns = [
path("", views.IndexView.as_view(), name="index"), path("", views.Index.as_view(), name="index"),
path("search/", views.MainSearchView.as_view(), name="search"), path("search/", views.MainSearch.as_view(), name="search"),
path("legal/", views.LegalInfoView.as_view(), name="legal_info"), path("legal/", views.LegalInfo.as_view(), name="legal_info"),
path("professional/", views.LegalInfoView.as_view(), name="professional"), path("professional/", views.LegalInfo.as_view(), name="professional"),
path('language/activate/<language_code>/', views.ActivateLanguageView.as_view(), name='activate_language'), path('language/activate/<language_code>/', views.ActivateLanguage.as_view(), name='activate_language'),
] ]

View File

@ -10,10 +10,24 @@ from django.views import View
from django.template import Template, context, loader from django.template import Template, context, loader
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from .forms import MainSearchForm
from .models import MainSearchEntry
class IndexView(TemplateView): from .forms import MainSearchForm
from .models import AbstractSearchable
from abc import ABC
class SearchableView(View, ABC):
"""
This abstract view implements some traits of views that should show up
in the main search
"""
title: str
subtitle: str
desc: str
date: None = None
keywords: list[str]
class Index(TemplateView, SearchableView):
""" """
The index page of the gawa app. The index page of the gawa app.
@ -28,7 +42,7 @@ class IndexView(TemplateView):
context["MainSearchForm"] = MainSearchForm() context["MainSearchForm"] = MainSearchForm()
return context return context
class ProfessionalView(TemplateView): class Professional(TemplateView, SearchableView):
""" """
Professional informations that might interest a professional employer Professional informations that might interest a professional employer
""" """
@ -40,7 +54,7 @@ class ProfessionalView(TemplateView):
context["MainSearchForm"] = MainSearchForm() context["MainSearchForm"] = MainSearchForm()
return context return context
class LegalInfoView(TemplateView): class LegalInfo(TemplateView, SearchableView):
""" """
Legal info that the german authorities want. Legal info that the german authorities want.
""" """
@ -52,7 +66,7 @@ class LegalInfoView(TemplateView):
context["MainSearchForm"] = MainSearchForm() context["MainSearchForm"] = MainSearchForm()
return context return context
class ActivateLanguageView(View): class ActivateLanguage(View):
""" """
Set the language to whatever Set the language to whatever
""" """
@ -66,12 +80,12 @@ class ActivateLanguageView(View):
request.session[translation.LANGUAGE_SESSION_KEY] = self.language_code request.session[translation.LANGUAGE_SESSION_KEY] = self.language_code
return redirect(self.redirect_to) return redirect(self.redirect_to)
class MainSearchView(ListView): class MainSearch(ListView):
""" """
Search for anything. Search for anything.
""" """
model = MainSearchEntry model = AbstractSearchable
object_list = [] # this is only declaration, the view breaks without it. object_list = [] # this is only declaration, the view breaks without it.
template_name: str = "start/search.html" template_name: str = "start/search.html"