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 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 increment_view(question, user=None, ip=None): db = database.getDB('questions') print('views: ' + str(question['view_count'])) if user is not None: if user not in question['viewer_usernames']: print('-------------- registered user ' + str(user)) newViews = question['view_count'] + 1 newUsers = question['viewer_usernames'] newUsers.append(user) db.update_one({'_id': question['_id']}, { '$set': { 'view_count': newViews, 'viewer_usernames': newUsers } }) elif ip is not None: if ip not in question['viewer_IPs']: print('-------------- registered IP ' + str(ip)) newViews = question['view_count'] + 1 newIPs = question['viewer_IPs'] newIPs.append(ip) db.update_one( {'_id': question['_id']}, {'$set': { 'view_count': newViews, 'viewer_IPs': newIPs }}) return
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 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 tagged_questions(tag): db = database.getDB('questions') questions = [] for q in db.find(): if tag in q['tags']: questions.append({'title':q['title'], 'id':q['id']}) if request.method == 'POST': return jsonify({'questions':questions}) elif request.method == 'GET': return render_template('tagged.html', questions=questions, tag=tag)
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