def _op_on_x(self, x_obj, op_table, op_fk=None, one_to_many=None, edit=False): x_name = x_obj.__class__.__name__ if not op_fk: op_fk_name = '%s_id' % x_name.lower() op_fk = getattr(op_table, op_fk_name) if not one_to_many: one_to_many_name = 'op_on_%ss' % x_name.lower() one_to_many = getattr(self, one_to_many_name) op = op_table.query.filter( db.and_(op_table.user_id == self.id, op_fk == x_obj.id)).first() if not op: if edit: one_to_many.append(x_obj) return self._op_on_x(x_obj, op_table, op_fk=op_fk, one_to_many=one_to_many) else: return None else: return op
def thanked_users(self): ops = UserOnAnswer.query.filter( db.and_( UserOnAnswer.answer_id == self.id, UserOnAnswer.thank == THANK_ON, )).all() return [op.user for op in ops]
def update_question_following_count(sender): assert isinstance(sender, Question) sender.following_count = \ UserOnQuestion.query.filter( db.and_(UserOnQuestion.question_id == sender.id, UserOnQuestion.follow == True) ).count() db.session.commit()
def votedown_users(self): ops = UserOnAnswer.query.filter( db.and_( UserOnAnswer.answer_id == self.id, UserOnAnswer.vote == VOTE_DOWN, )).all() return [op.user for op in ops]
def thanked_users(self): ops = UserOnAnswer.query.filter( db.and_( UserOnAnswer.answer_id == self.id, UserOnAnswer.thank == THANK_ON, ) ).all() return [op.user for op in ops]
def voteup_users(self): ops = UserOnComment.query.filter( db.and_( UserOnComment.comment_id == self.id, UserOnComment.vote == VOTE_UP, )).all() return [op.user for op in ops]
def voteup_users(self): ops = UserOnComment.query.filter( db.and_( UserOnComment.comment_id == self.id, UserOnComment.vote == VOTE_UP, ) ).all() return [op.user for op in ops]
def votedown_users(self): ops = UserOnAnswer.query.filter( db.and_( UserOnAnswer.answer_id == self.id, UserOnAnswer.vote == VOTE_DOWN, ) ).all() return [op.user for op in ops]
def update_answer_thanks_count(sender): assert isinstance(sender, Answer) thanks_count = UserOnAnswer.query.filter( db.and_( UserOnAnswer.answer_id == sender.id, UserOnAnswer.thank == THANK_ON, )).count() sender.thanks_count = thanks_count db.session.commit()
def update_comment_vote_count(sender): assert isinstance(sender, Comment) voteup_count = UserOnComment.query.filter( db.and_( UserOnComment.comment_id == sender.id, UserOnComment.vote == VOTE_UP, )).count() sender.voteup_count = voteup_count db.session.commit()
def update_collection_following_count(sender): assert isinstance(sender, Collection) following_count = UserOnCollection.query.filter( db.and_( UserOnCollection.collection_id == sender.id, UserOnCollection.follow == FOLLOW_ON, )).count() sender.following_count = following_count db.session.commit()
def update_answer_vote_count(sender): assert isinstance(sender, Answer) ops = UserOnAnswer.query.filter( db.and_( UserOnAnswer.answer_id == sender.id, UserOnAnswer.vote != VOTE_NONE, )).all() sender.voteup_count = len(filter(lambda x: x.vote == VOTE_UP, ops)) sender.votedown_count = len(filter(lambda x: x.vote == VOTE_DOWN, ops)) db.session.commit()
def update_answer_thanks_count(sender): assert isinstance(sender, Answer) thanks_count = UserOnAnswer.query.filter( db.and_( UserOnAnswer.answer_id == sender.id, UserOnAnswer.thank == THANK_ON, ) ).count() sender.thanks_count = thanks_count db.session.commit()
def update_collection_following_count(sender): assert isinstance(sender, Collection) following_count = UserOnCollection.query.filter( db.and_( UserOnCollection.collection_id == sender.id, UserOnCollection.follow == FOLLOW_ON, ) ).count() sender.following_count = following_count db.session.commit()
def update_comment_vote_count(sender): assert isinstance(sender, Comment) voteup_count = UserOnComment.query.filter( db.and_( UserOnComment.comment_id == sender.id, UserOnComment.vote == VOTE_UP, ) ).count() sender.voteup_count = voteup_count db.session.commit()
def update_answer_vote_count(sender): assert isinstance(sender, Answer) ops = UserOnAnswer.query.filter( db.and_( UserOnAnswer.answer_id == sender.id, UserOnAnswer.vote != VOTE_NONE, ) ).all() sender.voteup_count = len(filter(lambda x: x.vote == VOTE_UP, ops)) sender.votedown_count = len(filter(lambda x: x.vote == VOTE_DOWN, ops)) db.session.commit()
def add_answer(self, id, answer_id, next_url=None): collection = Collection.query.filter( db.and_( Collection.id == id, Collection.user_id == current_user.id ) ).first_or_404() answer = Answer.query.get_or_404(answer_id) collection.answers.append(answer) db.session.commit() signals.collection_answer_add.send(collection) return redirect(next_url or url_for('CollectionView:show', id=collection.id))
def delete(self, id, next_url=None): collection = Collection.query.filter( db.and_( Collection.id == id, Collection.user_id == current_user.id, ) ).first_or_404() collection.answers = [] db.session.delete(collection) db.session.commit() signals.collection_answer_delete.send(collection) return redirect(next_url or url_for('CollectionView:my'))
def _op_on_x(self, x_obj, op_table, op_fk=None, one_to_many=None, edit=False): x_name = x_obj.__class__.__name__ if not op_fk: op_fk_name = '%s_id' % x_name.lower() op_fk = getattr(op_table, op_fk_name) if not one_to_many: one_to_many_name = 'op_on_%ss' % x_name.lower() one_to_many = getattr(self, one_to_many_name) op = op_table.query.filter( db.and_(op_table.user_id == self.id, op_fk == x_obj.id) ).first() if not op: if edit: one_to_many.append(x_obj) return self._op_on_x(x_obj, op_table, op_fk=op_fk, one_to_many=one_to_many) else: return None else: return op
def edit(self, id): collection = Collection.query.filter( db.and_( Collection.id == id, Collection.user_id == current_user.id ) ).first_or_404() form = CollectionEditForm() if form.validate_on_submit(): if form.title.data: collection.title = form.title.data if form.description.data: collection.description = form.description.data db.session.commit() return redirect(url_for('CollectionView:show', id=collection.id)) else: form.title.data = collection.title form.description.data = collection.description return render_template('collection/edit.html', form=form)
def following_users(self): uoqs = UserOnQuestion.query.filter( db.and_(UserOnQuestion.question_id == self.id, UserOnQuestion.follow==True) ).all() return [uoq.user for uoq in uoqs]
def following_users(self): uoqs = UserOnQuestion.query.filter( db.and_(UserOnQuestion.question_id == self.id, UserOnQuestion.follow == True)).all() return [uoq.user for uoq in uoqs]