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 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 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 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 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 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 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 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 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 create(): form = UserCreateForm(request.form) if form.validate(): user = User( username=form.username.data, email=form.email.data, name=form.name.data, password=form.password.data ) session.add(user) session.commit() login_user(user) return redirect(url_for('base.index')) return render_template('users/new.html', form=form)
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 __init__(self, text_brief, text_long, image_url, user, community, answered=False): self.text_brief = text_brief self.text_long = text_long self.answered = answered self.image_url = image_url self.user = user self.community = community # Default Values now = dt.now().isoformat() # Current Time to Insert into Datamodels self.date_created = now self.date_modified = now # Each post starts with 1 upvote (whomever created the post) # We have to insert a record into the post_upvote table p_upvote = PostUpvote(self, self.user) session.add(p_upvote) session.commit()
# --- USERS --- # # We will have some users user0 = UserFactory() user1 = UserFactory() user2 = UserFactory() user3 = UserFactory() # --- COMMUNITIES --- # # Let's have 1 community community0 = CommunityFactory() community1 = CommunityFactory(active=False) community2 = CommunityFactory(active=False) session.commit() # Add all of our users to the community cu0 = CommunityUserFactory(user=user0, community=community0) cu1 = CommunityUserFactory(user=user1, community=community0) cu2 = CommunityUserFactory(user=user2, community=community0) session.commit() # --- POSTS --- # # user0 makes a post post0 = PostFactory(user=user0, community=community0) # user1 makes a post post1 = PostFactory(user=user1, community=community0)
def create(): # banner_url = "http://dsedutech.org/images/demo/placement_banner1.jpg" # thumbnail_url = "http://i.imgur.com/7mo7QHW.gif" form = CommunityCreateForm(request.form) if form.validate(): # First thing we are going to do is create a # Community object with the data from our form. community = Community(name=form.name.data, description=form.description.data, nsfw=form.nsfw.data) session.add(community) session.commit() # we need to create a record so we have the id # Now we have a community with an ID. Lets create # a directory to upload image files to. community_upload_relative = 'communities/' + str(community.id) community_upload_url = './amable/uploads/' + community_upload_relative if not os.path.exists(community_upload_url): os.makedirs(community_upload_url) # Lets do checks for Banner if 'banner' in request.files: banner_file = request.files['banner'] if banner_file.filename != '': if allowed_file(banner_file.filename): banner_filename = 'banner_' + \ secure_filename(banner_file.filename) # Save | Upload Banner file fullPath = os.path.join(community_upload_url, banner_filename) banner_file.save(fullPath) community.banner_url = community_upload_relative + "/" + banner_filename else: flash('Banner file type is not allowed, could not upload', 'error') # Now lets do checks for Thumbnail if 'thumbnail' in request.files: thumbnail_file = request.files['thumbnail'] if thumbnail_file.filename != '': if allowed_file(thumbnail_file.filename): thumbnail_filename = 'thumbnail_' + \ secure_filename(thumbnail_file.filename) # Save | Upload Thumbnail File fullPath = os.path.join(community_upload_url, thumbnail_filename) thumbnail_file.save(fullPath) community.thumbnail_url = community_upload_relative + "/" + thumbnail_filename else: flash( 'Thumbnail file type is not allowed, could not upload', 'error') # community.banner_url = banner_url # community.thumbnail_url = thumbnail_url community.vote(current_user) session.commit() return redirect( url_for('communities.show', permalink=community.permalink)) return redirect(url_for('communities.new'))