示例#1
0
文件: views.py 项目: deecewan/based
def create():
    form = ContactForm()
    if form.validate_on_submit():
        new_contact = Contact()
        new_name = Name()
        new_contact.email = form.email.data
        new_name.first_name = form.first_name.data
        new_name.last_name = form.last_name.data
        new_contact.name = new_name
        new_contact.group = form.group.data
        new_contact.known_from = form.known_from.data
        new_contact.general_comment = form.general_note.data
        new_contact.current_status = form.status.data
        for language in form.languages.data:
            if language:
                new_contact.tags.append(language)
        other_tags = form.other_tags.data.replace(', ', ',').split(',')
        for tag in other_tags:
            new_contact.tags.append(tag)
        try:
            new_contact.save()
            flash_string = '<a href=\"/{}\">{} {}</a> was added to the database.'
            flash_string = flash_string.format(new_contact.email, new_contact.name.first_name,
                                               new_contact.name.last_name)
            flash(Markup(flash_string))
            update_p_dict()
            return redirect(url_for('index'))
        except NotUniqueError:
            msg = "That email address is already in use.  <a href=\"/" + form.email.data + "\">View Entry.</a>"
            form.email.errors.append(Markup(msg))
        except Exception as e:
            flash("There were database-raised errors in the form.  Specifically, " + e.message)

    return render_template('create_contact.html', form=form)