def add(request): news = News() context = {} if request.method == 'POST': news.title = request.POST.get('title','') news.text = request.POST.get('text','') news.user = request.user try: if request.POST.get('preview', 'no-preview') != 'no-preview': news.check() context['preview'] = True else: news.save() success_msg(request, "Message created successfully.") return redirect('news-news') except news.Error as error: error_msg(request, "Could not create the message because of some errors.") context['error'] = error context['news'] = news return render(request,'news/add.html', context)
def new_function(request, **kwargs): try: kwargs['problem'] = Problem.objects.get(id = kwargs['problem']) except Problem.DoesNotExist: error_msg(request, "Problem with id={} does not exist.".format(kwargs['problem'])) raise Http404 return original_function(request, **kwargs)
def new_function(request, **kwargs): try: kwargs['thread'] = ForumThread.objects.get(id = kwargs['thread']) except ForumThread.DoesNotExist: error_msg(request, "Thread with id={} does not exist.".format(kwargs['thread'])) raise Http404 return original_function(request, **kwargs)
def new_function(request, **kwargs): try: kwargs['contest'] = Contest.objects.get(id = kwargs['contest']) except Contest.DoesNotExist: error_msg(request, "Contest with id={} does not exist.".format(kwargs['contest'])) raise Http404 return original_function(request, **kwargs)
def remove_package(request, problem, package): if package.user != request.user: error_msg(request, "You don't have permissions to remove this package.") raise PermissionDenied package.delete() success_msg(request, "Package was successfully deleted.") return redirect('problems-problem-packages', problem.id)
def new_function(request, **kwargs): try: kwargs['package'] = Package.objects.get(id = kwargs['package']) except Package.DoesNotExist: error_msg(request, "Package with id={} does not exist.".format(kwargs['package'])) raise Http404 if kwargs['package'].problem != kwargs['problem']: error_msg(request, "Package with id={} does not belong to this problem.".format(kwargs['package'])) raise Http404 return original_function(request, **kwargs)
def new_function(request, **kwargs): try: kwargs['round'] = Round.objects.get(id = kwargs['round']) except Round.DoesNotExist: error_msg(request, "Round with id={} does not exist.".format(kwargs['round'])) raise Http404 if kwargs['round'].contest != kwargs['contest']: error_msg(request, "Round with id={} does not belong to this contest.".format(kwargs['round'].id)) raise Http404 return original_function(request, **kwargs)
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 edit(request, problem): if request.user != problem.user: error_msg(request, "You don't have permissions to edit the problem with id={}".format(problem.id)) raise PermissionDenied context = {'problem' : problem, 'tab' : 'edit'} tags = list(Tag.objects.all()) tags.sort(key = lambda x : pl_filter(x.name.lower())) context['tags'] = tags if request.method == 'POST': if request.POST.get('remove','no-remove') != 'no-remove': problem.delete() success_msg(request, "Problem was deleted successfully.") return redirect('problems-problems') 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', '') 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 problem.tags.clear() for tag_id in request.POST.getlist('tags'): try: tag_obj = Tag.objects.get(id=int(tag_id)) problem.tags.add(tag_obj) except ValueError: pass except Tag.DoesNotExist: pass try: problem.save() success_msg(request, "The problem was edited successfully.") except problem.Error as error: error_msg(request, "Couldn't edit the problem because of some errors.") context['error'] = error return render(request, 'problems/problem/edit.html', context)
def round_add(request, contest): context = {} round = Round() if request.method == 'POST': round.name = request.POST.get('name', '') round.contest = contest try: round.save() success_msg(request, "The round was created successfully.") return redirect('contests-contest', contest.id) except round.Error as error: error_msg(request, "The round couldn't be created because of some errors.") context['error'] = error context['contest'] = contest return render(request, 'contests/contest/round/add.html', context)
def add(request): contest = Contest() context = {} if request.method == 'POST': contest.name = request.POST.get('name', '') contest.description = request.POST.get('description', '') contest.begin_date = request.POST.get('begin_date', '') contest.end_date = request.POST.get('end_date', '') try: contest.save() success_msg(request, "Contest was created successfully.") return redirect('contests-contest', contest.id) except contest.Error as error: error_msg(request, "Could not create the contest because of some errors.") context['error'] = error context['contest'] = contest return render(request, 'contests/contest/add.html', context)
def upload(request, problem): package = Package() context = {} if request.method == 'POST': package.user = request.user package.comment = request.POST.get('comment', '') package.problem = problem if re.search(r'^[a-zA-Z0-9_\.\-]+$', request.FILES['input_file'].name) is None: error_msg(request, "The file name may contain only those characters: {}{}_.-".format(string.ascii_letters, string.digits)) else: package.package.save(request.FILES['input_file'].name, request.FILES['input_file']) package.save() success_msg(request, "Package uploaded successfully.") return redirect('problems-problem-packages', problem.id) context['problem'] = problem context['package'] = package context['tab'] = 'packages' return render(request, 'problems/problem/packages/upload.html', context)
def new_thread(request): context = {} thread = ForumThread() new_post = Post() if request.method == 'POST': thread.title = request.POST.get('title', '') new_post.content = request.POST.get('post', '') new_post.user = request.user try: if request.POST.get('preview', 'no-preview') != 'no-preview': thread.check() new_post.check() context['preview'] = True else: thread.check() new_post.check() thread.save() new_post.thread = thread.thread new_post.save() success_msg(request, "Thread created successfully.") return redirect('forum-thread', thread.id) except thread.Error as error: error_msg(request, "Could not create the thread because of some errors.") context['error'] = error except new_post.Error as error: error_msg(request, "Could not create the thread because of some errors.") context['error'] = error context['thread'] = thread context['new_post'] = new_post return render(request, 'forum/new_thread.html', context)
def round_edit(request, contest, round): context = {} if request.method == 'POST': round.name = request.POST.get('name', '') if request.POST.get('remove', 'no-remove') != 'no-remove': round.delete() success_msg(request, "Round was deleted successfully.") return redirect('contests-contest', contest.id) try: round.save() success_msg(request, "Round was edited successfully.") return redirect('contests-contest', contest.id) except round.Error as error: error_msg(request, "Could not edit the round because of some errors.") context['error'] = error context['contest'] = contest context['round'] = round return render(request, 'contests/contest/round/edit.html', context)
def edit(request, contest): context = {} if request.method == 'POST': contest.name = request.POST.get('name', '') contest.description = request.POST.get('description', '') contest.begin_date = request.POST.get('begin_date', '') contest.end_date = request.POST.get('end_date' ,'') if request.POST.get('remove', 'no-remove') != 'no-remove': contest.delete() error_msg(request, "Contest was deleted successfully.") return redirect('contests-contests') try: contest.save() success_msg(request, "Contest was edited successfully.") return redirect('contests-contest', contest.id) except contest.Error as error: error_msg(request, "Could not create the contest because of some errors.") context['error'] = error context['contest'] = contest return render(request, "contests/contest/edit.html", context)
def edit(request, ID): try: news = News.objects.get(id = ID) except News.DoesNotExist: error_msg(request, "Message with id={} does not exist.".format(ID)) raise Http404 if news.user != request.user: error_msg(request, "You don't have permissions to edit this message.") raise PermissionDenied context = {} if request.method == 'POST': if request.POST.get('remove', 'no-remove') != 'no-remove': news.delete() success_msg(request, "Message deleted successfully.") return redirect('news-news') news.title = request.POST.get('title','') news.text = request.POST.get('text','') try: if request.POST.get('preview', 'no-preview') != 'no-preview': news.check() context['preview'] = True else: news.save() success_msg(request, "Message edited successfully.") return redirect('news-news') except news.Error as error: error_msg(request, "Could not edit the message because of some errors.") context['error'] = error context['news'] = news return render(request, 'news/edit.html', context)