Exemplo n.º 1
0
def new_post():
    """allows user to create a new post"""
    form = PostForm()
    the_post = Post()
    if form.validate_on_submit():
        the_author = Author.get_by_display_name(form.author_display_name.data)
        the_post.author_id = the_author.id
        the_post.title = form.title.data
        the_post.date_written = form.date_written.data
        the_post.body = form.body.data
        the_post.language_id = Language.query.filter(
            Language.name == form.language.data).first().id
        the_post.country_id = Country.query.filter(
            Country.name == form.nasod.data).first().id
        the_post.created_by = current_user.id
        the_post.modified_by = current_user.id
        db.session.add(the_post)
        db.session.commit()
        return redirect(url_for('post', post_id=the_post.id))
    elif request.method == 'GET':
        the_post.title = form.title.data
        the_post.date_written = form.date_written.data
        the_post.body = form.body.data
        the_post.language = form.language.data
    return render_template('post.html',
                           form=form,
                           post=the_post,
                           poster=current_user)
Exemplo n.º 2
0
 def post(self):
     """initialize post table"""
     with open('post.csv', encoding='utf-8-sig') as post_file:
         reader = csv.DictReader(post_file, delimiter=',')
         for row in reader:
             post = Post(title=row['title'],
                         body=row['body'],
                         date_written=row['date_written'])
             post.created_by, post.modified_by = self.user_id, self.user_id
             coun = Country.query.filter(
                 Country.name == row['country']).first()
             post.country_id = coun.id
             lang = Language.query.filter(
                 Language.name == row['language']).first()
             post.language_id = lang.id
             auth = Author.get_by_display_name(row['author'])
             post.author_id = auth.id
             db.session.add(post)
             self.posts += 1
         db.session.commit()
         print('Added {} posts to the database'.format(self.posts))