Skip to content

Commit 1cf2d63

Browse files
author
Michael Zhang
committed
Merge branch 'master' of github.com:easyctf/ctf-calendar
2 parents 2f60261 + 17161f3 commit 1cf2d63

File tree

15 files changed

+165
-33
lines changed

15 files changed

+165
-33
lines changed

Vagrantfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Vagrant.configure(2) do |config|
2424
# accessing "localhost:8080" will access port 80 on the guest machine.
2525
# config.vm.network "forwarded_port", guest: 80, host: 8080
2626
config.vm.network "forwarded_port", guest: 5000, host: 5000
27+
config.vm.network "forwarded_port", guest: 5432, host: 5432
2728

2829
# Create a private network, which allows host-only access to the machine
2930
# using a specific IP.

cal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import config
66
import views
7+
from filters import filters
78
from models import db, login_manager
89

910
app = Flask(__name__, static_url_path='')
@@ -19,3 +20,6 @@
1920

2021
app.jinja_env.trim_blocks = True
2122
app.jinja_env.lstrip_blocks = True
23+
24+
for name, func in filters.items():
25+
app.add_template_filter(func, name=name)

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def _load_secret_key(self):
2828
self.SECRET_KEY = os.environ['SECRET_KEY']
2929
else:
3030
secret_path = self.app_root / '.secret_key'
31-
with secret_path.open('a+b') as secret_file:
31+
with secret_path.open('rb') as secret_file:
3232
secret_file.seek(0)
3333
contents = secret_file.read()
3434
if not contents and len(contents) == 0:

constants.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections import OrderedDict
2+
3+
SECOND = 1
4+
MINUTE = 60 * SECOND
5+
HOUR = 60 * MINUTE
6+
DAY = 24 * HOUR
7+
WEEK = 7 * DAY
8+
9+
TIME_UNITS = OrderedDict([
10+
('second', SECOND),
11+
('minute', MINUTE),
12+
('hour', HOUR),
13+
('day', DAY),
14+
('week', WEEK),
15+
])
16+
17+
TIME_ABBREVIATIONS = OrderedDict([
18+
('second', 's'),
19+
('minute', 'm'),
20+
('hour', 'h'),
21+
('day', 'd'),
22+
('week', 'w'),
23+
])

filters.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import constants
2+
3+
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']
8+
parts = []
9+
for unit, value in constants.TIME_UNITS.items()[::-1]:
10+
if base_duration >= value:
11+
parts.append((base_duration / value, constants.TIME_ABBREVIATIONS[unit]))
12+
base_duration %= value
13+
return ' '.join('%d %s' % (value, abbr) for value, abbr in parts)
14+
15+
16+
filters = {
17+
'duration': formatted_duration,
18+
}

forms.py

Lines changed: 5 additions & 7 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,15 +50,13 @@ 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',
54+
validators=[InputRequired(), NumberRange(min=0, max=2147483647, message='Start time must be between 0 and 2147483647!')])
55+
duration = FloatField('Duration (hours)',
56+
validators=[InputRequired(), NumberRange(min=0, max=2147483647, message='Duration must be between 0 and 2147483647!')])
5557
description = StringField('Description', widget=TextArea(), validators=[InputRequired(), Length(max=1024)])
5658
link = StringField('Link', validators=[InputRequired(), Length(max=256)])
5759

5860
def validate_link(self, field):
5961
if not any(field.data.startswith(prefix) for prefix in [u'http://', u'https://']):
6062
raise ValidationError('Invalid link')
61-
62-
63-
class EventUpdateForm(EventForm):
64-
id = HiddenField()

migrations/versions/daf5b5301c75_.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""empty message
2+
3+
Revision ID: daf5b5301c75
4+
Revises: f79ba2365689
5+
Create Date: 2016-06-30 10:08:05.129124
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = 'daf5b5301c75'
11+
down_revision = 'f79ba2365689'
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
16+
17+
def upgrade():
18+
### commands auto generated by Alembic - please adjust! ###
19+
op.add_column('eventvotes', sa.Column('user_id', sa.Integer(), nullable=True))
20+
op.create_unique_constraint('eventvote_user_event_uc', 'eventvotes', ['user_id', 'event_id'])
21+
op.create_foreign_key('eventvote_user_id_fk', 'eventvotes', 'users', ['user_id'], ['id'])
22+
### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
### commands auto generated by Alembic - please adjust! ###
27+
op.drop_constraint('eventvote_user_id_fk', 'eventvotes', type_='foreignkey')
28+
op.drop_constraint('eventvote_user_event_uc', 'eventvotes', type_='unique')
29+
op.drop_column('eventvotes', 'user_id')
30+
### end Alembic commands ###

migrations/versions/f79ba2365689_.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""empty message
2+
3+
Revision ID: f79ba2365689
4+
Revises: 10d5b977cbe4
5+
Create Date: 2016-06-25 18:16:01.508669
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = 'f79ba2365689'
11+
down_revision = '10d5b977cbe4'
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
16+
17+
def upgrade():
18+
### commands auto generated by Alembic - please adjust! ###
19+
op.add_column('events', sa.Column('removed', sa.Boolean(), nullable=True))
20+
### end Alembic commands ###
21+
22+
23+
def downgrade():
24+
### commands auto generated by Alembic - please adjust! ###
25+
op.drop_column('events', 'removed')
26+
### end Alembic commands ###

models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from flask_login import LoginManager
22
from flask_sqlalchemy import SQLAlchemy
3+
from sqlalchemy import UniqueConstraint
4+
from sqlalchemy.orm import backref
35
from sqlalchemy.ext.hybrid import hybrid_property
46

57
import util
@@ -81,18 +83,22 @@ class Event(db.Model):
8183
__tablename__ = 'events'
8284
id = db.Column(db.Integer, primary_key=True)
8385
owner_id = db.Column(db.Integer, db.ForeignKey('users.id', name='event_owner_id_fk'))
84-
owner = db.relationship('User', backref='events')
86+
owner = db.relationship('User', backref=backref('events', lazy='dynamic'))
8587
approved = db.Column(db.Boolean, default=False)
8688
title = db.Column(db.Unicode(length=256))
8789
start_time = db.Column(db.Integer, index=True)
8890
duration = db.Column(db.Float)
8991
description = db.Column(db.UnicodeText)
9092
link = db.Column(db.Unicode(length=256))
93+
removed = db.Column(db.Boolean, default=False)
9194

9295

9396
class EventVote(db.Model):
9497
__tablename__ = 'eventvotes'
9598
id = db.Column(db.Integer, primary_key=True)
99+
user_id = db.Column(db.Integer, db.ForeignKey('users.id', name='eventvote_user_id_fk'))
100+
user = db.relationship('User', backref='event_votes')
96101
event_id = db.Column(db.Integer, db.ForeignKey('events.id', name='vote_event_id_fk'), index=True)
97102
event = db.relationship('Event', backref='votes')
98103
direction = db.Column(db.Boolean)
104+
__table_args__ = (UniqueConstraint('user_id', 'event_id', name='eventvote_user_event_uc'),)

setup.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ apt-get -y upgrade
77
apt-get -y install python-dev python-pip libffi-dev
88
apt-get -y install postgresql postgresql-contrib libpq-dev
99

10-
pip install -r /vagrant/requirements.txt
10+
cd /vagrant
11+
pip install -r requirements.txt

0 commit comments

Comments
 (0)