def setUp(self): """Prepare model test fixture.""" try: new_attrs = {} new_attrs.update(self.attrs) new_attrs.update(self.do_get_dependencies()) self.obj = self.klass(**new_attrs) DBSession.add(self.obj) DBSession.flush() return self.obj except: DBSession.rollback() raise
def createNew(self, **kw): problem = Problems() problem.code = kw['code'] problem.title = kw['title'] problem.text = kw['text'] problem.input = kw['input'] problem.output = kw['output'] problem.sample_input = kw['sample_input'] problem.sample_ouput = kw['sample_output'] problem.topic = kw['topic'] problem.serie = kw['serie'] problem.points = kw['points'] problem.lang = 'en' problem.date = datetime.datetime.today().isoformat() DBSession.add(problem) flash(_('Problem Saved for serie: ' + str(kw['serie']))) redirect('/problems/list_adm')
def save(self, **kw): if kw['file_source'] != '': # getting problem data problem = DBSession.query(Problems).filter_by(uid=kw['uid']).one() # getting attempt number q = DBSession.query(Submits).filter(Submits.user_id==request.identity['user'].user_id). \ filter(Submits.problem_id==problem.uid).order_by(desc(Submits.attempt)) if q.count() > 0: attempt_nro = q[0].attempt + 1 else: attempt_nro = 1 # getting the current serie of contest serie = DBSession.query(Series).filter_by(current=1) try: serie_num = serie.one().uid title = serie.one().title except: serie_num = 0 title = 'There is not a active contest' """ saving the file """ file = request.POST['file_source'] asset_dirname = os.path.join(os.getcwd(), 'devcon/public/files') #real path to store the file by user user_dir = os.path.join(asset_dirname, request.identity['user'].user_name) user_serie_dir = os.path.join(user_dir, 'serie_' + str(serie_num)) #is dir?? if not os.path.isdir(user_dir): os.mkdir(user_dir) if not os.path.isdir(user_serie_dir): os.mkdir(user_serie_dir) uploaded_filename = file.filename submit_filename = "%s_%d_%d.php" % (file.filename.lstrip(os.sep).replace('.php', ''), problem.serie, attempt_nro) filepath = os.path.join(user_serie_dir, submit_filename) permanent_file = open(filepath, 'w') shutil.copyfileobj(file.file, permanent_file) filesize = "%d Kb" % ((os.path.getsize(filepath)/1024)) file.file.close() #this_file = self.request.params["file"].filename permanent_file.close() os.chmod(filepath, stat.S_IRWXU) """ saving on db """ filename = "%s_%d" % (problem.code.lower(), problem.serie) inputfile_path = os.path.join(os.getcwd(), 'devcon/public/files/inputs', filename + '.in') output_filename = filename + '.out' gen_output_filename = "%s_%d.out" % (filename, attempt_nro) outputfile_path = os.path.join(os.getcwd(), 'devcon/public/files/outputs', output_filename) tmp_filepath = os.path.join(user_serie_dir, gen_output_filename) cmd = "php -f %s < %s > %s" % (filepath, inputfile_path, tmp_filepath) excution_res = os.system(cmd) os.chmod(tmp_filepath, stat.S_IRWXU) # verify if the file was generated if excution_res == 0: if os.path.isfile(tmp_filepath): test_lines = open(tmp_filepath).readlines() correct_lines = open(outputfile_path).readlines() failed = 0 len_diff = len(test_lines) - len(correct_lines) if len_diff != 0: result = 'wrong answer' failed = 1 else: for test, correct in zip(test_lines, correct_lines): if test != correct: result = 'wrong answer' failed = 1 break if failed == 0: ok = 1 else: ok = 0 else: ok = 0 result = 'compilation output missing' else: ok = 0 result = 'compilation error' if ok == 1: result = 'accepted' _submit = Submits() _submit.user_id = request.identity['user'].user_id _submit.problem_id = problem.uid _submit.user_name = request.identity['user'].user_name _submit.problem_title = problem.title _submit.datetime = datetime.datetime.today().isoformat() _submit.filename = uploaded_filename _submit.output_filename = gen_output_filename _submit.submit_filename = submit_filename _submit.attempt = attempt_nro _submit.result = result _submit.comments = kw['Comments'] _submit.serie = serie_num _submit.accepted = ok DBSession.add(_submit) if ok == 1: flash('Congratulations, your solution was accepted') else: flash('Sorry, someting was wrong', 'error') redirect('/problems/submits_list') #return dict(filename=filename, filesize=filesize, problem=problem) else: flash(_('The file is required'), 'warning') redirect('/problems/submit?uid=' + kw['uid'])