Skip to content

Commit

Permalink
Update the cache mechanizm (closes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Chojnowski committed Nov 28, 2014
1 parent 5e7cc67 commit 249b0e1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pyfsw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ def init_globals():
from pyfsw.helpers import *

from pyfsw.views import news, forum, account, community, community_guilds, library, shop
from pyfsw.views import error, captcha, outfit, paypal, zaypay
from pyfsw.views import error, captcha, messages, outfit, paypal, zaypay
from pyfsw.views.admin import dashboard, news, forum, community, shop
8 changes: 8 additions & 0 deletions pyfsw/static/js/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$(function() {
$.get('/messages', function(data) {
if(!data.length) return;

$('#messages').show();
$('#messages').append(data);
});
});
24 changes: 24 additions & 0 deletions pyfsw/templates/messages.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% with messages = get_flashed_messages(category_filter=['error']) %}
{% if messages %}
<div class="alert alert-danger">
<strong>Error</strong>
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}
{% with messages = get_flashed_messages(category_filter=['success']) %}
{% if messages %}
<div class="alert alert-success">
<strong>Success</strong>
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}
27 changes: 2 additions & 25 deletions pyfsw/templates/template.htm
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,7 @@
</div>
</div>
<div class="container">
{% with messages = get_flashed_messages(category_filter=['error']) %}
{% if messages %}
<div class="alert alert-danger">
<strong>Error</strong>
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}

{% with messages = get_flashed_messages(category_filter=['success']) %}
{% if messages %}
<div class="alert alert-success">
<strong>Success</strong>
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}
<div id="messages"></div>

{% block body %}
{% endblock %}
Expand All @@ -130,5 +106,6 @@
</div>
</div>
<script type="text/javascript" src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/custom.js') }}"></script>
</body>
</html>
20 changes: 10 additions & 10 deletions pyfsw/views/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def route_community_player_get():
return render_template('community/player_search.htm')

@app.route('/community/player/<string:name>')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_player_get_name(name):
player = Player.query.filter(Player.name == name).first()

Expand Down Expand Up @@ -61,7 +61,7 @@ def route_community_player_get_name(name):
)

@app.route('/community/player', methods=['POST'])
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_player_post():
name = request.form.get('name', '', type=str)
player = Player.query.filter(Player.name == name).first()
Expand Down Expand Up @@ -96,7 +96,7 @@ def route_community_player_post():
)

@app.route('/community/highscores/<string:type>/<int:page>')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_highscores(type, page):
current = HS_TYPES.get(type)
if not current:
Expand Down Expand Up @@ -131,7 +131,7 @@ def route_community_highscores(type, page):
)

@app.route('/community/houses/<int:town_id>')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_houses(town_id):
houses = House.query.order_by(House.id)

Expand All @@ -148,7 +148,7 @@ def route_community_houses(town_id):
return render_template('community/houses.htm', towns=TOWNS, town_id=town_id, price=HOUSE_PRICE, houses=houses)

@app.route('/community/staff')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_staff():
staff = db.session().query(
Player.name, Player.lastlogin, Player.lastlogout, Player.group_id,
Expand All @@ -158,7 +158,7 @@ def route_community_staff():
return render_template('community/staff.htm', staff=staff)

@app.route('/community/deaths')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_deaths():
deaths = PlayerDeath.query.limit(25).all()
for death in deaths:
Expand All @@ -171,7 +171,7 @@ def route_community_deaths():
return render_template('community/deaths.htm', deaths=deaths)

@app.route('/community/online')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_online():
ids = []
for entry in PlayerOnline.query.all():
Expand All @@ -190,7 +190,7 @@ def route_community_online():
return render_template('community/online.htm', online=online)

@app.route('/community/market')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_market():
sell = MarketOffer.query.filter(MarketOffer.sale == 1).all()
for offer in sell:
Expand All @@ -210,7 +210,7 @@ def route_community_market():
return render_template('community/market.htm', sell=sell, buy=buy, history=history)

@app.route('/community/wars')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_wars():
active = GuildWar.query.filter(GuildWar.status == GuildWar.Active).all()
pending = GuildWar.query.filter(GuildWar.status == GuildWar.Pending).all()
Expand All @@ -219,7 +219,7 @@ def route_community_wars():
return render_template('community/wars.htm', active=active, pending=pending, ended=ended)

@app.route('/community/war/<int:id>')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_war(id):
war = db.session().query(GuildWar.id, GuildWar.guild1, GuildWar.guild2).filter(GuildWar.id == id).first()
if not war:
Expand Down
4 changes: 2 additions & 2 deletions pyfsw/views/community_guilds.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
GUILD_NAME_EXPR = re.compile('^([a-zA-Z ]+)$')

@app.route('/community/guilds')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_guilds():
guilds = Guild.query.order_by(Guild.name).all()
return render_template('community/guilds/list.htm', guilds=guilds, user=current_user())

@app.route('/community/guild/<int:id>')
@cache.cached(timeout=CACHE_TIME)
@cache.memoize(timeout=CACHE_TIME)
def route_community_guild(id):
guild = Guild.query.filter(Guild.id == id).first()
members = GuildMembership.query.filter(GuildMembership.guild_id == guild.id).all()
Expand Down
7 changes: 7 additions & 0 deletions pyfsw/views/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from flask import render_template

from pyfsw import app

@app.route('/messages')
def route_messages():
return render_template('messages.htm')

0 comments on commit 249b0e1

Please sign in to comment.