Redirect searches with an info hash to torrent if found

Redirect will not happen on RSS or user page views (/searches).
Adds a helper to models.Torrent to search with a hex hash
Reformats "special search logic"
This commit is contained in:
TheAMM 2017-05-24 17:18:44 +03:00
parent 0e89d3f24b
commit 3a4280ccc5
3 changed files with 33 additions and 11 deletions

View File

@ -205,6 +205,11 @@ class Torrent(db.Model):
def by_info_hash(cls, info_hash):
return cls.query.filter_by(info_hash=info_hash).first()
@classmethod
def by_info_hash_hex(cls, info_hash_hex):
info_hash_bytes = bytearray.fromhex(info_hash_hex)
return cls.by_info_hash(info_hash_bytes)
class TorrentNameSearch(FullText, Torrent):
__fulltext_columns__ = ('display_name',)

View File

@ -182,16 +182,25 @@ def home(rss):
flask.abort(404)
user_id = user.id
user_query = {
special_results = {
'first_word_user': None,
'query_sans_user': None
'query_sans_user': None,
'infohash_torrent': None
}
if search_term:
# Add advanced features to searches (but not RSS or user searches)
if search_term and not render_as_rss and not user_id:
# Check if the first word of the search is an existing user
user_word_match = re.match(r'^([a-zA-Z0-9_-]+) *(.*|$)', search_term)
if user_word_match:
first_word_username = user_word_match.group(1)
user_query['first_word_user'] = models.User.by_username(first_word_username)
user_query['query_sans_user'] = user_word_match.group(2)
special_results['first_word_user'] = models.User.by_username(user_word_match.group(1))
special_results['query_sans_user'] = user_word_match.group(2)
# Check if search is a 40-char torrent hash
infohash_match = re.match(r'(?i)^([a-f0-9]{40})$', search_term)
if infohash_match:
# Check for info hash in database
matched_torrent = models.Torrent.by_info_hash_hex(infohash_match.group(1))
special_results['infohash_torrent'] = matched_torrent
query_args = {
'user': user_id,
@ -210,6 +219,14 @@ def home(rss):
if flask.g.user.is_moderator: # God mode
query_args['admin'] = True
infohash_torrent = special_results.get('infohash_torrent')
if infohash_torrent:
# infohash_torrent is only set if this is not RSS or userpage search
flask.flash(flask.Markup('You were redirected here because '
'the given hash matched this torrent.'), 'info')
# Redirect user from search to the torrent if we found one with the specific info_hash
return flask.redirect(flask.url_for('view_torrent', torrent_id=infohash_torrent.id))
# If searching, we get results from elastic search
use_elastic = app.config.get('USE_ELASTIC_SEARCH')
if use_elastic and search_term:
@ -243,7 +260,7 @@ def home(rss):
torrent_query=query_results,
search=query_args,
rss_filter=rss_query_string,
user_query=user_query)
special_results=special_results)
else:
# If ES is enabled, default to db search for browsing
if use_elastic:
@ -265,7 +282,7 @@ def home(rss):
torrent_query=query,
search=query_args,
rss_filter=rss_query_string,
user_query=user_query)
special_results=special_results)
@app.route('/user/<user_name>', methods=['GET', 'POST'])

View File

@ -9,10 +9,10 @@
</th>
{% endmacro %}
{% if user_query is defined %}
{% if user_query.first_word_user and not search.user %}
{% if special_results is defined and not search.user %}
{% if special_results.first_word_user %}
<div class="alert alert-info">
<a href="/user/{{ user_query.first_word_user.username }}{{ modify_query(q=user_query.query_sans_user)[1:] }}">Click here to see only results uploaded by {{ user_query.first_word_user.username }}</a>
<a href="/user/{{ special_results.first_word_user.username }}{{ modify_query(q=special_results.query_sans_user)[1:] }}">Click here to see only results uploaded by {{ special_results.first_word_user.username }}</a>
</div>
{% endif %}
{% endif %}