Add event creation
This commit is contained in:
@@ -19,7 +19,7 @@ import os
|
||||
|
||||
from flask import Flask, current_app, redirect, render_template, url_for
|
||||
from flask_babel import Babel, get_locale as babel_get_locale
|
||||
from flask_security import SQLAlchemyUserDatastore, current_user
|
||||
from flask_security import SQLAlchemyUserDatastore, current_user, login_required
|
||||
|
||||
|
||||
def get_locale():
|
||||
@@ -103,5 +103,25 @@ def register():
|
||||
return render_template('registration.html', form=form)
|
||||
|
||||
|
||||
@app.route('/new-event', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def new_event():
|
||||
from app.forms import EventForm
|
||||
from app.models import db, Event
|
||||
|
||||
form = EventForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
event = Event(user=current_user)
|
||||
form.populate_obj(event)
|
||||
|
||||
db.session.add(event)
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for('hello'))
|
||||
|
||||
return render_template('event-edit.html', form=form)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
17
app/forms.py
17
app/forms.py
@@ -1,7 +1,9 @@
|
||||
from flask_babel import lazy_gettext as _
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import PasswordField, StringField
|
||||
from wtforms import PasswordField, StringField, BooleanField
|
||||
from wtforms.ext.dateutil.fields import DateTimeField
|
||||
from wtforms.validators import DataRequired, Email, ValidationError
|
||||
from wtforms.widgets import TextArea
|
||||
|
||||
|
||||
class RegistrationForm(FlaskForm):
|
||||
@@ -13,3 +15,16 @@ class RegistrationForm(FlaskForm):
|
||||
def validate_password_retype(self, field):
|
||||
if field.data != self.password.data:
|
||||
raise ValidationError(_('The two passwords must match!'))
|
||||
|
||||
|
||||
class EventForm(FlaskForm):
|
||||
title = StringField(_('Title'), validators=[DataRequired()])
|
||||
time_zone = StringField(_('Time zone'), validators=[DataRequired()])
|
||||
start_time = DateTimeField(_('Start time'), validators=[DataRequired()])
|
||||
end_time = DateTimeField(_('End time'), validators=[DataRequired()])
|
||||
all_day = BooleanField(_('All day'))
|
||||
description = StringField(_('Description'), widget=TextArea())
|
||||
|
||||
def validate_end_time(self, field):
|
||||
if field.data < self.start_time.data:
|
||||
raise ValidationError(_('End time must be later than start time!'))
|
||||
|
@@ -25,6 +25,7 @@
|
||||
<li><a href="{{ url_for('security.login') }}">{% trans %}Login{% endtrans %}</a></li>
|
||||
{% else %}
|
||||
<li><a href="{{ url_for('security.logout') }}">{% trans %}Logout{% endtrans %}</a></li>
|
||||
<li><a href="{{ url_for('hello') }}">{% trans %}Calendar view{% endtrans %}</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</nav>
|
||||
|
43
app/templates/event-edit.html
Normal file
43
app/templates/event-edit.html
Normal file
@@ -0,0 +1,43 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
{{ form.hidden_tag() }}
|
||||
|
||||
{{ form.errors }}
|
||||
<br>
|
||||
|
||||
{{ form.title.errors }}
|
||||
{{ form.title.label }}
|
||||
{{ form.title }}
|
||||
<br>
|
||||
|
||||
{{ form.time_zone.errors }}
|
||||
{{ form.time_zone.label }}
|
||||
{{ form.time_zone }}
|
||||
<br>
|
||||
|
||||
{{ form.start_time.errors }}
|
||||
{{ form.start_time.label }}
|
||||
{{ form.start_time }}
|
||||
<br>
|
||||
|
||||
{{ form.end_time.errors }}
|
||||
{{ form.end_time.label }}
|
||||
{{ form.end_time }}
|
||||
<br>
|
||||
|
||||
{{ form.all_day.errors }}
|
||||
{{ form.all_day.label }}
|
||||
{{ form.all_day }}
|
||||
<br>
|
||||
|
||||
{{ form.description.errors }}
|
||||
{{ form.description.label }}
|
||||
{{ form.description }}
|
||||
<br>
|
||||
|
||||
<button type="submit">{% trans %}Save{% endtrans %}</button>
|
||||
<a href="{{ url_for('hello') }}">Cancel</a>
|
||||
</form>
|
||||
{% endblock content %}
|
@@ -4,4 +4,6 @@
|
||||
{{ _('Welcome to Calendar.social, %(username)s!', username=current_user.username) }}
|
||||
|
||||
{% include 'month-view.html' %}
|
||||
|
||||
<a href="{{ url_for('new_event') }}">{% trans %}Add event{% endtrans %}</a>
|
||||
{% endblock content %}
|
||||
|
Reference in New Issue
Block a user