Skip to content

Commit 0a2f3f5

Browse files
committed
Validate start time and duration of event form
1 parent a556150 commit 0a2f3f5

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

filters.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import constants
22

33

4-
def formatted_duration(duration, input='hour'):
5-
base_duration = float(duration * constants.TIME_UNITS[input])
4+
def formatted_duration(duration, input_units):
5+
base_duration = float(duration * constants.TIME_UNITS[input_units])
6+
if base_duration < 1:
7+
return '<1 %s' % constants.TIME_ABBREVIATIONS['second']
68
parts = []
79
for unit, value in constants.TIME_UNITS.items()[::-1]:
810
if base_duration >= value:

forms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from sqlalchemy import func
55
from wtforms import ValidationError
66
from wtforms.fields import *
7-
from wtforms.validators import InputRequired, Length
7+
from wtforms.validators import *
88
from wtforms.widgets import TextArea
99

1010
import util
@@ -50,8 +50,8 @@ def validate_username(self, field):
5050

5151
class EventForm(Form):
5252
title = StringField('Title', validators=[InputRequired(), Length(max=256)])
53-
start_time = IntegerField('Start Time', validators=[InputRequired()])
54-
duration = FloatField('Duration (hours)', validators=[InputRequired()])
53+
start_time = IntegerField('Start Time', validators=[InputRequired(), NumberRange(min=0, message='Start time must be >0!')])
54+
duration = FloatField('Duration (hours)', validators=[InputRequired(), NumberRange(min=0, message='Start time must be >0!')])
5555
description = StringField('Description', widget=TextArea(), validators=[InputRequired(), Length(max=1024)])
5656
link = StringField('Link', validators=[InputRequired(), Length(max=256)])
5757

templates/events/detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<br/>
1010
Start: {{ event.start_time }}
1111
<br/>
12-
Duration: {{ event.duration }}
12+
Duration: {{ event.duration|duration('hour') }}
1313
<br/>
1414
Link: <a href="{{ event.link }}">link</a>
1515
<br/>

templates/events/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<td><a href="{{ url_for('events.events_detail', event_id=event.id) }}">{{ event.title }}</a></td>
4545
<td>{{ event.description }}</td>
4646
<td><time class="timeago" datetime="{{ event.start_time_format }}">{{ event.start_time_format }}</time></td>
47-
<td>{{ event.duration|duration }}</td>
47+
<td>{{ event.duration|duration('hour') }}</td>
4848
<td><a href="{{ event.link }}" target="_blank">Website</a></td>
4949
{% if enabled_actions %}
5050
<td>

0 commit comments

Comments
 (0)