示例#1
0
def add_category(request, contest_id):
    contest = get_object_or_404(models.TaskBasedContest, pk=contest_id)
    participant = contest.get_participant_for_user(request.user)
    if contest.tasks_grouping != models.TasksGroping.ByCategories:
        return HttpResponseNotFound()

    if request.method == 'POST':
        form = forms.CategoryForm(data=request.POST)
        if form.is_valid():
            category = categories_models.Category(
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description']
            )
            with transaction.atomic():
                category.save()

                contest.categories_list.categories.add(category)
                contest.save()
            return redirect(urlresolvers.reverse('contests:tasks', args=[contest.id]))
    else:
        form = forms.CategoryForm()

    return render(request, 'contests/create_category.html', {
        'current_contest': contest,

        'contest': contest,
        'participant': participant,
        'form': form,
    })
示例#2
0
def edit_category(request, contest_id, category_id):
    contest = get_object_or_404(models.TaskBasedContest, pk=contest_id)
    participant = contest.get_participant_for_user(request.user)

    if contest.tasks_grouping != models.TasksGroping.ByCategories:
        return HttpResponseNotFound()
    category = get_object_or_404(categories_models.Category, pk=category_id)
    if not contest.categories_list.categories.filter(id=category.id).exists():
        return HttpResponseNotFound()

    if request.method == 'POST':
        form = forms.CategoryForm(data=request.POST)
        if form.is_valid():
            new_category = categories_models.Category(
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description']
            )
            new_category.id = category.id
            new_category.save()
            return redirect(urlresolvers.reverse('contests:tasks', args=[contest.id]))
    else:
        form = forms.CategoryForm(initial=category.__dict__)

    return render(request, "contests/edit_category.html", {
        'current_contest': contest,

        'contest': contest,
        'participant': participant,
        'category': category,
        'form': form,
    })
示例#3
0
    def to_model(self, ctx):
        contest, _ = contest_models.TaskBasedContest.objects.update_or_create(
            name=self.name,
            defaults={
                'is_visible_in_list': self.is_visible_in_list,
                'registration_type': self.registration_type,
                'participation_mode': self.participation_mode,
                'start_time': self.start_time,
                'finish_time': self.finish_time,
                'registration_start_time': self.registration_start_time,
                'registration_finish_time': self.registration_finish_time,
                'short_description': self.short_description,
                'description': self.description,
                'tasks_grouping': self.tasks_grouping,
            },
        )

        if self.task_opening_policy == "All":
            task_models.AllTasksOpenedOpeningPolicy.objects.get_or_create(
                contest=contest)
        elif self.task_opening_policy == "Manual":
            task_models.ManualTasksOpeningPolicy.objects.get_or_create(
                contest=contest)
        elif self.task_opening_policy == "Welcome":
            task_models.WelcomeTasksOpeningPolicy.objects.get_or_create(
                contest=contest)
        else:
            raise RuntimeError("Unknown task opening policy: %s" %
                               self.task_opening_policy)

        for region in self.regions:
            contest_models.ContestRegion.objects.update_or_create(
                contest=contest,
                name=region['name'],
                defaults={
                    'start_time': region['start_time'],
                    'finish_time': region['finish_time'],
                    'timezone': region.get('timezone', 'UTC'),
                },
            )

        if contest.tasks_grouping == contest_models.TasksGroping.OneByOne:
            contest_tasks, _ = task_models.ContestTasks.objects.get_or_create(
                contest=contest)
            for task in self.task_set.to_model(ctx):
                contest_tasks.tasks.add(task)
            contest_tasks.save()
        else:
            contest_categories, _ = categories_models.ContestCategories.objects.get_or_create(
                contest=contest)
            old_categories = {
                c.name: c
                for c in contest_categories.categories.all()
            }
            contest_categories.categories.clear()
            for category in self.task_set.to_model(ctx):
                db_category = old_categories.pop(category['name'], None)
                if db_category is None:
                    db_category = categories_models.Category(
                        name=category['name'])
                db_category.description = category.get('description', '')
                db_category.save()

                db_category.tasks.set(category['tasks'])
                contest_categories.categories.add(db_category)

            for old_category in old_categories.values():
                old_category.delete()

        return contest