Example #1
0
def main(filename):

    for row in csv.DictReader(open(filename)):
        family = Family.create(*_extract_values(row, (
            "Parent 1 Last Name",
            "Parent 2 Last Name",
            "Street Address",
            "City",
            "Zip Code",
            "Phone",
            "Pets"
        )))

        for i in range(1, 3):
            if row.get('Parent {0} First Name'.format(i)):
                Parent.create(family, *_extract_values(row, [x.format(i) for x in
                              "Parent {0} First Name", "Parent {0} Last Name", "Parent {0} email", "Parent {0} best phone number"]))

        for i in range(1, 4):
            if row.get('Child {0} Name'.format(i)):
                Child.create(family, *_extract_values(row, ("Child {0} Name".format(i), "Child {0} Birthdate".format(i))))

        print "Created family '{0}'".format(family.name)

        db.session.add(family)
        db.session.commit()
Example #2
0
def admin_add_submit():

  family_form = FamilyForm(request.form, prefix='family')
  parent1_form = ParentForm(request.form, prefix='parent1')
  parent2_form = ParentForm(request.form, prefix='parent2')

  child1_form = ChildForm(request.form, prefix='child1')
  child2_form = ChildForm(request.form, prefix='child2')
  child3_form = ChildForm(request.form, prefix='child3')

  # TODO: Figure out how to dynamically add the forms.
  if not family_form.validate_on_submit():
    if family_form.errors:
      return jsonify(errors=family_form.errors)

  family = Family.create(*[family_form.data.get(k, None) for k in [
    'name', 'name', 'address', 'city', 'zipcode', 'phone', 'pets'
  ]])

  db.session.add(family)

  # Not great, but WTForms and rendering fields doesn't work well with dynamically added javascript.
  for form in (parent1_form, parent2_form):

    # Parent 2 might not exist.
    if not form.data.get('first_name'):
      continue

    if not form.validate_on_submit():
      if form.errors:
        return jsonify(errors=form.errors)

    parent = Parent.create(family, *[form.data.get(k, None) for k in [
      'first_name', 'last_name', 'email', 'cell_phone', 'work_phone'
    ]])

    db.session.add(parent)

  for form in (child1_form, child2_form, child3_form):

    if not form.data.get('name'):
      continue

    print form.data

    if not form.validate_on_submit():
      if form.errors:
        return jsonify(errors=form.errors)

    child = Child.create(family, form.data.get('name'), form.data.get('birthday'))
    db.session.add(child)

  db.session.commit()

  flash("Added the {0} family!".format(family.name), 'info')

  return redirect(url_for(request.endpoint), 303)