questions displayed dynamically fixed

merge-requests/2/head
Shenyuan Jin 3 years ago
parent fe9e5f63e2
commit 01033fb660

@ -738,6 +738,7 @@ def new_answer_md(question_id):
return redirect(url_for('.view_question',question_id=question_id))
return render_template('new_posting/new_mdanswer.html', form=form)
@main.route('/questions/<question_id>', methods=['GET', 'POST'])
def view_question(question_id):
if request.method == 'GET':
@ -766,3 +767,18 @@ def view_question(question_id):
else:
inf = request.form["search"]
return redirect(url_for('.query', content=inf))
@main.route('/savequestions/<question_id>')
def save_question(question_id):
question = Question.query.filter_by(id=question_id).first()
if question is None:
flash('Invalid post.')
return redirect(url_for('.index'))
if current_user.is_savingquestion(question):
flash('You are already liking this post.')
return redirect(url_for('.post', id=question_id))
current_user.savequestion(question)
db.session.commit()
flash('You are now liking this post')
return redirect(url_for('.view_question', question_id=question_id))

@ -79,6 +79,13 @@ class Students(db.Model):
role_id = db.Column(db.Integer, default=1, index=True)
class Savequestion(db.Model):
__tablename__ = 'savequestion'
saver_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
question_id = db.Column(db.Integer, db.ForeignKey('questions.id'), primary_key=True)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
class Follow(db.Model):
follower_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
followed_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
@ -141,8 +148,8 @@ class User(UserMixin, db.Model):
collected_transaction = db.relationship('Collect', back_populates='collecter',
lazy='dynamic', cascade='all, delete-orphan')
# # # # save
# saved_by_post = db.relationship('Save_post', back_populates='saver', lazy='dynamic', cascade='all, delete-orphan')
saved_questions = db.relationship('Savequestion', foreign_keys=[Savequestion.saver_id], backref=db.backref('saver', lazy='joined'),
lazy='dynamic', cascade='all, delete-orphan')
@staticmethod
def add_self_follows():
@ -336,6 +343,19 @@ class User(UserMixin, db.Model):
return self.followers.filter_by(
follower_id=user.id).first() is not None
def is_savingquestion(self, question):
return self.saved_questions.filter_by(question_id=question.id).first() is not None
def savequestion(self, question):
if not self.is_savingquestion(question):
s = Savequestion(saver=self, question=question)
db.session.add(s)
def unsavequestion(self, question):
s = self.saved_questions.filter_by(question_id=question.id).first()
db.session.delete(s)
@property
def followed_posts(self):
return Post.query.join(Follow, Follow.followed_id == Post.author_id) \
@ -389,6 +409,8 @@ def load_user(user_id):
return User.query.get(int(user_id))
class Question(db.Model):
__tablename__ = 'questions'
id = db.Column(db.Integer, primary_key=True)
@ -401,6 +423,10 @@ class Question(db.Model):
recent_activity = db.Column(db.DateTime, index=True, default=datetime.utcnow())
answers = db.relationship('Post', back_populates='question', cascade='all, delete-orphan', lazy='dynamic')
is_anonymous = db.Column(db.Boolean, default=False)
savers = db.relationship('Savequestion', foreign_keys=[Savequestion.question_id],
backref=db.backref('question', lazy='joined'), lazy='dynamic',
cascade='all, delete-orphan')
class Post(db.Model):
__tablename__ = 'posts'
@ -591,3 +617,6 @@ class Activity(db.Model):
return False
return self.wanter.filter_by(
wanter_id=user.id).first() is not None

@ -124,8 +124,9 @@
<div class="tabbable">
<ul class="nav post-nav">
{% if %}
<li id="btn-1">
<a href="#" class="glyphicon glyphicon-bookmark"> Follow</a>
<a href="{{ url_for('.save_question', question_id=question.id ) }}" class="glyphicon glyphicon-bookmark"> Follow</a>
</li>
<li id="btn-2">
<a href="{{ url_for('.new_answer_md',question_id=question_id )}}" class="glyphicon glyphicon-pencil">

Binary file not shown.
Loading…
Cancel
Save