def viewTopic(id): """Loads the form/page to view a post""" form = CommentForm() sub_btn = SubscriptionForm() unsub_btn = UnsubscriptionForm() topic = DatabaseManager.getTopic(id) DatabaseManager.removeNotification(current_user.id, id) if topic.group_id is not None: group = DatabaseManager.getGroup(topic.group_id) if DatabaseManager.checkMember(current_user,group): if form.validate_on_submit(): DatabaseManager.addComment(user_id=current_user.id,topic_id=id,body=form.comment.data) DatabaseManager.notifyUsers(id) return redirect(url_for('viewTopic',id=id)) if request.method == "POST" and request.form['btn'] == "sub": ## add topic to user subscriptions DatabaseManager.addSubscription(current_user.id, id) return redirect(url_for('viewTopic',id=id)) elif request.method == "POST" and request.form['btn'] == "unsub": ## Remove the topic from user subscriptions DatabaseManager.removeSubscription(current_user.id, id) return redirect(url_for('viewTopic',id=id)) if DatabaseManager.checkForSub(current_user.id, id): return render_template('post.html', title='View Topic',\ topic=topic,form=form,btn=unsub_btn,val="unsub") else: return render_template('post.html', title='View Topic',\ topic=topic,form=form,btn=sub_btn,val="sub") else: flash('You are not a member of the group this topic belongs to','error') return redirect(url_for('home')) else: if form.validate_on_submit(): DatabaseManager.addComment(user_id=current_user.id,topic_id=id,body=form.comment.data) DatabaseManager.notifyUsers(id) return redirect(url_for('viewTopic',id=id)) if request.method == "POST" and request.form['btn'] == "sub": ## add topic to user subscriptions DatabaseManager.addSubscription(current_user.id, id) return redirect(url_for('viewTopic',id=id)) elif request.method == "POST" and request.form['btn'] == "unsub": ## Remove the topic from user subscriptions DatabaseManager.removeSubscription(current_user.id, id) return redirect(url_for('viewTopic',id=id)) ## Check if user is subscribed to this topic. If true, show unsub button if DatabaseManager.checkForSub(current_user.id, id): return render_template('post.html', title='View Topic',\ topic=topic,form=form,btn=unsub_btn,val="unsub") else: return render_template('post.html', title='View Topic',\ topic=topic,form=form,btn=sub_btn,val="sub")
def viewGroup(id): """loads the form/page to view a group""" group = DatabaseManager.getGroup(id) leaveGroup = LeaveGroupForm(prefix="leaveGroup") if DatabaseManager.checkMember(current_user,group): form = AddUserForm(prefix="form") if form.submit.data and form.validate_on_submit(): user = DatabaseManager.getUserByUsername(form.username.data) if user is None: flash('No such user exists','error') else: DatabaseManager.addGroupMember(user=user,group=group) flash('You have successfully added a user to the group','info') return render_template('group.html', group=group, title=group.name,form=form,leaveGroup=leaveGroup) elif leaveGroup.submit.data and leaveGroup.validate_on_submit(): DatabaseManager.leaveGroup(current_user,group) flash('You have left the group','info') return redirect(url_for('home')) return render_template('group.html', group=group, title=group.name,form=form,leaveGroup=leaveGroup) else: flash('You are not a member of the specified group','error') return redirect(url_for('home'))