def add_category(): py = Category("Python") p = Post("hello python!", "python is pretty cool ", py) py.save() p.save() return "add category success"
def index(): form = PostForm() if form.validate_on_submit(): language = guess_language(form.post.data) if language == 'UNKOWN' or len(language) > 5: language = '' post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash(_('Your post is now live!')) return redirect(url_for('index')) #posts = current_user.followed_posts().all() page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, App.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template('index.html', title=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def new_post(): form = PostForm() if form.validate_on_submit(): post=Post(title=form.title.data , content=form.content.data , author=current_user) db.session.add(post) db.session.commit() flash("Your post has been Created!","flash-success") return redirect(url_for('main.blog')) return render_template("New_Post.html",title="Create New Post",form=form,legend="New Post")
def test_follow_posts(self): u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) now = datetime.utcnow() p1 = Post(body='post from John', author=u1, timestamp=now + timedelta(seconds=1)) p2 = Post(body='post from Susan', author=u2, timestamp=now + timedelta(seconds=4)) p3 = Post(body='post from Mary', author=u3, timestamp=now + timedelta(seconds=3)) p4 = Post(body='post from David', author=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() u1.follow(u2) u1.follow(u4) u2.follow(u3) u3.follow(u4) db.session.commit() f1 = u1.followed_posts().all() f2 = u2.followed_posts().all() f3 = u3.followed_posts().all() f4 = u4.followed_posts().all() self.assertEqual(f1, [p2, p4, p1]) self.assertEqual(f2, [p2, p3]) self.assertEqual(f3, [p3, p4]) self.assertEqual(f4, [p4])
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data content = form.content.data post = Post(title=title, content=content, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', category='success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def new_post(): form = PostForm() if form.validate_on_submit(): picture_file = save_cover_picture(form.picture.data) the_post = Post(title=form.title.data, content=form.content.data, coverPicture=picture_file, price=form.price.data, author=current_user) db.session.add(the_post) db.session.commit() flash('Your post has been created', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def home(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash( _('%(username)s just posted a message', username=current_user.username)) return redirect(url_for('home')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('home', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('home', page=posts.prev_num) \ if posts.has_prev else None return render_template('user/home.html', title=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def create_post(): Batch_size = 1 Vector_size = 300 Maxseq_length = 500 # Max length of training data learning_rate = 0.001 lstm_units = 128 num_class = 4 keep_prob = 1.0 X = tf.compat.v1.placeholder(tf.float32, shape=[None, Maxseq_length, Vector_size], name='X') Y = tf.compat.v1.placeholder(tf.float32, shape=[None, num_class], name='Y') seq_len = tf.compat.v1.placeholder(tf.int32, shape=[None]) BiLSTM = Bi_LSTM2(lstm_units, num_class, keep_prob) with tf.compat.v1.variable_scope("loss", reuse=tf.compat.v1.AUTO_REUSE): logits = BiLSTM.logits(X, BiLSTM.W, BiLSTM.b, seq_len) loss, optimizer = BiLSTM.model_build(logits, Y, learning_rate) prediction = tf.nn.softmax(logits) # softmax def tokenize(doc): # 토크나이징 하는 부분 pos_tagger = Okt() return [ '/'.join(t) for t in pos_tagger.pos(doc, norm=True, stem=True) ] # 품사 테깅하는 부분 def Convert2Vec(model_name, sentence): word_vec = [] sub = [] model = gensim.models.word2vec.Word2Vec.load(model_name) for word in sentence: if (word in model.wv.vocab): sub.append(model.wv[word]) else: sub.append(np.random.uniform(-0.25, 0.25, 300)) # used for OOV words word_vec.append(sub) return word_vec def Zero_padding(train_batch_X, Batch_size, Maxseq_length, Vector_size): # 길이를 다 같이 맞추기 위해서 # 제일 긴 단어를 찾고 다른 단어들은 그 길이에 맞춰 0으로 빈칸을 체워주는 작업 zero_pad = np.zeros((Batch_size, Maxseq_length, Vector_size)) for i in range(Batch_size): zero_pad[i, :np.shape(train_batch_X[i])[0], :np. shape(train_batch_X[i])[1]] = train_batch_X[i] return zero_pad form = PostForm() if form.validate_on_submit(): saver2 = tf.compat.v1.train.Saver() init2 = tf.compat.v1.global_variables_initializer() modelName2 = "/Users/alex/Documents/GitHub/Graduation/Flask Web Server/App/model/category/BiLSTM.model" sess2 = tf.compat.v1.Session() sess2.run(init2) saver2.restore(sess2, modelName2) post = Post(title=form.title.data, content=form.content.data, author=current_user) message = form.content.data print('Comment : ' + message) print( '************************ Tokenizing Result *****************************' ) tokens = tokenize(message) print(tokens) embedding2 = Convert2Vec( '/Users/alex/Documents/GitHub/Graduation/Flask Web Server/App/model/category/league_category.embedding', tokens) zero_pad = Zero_padding(embedding2, Batch_size, Maxseq_length, Vector_size) sess2 result2 = sess2.run( prediction, feed_dict={ X: zero_pad, seq_len: [len(tokens)] }) # tf.argmax(prediction, 1)이 여러 prediction 값중 max 값 1개만 가져옴 point = result2.ravel().tolist() Tag = [ "EPL(England) :", "Laliga (Spain) :", "Bundesliga (Germany) :", "Seria (Italy) :" ] print( '************************ Category classification Result ***********************' ) for t, i in zip(Tag, point): print(t, round(i * 100, 2), "%") db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('post')) return render_template('post.html', title='Create post', form=form)
def publish_post(cid): sponsor = Sponsor.query.first() # current date current_year = datetime.datetime.now().year time_now = datetime.datetime.now().strftime("%m-%d %H:%M") # query to find the specified sub-section other_layer = Category.query.filter(Category.cid == cid).all() other_layer_name = other_layer[0].classname other_layer_parent = other_layer[0].parentid # find first-level section according to the sub-section first_layer = Category.query.filter( and_(Category.parentid == 0, Category.cid == other_layer_parent)).all() first_layer_name = first_layer[0].classname first_layer_cid = first_layer[0].cid if request.method == "GET": return render_template("foreground/addc.html", **locals()) else: # add a record of the post to the database post_title = request.form.get("subject") post_content = request.form.get("content") post_price = request.form.get("price") post = Post() post.authorid = session['user_id'] post.title = post_title post.content = post_content post.addtime = datetime.datetime.now() post.addip = 1122122192 post.classid = cid post.replycount = 0 post.hits = 0 post.istop = 0 post.elite = 0 post.ishot = 0 post.rate = post_price post.isdel = 0 post.isdisplay = 0 db.session.add(post) db.session.commit() return redirect(url_for("bbs.list_category", cid=cid, page=1))