Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Pilot Selection #211

Merged
merged 4 commits into from Dec 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions skylines/forms/aircraft_model.py
@@ -1,10 +1,10 @@
from flask.ext.babel import _

from skylines.forms.select import SelectField
from skylines.forms.select import GroupSelectField
from skylines.model import AircraftModel


class AircraftModelSelectField(SelectField):
class AircraftModelSelectField(GroupSelectField):
def __init__(self, *args, **kwargs):
super(AircraftModelSelectField, self).__init__(*args, **kwargs)
self.coerce = int
Expand Down
5 changes: 4 additions & 1 deletion skylines/forms/flight.py
Expand Up @@ -6,11 +6,14 @@

from skylines.forms.pilot import ClubPilotsSelectField
from skylines.forms.aircraft_model import AircraftModelSelectField
from skylines.forms.validators import NotEqualTo


class ChangePilotsForm(Form):
pilot_id = ClubPilotsSelectField(l_('Pilot'))
co_pilot_id = ClubPilotsSelectField(l_('Co-Pilot'))
co_pilot_id = ClubPilotsSelectField(l_('Co-Pilot'), validators=[
NotEqualTo('pilot_id', message=l_('Pilot and co-pilot can not be the same person.')),
])


class ChangeAircraftForm(Form):
Expand Down
20 changes: 16 additions & 4 deletions skylines/forms/pilot.py
Expand Up @@ -15,17 +15,29 @@
UnitsPresetSelectField, DistanceUnitSelectField, AltitudeUnitSelectField,
LiftUnitSelectField, SpeedUnitSelectField
)
from skylines.forms.select import GroupSelectField


class ClubPilotsSelectField(SelectField):
class ClubPilotsSelectField(GroupSelectField):
def __init__(self, *args, **kwargs):
super(ClubPilotsSelectField, self).__init__(*args, **kwargs)
self.coerce = int

def process(self, *args, **kwargs):
users = User.query(club_id=g.current_user.club_id).order_by(User.name)
self.choices = [(0, '[unspecified]')]
self.choices.extend([(user.id, user) for user in users])
self.choices = [
(0, '[unspecified]'),
(g.current_user.id, g.current_user.name),
]

club = g.current_user.club
if club:
members = User.query(club_id=club.id) \
.order_by(User.name) \
.filter(User.id != g.current_user.id)

members = [(member.id, member.name) for member in members]

self.choices.append((club.name, members))

super(ClubPilotsSelectField, self).process(*args, **kwargs)

Expand Down
12 changes: 6 additions & 6 deletions skylines/forms/select.py
@@ -1,13 +1,13 @@
from wtforms.fields import SelectField as BaseSelectField
from wtforms.fields import SelectField
from wtforms.validators import ValidationError
from wtforms.widgets import Select as BaseSelectWidget
from wtforms.widgets import Select
from wtforms.widgets.core import HTMLString, html_params, escape


__all__ = ('SelectField', 'SelectWidget')
__all__ = ('GroupSelectField', 'GroupSelectWidget')


class SelectWidget(BaseSelectWidget):
class GroupSelect(Select):
"""
Add support of choices with ``optgroup`` to the ``Select`` widget.
"""
Expand Down Expand Up @@ -41,7 +41,7 @@ def render_option(cls, value, label, mixed):
return HTMLString(html % data)


class SelectField(BaseSelectField):
class GroupSelectField(SelectField):
"""
Add support of ``optgorup``'s' to default WTForms' ``SelectField`` class.

Expand All @@ -61,7 +61,7 @@ class SelectField(BaseSelectField):
)

"""
widget = SelectWidget()
widget = GroupSelect()

def iter_choices(self):
"""
Expand Down
33 changes: 33 additions & 0 deletions skylines/forms/validators.py
@@ -0,0 +1,33 @@
from wtforms.validators import ValidationError


class NotEqualTo(object):
"""
Compares the values of two fields.

:param fieldname:
The name of the other field to compare to.
:param message:
Error message to raise in case of a validation error. Can be
interpolated with `%(other_label)s` and `%(other_name)s` to provide a
more helpful error.
"""
def __init__(self, fieldname, message=None):
self.fieldname = fieldname
self.message = message

def __call__(self, form, field):
try:
other = form[self.fieldname]
except KeyError:
raise ValidationError(field.gettext("Invalid field name '%s'.") % self.fieldname)
if field.data == other.data:
d = {
'other_label': hasattr(other, 'label') and other.label.text or self.fieldname,
'other_name': self.fieldname
}
message = self.message
if message is None:
message = field.gettext('Field must be equal to %(other_name)s.')

raise ValidationError(message % d)