def show_question(qID): print("GET to /questions/" + str(qID)) question = database.getDoc('questions', {'id': qID}) if question is None: print("--------error!") return jsonify({'status': 'error', 'error': 'Invalid question ID.'}) username = request.args['username'] user = None loggedIn = False if username is not None and username != '': user = database.getDoc('users', { 'username': username, 'enabled': True }) if user is not None: loggedIn = True if loggedIn: print('-------------- logged in') increment_view(question, user=username) else: print('-------------- not logged in') increment_view(question, ip=request.remote_addr) question.pop('_id', None) question.pop('viewer_usernames', None) question.pop('viewer_IPs', None) return jsonify({'status': 'OK', 'question': question})
def add_answer(qID): print("add answer to " + str(qID)) question = database.getDoc('questions', {'id': qID}) if question is None: print("Invalid question") return jsonify({'status': 'error', 'error': 'Invalid question ID.'}) username = request.get_json(force=True)['username'] user = database.getDoc('users', {'username': username, 'enabled': True}) if user is None: print("Invalid user") return jsonify({'status': 'error', 'error': 'Invalid credentials.'}) body = request.get_json(force=True)['body'] media = request.get_json(force=True)['media'] aID = database.getID() answer = { 'id': aID, 'user': username, 'body': body, 'score': 0, 'is_accepted': False, 'timestamp': time.time(), 'media': media, 'qID': qID } db = database.getDB('answers') db.insert(answer) print("answer added") return jsonify({'status': 'OK', 'id': aID})
def adduser(): uname = request.get_json(force=True)['username'] pwd = request.get_json(force=True)['password'] email = request.get_json(force=True)['email'] users = database.getDB('users') doc = database.getDoc('users', {'username':uname}) if doc is not None: return jsonify({'status':'error', 'error':'That username is already in use.'}) doc = database.getDoc('users', {'email':email}) if doc is not None: return jsonify({'status':'error', 'error':'That email is already in use.'}) key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16)) userObject = { 'username':uname, 'password':pwd, 'email':email, 'key':key, 'enabled':False, 'reputation':1} #'gamestate':[], 'gameID':[], 'gameStartDate':"", 'completedGameStates':[], 'completedGameList':[], 'wins':0, 'losses':0, 'ties':0} users.insert(userObject) verify = "http://130.245.170.46/verify?key=" + key + "&email=" + email msg = MIMEText("Hi " + uname + "! Please use this link to verify your account: " + verify + "\n\nvalidation key: <" + key + ">") #me = os.environ['MAIL_USER'] me = "*****@*****.**" to = email msg['Subject'] = 'Hello there!' msg['To'] = to msg['From'] = me s = smtplib.SMTP_SSL('smtp.gmail.com') #s.login(me, os.environ['MAIL_PASSWORD']) s.login(me, [REDACTED]) s.sendmail(me, [to], msg.as_string()) return jsonify({"status": "OK"})
def add_question(): username = request.get_json(force=True)['username'] user = database.getDoc('users', {'username': username, 'enabled': True}) if user is None: return jsonify({'status': 'error', 'error': 'Invalid credentials.'}) title = request.get_json(force=True)['title'] body = request.get_json(force=True)['body'] tags = request.get_json(force=True)['tags'] db = database.getDB('questions') qID = database.getID() qObject = { 'id': qID, 'user': { 'username': username, 'reputation': user['reputation'] }, 'title': title, 'body': body, 'score': 0, 'view_count': 0, 'answer_count': 0, 'timestamp': time.time(), 'media': [], 'tags': tags, 'accepted_answer_id': None, 'viewer_usernames': [], 'viewer_IPs': [] } db.insert(qObject) return jsonify({'status': 'OK', 'id': qID})
def delete_question(qID): question = database.getDoc('questions', {'id':qID}) if question is None: return jsonify({'status':'error', 'error':'Invalid question ID'}), 400 try: username = request.cookies['cse356user'] except KeyError: return jsonify({'status':'error', 'error':'You\'re not logged in'}), 400 if question['user']['username'] != username: return jsonify({'status':'error', 'error':'You can not delete another user\'s question'}), 400 user = database.getDoc('users', {'username':username, 'enabled':True}) if user is None: return jsonify({'status':'error', 'error':'Invalid credentials'}), 400 db = database.getDB('questions') db.delete_one(question) return jsonify({'status':'ok'}), 200
def get_answers(qID): question = database.getDoc('questions', {'id': qID}) if question is None: return jsonify({'status': 'error', 'error': 'Invalid question ID.'}) answers = database.getMatchingAnswers(qID) for a in answers: a.pop('_id', None) a.pop('qID', None) return jsonify({'status': 'OK', 'answers': answers})
def login(): uname = request.get_json(force=True)['username'] pwd = request.get_json(force=True)['password'] user = database.getDoc('users', {'username':uname, 'password':pwd, 'enabled':True}) if user is not None: resp = make_response(jsonify({'status':'OK'})) resp.set_cookie('cse356user', str(user['username'])) return resp return jsonify({'status':'error', 'error':'Invalid credentials.'})
def splash(): try: username = request.cookies['cse356user'] user = database.getDoc('users', {'username':username, 'enabled':True}) if user is not None: return render_template('main.html', loggedIn=True, username=username) except KeyError: pass return render_template('main.html', loggedIn=False, username="")
def user_answers(username): user = database.getDoc('users', {'username': username}) if user is None: return jsonify({'status': 'error'}) matches = [] db = database.getDB('answers') for a in db.find(): if a['user'] == username: matches.append(a['id']) return jsonify({'status': 'OK', 'answers': matches})
def user_questions(username): user = database.getDoc('users', {'username': username}) if user is None: return jsonify({'status': 'error'}) matches = [] db = database.getDB('questions') for q in db.find(): if q['user']['username'] == username: matches.append(q['id']) return jsonify({'status': 'OK', 'questions': matches})
def user_info(username): user = database.getDoc('users', {'username': username}) if user is None: return jsonify({'status': 'error'}) return jsonify({ 'status': 'OK', 'user': { 'email': user['email'], 'reputation': user['reputation'] } })
def view_user(username): user = database.getDoc('users', {'username':username}) if user is None: return render_template('u_notfound.html') return render_template('user.html', user=user)