Merge pull request #194 from shm0o/url-stuff

More `url_for`
This commit is contained in:
Anna-Maria Meriniemi 2017-05-28 02:42:42 +03:00 committed by GitHub
commit f02836f75e
4 changed files with 32 additions and 33 deletions

View File

@ -52,7 +52,7 @@ def redirect_url():
@app.template_global() @app.template_global()
def static_cachebuster(static_filename): def static_cachebuster(filename):
''' Adds a ?t=<mtime> cachebuster to the given path, if the file exists. ''' Adds a ?t=<mtime> cachebuster to the given path, if the file exists.
Results are cached in memory and persist until app restart! ''' Results are cached in memory and persist until app restart! '''
# Instead of timestamps, we could use commit hashes (we already load it in __init__) # Instead of timestamps, we could use commit hashes (we already load it in __init__)
@ -61,19 +61,18 @@ def static_cachebuster(static_filename):
if app.debug: if app.debug:
# Do not bust cache on debug (helps debugging) # Do not bust cache on debug (helps debugging)
return static_filename return flask.url_for('static', filename=filename)
# Get file mtime if not already cached. # Get file mtime if not already cached.
if static_filename not in _static_cache: if filename not in _static_cache:
file_path = os.path.join(app.config['BASE_DIR'], 'nyaa', static_filename[1:]) file_path = os.path.join(app.static_folder, filename)
file_mtime = None
if os.path.exists(file_path): if os.path.exists(file_path):
file_mtime = int(os.path.getmtime(file_path)) file_mtime = int(os.path.getmtime(file_path))
_static_cache[static_filename] = static_filename + '?t=' + str(file_mtime)
else:
# Throw a warning?
_static_cache[static_filename] = static_filename
return _static_cache[static_filename] _static_cache[filename] = file_mtime
return flask.url_for('static', filename=filename, t=_static_cache[filename])
@app.template_global() @app.template_global()

View File

@ -6,9 +6,9 @@
<meta name="viewport" content="width=device-width"> <meta name="viewport" content="width=device-width">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="shortcut icon" type="image/png" href="/static/favicon.png"> <link rel="shortcut icon" type="image/png" href="{{ url_for('static', filename='favicon.png') }}">
<link rel="icon" type="image/png" href="/static/favicon.png"> <link rel="icon" type="image/png" href="{{ url_for('static', filename='favicon.png') }}">
<link rel="mask-icon" href="/static/pinned-tab.svg" color="#3582F7"> <link rel="mask-icon" href="{{ url_for('static', filename='pinned-tab.svg') }}" color="#3582F7">
<link rel="alternate" type="application/rss+xml" href="{% if rss_filter %}{{ url_for('home', page='rss', _external=True, **rss_filter) }}{% else %}{{ url_for('home', page='rss', _external=True) }}{% endif %}" /> <link rel="alternate" type="application/rss+xml" href="{% if rss_filter %}{{ url_for('home', page='rss', _external=True, **rss_filter) }}{% else %}{{ url_for('home', page='rss', _external=True) }}{% endif %}" />
<meta property="og:site_name" content="{{ config.SITE_NAME }}"> <meta property="og:site_name" content="{{ config.SITE_NAME }}">
@ -25,10 +25,10 @@
make the navbar not look awful on tablets. make the navbar not look awful on tablets.
--> -->
{# These are extracted here for the dark mode toggle #} {# These are extracted here for the dark mode toggle #}
{% set bootstrap_light = static_cachebuster('/static/css/bootstrap.min.css') %} {% set bootstrap_light = static_cachebuster('css/bootstrap.min.css') %}
{% set bootstrap_dark = static_cachebuster('/static/css/bootstrap-dark.min.css') %} {% set bootstrap_dark = static_cachebuster('css/bootstrap-dark.min.css') %}
<link href="{{ bootstrap_light }}" rel="stylesheet" id="bsThemeLink"> <link href="{{ bootstrap_light }}" rel="stylesheet" id="bsThemeLink">
<link href="{{ static_cachebuster('/static/css/bootstrap-xl-mod.css') }}" rel="stylesheet"> <link href="{{ static_cachebuster('css/bootstrap-xl-mod.css') }}" rel="stylesheet">
<!-- <!--
This theme changer script needs to be inline and right under the above stylesheet link to prevent FOUC (Flash Of Unstyled Content) This theme changer script needs to be inline and right under the above stylesheet link to prevent FOUC (Flash Of Unstyled Content)
Development version is commented out in static/js/main.js at the bottom of the file Development version is commented out in static/js/main.js at the bottom of the file
@ -38,15 +38,15 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" />
<!-- Custom styles for this template --> <!-- Custom styles for this template -->
<link href="{{ static_cachebuster('/static/css/main.css') }}" rel="stylesheet"> <link href="{{ static_cachebuster('css/main.css') }}" rel="stylesheet">
<!-- Core JavaScript --> <!-- Core JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha256-U5ZEeKfGNOja007MMD3YBI0A3OSZOQbeG6z2f2Y0hu8=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha256-U5ZEeKfGNOja007MMD3YBI0A3OSZOQbeG6z2f2Y0hu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/commonmark/0.27.0/commonmark.min.js" integrity="sha256-10JreQhQG80GtKuzsioj0K46DlaB/CK/EG+NuG0q97E=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/commonmark/0.27.0/commonmark.min.js" integrity="sha256-10JreQhQG80GtKuzsioj0K46DlaB/CK/EG+NuG0q97E=" crossorigin="anonymous"></script>
<!-- Modified to not apply border-radius to selectpickers and stuff so our navbar looks cool --> <!-- Modified to not apply border-radius to selectpickers and stuff so our navbar looks cool -->
<script src="{{ static_cachebuster('/static/js/bootstrap-select.js') }}"></script> <script src="{{ static_cachebuster('js/bootstrap-select.js') }}"></script>
<script src="{{ static_cachebuster('/static/js/main.js') }}"></script> <script src="{{ static_cachebuster('js/main.js') }}"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]> <!--[if lt IE 9]>
@ -65,21 +65,21 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<a class="navbar-brand" href="/">{{ config.SITE_NAME }}</a> <a class="navbar-brand" href="{{ url_for('home') }}">{{ config.SITE_NAME }}</a>
</div> </div>
{% set search_username = (user.username + ("'" if user.username[-1] == 's' else "'s")) if user_page else None %} {% set search_username = (user.username + ("'" if user.username[-1] == 's' else "'s")) if user_page else None %}
{% set search_placeholder = 'Search {} torrents...'.format(search_username) if user_page else 'Search...' %} {% set search_placeholder = 'Search {} torrents...'.format(search_username) if user_page else 'Search...' %}
<div id="navbar" class="navbar-collapse collapse"> <div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li {% if request.path == "/upload" %} class="active"{% endif %}><a href="/upload">Upload</a></li> <li {% if request.path == url_for('upload') %}class="active"{% endif %}><a href="{{ url_for('upload') }}">Upload</a></li>
<li class="dropdown"> <li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
About About
<span class="caret"></span> <span class="caret"></span>
</a> </a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li {% if request.path == "/rules" %} class="active"{% endif %}><a href="/rules">Rules</a></li> <li {% if request.path == url_for('site_rules') %}class="active"{% endif %}><a href="{{ url_for('site_rules') }}">Rules</a></li>
<li {% if request.path == "/help" %} class="active"{% endif %}><a href="/help">Help</a></li> <li {% if request.path == url_for('site_help') %}class="active"{% endif %}><a href="{{ url_for('site_help') }}">Help</a></li>
</ul> </ul>
</li> </li>
<li><a href="{% if rss_filter %}{{ url_for('home', page='rss', **rss_filter) }}{% else %}{{ url_for('home', page='rss') }}{% endif %}">RSS</a></li> <li><a href="{% if rss_filter %}{{ url_for('home', page='rss', **rss_filter) }}{% else %}{{ url_for('home', page='rss') }}{% endif %}">RSS</a></li>
@ -116,13 +116,13 @@
</a> </a>
</li> </li>
<li> <li>
<a href="/profile"> <a href="{{ url_for('profile') }}">
<i class="fa fa-gear fa-fw"></i> <i class="fa fa-gear fa-fw"></i>
Profile Profile
</a> </a>
</li> </li>
<li> <li>
<a href="/logout"> <a href="{{ url_for('logout') }}">
<i class="fa fa-times fa-fw"></i> <i class="fa fa-times fa-fw"></i>
Logout Logout
</a> </a>
@ -142,13 +142,13 @@
</a> </a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li> <li>
<a href="/login"> <a href="{{ url_for('login') }}">
<i class="fa fa-sign-in fa-fw"></i> <i class="fa fa-sign-in fa-fw"></i>
Login Login
</a> </a>
</li> </li>
<li> <li>
<a href="/register"> <a href="{{ url_for('register') }}">
<i class="fa fa-pencil fa-fw"></i> <i class="fa fa-pencil fa-fw"></i>
Register Register
</a> </a>
@ -204,7 +204,7 @@
{% if user_page %} {% if user_page %}
<form class="navbar-form navbar-right form" action="{{ url_for('view_user', user_name=user.username) }}" method="get"> <form class="navbar-form navbar-right form" action="{{ url_for('view_user', user_name=user.username) }}" method="get">
{% else %} {% else %}
<form class="navbar-form navbar-right form" action="/" method="get"> <form class="navbar-form navbar-right form" action="{{ url_for('home') }}" method="get">
{% endif %} {% endif %}
<input type="text" class="form-control" name="q" placeholder="{{ search_placeholder }}" value="{{ search["term"] if search is defined else '' }}"> <input type="text" class="form-control" name="q" placeholder="{{ search_placeholder }}" value="{{ search["term"] if search is defined else '' }}">
@ -240,7 +240,7 @@
{% if user_page %} {% if user_page %}
<form class="navbar-form navbar-right form" action="{{ url_for('view_user', user_name=user.username) }}" method="get"> <form class="navbar-form navbar-right form" action="{{ url_for('view_user', user_name=user.username) }}" method="get">
{% else %} {% else %}
<form class="navbar-form navbar-right form" action="/" method="get"> <form class="navbar-form navbar-right form" action="{{ url_for('home') }}" method="get">
{% endif %} {% endif %}
<div class="input-group search-container hidden-xs hidden-sm"> <div class="input-group search-container hidden-xs hidden-sm">
<input type="text" class="form-control search-bar" name="q" placeholder="{{ search_placeholder }}" value="{{ search["term"] if search is defined else '' }}"> <input type="text" class="form-control search-bar" name="q" placeholder="{{ search_placeholder }}" value="{{ search["term"] if search is defined else '' }}">

View File

@ -12,7 +12,7 @@
{% if special_results is defined and not search.user %} {% if special_results is defined and not search.user %}
{% if special_results.first_word_user %} {% if special_results.first_word_user %}
<div class="alert alert-info"> <div class="alert alert-info">
<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> <a href="{{ url_for('view_user', user_name=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> </div>
{% endif %} {% endif %}
{% endif %} {% endif %}
@ -62,11 +62,11 @@
{% set icon_dir = config.SITE_FLAVOR %} {% set icon_dir = config.SITE_FLAVOR %}
<td style="padding:0 4px;"> <td style="padding:0 4px;">
{% if use_elastic %} {% if use_elastic %}
<a href="/?c={{ cat_id }}" title="{{ category_name(cat_id) }}"> <a href="{{ url_for('home', c=cat_id) }}" title="{{ category_name(cat_id) }}">
{% else %} {% else %}
<a href="/?c={{ cat_id }}" title="{{ torrent.main_category.name }} - {{ torrent.sub_category.name }}"> <a href="{{ url_for('home', c=cat_id) }}" title="{{ torrent.main_category.name }} - {{ torrent.sub_category.name }}">
{% endif %} {% endif %}
<img src="/static/img/icons/{{ icon_dir }}/{{ cat_id }}.png" alt="{{ category_name(cat_id) }}"> <img src="{{ url_for('static', filename='img/icons/%s/%s.png'|format(icon_dir, cat_id)) }}" alt="{{ category_name(cat_id) }}">
</a> </a>
</td> </td>
<td colspan="2"> <td colspan="2">

View File

@ -10,7 +10,7 @@
<div class="panel-heading"{% if torrent.hidden %} style="background-color: darkgray;"{% endif %}> <div class="panel-heading"{% if torrent.hidden %} style="background-color: darkgray;"{% endif %}>
<h3 class="panel-title"> <h3 class="panel-title">
{% if can_edit %} {% if can_edit %}
<a href="{{ request.url }}/edit" title="Edit torrent"><i class="fa fa-fw fa-pencil"></i></a> <a href="{{ url_for('edit_torrent', torrent_id=torrent.id) }}" title="Edit torrent"><i class="fa fa-fw fa-pencil"></i></a>
{% endif %} {% endif %}
{{ torrent.display_name }} {{ torrent.display_name }}
</h3> </h3>