Beispiel #1
0
def makePosts():
    form = NewPost()
    if not session.get('logged_in'):
        return redirect(url_for('login'))
#If the user tries to post and is not logged in, redirects them to login.
    if form.validate_on_submit():
        body = form.body.data
        title = form.title.data
        user = User.objects(alias = session.get("alias")).get()
        p = Posts(author = user, title = title, content = body, postid=str(uuid.uuid4())[:8])
        p.save()
        return redirect(url_for('viewPost', pid=str(p.postid)))
    return render_template("submitPost.html", form=form)
Beispiel #2
0
def newPost():
    form = NewPost()
    topic = request.args.get('topic')
    if form.validate_on_submit():
        data = request.form
        newPost = Post(content=data['content'],
                       topic=topic,
                       author=current_user.username)
        activity = Activity(name=current_user.username,
                            type='p',
                            topic=newPost.topic)
        db.session.add(newPost)
        db.session.add(activity)
        db.session.commit()
        return redirect(request.referrer)
    return redirect(request.referrer)
Beispiel #3
0
def new_post():
    """Find coordinates for post"""

    form = NewPost()

    if form.validate_on_submit():
        # make new model

        title = form.title.data
        image = form.image.data
        description = form.description.data
        time = datetime.now()

        #return coordinates [lng, lat] format for db storage
        lat = float(request.form['coord_lat'])
        lng = float(request.form['coord_lng'])

        # get location name
        location_url = f"https://api.mapbox.com/geocoding/v5/mapbox.places/{lng},{lat}.json?access_token={MAPBOX_TOKEN}"

        res = requests.get(location_url).json()
        loc = res['features'][0]['place_name']

        user = g.user.id

        new_post = Post(title=title,
                        image=image,
                        description=description,
                        lat=lat,
                        lng=lng,
                        created_dt=time,
                        place_name=loc,
                        user_id=user)
        db.session.add(new_post)
        db.session.commit()
        return redirect(f"/users/{g.user.username}")
    else:
        return render_template('/posts/new_post.html', form=form)

    return render_template('/posts/new_post.html', form=form)