Skip to content

Commit

Permalink
Gravatars, better tags, better project views.
Browse files Browse the repository at this point in the history
  • Loading branch information
illume committed Mar 3, 2017
1 parent bfd2317 commit 63faa8d
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 64 deletions.
2 changes: 0 additions & 2 deletions pygameweb/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def create_app(object_name='pygameweb.config.Config',
db.init(app, engine, session_factory)
Mail(app)


# https://flask-debugtoolbar.readthedocs.io/en/latest/
if app.config['DEBUG'] and not app.config['TESTING']:
app.config['DEBUG_TB_PROFILER_ENABLED'] = True
Expand All @@ -38,7 +37,6 @@ def create_app(object_name='pygameweb.config.Config',
except TypeError:
DebugToolbarExtension(app)


from pygameweb.cache import cache, limiter
cache.init_app(app)
limiter.init_app(app)
Expand Down
3 changes: 1 addition & 2 deletions pygameweb/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
# https://flask-limiter.readthedocs.io/en/stable/
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
limiter = Limiter(key_func=get_remote_address,
global_limits=["200 per day", "50 per hour"])
limiter = Limiter(key_func=get_remote_address)
5 changes: 5 additions & 0 deletions pygameweb/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ class Config(object):
CACHE_TYPE = 'null' if truthy_config('APP_DEBUG') else 'simple'
""" flask_caching is off in debug mode.
"""

RATELIMIT_GLOBAL = ('5000 per day, 5000 per hour'
if truthy_config('APP_DEBUG')
else '200 per day, 50 per hour')

4 changes: 4 additions & 0 deletions pygameweb/project/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class Project(Base):
def summary_html(self):
return sanitize_html(self.summary)

@property
def description_html(self):
return sanitize_html(self.description)

def image_thumb(self, width, height):
""" Return path to the thumbnail for this image.
"""
Expand Down
13 changes: 11 additions & 2 deletions pygameweb/project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,24 @@ def view(project_id):
return render_template('project/view.html', project=project)


@project_blueprint.route('/project/<project_id>/<release_id>', methods=['GET'])
# @project_blueprint.route('/project/<project_id>/<release_id>', methods=['GET'])
@project_blueprint.route('/project/<int:project_id>/<int:release_id>', methods=['GET'])
def release(project_id, release_id):
""" of the wiki page.
"""
# import pdb;pdb.set_trace()
return render_template('project/view.html',
project=project_for(project_id),
release=release_for(release_id))



def inchunks(alist, chunk_size):
""" [1,2,3, 4,5,6] -> [[1,2,3], [4,5,6]]
"""
for i in range(0, len(alist), chunk_size):
yield alist[i:i + chunk_size]

@project_blueprint.route('/tags/<tag>', methods=['GET'])
def tags(tag):
""" shows the projects for the tag.
Expand All @@ -109,11 +117,12 @@ def tags(tag):
.offset(start)
.limit(per_page)
.all())

return render_template('project/tags.html',
tag=tag,
tags=tags,
top_tag_counts=top_tag_counts,
projects=projects,
projects=inchunks(projects, 3),
prev_start=prev_start,
next_start=next_start)

Expand Down
4 changes: 4 additions & 0 deletions pygameweb/static/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ def add_static_blueprint(app):
for file in files:
add_file(app, static_blueprint, file)


from pygameweb.cache import limiter
limiter.limit("1000/hour")(static_blueprint)

app.register_blueprint(static_blueprint)
7 changes: 2 additions & 5 deletions pygameweb/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ <h2>Recent Releases</h2>
</div>


<h2>Projects</h2>
<div class="list-group">
<div>
{% for value, count, size in top_tag_counts %}
{% set size2 = size//2 %}
<a href='/tags/{{value}}' class="list-group-item">{{value}}<span class="pull-right">({{count}})</span></a>

<a href='/tags/{{value}}' class="btn btn-default" role="button">{{value}} <span class="badge">{{ count }}</span></a>
{% endfor %}
</div>

Expand Down
65 changes: 35 additions & 30 deletions pygameweb/templates/project/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,45 @@
<h1>Project</h1>

{% block content %}
<h1>{{ tag }}</h1>

<h1>Tags</h1>

{% for value, count, size in top_tag_counts %}
{% set size2 = size//2 %}
<a href='/tags/{{value}}' style='font-size:{{size}}px;margin-left:{{size2}}px;margin-right:{{size2}}px;'>{{value}}</a>
{% endfor %}


<h3>{{ tag }}</h3>

<div>
{% for project in projects %}
<div>
<a href="{{ url_for('project.view', project_id=project.id) }}">{{ project.title }}</a>
<div>
{% set thumb = project.image_thumb(100,100) %}
{% if thumb %}
<a href="{{ url_for('project.view', project_id=project.id) }}">
<img src="{{ thumb }}">
</a>
{% endif %}
</div>
<div>
{{ project.summary_html|safe }}
<div class="row">
<div class="col-md-12">
{% for value, count, size in top_tag_counts %}
<a href='/tags/{{value}}' class="btn btn-default" role="button">{{value}} <span class="badge">{{ count }}</span></a>
{% endfor %}
</div>
</div>
<br>

{% for chunk in projects %}
<div class="row">
{% for project in chunk %}
<div class="col-md-4">
<div class="media">
<div class="media-left">
{% set thumb = project.image_thumb(100,100) %}
{% if thumb %}
<a href="{{ url_for('project.view', project_id=project.id) }}">
<img class="media-object" src="{{ thumb }}" alt="{{ project.title }}">
</a>
{% endif %}
</div>
<div class="media-body">
<h4 class="media-heading"><a href="{{ url_for('project.view', project_id=project.id) }}">{{ project.title }}</a></h4>
{{ project.summary_html|safe }}
</div>
</div>
<br>
</div>
{% endfor %}

{% if prev_start %}
<a href='?start={{prev_start}}'>previous</a>
{% endif %}
<a href='?start={{next_start}}'>next</a>
{% endfor %}
</div>
{% endfor %}

{% if prev_start %}
<a href='?start={{prev_start}}'>previous</a>
{% endif %}
<a href='?start={{next_start}}'>next</a>



Expand Down
70 changes: 48 additions & 22 deletions pygameweb/templates/project/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,70 @@ <h1>Project</h1>

{% block content %}
{% cache 60*5 %}


<h1>{{ project.title }}{% if release and release.version %} - {{ release.version }}{% endif %}</h1>
<h3>{{ project.user.title }} ({{ project.user.name }})</h3>

<h3>Tags:</h3>
<div>
{% for value, count, size in project.tag_counts %}
{% set size2 = size//2 %}
<a href='/tags/{{value}}' style='font-size:{{size}}px;margin-left:{{size2}}px;margin-right:{{size2}}px;'>{{value}}</a>
{% endfor %}
{# So images in description are shown the full width. #}
<style>
.row.description img, .row.screenshot img {
max-width: 100%;
}
</style>

<div class="row">
<div class="col-md-10">
<h1>{{ project.title }}{% if release and release.version %} - {{ release.version }}{% endif %}</h1>
<p class="lead">{{ project.summary_html|safe }}</p>
</div>
<div class="col-md-2">
<img src="{{ project.user.email | gravatar }}">
<br>
{{ project.user.title }}
<br>
({{ project.user.name }})
</div>
</div>

{# update tags input box. #}
<div class="row description">
<div class="col-md-12">
{{ project.description_html|safe }}
</div>
</div>

<div class="row">
<div class="col-md-6">
<img class="img-responsive" src="{{ project.image_thumb(400,400) }}">
</div>
<div class="col-md-6">
{% for value, count, size in project.tag_counts %}
<a href='/tags/{{value}}' class="btn btn-default" role="button">{{value}} <span class="badge">{{ count }}</span></a>
{% endfor %}
</div>
</div>

<h3>Description</h3>
{{ project.description_html|safe }}
<div class="row">
</div>

<h3>Links</h3>
<div>
<dl>
<dt>Home Page</dt>
<dd><a href="{{ project.uri }}">{{ project.uri }}</a></dd>
{% if release %}
<dt>Source</dt>
<dd><a href="{{ release.srcuri }}">{{ release.srcuri }}</a></dd>
<dt>Windows</dt>
<dd><a href="{{ release.winuri }}">{{ release.winuri }}</a></dd>
<dt>Mac</dt>
<dd><a href="{{ release.macuri }}">{{ release.macuri }}</a></dd>
{% if release.srcuri %}
<dt>Source</dt>
<dd><a href="{{ release.srcuri }}">{{ release.srcuri }}</a></dd>
{% endif %}
{% if release.winuri %}
<dt>Windows</dt>
<dd><a href="{{ release.winuri }}">{{ release.winuri }}</a></dd>
{% endif %}
{% if release.macuri %}
<dt>Mac</dt>
<dd><a href="{{ release.macuri }}">{{ release.macuri }}</a></dd>
{% endif %}
{% endif %}
</dl>
</div>


<h3>Screenshot</h3>
<img src="{{ project.image_thumb(400,400) }}">


<h3>Releases</h3>
Expand Down
3 changes: 3 additions & 0 deletions pygameweb/thumb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
def add_thumb_blueprint(app):
""" to the app.
"""
from pygameweb.cache import limiter
limiter.limit("1000/hour")(thumb_blueprint)

app.register_blueprint(thumb_blueprint)
9 changes: 9 additions & 0 deletions pygameweb/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ def add_user_blueprint(app):
# 'security.reset_password',
# 'security.send_confirmation',

# https://pypi.python.org/pypi/Flask-Gravatar
from flask_gravatar import Gravatar
gravatar = Gravatar(app,
size=100,
rating='g',
default='retro',
force_default=False,
use_ssl=False,
base_url=None)

from flask_security import user_confirmed
@user_confirmed.connect_via(app)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ flask_admin
flask-bootstrap
Flask-Caching
flask-debugtoolbar
Flask-Gravatar
flask_limiter
flask-nav
Flask-Security-Fork==2.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def dashboard_client(app, session, client):
def test_dashboard_view(dashboard_client, session):
""" is shown as the default.
"""
resp = dashboard_client.get('/dashboard')
resp = dashboard_client.get('/dashboard-dev')
assert resp.status_code == 200

# resp = dashboard_client.get('/screenshot-300/1.jpg')
Expand Down

0 comments on commit 63faa8d

Please sign in to comment.