Browse Source

added searching images

main
crunk 4 months ago
parent
commit
f71152cc78
  1. 2
      verse/app.py
  2. 6
      verse/describer/describe_files.py
  3. 7
      verse/describer/forms/describe_files_form.py
  4. 8
      verse/describer/templates/describe_files/describe.html
  5. 4
      verse/distribusikan/add_files_to_describer.py
  6. 18
      verse/search/search.py
  7. 38
      verse/search/search_index.py
  8. 8
      verse/search/templates/search/search.html
  9. 5
      verse/static/css/style.css

2
verse/app.py

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

6
verse/describer/describe_files.py

@ -141,7 +141,8 @@ def save_described_file_to_db(describe_form, distribusi_file):
def add_alttext_to_file(describe_form, distribusi_file): def add_alttext_to_file(describe_form, distribusi_file):
if not describe_form.alttext.data: if not describe_form.alttext.data:
return 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: with open(f"{filename_no_ext}_alttext.txt", "w") as alttext_file:
alttext_file.write(describe_form.alttext.data) alttext_file.write(describe_form.alttext.data)
return return
@ -150,7 +151,8 @@ def add_alttext_to_file(describe_form, distribusi_file):
def add_description_to_file(describe_form, distribusi_file): def add_description_to_file(describe_form, distribusi_file):
if not describe_form.description.data: if not describe_form.description.data:
return 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( with open(
f"{filename_no_ext}_dv_description.txt", "w" f"{filename_no_ext}_dv_description.txt", "w"
) as description_file: ) as description_file:

7
verse/describer/forms/describe_files_form.py

@ -39,10 +39,3 @@ class DescribeFilesForm(FlaskForm):
self.searchtags.id = f"searchtags-{id}" self.searchtags.id = f"searchtags-{id}"
self.description.id = f"description-{id}" self.description.id = f"description-{id}"
self.save.id = f"save-{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', {})})

8
verse/describer/templates/describe_files/describe.html

@ -31,13 +31,13 @@
{% for id, describe_form in distribusi_file_forms.items() %} {% for id, describe_form in distribusi_file_forms.items() %}
<div class="distribusi_file"> <div class="distribusi_file">
{% if describe_form.type == "image" %} {% 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" %} {% 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" %} {% 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 %} {% 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 %} {% endif %}
<form id={{id}} method="POST" enctype="multipart/form-data" action="{{ url_for('describer.describe_file', file_id=id) }}"> <form id={{id}} method="POST" enctype="multipart/form-data" action="{{ url_for('describer.describe_file', file_id=id) }}">
{{ describe_form.csrf_token }} {{ describe_form.csrf_token }}

4
verse/distribusikan/add_files_to_describer.py

@ -30,14 +30,14 @@ def _get_distribusi_from_path(path):
def _add_distribusi_file_to_db(distribusi, full_path, type): def _add_distribusi_file_to_db(distribusi, full_path, type):
app = get_app() 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() distribusi_file = DistribusiFiles.query.filter_by(path=full_path).first()
if distribusi_file is not None: if distribusi_file is not None:
app.logger.error(f"File already in database: {full_path}") app.logger.error(f"File already in database: {full_path}")
return return
try: try:
new_distribusi_file = DistribusiFiles( new_distribusi_file = DistribusiFiles(
path=full_path, path=full_path.lstrip("stash/"),
type=type, type=type,
distribusi=distribusi.id, distribusi=distribusi.id,
) )

18
verse/search/search.py

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

38
verse/search/search_index.py

@ -3,6 +3,7 @@ from whoosh.fields import *
from whoosh.index import create_in from whoosh.index import create_in
from whoosh.qparser import QueryParser from whoosh.qparser import QueryParser
from models.distribusi_model import Distribusis from models.distribusi_model import Distribusis
from models.distribusi_file_model import DistribusiFiles
import flask_apscheduler import flask_apscheduler
@ -15,21 +16,26 @@ def init_search_index(APP):
scheduler.api_enabled = False scheduler.api_enabled = False
scheduler.init_app(APP) scheduler.init_app(APP)
scheduler.start() 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( schema = Schema(
title=TEXT(stored=True), path=ID(stored=True), content=TEXT title=TEXT(stored=True), path=ID(stored=True), content=TEXT
) )
ix = create_in(SEARCH_DATA_DIR, schema) ix = create_in(SEARCH_DATA_DIR, schema)
writer = ix.writer() 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) distribusis = _visible_distribusis(APP)
for distribusi in distribusis: for distribusi in distribusis:
writer.add_document( writer.add_document(
@ -37,11 +43,19 @@ def index_distribusis(APP):
path="/a", path="/a",
content=distribusi.description, content=distribusi.description,
) )
writer.commit(optimize=True)
def index_distribusi_files(APP): def index_distribusi_files(APP, writer):
APP.logger.info("searching distribusi files not implemented yet.") 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): def _visible_distribusis(APP):

8
verse/search/templates/search/search.html

@ -19,5 +19,13 @@
<a href='{{ url_for('shortstashurl')}}/{{found_distribusi}}/index.html'>{{found_distribusi}}</a> <a href='{{ url_for('shortstashurl')}}/{{found_distribusi}}/index.html'>{{found_distribusi}}</a>
{% endfor %} {% endfor %}
</div> </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> </div>
{% endblock %} {% endblock %}

5
verse/static/css/style.css

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

Loading…
Cancel
Save