def profile(userID): user = USERS_COLLECTION.find_one({'_id': userID}) quesPosted = user['quesPosted'] for i in range(len(quesPosted)): quesPosted[i] = ObjectId(quesPosted[i]) bookmarks = [] bques = user['bookmarks'] for i in range(0, len(bques)): bques[i] = ObjectId(bques[i]) for q in QUESTIONS_COLLECTION.find({'_id': {'$in': bques}}): bookmarks.append(q) ques = [] for q in QUESTIONS_COLLECTION.find({'_id': {'$in': quesPosted}}): ques.append(q) data = { 'userID': user['_id'], 'fname': user['firstname'], 'lname': user['lastname'], 'email': user['email'], 'karma': user['karma'], 'quesPosted': ques } return render_template('profile.html', title='HoverSpace | Profile', data=data, bookmarks=bookmarks)
def signup(): form = SignUpForm() if request.method == 'POST' and form.validate_on_submit(): user = USERS_COLLECTION.find_one( {'email': form.email.data} ) if user: flash("You have already signed up from this email id", category='error') else: user = USERS_COLLECTION.find_one( {'_id': form.username.data} ) if user: flash("That username has already been taken", category='error') else: user_obj = User(form.username.data, form.email.data, form.firstname.data, form.lastname.data, form.password.data, db=True) flash("SignUp successfull!", category='success') return redirect(url_for('login')) return render_template('signup.html', title='HoverSpace | Signup', form=form)
def login(): form = LoginForm() if request.method == 'POST' and form.validate_on_submit(): user = USERS_COLLECTION.find_one({"_id": form.username.data}) if user and User.validate_login(user['password'], form.password.data): user_obj = User(user['_id']) login_user(user_obj, remember=True) flash("Logged in successfully!", category='success') return redirect(url_for('home')) flash("Wrong username or password!", category='error') return render_template('login.html', title='HoverSpace | Login', form=form)
def alreadyVotedAns(self, ansID): try: voted_ans = list() voted_ans = (USERS_COLLECTION.find_one({'_id': self.username }))['voted_ans'] for ans in voted_ans: if ans['ansID'] == ansID: return True return False except: return False
def setBookmark(self, quesID): bookmarked = (USERS_COLLECTION.find_one({'_id': self.username}))['bookmarks'] if quesID in bookmarked: USERS_COLLECTION.update_one({'_id': self.username}, {'$pull': { 'bookmarks': quesID }}) return False else: USERS_COLLECTION.find_one_and_update( {'_id': self.username}, {'$addToSet': { 'bookmarks': quesID }}) return True
def voteAns(self, ansID, voteType): fl = False status = {'votesChange': None, 'type': None} voteChange = 0 vote = None try: voted_ans = list() voted_ans = (USERS_COLLECTION.find_one({'_id': self.username }))['voted_ans'] for ans in voted_ans: if ans['ansID'] == ansID: if ans['vote'] == 1: if voteType == 'up': self.removeAnsVote(ansID) voteChange = -1 else: self.updateAnsVote(ansID, -1) voteChange = -2 vote = "downvote" else: if voteType == 'up': self.updateAnsVote(ansID, 1) voteChange = 2 vote = "upvote" else: self.removeAnsVote(ansID) voteChange = 1 fl = True if not fl: if voteType == "up": self.addAnsVote(ansID, 1) voteChange = 1 vote = "upvote" else: self.addAnsVote(ansID, -1) voteChange = -1 vote = "downvote" status = {'votesChange': voteChange, 'type': vote} return status except: print("voteQues except") status = {'votesChange': voteChange, 'type': vote} return status
def load_user(username): u = USERS_COLLECTION.find_one({"_id": username}) if not u: return None return User(u['_id'])