Exemple #1
0
        def put(self):
            pprint(self.request.body)
            submission = dict()
            try:
                data = json.loads(self.request.body)
            except:
                self.write(fx.gen_result("1","Wrong request"))
                return

            fields = ["email","contest","language","source","problem"]

            if not set(fields).issubset(data):
                self.write(fx.gen_result("2","Missing data"))
                return

            submission['user'] = {'email':data['email'], 'user_class':0, 'name':data['email']}
            n = db.users.find({"email":submission['user']['email']}).count()
            if n == 0:
                self.write(fx.gen_result("3","No user"))
                return

            filename = data['problem']

            submission['contest'] = data['contest']
            submission['problem'] = filename
            submission['lang'] = data['language']
            n = db.contests.find({"problems":submission['problem'],"name":submission['contest']}).count()
            if n == 0:
                self.write(fx.gen_result("5","No such problem"))
                return

            if not (submission['lang'] in ["pas","c","cpp"]):
                self.write(fx.gen_result("6","Wrong language"))
                return

            submission['time'] = now()
            submission['id'] = int(fx.get_submission_id())
            submission['body'] = data['source']
 

            submission_obj = models.Submission(**submission)
            result = submission_obj.validate()
            if not result:
                self.write(fx.gen_result("7","Security alert"))
                return
            result = submission_obj.add()
            print "\n"
            pprint(submission)
            f = open("data/users/"+submission["user"]['email']+"/"+str(int(submission["id"]))+"."+submission["lang"],"w")
            f.write(submission['body'])
            f.close()
            self.write(fx.gen_result(**result))
Exemple #2
0
        def post(self):
            curr_time = now()
            last = db.submissions.find_one({"user":self.current_user},sort=[("time", pymongo.DESCENDING)])
            if last and curr_time-int(last["time"]) < 20:
                self.write(fx.gen_result("6","Wait for 20 seconds"))
                return
            try:
                source = self.request.files['source']
                if len(source[0]['body']) > 1024*1024:
                    self.write(fx.gen_result("6","File too big"))
                    return
            except:
                self.write(fx.gen_result("2","No source"))
                return
            contest = self.get_argument("contest",None)

                
            filename = source[0]['filename'].split('.')
            problem = filename[0]
            if len(filename) < 2:
                lang = "exe"
            else:
                lang = filename[1]
            body = source[0]['body']
            print problem
            if not (lang in ["pas","c","cpp"]):
                self.write(fx.gen_result("4","Wrong language"))
                return

            if contest:
                n = db.contests.find({"problems":problem,"name":contest}).count()
            else:
                n = db.problems.find({"name":problem,"archive":True}).count()
            if n == 0:
                self.write(fx.gen_result("4","No such problem"))
                return

            time = now()
            if contest:
                n = db.contests.find({
                "start" : { "$lte": time },
                "end" : { "$gte" : time }
                }).count()

                if n == 0:
                    self.write(fx.gen_result("5","Too late"))
                    return

            submission_data = dict(
                user = self.current_user,
                problem = problem,
                time = time,
                contest = contest,
                lang = lang,
                id = fx.get_submission_id(),
                body = body
                )
            submission = models.Submission(**submission_data)
            result = submission.validate()
            if not result:
                self.write(fx.gen_result("1","Security alert"))
                return

            result = submission.add()
            os.umask(0)
            f = open("data/users/"+self.current_user["email"]+"/"+str(int(submission_data["id"]))+"."+submission_data["lang"],"w")
            f.write(body)
            f.close()
            self.write(fx.gen_result(**result))
Exemple #3
0
        def post(self):
            curr_time = now()
            last = db.submission.find_one({"user":self.current_user}).sort("_id",-1)
            if last and now()-int(last["time"]) < 60:
                self.write(fx.gen_result("6","Wait"))
                return
        	try:
                source = self.request.files['source']
            except:
                self.write(fx.gen_result("2","No source"))
                return
            contest = self.get_argument("contest",None)

                
            filename = source[0]['filename'].split('.')
            problem = filename[0]
            if len(filename) < 2:
                lang = "exe"
            else:
                lang = filename[1]
            body = source[0]['body']
            print problem
            if not (lang in ["pas","c","cpp"]):
                self.write(fx.gen_result("4","Wrong language"))
                return

            if contest:
                n = db.contests.find({"problems":problem,"name":contest}).count()
            else:
                n = db.problems.find({"name":problem,"archive":True}).count()
            if n == 0:
                self.write(fx.gen_result("5","No such problem"))
                return

            time = now()
            if contest:
                n = db.contests.find({
                "start" : { "$lte": time },
                "end" : { "$gte" : time }
                }).count()

                if n == 0:
                    self.write(fx.gen_result("5","Too late"))
                    return

            submission_data = dict(
                user = self.current_user,
                problem = problem,
                time = time,
                contest = contest,
                lang = lang,
                id = fx.get_submission_id(),
                body = body
                )
            submission = models.Submission(**submission_data)
            result = submission.validate()
            if not result:
                self.write(fx.gen_result("1","Security alert"))
                return

            result = submission.add()
            os.umask(0)
            f = open("data/users/"+self.current_user["email"]+"/"+str(int(submission_data["id"]))+"."+submission_data["lang"],"w")
            f.write(body)
            f.close()
            self.write(fx.gen_result(**result))
            
        @tornado.web.authenticated
        def get(self):
            id = self.get_argument("id",None)
            if not id:
                submissions = db.submissions.find({"user.email":self.current_user['email']}).sort("time",-1)
                self.write(dumps(list(submissions)))
                return

            submission = db.submissions.find_one({"id":int(id)})

            if not submission:
                self.write(fx.gen_result("1","Error"))
                return

            field = "result"
            type = self.get_argument("type",None)
            if self.current_user['user_class'] == 1 and not type:
                field = "data"

            if self.request.headers.get('Referer').split("/")[-2] == "archive":
            	field = "data"

            if not submission[field]:
            	field = "result"
            self.write(fx.gen_result("0","Success",submission[field]))