added searching images

This commit is contained in:
crunk 2024-07-08 21:07:00 +02:00
parent 4352633eba
commit f71152cc78
9 changed files with 66 additions and 32 deletions

View File

@ -51,6 +51,8 @@ def create_app():
def inject_title():
return dict(title=APP.config["title"])
APP.logger.setLevel("INFO")
APP.logger.info("Distribusi-verse successfully started")
return APP

View File

@ -141,7 +141,8 @@ def save_described_file_to_db(describe_form, distribusi_file):
def add_alttext_to_file(describe_form, distribusi_file):
if not describe_form.alttext.data:
return
filename_no_ext = os.path.splitext(distribusi_file.path)[0]
filename = os.path.join("stash", distribusi_file.path)
filename_no_ext = os.path.splitext(filename)[0]
with open(f"{filename_no_ext}_alttext.txt", "w") as alttext_file:
alttext_file.write(describe_form.alttext.data)
return
@ -150,7 +151,8 @@ def add_alttext_to_file(describe_form, distribusi_file):
def add_description_to_file(describe_form, distribusi_file):
if not describe_form.description.data:
return
filename_no_ext = os.path.splitext(distribusi_file.path)[0]
filename = os.path.join("stash", distribusi_file.path)
filename_no_ext = os.path.splitext(filename)[0]
with open(
f"{filename_no_ext}_dv_description.txt", "w"
) as description_file:

View File

@ -39,10 +39,3 @@ class DescribeFilesForm(FlaskForm):
self.searchtags.id = f"searchtags-{id}"
self.description.id = f"description-{id}"
self.save.id = f"save-{id}"
# def StringFieldWithId(form_name, **kwargs):
# name = kwargs.get('name', 'Bar')
# return wtf.StringField(name, render_kw={
# 'id': f'{}{}'.format(form_name, name.lower(),
# **kwargs.get('render_kw', {})})

View File

@ -31,13 +31,13 @@
{% for id, describe_form in distribusi_file_forms.items() %}
<div class="distribusi_file">
{% if describe_form.type == "image" %}
<img src="{{describe_form.file_path}}" alt="" data-src="{{describe_form.file_path}}" loading="lazy"/>
<img src="{{ url_for('shortstashurl')}}/{{describe_form.file_path}}" alt="" data-src="{{ url_for('shortstashurl')}}/{{describe_form.file_path}}" loading="lazy"/>
{% elif describe_form.type == "video" %}
<video src="{{describe_form.file_path}}" preload="auto" alt="" controls loading="lazy"></video>
<video src="{{ url_for('shortstashurl')}}/{{describe_form.file_path}}" preload="auto" alt="" controls loading="lazy"></video>
{% elif describe_form.type == "audio" %}
<audio src="{{describe_form.file_path}}" preload="auto" alt="" controls loading="lazy"> </audio>
<audio src="{{ url_for('shortstashurl')}}/{{describe_form.file_path}}" preload="auto" alt="" controls loading="lazy"> </audio>
{% else %}
<a href="{{describe_form.file_path}}">file: {{describe_form.file_path}}</a>
<a href="{{ url_for('shortstashurl')}}/{{describe_form.file_path}}">file: {{describe_form.file_path}}</a>
{% endif %}
<form id={{id}} method="POST" enctype="multipart/form-data" action="{{ url_for('describer.describe_file', file_id=id) }}">
{{ describe_form.csrf_token }}

View File

@ -30,14 +30,14 @@ def _get_distribusi_from_path(path):
def _add_distribusi_file_to_db(distribusi, full_path, type):
app = get_app()
app.logger.info(f"adding file to database: {full_path} type: {type}")
app.logger.info(f"Adding file to database: {full_path} type: {type}")
distribusi_file = DistribusiFiles.query.filter_by(path=full_path).first()
if distribusi_file is not None:
app.logger.error(f"File already in database: {full_path}")
return
try:
new_distribusi_file = DistribusiFiles(
path=full_path,
path=full_path.lstrip("stash/"),
type=type,
distribusi=distribusi.id,
)

View File

@ -20,12 +20,16 @@ SEARCH_DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "searchdata"))
def searchpage():
searchform = SearchForm()
found_distribusis = []
found_distribusi_files = []
if searchform.validate_on_submit():
found_distribusis = search(searchform.searchfield.data)
found_distribusis, found_distribusi_files = search(
searchform.searchfield.data
)
template = render_template(
"search.html",
searchform=searchform,
found_distribusis=found_distribusis,
found_distribusi_files=found_distribusi_files,
)
return template
@ -36,7 +40,15 @@ def search(searchinput):
with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(searchinput)
search_results = searcher.search(query)
found_distribusis = []
for result in search_results:
found_distribusis.append(result["title"])
return found_distribusis
print(result["title"])
print(result["path"])
found_distribusis = []
found_distribusi_files = []
for result in search_results:
if result["path"] == "/a":
found_distribusis.append(result["title"])
if result["path"] == "/b":
found_distribusi_files.append(result["title"])
return found_distribusis, found_distribusi_files

View File

@ -3,6 +3,7 @@ from whoosh.fields import *
from whoosh.index import create_in
from whoosh.qparser import QueryParser
from models.distribusi_model import Distribusis
from models.distribusi_file_model import DistribusiFiles
import flask_apscheduler
@ -15,21 +16,26 @@ def init_search_index(APP):
scheduler.api_enabled = False
scheduler.init_app(APP)
scheduler.start()
index_distribusis(APP)
index_distribusi_files(APP)
@scheduler.task("interval", id="update", minutes=60)
def update_search_index():
index_distribusis(APP)
index_distribusi_files(APP)
def index_distribusis(APP):
schema = Schema(
title=TEXT(stored=True), path=ID(stored=True), content=TEXT
)
ix = create_in(SEARCH_DATA_DIR, schema)
writer = ix.writer()
index_distribusis(APP, writer)
index_distribusi_files(APP, writer)
writer.commit(optimize=True)
@scheduler.task("interval", id="update", minutes=60)
def update_search_index():
ix = index.open_dir(SEARCH_DATA_DIR)
update_writer = ix.writer()
index_distribusis(APP, update_writer)
index_distribusi_files(APP, update_writer)
update_writer.commit(optimize=True)
def index_distribusis(APP, writer):
distribusis = _visible_distribusis(APP)
for distribusi in distribusis:
writer.add_document(
@ -37,11 +43,19 @@ def index_distribusis(APP):
path="/a",
content=distribusi.description,
)
writer.commit(optimize=True)
def index_distribusi_files(APP):
APP.logger.info("searching distribusi files not implemented yet.")
def index_distribusi_files(APP, writer):
with APP.app_context():
for distribusi_file in DistribusiFiles.query.all():
APP.logger.info(
f"adding distribusi file {distribusi_file.path} to search index"
)
writer.add_document(
title=distribusi_file.path,
path="/b",
content=distribusi_file.description,
)
def _visible_distribusis(APP):

View File

@ -19,5 +19,13 @@
<a href='{{ url_for('shortstashurl')}}/{{found_distribusi}}/index.html'>{{found_distribusi}}</a>
{% endfor %}
</div>
<div class="searchresults">
{% for found_distribusi_file in found_distribusi_files %}
<a href="{{ url_for('shortstashurl')}}/{{found_distribusi_file}}">
<img src="{{ url_for('shortstashurl')}}/{{found_distribusi_file}}" alt="">
{{found_distribusi_file}}
</a>
{% endfor %}
</div>
</div>
{% endblock %}

View File

@ -8,7 +8,10 @@ body
line-height: 1.1;
}
img {
width: 100%;
height: 100%;
}
#mainworkflow
{