zipgrep: Add ability to filter by package type
This commit is contained in:
@@ -14,27 +14,27 @@
|
||||
# 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/>.
|
||||
|
||||
|
||||
from celery import uuid
|
||||
from flask import Blueprint, render_template, redirect, request, abort, url_for
|
||||
from flask_babel import lazy_gettext
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, BooleanField, SubmitField
|
||||
from wtforms.validators import InputRequired, Length
|
||||
from wtforms import StringField, BooleanField, SubmitField, SelectMultipleField
|
||||
from wtforms.validators import InputRequired, Length, Optional
|
||||
|
||||
from app.tasks import celery
|
||||
from app.utils import rank_required
|
||||
|
||||
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
|
||||
|
||||
|
||||
class SearchForm(FlaskForm):
|
||||
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")
|
||||
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"))
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ def zipgrep_search():
|
||||
form = SearchForm(request.form)
|
||||
if form.validate_on_submit():
|
||||
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)
|
||||
return redirect(url_for("tasks.check", id=task_id, r=result_url))
|
||||
|
||||
|
||||
@@ -16,16 +16,20 @@
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
from subprocess import Popen, PIPE
|
||||
from typing import Optional
|
||||
from subprocess import Popen, PIPE, TimeoutExpired
|
||||
from typing import Optional, List
|
||||
|
||||
from app.models import Package, PackageState, PackageRelease
|
||||
from app.tasks import celery
|
||||
|
||||
|
||||
@celery.task(bind=True)
|
||||
def search_in_releases(self, query: str, file_filter: str):
|
||||
packages = list(Package.query.filter(Package.state == PackageState.APPROVED).all())
|
||||
def search_in_releases(self, query: str, file_filter: str, types: List[str]):
|
||||
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 = []
|
||||
|
||||
total = len(packages)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field(form.query, hint=self.query_hint()) }}
|
||||
{{ 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) }}
|
||||
</form>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user