In this exercise, we are going to let the authenticated user add a contribution to an existing repository. Again, we will use the WTForm Flask extension to manage HTML forms. Our form is even more simple than the login form since it contains only one text input: the name of the repository the user contributed to:
from models import Repository
class NewContributionForm(FlaskForm):
name = StringField('Name', validators=[DataRequired()])
submit = SubmitField('Submit')
The provided repository name is validated to make sure a Repository node with that name already exists, and raise a ValidationError otherwise:
def validate_name(self, field):
r = Repository.nodes.get_or_none(name=field.data)
if r is None:
raise ValidationError('Can ony add contributions to existing repositories')
Now, let's build the template that will render this form. This template should not be surprising...