def submit(id): data = request.form.to_dict() ops = [] lang = langs[data['lang']] user = get_jwt_identity() try: post = Post.objects(ID=id)[0].to_mongo() if any(bad in data['code'] for bad in lang.invalid): ret = '' for i in lang.invalid: ret += i return Response(json.dumps({'success': False, 'err': 'Unsupported functions used.\nCannot use These Functions : '+ret}), mimetype="application/json", status=200) else: for i, tc in enumerate(post['testcases']): op = compile_and_run_code(lang, data['code'], tc) if not op['success']: if op['timeout']: return Response(json.dumps({'success': False, 'err': 'Time out occured for Case #'+str(i+1)}), mimetype="application/json", status=200) else: return Response(json.dumps({'success': False, 'err': 'Error in TestCase #'+str(i+1)+op['err']}), mimetype="application/json", status=200) else: ops.append(op['output']) for i, op in enumerate(ops): if op == post['outputs'][i]: continue else: return Response(json.dumps({'success': False, 'err': 'Error in TestCase #'+str(i+1)+'\nExpected : '+post['outputs'][i]+'\nGot : '+op}), mimetype="application/json", status=200) User.objects(username=get_jwt_identity() ).update_one(push__posts=post['_id']) return Response(json.dumps({'success': True, 'link': '/dashboard'}), mimetype="application/json", status=201) except: return Response(json.dumps({'Success': False, 'redirect': '/'}), mimetype="application/json", status=200)
def mypost(id): user = User.objects(username=get_jwt_identity())[0].to_mongo() if id not in user['posts']: return redirect('/dashboard') post = Post.objects(ID=id)[0].to_mongo() return render_template('viewpost.html', logged_in=True, post=post)
def my_posts(): items = [] posts = Post.objects(originalPostBy=get_jwt_identity()) for post in posts: item = post.to_mongo() items.append(item) return render_template('myposts.html', posts=items, logged_in=True)
def solve(id): user = User.objects(username=get_jwt_identity())[0].to_mongo() if id in user['posts']: return redirect('/dashboard') try: post = Post.objects(ID=id)[0].to_mongo() return render_template('solve.html', langs=langs, logged_in=True, post=post) except: return redirect('/dashboard')
def viewed_posts(): items = [] user = User.objects(username=get_jwt_identity())[0].to_mongo() posts_id = user['posts'] for postid in posts_id: post = Post.objects(ID=postid)[0].to_mongo() if post['originalPostBy'] != user['username']: items.append(post) return render_template('viewedposts.html', posts=items, logged_in=True)
def globalwall(): user = User.objects(username=get_jwt_identity())[0].to_mongo() items = Post.objects(qtype='Global') items = [item.to_mongo() for item in items] items = [item for item in items if item['_id'] not in user['posts']] items.reverse() return render_template('globalwall.html', posts=items[:MAX_POSTS_GLOBAL_WALL], logged_in=True)
def solution(id): user = User.objects(username=get_jwt_identity())[0].to_mongo() try: post = Post.objects(ID=id)[0].to_mongo() if id not in user['posts']: User.objects(username=get_jwt_identity()).update_one( push__posts=post['_id']) return render_template('solution.html', logged_in=True, post=post) except: return redirect('/')
def test(id): data = request.form.to_dict() lang = langs[data['lang']] post = Post.objects(ID=id)[0].to_mongo() if any(bad in data['code'] for bad in lang.invalid): ret = '' for i in lang.invalid: ret += i return Response(json.dumps({ 'success': False, 'err': 'Unsupported functions used.\nCannot use These Functions : ' + ret }), mimetype="application/json", status=200) else: op = compile_and_run_code(lang, data['code'], post['testcases'][0]) if not op['success']: if op['timeout']: return Response(json.dumps({ 'success': False, 'err': 'Time out occured' }), mimetype="application/json", status=200) else: return Response(json.dumps({ 'success': False, 'err': 'Error ' + op['err'] }), mimetype="application/json", status=200) if op['output'] == post['outputs'][0]: return Response(json.dumps({ 'success': True, 'output': 'Test Case Passes' + op['output'] }), mimetype="application/json", status=200) else: return Response(json.dumps({ 'success': False, 'err': 'Expected : ' + post['outputs'][0] + ' Got ' + op['output'] }), mimetype="application/json", status=200)
def dashboard(): user = User.objects(username=get_jwt_identity())[0].to_mongo() posts = [] for id in user['following']: u = User.objects(id=id)[0].to_mongo() for postid in u['posts']: if postid not in user['posts']: p = Post.objects(ID=postid)[0].to_mongo() p['by'] = u['username'] posts.append(p) posts = posts[:MAX_POSTS_DASHBOARD] random.shuffle(posts) items = [] x = Post.objects(originalPostBy=get_jwt_identity()) for post in x: item = post.to_mongo() items.append(item) return render_template('dashboard.html', logged_in=True, posts=posts, user=user, items=items)
def past_status(): post_map = dict() posts = Post.objects(owner=current_user.id).order_by('-date').limit(29) for post in posts: post_map[post.date_string] = ( post.completed, post.length, post.url_string() ) return [ post_map.get( make_datestring(today() - timedelta(days=i)), (False, 0, None) ) for i in reversed(range(1, 29)) ]
def get_past(): if not current_user.is_authenticated(): return redirect(url_for("index")) posts = Post.objects(owner=current_user.id, length__gt=0).order_by('-date') return render_template('past.html', posts=posts)