zipgrep: Add ability to filter by package type

This commit is contained in:
rubenwardy
2025-04-27 18:08:42 +01:00
parent 3a468a9b85
commit 4e502f38aa
3 changed files with 15 additions and 10 deletions

View File

@@ -14,27 +14,27 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from celery import uuid from celery import uuid
from flask import Blueprint, render_template, redirect, request, abort, url_for from flask import Blueprint, render_template, redirect, request, abort, url_for
from flask_babel import lazy_gettext from flask_babel import lazy_gettext
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, BooleanField, SubmitField from wtforms import StringField, BooleanField, SubmitField, SelectMultipleField
from wtforms.validators import InputRequired, Length from wtforms.validators import InputRequired, Length, Optional
from app.tasks import celery from app.tasks import celery
from app.utils import rank_required from app.utils import rank_required
bp = Blueprint("zipgrep", __name__) bp = Blueprint("zipgrep", __name__)
from app.models import UserRank, Package from app.models import UserRank, Package, PackageType
from app.tasks.zipgrep import search_in_releases from app.tasks.zipgrep import search_in_releases
class SearchForm(FlaskForm): class SearchForm(FlaskForm):
query = StringField(lazy_gettext("Text to find (regex)"), [InputRequired(), Length(1, 100)]) query = StringField(lazy_gettext("Text to find (regex)"), [InputRequired(), Length(1, 100)])
file_filter = StringField(lazy_gettext("File filter"), [InputRequired(), Length(1, 100)], default="*.lua") file_filter = StringField(lazy_gettext("File filter"), [InputRequired(), Length(1, 100)], default="*.lua")
remember_me = BooleanField(lazy_gettext("Remember me"), default=True) type = SelectMultipleField(lazy_gettext("Type"), [Optional()],
choices=PackageType.choices(), coerce=PackageType.coerce)
submit = SubmitField(lazy_gettext("Search")) submit = SubmitField(lazy_gettext("Search"))
@@ -44,7 +44,7 @@ def zipgrep_search():
form = SearchForm(request.form) form = SearchForm(request.form)
if form.validate_on_submit(): if form.validate_on_submit():
task_id = uuid() task_id = uuid()
search_in_releases.apply_async((form.query.data, form.file_filter.data), task_id=task_id) search_in_releases.apply_async((form.query.data, form.file_filter.data, [x.name for x in form.type.data]), task_id=task_id)
result_url = url_for("zipgrep.view_results", id=task_id) result_url = url_for("zipgrep.view_results", id=task_id)
return redirect(url_for("tasks.check", id=task_id, r=result_url)) return redirect(url_for("tasks.check", id=task_id, r=result_url))

View File

@@ -16,16 +16,20 @@
import subprocess import subprocess
import sys import sys
from subprocess import Popen, PIPE from subprocess import Popen, PIPE, TimeoutExpired
from typing import Optional from typing import Optional, List
from app.models import Package, PackageState, PackageRelease from app.models import Package, PackageState, PackageRelease
from app.tasks import celery from app.tasks import celery
@celery.task(bind=True) @celery.task(bind=True)
def search_in_releases(self, query: str, file_filter: str): def search_in_releases(self, query: str, file_filter: str, types: List[str]):
packages = list(Package.query.filter(Package.state == PackageState.APPROVED).all()) pkg_query = Package.query.filter(Package.state == PackageState.APPROVED)
if len(types) > 0:
pkg_query = pkg_query.filter(Package.type.in_(types))
packages = list(pkg_query.all())
results = [] results = []
total = len(packages) total = len(packages)

View File

@@ -17,6 +17,7 @@
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field(form.query, hint=self.query_hint()) }} {{ render_field(form.query, hint=self.query_hint()) }}
{{ render_field(form.file_filter, hint="Supports wildcards and regex") }} {{ render_field(form.file_filter, hint="Supports wildcards and regex") }}
{{ render_field(form.type, hint=_("Use shift to select multiple. Leave selection empty to match any type.")) }}
{{ render_submit_field(form.submit, tabindex=180) }} {{ render_submit_field(form.submit, tabindex=180) }}
</form> </form>