From 201e6e165475f4ec459323a624249edc143dab55 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 3 Jun 2023 14:40:57 +0200 Subject: [PATCH] basic working search --- gawa/blog/admin.py | 2 + gawa/blog/migrations/0001_initial.py | 23 +--------- .../migrations/0002_auto_20230602_1200.py | 36 --------------- .../blog/migrations/0002_blogpost_category.py | 35 ++++++++++++++ .../migrations/0003_auto_20230603_0009.py | 38 --------------- gawa/blog/models.py | 9 +--- gawa/start/admin.py | 17 ++++++- gawa/start/migrations/0001_initial.py | 17 +------ .../migrations/0002_auto_20230531_2254.py | 37 --------------- .../0002_keyword_searchable_staticsite.py | 46 +++++++++++++++++++ gawa/start/models.py | 31 +++++++------ gawa/start/templates/errors/bad_request.html | 6 ++- gawa/start/templates/start/search.html | 23 +++++++--- gawa/start/views.py | 23 +++++++--- web/requirements.txt | 1 + 15 files changed, 159 insertions(+), 185 deletions(-) delete mode 100644 gawa/blog/migrations/0002_auto_20230602_1200.py create mode 100644 gawa/blog/migrations/0002_blogpost_category.py delete mode 100644 gawa/blog/migrations/0003_auto_20230603_0009.py delete mode 100644 gawa/start/migrations/0002_auto_20230531_2254.py create mode 100644 gawa/start/migrations/0002_keyword_searchable_staticsite.py diff --git a/gawa/blog/admin.py b/gawa/blog/admin.py index 96fe54e..3a87df2 100644 --- a/gawa/blog/admin.py +++ b/gawa/blog/admin.py @@ -12,3 +12,5 @@ class BlogPostAdmin(admin.ModelAdmin): """ The admin model for BlogPost """ + list_display = ["title_en", "subtitle_en", "title_de", "subtitle_de", "date", "category", "suburl"] + date_hierarchy = "date" diff --git a/gawa/blog/migrations/0001_initial.py b/gawa/blog/migrations/0001_initial.py index 8981bef..b5a62a1 100644 --- a/gawa/blog/migrations/0001_initial.py +++ b/gawa/blog/migrations/0001_initial.py @@ -1,31 +1,12 @@ -# Generated by Django 3.2.19 on 2023-05-31 20:54 +# Generated by Django 3.2.19 on 2023-06-03 12:03 -from django.db import migrations, models +from django.db import migrations 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, - }, - ), ] diff --git a/gawa/blog/migrations/0002_auto_20230602_1200.py b/gawa/blog/migrations/0002_auto_20230602_1200.py deleted file mode 100644 index c663795..0000000 --- a/gawa/blog/migrations/0002_auto_20230602_1200.py +++ /dev/null @@ -1,36 +0,0 @@ -# Generated by Django 3.2.19 on 2023-06-02 10:00 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('start', '0002_auto_20230531_2254'), - ('blog', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='Category', - 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)), - ('name', models.CharField(max_length=50)), - ('slug', models.SlugField()), - ('keywords', models.ManyToManyField(to='start.Keyword')), - ], - options={ - 'abstract': False, - }, - ), - migrations.AddField( - model_name='blogpost', - name='category', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='blog.category'), - ), - ] diff --git a/gawa/blog/migrations/0002_blogpost_category.py b/gawa/blog/migrations/0002_blogpost_category.py new file mode 100644 index 0000000..d4c110c --- /dev/null +++ b/gawa/blog/migrations/0002_blogpost_category.py @@ -0,0 +1,35 @@ +# Generated by Django 3.2.19 on 2023-06-03 12:03 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('start', '0002_keyword_searchable_staticsite'), + ('blog', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Category', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50)), + ('slug', models.SlugField()), + ], + ), + migrations.CreateModel( + name='BlogPost', + fields=[ + ('searchable_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='start.searchable')), + ('body', models.TextField()), + ('thumbnail', models.ImageField(blank=True, upload_to='')), + ('category', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='blog.category')), + ], + bases=('start.searchable',), + ), + ] diff --git a/gawa/blog/migrations/0003_auto_20230603_0009.py b/gawa/blog/migrations/0003_auto_20230603_0009.py deleted file mode 100644 index f97b688..0000000 --- a/gawa/blog/migrations/0003_auto_20230603_0009.py +++ /dev/null @@ -1,38 +0,0 @@ -# Generated by Django 3.2.19 on 2023-06-02 22:09 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('blog', '0002_auto_20230602_1200'), - ] - - operations = [ - migrations.RemoveField( - model_name='category', - name='date', - ), - migrations.RemoveField( - model_name='category', - name='desc', - ), - migrations.RemoveField( - model_name='category', - name='keywords', - ), - migrations.RemoveField( - model_name='category', - name='subtitle', - ), - migrations.RemoveField( - model_name='category', - name='title', - ), - migrations.AddField( - model_name='blogpost', - name='thumbnail', - field=models.ImageField(blank=True, upload_to=''), - ), - ] diff --git a/gawa/blog/models.py b/gawa/blog/models.py index 5cf0ab3..f1e5761 100644 --- a/gawa/blog/models.py +++ b/gawa/blog/models.py @@ -1,6 +1,6 @@ from django.db import models -from start.models import AbstractSearchable +from start.models import Searchable class Category(models.Model): """ @@ -9,15 +9,10 @@ class Category(models.Model): name = models.CharField(max_length=50) slug = models.SlugField() -class BlogPost(AbstractSearchable): +class BlogPost(Searchable): """ Should contain a blogpost """ - 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) category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True) thumbnail = models.ImageField(blank=True) - slug = models.SlugField() diff --git a/gawa/start/admin.py b/gawa/start/admin.py index 8c38f3f..4a31d70 100644 --- a/gawa/start/admin.py +++ b/gawa/start/admin.py @@ -1,3 +1,18 @@ from django.contrib import admin +from django.db.models import CASCADE, AutoField, OneToOneField +from django.views.generic import View +from .models import * -# Register your models here. +@admin.register(Keyword) +class KeywordAdmin(admin.ModelAdmin): + """ + Admin Interface for Keyword + """ + list_display = ["text_en", "text_de"] + +@admin.register(StaticSite) +class StaticSiteAdmin(admin.ModelAdmin): + """ + Admin Interface for StaticSite + """ + list_display = ["title_en", "subtitle_en", "title_de", "subtitle_de", "suburl"] diff --git a/gawa/start/migrations/0001_initial.py b/gawa/start/migrations/0001_initial.py index f414316..b5a62a1 100644 --- a/gawa/start/migrations/0001_initial.py +++ b/gawa/start/migrations/0001_initial.py @@ -1,25 +1,12 @@ -# Generated by Django 3.2.19 on 2023-05-30 17:08 +# Generated by Django 3.2.19 on 2023-06-03 12:03 -from django.db import migrations, models +from django.db import migrations class Migration(migrations.Migration): - initial = True - dependencies = [ ] operations = [ - migrations.CreateModel( - name='MainSearchEntry', - 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)), - ('link', models.URLField()), - ], - ), ] diff --git a/gawa/start/migrations/0002_auto_20230531_2254.py b/gawa/start/migrations/0002_auto_20230531_2254.py deleted file mode 100644 index 694756f..0000000 --- a/gawa/start/migrations/0002_auto_20230531_2254.py +++ /dev/null @@ -1,37 +0,0 @@ -# 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', - ), - ] diff --git a/gawa/start/migrations/0002_keyword_searchable_staticsite.py b/gawa/start/migrations/0002_keyword_searchable_staticsite.py new file mode 100644 index 0000000..77e2dae --- /dev/null +++ b/gawa/start/migrations/0002_keyword_searchable_staticsite.py @@ -0,0 +1,46 @@ +# Generated by Django 3.2.19 on 2023-06-03 12:03 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + 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_de', models.CharField(max_length=40)), + ('text_en', models.CharField(max_length=40)), + ], + ), + migrations.CreateModel( + name='Searchable', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title_de', models.CharField(default='Titel DE', max_length=50)), + ('title_en', models.CharField(default='title en', max_length=50)), + ('subtitle_de', models.CharField(blank=True, max_length=50)), + ('subtitle_en', models.CharField(blank=True, max_length=50)), + ('desc_de', models.CharField(default='Beschreibung DE', max_length=250, unique=True)), + ('desc_en', models.CharField(default='Description EN', max_length=250, unique=True)), + ('date', models.DateField(blank=True, null=True)), + ('suburl', models.CharField(blank=True, max_length=200, null=True)), + ('keywords', models.ManyToManyField(to='start.Keyword')), + ], + ), + migrations.CreateModel( + name='StaticSite', + fields=[ + ('searchable_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='start.searchable')), + ], + bases=('start.searchable',), + ), + ] diff --git a/gawa/start/models.py b/gawa/start/models.py index b9d76a0..3037556 100644 --- a/gawa/start/models.py +++ b/gawa/start/models.py @@ -8,25 +8,26 @@ class Keyword(models.Model): """ this is the model that should contain searchable keywords """ - text = models.CharField(max_length=40) + text_de = models.CharField(max_length=40) + text_en = models.CharField(max_length=40) -class AbstractSearchable(models.Model): +class Searchable(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) - subtitle = models.CharField(max_length=50) - desc = models.CharField(max_length=250, unique=True) - # may be empty/blank for some entries - date = models.DateField(blank=True) - keywords = models.ManyToManyField(Keyword) - class Meta: - """ - AbstractSearchable is an abstract model - """ - abstract = True + This class is not a real abstract class, I need to query it in the main search, so thats impossible + """ + title_de = models.CharField(max_length=50, default="Titel DE") + title_en = models.CharField(max_length=50, default="title en") + subtitle_de = models.CharField(max_length=50, blank=True) + subtitle_en = models.CharField(max_length=50, blank=True) + desc_de = models.CharField(max_length=250, unique=True, default="Beschreibung DE") + desc_en = models.CharField(max_length=250, unique=True, default="Description EN") + # may be empty/blank for some entries + date = models.DateField(blank=True, null=True) + keywords = models.ManyToManyField(Keyword) + suburl = models.CharField(max_length=200, blank=True, null=True) @classmethod def regenerate_all_entries(cls): @@ -35,7 +36,7 @@ class AbstractSearchable(models.Model): """ raise NotImplementedError -class StaticSite(AbstractSearchable): +class StaticSite(Searchable): """ This model represents any static site, such as start:index, that should show up in search. diff --git a/gawa/start/templates/errors/bad_request.html b/gawa/start/templates/errors/bad_request.html index c380aea..514687d 100644 --- a/gawa/start/templates/errors/bad_request.html +++ b/gawa/start/templates/errors/bad_request.html @@ -1,4 +1,4 @@ -{% extends 'start/base.html' %} +{% extends 'base.html' %} {% load i18n %} {% get_current_language as LANGUAGE_CODE %} {% block languagecode %}{{ LANGUAGE_CODE }}{% endblock languagecode %} @@ -8,4 +8,8 @@

{% translate "Bad request" %}

{% translate "You fucked up" %}

+
+

{% translate "Looking for anything specific?" %}

+ {% include 'main_search_form.html' %} +
{% endblock main %} diff --git a/gawa/start/templates/start/search.html b/gawa/start/templates/start/search.html index ef63f8d..ca60cd8 100644 --- a/gawa/start/templates/start/search.html +++ b/gawa/start/templates/start/search.html @@ -5,13 +5,22 @@ {% block title %}{% translate "cscherr.de" %} - {% translate "startpage" %}{% endblock title %} {% block main %}
-
-

{% translate "Search for:" %} {{ searchstr }}

-
-
-
- I don't know what to put here +
+

{% translate "Search for:" %} {{ request.GET }}

+
+
+ {% for result in object_list %} +
+
+ {{ result.title_de }} +
+
+
{{ result.subtitle_de }}
+

{{result.desc_de}}

+ Go somewhere +
+
+ {% endfor %}
-
{% endblock main %} diff --git a/gawa/start/views.py b/gawa/start/views.py index ad71798..c2953df 100644 --- a/gawa/start/views.py +++ b/gawa/start/views.py @@ -1,4 +1,5 @@ from http.client import HTTPResponse +from typing import List from django.shortcuts import redirect from django.utils import translation from django.views.generic.base import View @@ -10,9 +11,11 @@ from django.views import View from django.template import Template, context, loader from django.http import HttpResponseRedirect from django.shortcuts import render +from django.views.generic.list import QuerySet +from django.db.models import Q from .forms import MainSearchForm -from .models import AbstractSearchable +from .models import Searchable from abc import ABC @@ -71,16 +74,22 @@ class MainSearch(ListView): Search for anything. """ - model = AbstractSearchable + model = Searchable object_list = [] # this is only declaration, the view breaks without it. template_name: str = "start/search.html" + def get_queryset(self) -> QuerySet: + search = self.request.GET.get("search") + object_list = Searchable.objects.filter( + Q(title_de__icontains=search) | Q(title_en__icontains=search) | + Q(subtitle_de__icontains=search) | Q(subtitle_en__icontains=search) | + Q(desc_de__icontains=search) | Q(desc_en__icontains=search) + ) + print(object_list) + return object_list + def get(self, request, *args, **kwargs): form = MainSearchForm(request.GET) if not form.is_valid(): return render(request, "errors/bad_request.html") - - context = self.get_context_data(**kwargs) - context["searchstr"] = form.cleaned_data['search'] - #object_list = self.model.objects.filter("something") - return render(request=request, template_name=self.template_name, context=context) + return super().get(request, *args, **kwargs) diff --git a/web/requirements.txt b/web/requirements.txt index a5b754a..6f23a42 100644 --- a/web/requirements.txt +++ b/web/requirements.txt @@ -3,3 +3,4 @@ psycopg2>=2.8 mysqlclient>=1.4.3 django_compressor>=2.2 django-libsass>=0.7 +pillow>=9.0.0