def problem_copy(user, original_problem, new_problem_set): """ Copies the given problem to the given problem set. Verifies if: a) user can edit the problem. b) user can edit the problem set. Returns the copy of the given problem. """ verify(user.can_view_problem(original_problem)) verify(request.user.can_edit_problem_set(new_problem_set)) new_problem = Problem() new_problem.title = original_problem.title new_problem.description = original_problem.description new_problem.problem_set = new_problem_set new_problem.tags = original_problem.tags new_problem.save() original_parts = original_problem.parts.all() for original_part in original_parts: new_part = Part() new_part.problem = new_problem new_part.description = original_part.description new_part.solution = original_part.solution new_part.validation = original_part.validation new_part.secret = original_part.secret new_part.save() return new_problem
def test_new_problem_not_published_by_default(self): problem = Problem( title="Problem Title", description="Problem Description", ) problem.save() self.assertFalse(problem.published)
def test_add_slug_to_problem_if_not_provided(self): problem = Problem( title="Problem Title", description="Problem Description", ) problem.save() saved_problem = Problem.objects.get(pk=problem.pk) self.assertEqual(saved_problem.slug, "problem-title")
def form_valid(self, form): user = self.request.user problem = Problem(user=Profile.objects.get(user=user.pk), title=form['title'].data, description=form['description'].data, photo=form['photo'].data, tag=Tag.objects.get(name=form['tag'].data), latitude=form.latitude, longitude=form.longitude, address=form.address) problem.save() return super().form_valid(form)
def test_create_new_problem(self): problem = Problem( title="Problem Title", slug="problem-title", description="Problem Description", published=False, author="Problem Author or Contributor", ) problem.save() saved_problems = Problem.objects.all() self.assertEqual(saved_problems.count(), 1)
def AddProblem(self, event): # wxGlade: ProblemTools.<event_handler> dlg = ProblemSelector(self.GetParent()) if dlg.ShowModal()==wx.ID_OK: p = Problem() p.name = dlg.active_code p.severity = Problem.SEVERITY_CHOICES[dlg.severity.GetSelection()][0] p.activity = Problem.ACTIVITY_CHOICES[dlg.activity.GetSelection()][0] p.expiry = dlg.expiry.GetValue() if p.expiry.IsValid(): p.expiry = p.expiry.FormatISODate() else: p.expiry = None p.parent = None p.save() self.GetParent().Reload()
def new(request): problem = Problem() context = {} if request.method == "POST": problem.title = request.POST.get('title','') problem.author = request.POST.get('author','') problem.description = request.POST.get('description','') problem.task = request.POST.get('task', '') problem.solution = request.POST.get('solution', '') problem.user = request.user try: problem.difficulty = int(request.POST.get('difficulty', '0')) except ValueError: problem.difficulty = 0 try: problem.coolness = int(request.POST.get('coolness', '0')) except ValueError: problem.coolness = 0 try: problem.save() for tag_id in request.POST.getlist('tags'): try: tag_obj = Tag.objects.get(id = tag_id) problem.tags.add(tag_obj) except tag.DoesNotExist: pass success_msg(request, "The problem was created successfully.") return redirect('problems-problem', problem.id) except problem.Error as error: error_msg(request, "Could not create the problem because of some errors.") context['error'] = error context['problem'] = problem tags = list(Tag.objects.all()) tags.sort(key = lambda x : pl_filter(x.name.lower())) context['tags'] = tags selected_tags = [] for tag_id in request.POST.getlist('tags'): try: selected_tags.append(int(tag_id)) except ValueError: pass context['selected_tags'] = selected_tags return render(request, 'problems/new.html', context)
def problem_new(request): profile = check_auth(request.POST.get('token')) if not isinstance(profile, Profile): return profile try: title = request.POST['title'] description = request.POST['description'] photo = request.POST.get('photo') tag = Tag.objects.get(name=request.POST['tag']) except KeyError as e: return HttpResponseBadRequest('Не все поля заполнены: ' + str(e.args[0])) latitude = request.POST.get('latitude') longitude = request.POST.get('longitude') address = request.POST.get('address') try: if latitude and longitude: latitude, longitude, address = ll_to_address(latitude, longitude) elif address: latitude, longitude, address = address_to_ll(address) else: return HttpResponseBadRequest( 'Отсутствует местоположение (координаты или адрес)') except Exception: return HttpResponseServerError('Ошибка Yandex API') try: problem = Problem(user=profile, title=title, description=description, photo=photo, tag=tag, latitude=latitude, longitude=longitude, address=address) problem.save() except Exception: return HttpResponseServerError('Ошибка сохранения в БД') return HttpResponse()
def update_problem(): problem_file = os.path.join(cur_dir, 'json/problem.json') with open(problem_file, 'r') as f: problems = json.loads(f.read()) for p in problems: keyword = p['keyword'][0].strip() name = p['name'][0].strip() if p['source']: source = p['source'][0].strip() else: source = '기타' category = [c.strip() for c in p['category']] submitted = int(p['submitted'][0]) accepted = int(p['accepted'][0]) try: s = Source.objects.get(name=source) except Source.DoesNotExist: s = Source(name=source) s.save() try: p = Problem.objects.get(keyword=keyword) p.submitted = submitted p.accepted = accepted except: p = Problem(keyword=keyword, name=name, source=s, submitted=submitted, accepted=accepted) finally: p.save() for c in category: try: c = Category.objects.get(name=c) except Category.DoesNotExist: c = Category(name=c) c.save() p.category.add(c)
def download_problem(id): url = archive_url + "/?q=node/" + id print(url) r = requests.get(url, cookies=js_cookie) soup = BeautifulSoup(r.text, 'html.parser') problem_data = fetch_statement(soup, 'node-' + id) statement = problem_data[0] solution = problem_data[1] problem = Problem(statement=statement, solution=solution) problem.save() for tag_name in fetch_tags(soup): if Tag.objects.filter(name=tag_name): tag = Tag.objects.get(name=tag_name) else: tag = Tag(name=tag_name, type_id=tag_types.index('Źródło')) tag.save() problem.tag_set.add(tag) problem.save()
import json from problems.models import Problem with open('problems.json') as f: problems_json = json.load(f) for problem in problems_json: problem = Problem(oj_name=problem['oj_name'], prob_id=problem['prob_id'], name=problem['name'], category=problem['category'], link=problem['link']) for problem in problems_json: problem = Problem(oj_name=problem['oj_name'], prob_id=problem['prob_id'], name=problem['name'], category=problem['category'], link=problem['link']) problem.save()
id = 1000 for item in problems: p = Problem() p.problem_id = str(id) p.title = item.get('Title', '') p.description = item.get('ProblemDescription', '') p.input_description = item.get('Input', '') p.output_description = item.get('Output', '') p.sample_input = item.get('SampleInput', '') p.sample_output = item.get('SampleOutput', '') p.hint = item.get('Hint', '') p.source = item.get('Author', '') # 插入1个题目 , 插入外键需要 两个contest 以及 problem 实例 不好改 if id <= 1010: p.contest = Contest.objects.get(pk=1) p.save() id += 1 # str_id = str(id + 2000) # CP = Contest_problem() # CP.contest_problem_id = str_id # CP.contest_id = 'A001' # CP.accepted = id # CP.submitted = (id) * id / 10 # # CP.problem_id = p.problem_id # CP.title = p.title # CP.description = p.description # CP.input_decscription = p.input_decscription # CP.output_decscription = p.output_decscription # CP.sample_input = p.sample_input # CP.sample_output = p.sample_output