Ejemplo n.º 1
0
def upload():
    """Page to add a new catcat sighting."""
    form = forms.NewSightingForm()
    if form.validate_on_submit():
        file = request.files['imageFile']
        if file and uploadhelper.allowed_file(file.filename):
            filename = uploadhelper.save_with_rename(file)

            #Create the DB model objects
            i = Image()
            i.creator = current_user
            i.address_text = form.address.data
            i.title = form.title.data
            i.description = form.description.data
            i.filename = filename
            l = Location()
            #Safety first. They should already but Decimals, but double-check.
            lng = Decimal(form.loc_lng.data)
            lat = Decimal(form.loc_lat.data)
            #POINT(lng lat)
            loc_wkt = "POINT({0} {1})".format(lng, lat)
            l.loc = WKTElement(loc_wkt, srid=4326) #4326 is "normal" lag/lng
            i.location = l
            db.session.add(i)
            db.session.commit()

            new_id = i.id
            return redirect(url_for('main.image', id=new_id))
        else:
            flash("File type not allowed.")
    #otherwise...
    return render_template(
        'main/upload.html',
        title='Add A Cat',
        form=form
    )