示例#1
0
    def find_or_create_from_token(access_token):
        data = GitHub.get_user_from_token(access_token)
        """Find existing user or create new User instance"""
        instance = User.query.filter_by(username=data['login']).first()

        if not instance:
            instance = User(data['login'], data['avatar_url'], data['id'])
            db.session.add(instance)
            db.session.commit()

        return instance
def makePost():
    form = PostForm()
    if form.validate_on_submit():
        data = GitHub.get_user_from_token(session['access_token'])
        post = Post(Event=form.Event.data,
                    Location=form.Location.data,
                    Date=form.Date.data,
                    Cap=form.Cap.data,
                    Description=form.Description.data,
                    author=db.session.query(User).get(data['id']))
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('.makePost'))
        # return render_template('tutorial/showPosts.html', post=post)
    posts = Post.query.order_by(Post.timestamp.desc()).all()
    return render_template('tutorial/postForm.html', form=form, posts=posts)