def report_post(): form = PostReportForm(request.form) post = None if form.validate(): # Lets get the post in question post = session.query(Post).filter_by(id=int(form.post.data)).first() # Has the user already reported this post? postReportCount = session.query(PostReport).filter_by( user=current_user, parent=post).count() if postReportCount == 0: postReport = PostReport(title=form.title.data, content=form.content.data, user=current_user, post=post, category=form.category.data) session.add(postReport) session.commit() else: flash("You have already reported this post") else: flash_errors(form) return redirect( url_for('communities.show', permalink=post.community.permalink))
def membership(id): json = {} community = session.query(Community).filter_by(id=id).first() # See if they're already in the community community_user = session.query(CommunityUser).filter_by( user=current_user, community=community).first() # If they're not in the community, add them. Otherwise, remove them if community_user is None: community_user = CommunityUser(user=current_user, community=community) session.add(community_user) session.commit() json['action'] = 'joined' else: session.delete(community_user) session.commit() json['action'] = 'left' json['success'] = True return jsonify(**json)
def show(username): user = session.query(User).filter_by(username=username).first() if not user: return abort(404) posts = session.query(Post).filter_by(user_id=user.id).order_by( desc(Post.date_created)).all() return render_template('users/show.html', user=user, posts=posts, comment_form=CommentCreateForm(), report_form=PostReportForm())
def create(): comment = None parent_post = None form = CommentCreateForm(request.form) if form.validate(): # Valid data # We need to get the post that the com # session.query(Post) if form.parent.data is not "": # We need to get the post that the com parent_comment = session.query(Comment).filter_by( id=int(form.parent.data)).first() parent_post = parent_comment.post comment = Comment(content=form.content.data, user=current_user, post=parent_post, parent=parent_comment) elif form.post.data is not "": # Lets get the post parent_post = session.query(Post).filter_by( id=int(form.post.data)).first() comment = Comment(content=form.content.data, user=current_user, post=parent_post) try: if comment is not None: session.add(comment) session.commit() except: print("Unexpected Error : %s" % sys.exc_info()[0]) print(request.form.get('redirect_to')) if request.form.get('redirect_to') is not None: return redirect(request.form.get('redirect_to')) return redirect( url_for('communities.show', permalink=parent_post.community.permalink)) else: flash_errors(form) return redirect(url_for('base.index'))
def updatePHCount(): phCount = session.query(Comment).filter_by( user_id=self.id).group_by(Comment.post_id, Comment.id).count() cache.set(str(self.id) + "_praying_hands", phCount, timeout=10 * 60) return phCount
def upvoted_by(self, user): if user: num_upvotes = session.query(CommunityUpvote).filter_by( user_id=user.id, community_id=self.id).count() return num_upvotes >= 1 else: return False
def reports(permalink): community = session.query(Community).filter_by(permalink=permalink).first() reports = session.query(PostReport).all() post_array = [] for x in range(0, len(reports)): post_array.append( session.query(Post).filter_by(id=reports[x].parent_id).all()) post_array = [val for sublist in post_array for val in sublist] post_array = list(set(post_array)) return render_template('communities/reports.html', community=community, reports=reports, post_array=post_array)
def index(): communities = session.query(Community).order_by( Community.date_created.desc()).all() return render_template('communities/index.html', title="Communities", communities=communities, form=CommunitySearchForm())
def updateHammerCount(): hammerCount = 0 allReports = session.query(PostReport).filter_by(user_id=self.id) for report in allReports: # Has someone else reported this post? reportCheckCount = session.query(PostReport).filter( PostReport.parent_id == report.parent_id, PostReport.user_id != self.id).count() if reportCheckCount > 0: hammerCount += 1 else: continue cache.set(str(self.id) + "_hammer", hammerCount, timeout=10 * 60) return hammerCount
def create(): community = None # This will be just a filler object for our community # Get the form data form = PostCreateForm(request.form) # Set the valid community choices form.community_select.choices = [(c.id, c.name) for c in current_user.get_communities()] if form.validate(): # We need to get the community to pass through to post # constructor # First we check if there was the hidden field, or if # it was a select box if form.community_id.data is not "": # Hidden Field community = session.query(Community).filter_by( id=int(form.community_id.data)).first() elif form.community_select is not None: # Select Box community = session.query(Community).filter_by( id=int(form.community_select.data)).first() # Create the post object post = Post(text_brief=form.text_brief.data, text_long=None, image_url=None, user=current_user, community=community) # Add to the session and commit to the database session.add(post) session.commit() flash(u"Post Successfully Created", "success") else: pprint(form.errors) flash(u"Post failed", "error") # lets also flash the errors flash_errors(form) return redirect(url_for('communities.show', permalink=community.permalink))
def total_upvotes(self): cacheTotal = cache.get(str(self.id) + "_post_upvotes") if cacheTotal is None: cacheTotal = session.query(PostUpvote).filter_by( post_id=self.id).count() cache.set(str(self.id) + "_post_upvotes", cacheTotal, timeout=5 * 60) return cacheTotal
def destroy(id): post = session.query(Post).filter_by(id=id).first() if post.destroyable_by(current_user): session.delete(post) session.commit() else: flash("Can't delete a post you didn't create") return redirect(request.form["redirect_to"])
def vote_for_community(self, community): # First we want to make sure that this user # hasn't yet voted for this community upvoteCount = session.query(CommunityUpvote).filter_by( user_id=self.id, community_id=community.id).count() if upvoteCount == 0: # Has not voted newUpvote = CommunityUpvote(self, community) session.add(newUpvote) session.commit() else: flash("You have already voted for this community")
def unanswer_post(id): # First lets get the post were answering post = session.query(Post).filter_by(id=id).first() if post.user == current_user: post.answered = False session.commit() else: flash("Cannot unanswer a post you didn't create") return redirect( url_for('communities.show', permalink=post.community.permalink))
def vote_community(community_id): returnDict = {} # First lets get the community in question tempCommunity = session.query(Community).filter_by(id=community_id).first() if tempCommunity.vote(current_user): returnDict['success'] = True else: returnDict['success'] = False return jsonify(**returnDict)
def show(permalink): community = session.query(Community).filter_by(permalink=permalink).first() postCreateForm = PostCreateForm() postCreateForm.community_id.data = community.id commentCreateForm = CommentCreateForm() postReportForm = PostReportForm() if not community: return abort(404) posts = session.query(Post).filter_by(community_id=community.id).order_by( desc(Post.date_created)).all() return render_template('communities/show.html', community=community, post_form=postCreateForm, posts=posts, comment_form=commentCreateForm, report_form=postReportForm)
def destroy(id): user = session.query(User).filter_by(id=id).first() if user.destroyable_by(current_user) is not True: return redirect(url_for('sessions.login')) user.active = False session.commit() if user == current_user: logout_user() return redirect(url_for('base.index'))
def can_be_shown(self, invalidate=False): reportCount = cache.get(str(self.id) + "_report_count") if reportCount is None or invalidate: reportCount = session.query(PostReport).filter_by( parent=self).count() cache.set(str(self.id) + "_report_count", reportCount, timeout=5 * 60) if int(reportCount) >= 10: return False else: return True
def unfollow_user(id): returnDict = {} follower_to_delete = session.query(Follower).filter_by( target_id=id, source_id=current_user.id).first() if follower_to_delete is not None: returnDict['success'] = True session.delete(follower_to_delete) session.commit() else: returnDict['success'] = False return jsonify(**returnDict)
def search(): if 'community' in request.args: if len(request.args['community']) > 0: queryToSearch = request.args['community'] communityList = session.query(Community).filter( func.lower(Community.name).like(func.lower(queryToSearch + "%"))).all() return jsonify(communities=[i.serialize for i in communityList]) else: return jsonify(communities={}) else: flash("Arguments missing")
def comment_tree(self): root_tree = OrderedDict() root_level = session.query(Comment).filter_by(post_id=self.id, parent_id=None).all() def get_children(comment, child_tree): for child in comment.children: child_tree[child] = get_children(child, OrderedDict()) return child_tree for comment in root_level: root_tree[comment] = get_children(comment, OrderedDict()) return root_tree
def downvote_post(id): returnDict = {} # First lets get the post were upvoting pu = session.query(PostUpvote).filter_by(post_id=id, user_id=current_user.id).first() # Check to see user has actually upvoted if pu is not None: returnDict['success'] = True # Delete the post upvote session.delete(pu) session.commit() else: returnDict['success'] = False return jsonify(**returnDict)
def follow(id): returnDict = {} user_to_follow = session.query(User).filter_by(id=id).first() # First make sure a Follower doesn't exist if user_to_follow is not None: returnDict['success'] = True follower = Follower(source_user=current_user, target_user=user_to_follow) session.add(follower) session.commit() else: returnDict['success'] = False return jsonify(**returnDict)
def vote(self, user): # First lets make sure the user hasn't voted # for this community yet voteCount = session.query(CommunityUpvote).filter_by( user_id=user.id, community_id=self.id).count() if voteCount == 0: newUpvote = CommunityUpvote(user, self) session.add(newUpvote) session.commit() if self.queryNumUpvotes() >= 10: self.active = True session.commit() return True else: return False
def upvote_post(id): returnDict = {} # First lets get the post were upvoting post = session.query(Post).filter_by(id=id).first() # Check to see user hasn't already upvoted if not current_user.has_upvoted_post(post): returnDict['success'] = True pu = PostUpvote(post, current_user) # Add the new post upvote session.add(pu) session.commit() else: returnDict['success'] = False # flash("User has already upvoted post") return jsonify(**returnDict)
def update(id): user = session.query(User).filter_by(id=id).first() if user.updatable_by(current_user) is not True: return redirect(url_for('sessions.login')) form = UserUpdateForm(request.form) if form.validate(): data = form.data if request.files['profile_image']: filename = secure_filename(request.files['profile_image'].filename) if not os.path.exists('./amable/uploads/avatars/' + str(user.id)): os.makedirs('./amable/uploads/avatars/' + str(user.id)) request.files['profile_image'].save( './amable/uploads/avatars/' + str(user.id) + '/' + filename) user.profile_image = '/uploads/avatars/' + \ str(user.id) + '/' + filename user.set_password(data['password']) del(data['profile_image']) del(data['password']) user.update(data) session.commit() flash(u"Your account has been updated.", "success") return redirect(url_for('users.edit')) flash(form.errors) return redirect(url_for('users.edit'))
def create(): # email 'variable' is the column. request.form['email'] is the post data # print("email provided : " + request.form['email']) # testing # print("create() ~ User Password : "******"create() ~ Username : "******"No User Found") return redirect('/login') elif user.active is True: # print("Form validated") if check_password(user, request.form["password"]): # print("password checked good to go") login_user(user) # flash("Login Successful") return redirect("/") else: flash("Error validating login credentials, please try again") return redirect('/login') else: flash("Account as been deactivated or deleted") return redirect('/login') else: flash_errors(form) flash("Error validating login format, please try again") return redirect('/login')
def has_children(self): return session.query(Comment).filter_by(parent=self.id).count() > 0
def has_followed_user(self, user): return session.query(Follower).filter_by( target_id=user.id, source_id=self.id).count() == 1
def load_user(user_id): return session.query(User).filter_by(id=user_id).first()