예제 #1
0
def add_cafe_form():
    """ GET: show add cafe form, POST: adds cafe to database"""
    if not g.user or not g.user.admin:
        return 'not authorized', 401

    form = AddOrEditCafeForm()
    form.city_code.choices = db.session.query(City.code, City.name).all()

    if form.validate_on_submit():
        name = form.name.data
        description = form.description.data
        url = form.url.data
        address = form.address.data
        city_code = form.city_code.data
        image_url = form.image_url.data

        cafe = Cafe(name=name,
                    description=description,
                    url=url,
                    address=address,
                    city_code=city_code,
                    image_url=image_url)

        db.session.add(cafe)
        db.session.commit()
        cafe.save_map()

        flash(f'{name} added')
        return redirect(f'/cafes/{cafe.id}')
    else:
        return render_template('cafe/add-form.html', form=form)
예제 #2
0
def add_cafe():
    """Handle add_cafe form.
    Only logged-in admin users can add/edit cafes."""

    if not g.user or not g.user.admin:
        flash("Only admins can add cafes.", "danger")
        return redirect("/login")

    form = CafeAddEditForm()

    form.city_code.choices = City.get_city_codes()

    if form.validate_on_submit():
        cafe = Cafe(
            name=form.name.data,
            description=form.description.data,
            url=form.url.data,
            address=form.address.data,
            city_code=form.city_code.data,
            image_url=form.image_url.data or None,
        )

        db.session.add(cafe)

        # In order to run save_map, we need to make sure the cafe has been
        # given an ID, so we need the database to "flush" --- this runs the
        # SQL [so postgres gives it an id] but doesn't commit the transaction
        db.session.flush()
        cafe.save_map()

        db.session.commit()

        flash(f"{cafe.name} added!", "success")
        return redirect(f"/cafes/{cafe.id}")

    else:
        return render_template("cafe/add-form.html", form=form)
예제 #3
0
    first_name="Testy",
    last_name="MacTest",
    description="I am the ultimate representative user.",
    email="*****@*****.**",
    password="******",
)

db.session.add_all([u1, ua])
db.session.commit()


#######################################
# add likes

u1.liked_cafes.append(c1)
u1.liked_cafes.append(c2)
ua.liked_cafes.append(c1)

db.session.commit()


#######################################
# cafe maps

c1.save_map()
c2.save_map()
c3.save_map()
c4.save_map()

#db.session.commit()
예제 #4
0
u1 = User.register(
    username="******",
    first_name="Testy",
    last_name="MacTest",
    description="I am the ultimate representative user.",
    email="*****@*****.**",
    password="******",
)

db.session.add_all([ua, u1])
db.session.commit()


#######################################
# add likes

u1.liked_cafes.append(c1)
u1.liked_cafes.append(c2)
ua.liked_cafes.append(c1)

db.session.commit()


#######################################
# cafe maps

c1.save_map()
c2.save_map()

db.session.commit()