Browse files

add a league settings page

  • Loading branch information...
1 parent 4d817e7 commit 65ccc30c1fe7e0e1e8aac648e2903c199450a148 @artnez committed May 13, 2012
View
7 src/faceoff/forms.py
@@ -70,3 +70,10 @@ class NewLeagueForm(Form):
id='name',
validators=[Required(), Length(2), NoneOf(['new'], values_formatter=lambda v: map(str.lower, v))]
)
+
+class SettingsForm(Form):
+ name = TextField(
+ label='League Name',
+ id='name',
+ validators=[Required(), Length(2), NoneOf(['new'], values_formatter=lambda v: map(str.lower, v))]
+ )
View
21 src/faceoff/models/league.py
@@ -24,18 +24,33 @@ def get_active_leagues(db):
return search_leagues(db, active=1)
@use_db
-def create_league(db, name, slug=None, description=None, active=True):
+def create_league(db, name, description=None, active=True):
+ name = name.strip()
return db.insert(
'league',
name = name,
- slug = slug if slug else generate_league_slug(db, name),
+ slug = generate_league_slug(db, name),
description = description,
active = '1' if active else '0',
date_created = int(time())
)
@use_db
-def generate_league_slug(db, name):
+def change_league_name(db, league_id, name, slug=None):
+ league = find_league(db, id=league_id)
+ name = name.strip()
+ if league is None or league['name'] == name:
+ return league
+ db.update(
+ 'league',
+ league_id,
+ name = name,
+ slug = generate_league_slug(db, name)
+ )
+ return find_league(db, id=league_id)
+
+@use_db
+def generate_league_slug(db, name, omit=None):
short = re.sub(r'[^0-9a-zA-Z]+', '-', name.lower()).strip('-')
count = 0
while True:
View
7 src/faceoff/templates/forms/settings.html
@@ -0,0 +1,7 @@
+{% from 'macros/form.html' import render_field %}
+<form id='settings' action='{{ request.url }}' method='post'>
+ <fieldset>
+ {{ render_field(settings_form.name, errpos='inline') }}
+ <button type='submit' class='btn btn-large'>Save</button>
+ </fieldset>
+</form>
View
1 src/faceoff/templates/layouts/league.html
@@ -16,6 +16,7 @@
{'title': 'Dashboard', 'endpoint': 'dashboard'},
{'title': 'Standings', 'endpoint': 'standings'},
{'title': 'History', 'endpoint': 'history'},
+ {'title': 'Settings', 'endpoint': 'settings'}
] %}
<ul class='menu'>
{% for page in pages -%}
View
8 src/faceoff/templates/settings.html
@@ -0,0 +1,8 @@
+{% extends 'layouts/league.html' %}
+{% set head_title = 'Settings' %}
+{% block content %}
+<div class='section'>
+ <h2 class='center'><span>League Settings</span></h2>
+ {% include 'forms/settings.html' %}
+</div>
+{% endblock %}
View
18 src/faceoff/views.py
@@ -10,10 +10,10 @@
from flask import g, request, session, abort, redirect, url_for, send_from_directory
from faceoff import app
from faceoff.debug import debug
-from faceoff.forms import LoginForm, JoinForm, ReportForm, NewLeagueForm
+from faceoff.forms import LoginForm, JoinForm, ReportForm, NewLeagueForm, SettingsForm
from faceoff.helpers.decorators import authenticated, templated
from faceoff.models.user import get_active_users, create_user, auth_login, auth_logout
-from faceoff.models.league import find_league, get_active_leagues, create_league
+from faceoff.models.league import find_league, get_active_leagues, create_league, change_league_name
from faceoff.models.match import create_match, get_match_history, get_league_ranking, get_user_standing
from faceoff.models.setting import get_setting
@@ -165,3 +165,17 @@ def standings():
@authenticated
def history():
return dict(match_history=get_match_history(g.current_league['id']))
+
+@app.route('/<league>/settings/', methods=['GET', 'POST'])
+@templated()
+@authenticated
+def settings():
+ league = g.current_league
+ form = SettingsForm(request.form)
+ if request.method != 'POST':
+ form.name.data = g.current_league['name']
+ return dict(settings_form=form)
+ if form.validate():
+ league = change_league_name(league['id'], form.name.data)
+ return redirect(url_for('dashboard', league=league['slug']))
+ return dict(settings_form=form)

0 comments on commit 65ccc30

Please sign in to comment.