Ejemplo n.º 1
0
Archivo: views.py Proyecto: xelez/drapo
def create(request):
    if request.method == 'POST':
        form = forms.TaskBasedContestForm(data=request.POST)
        if form.is_valid():
            with transaction.atomic():
                contest_data = copy.copy(form.cleaned_data)
                del contest_data['by_categories_tasks_opening_policy']
                del contest_data['manual_tasks_opening_policy']

                contest = models.TaskBasedContest(**contest_data)
                contest.save()
                if contest.tasks_grouping == models.TasksGroping.ByCategories:
                    categories_models.ContestCategories(contest=contest).save()
                elif contest.tasks_grouping == models.TasksGroping.OneByOne:
                    tasks_models.ContestTasks(contest=contest).save()

                if form.cleaned_data[
                        'by_categories_tasks_opening_policy'] != '-':
                    tasks_models.ByCategoriesTasksOpeningPolicy(
                        contest=contest,
                        opens_for_all_participants=form.
                        cleaned_data['by_categories_tasks_opening_policy'] ==
                        'E').save()

                if form.cleaned_data['manual_tasks_opening_policy']:
                    tasks_models.ManualTasksOpeningPolicy(
                        contest=contest).save()
            messages.success(request, 'Contest «%s» created' % contest.name)
            return redirect(contest)
    else:
        form = forms.TaskBasedContestForm()

    return render(request, 'contests/create.html', {'form': form})
Ejemplo n.º 2
0
def edit(request, contest_id):
    contest = get_object_or_404(models.TaskBasedContest, pk=contest_id)

    if request.method == 'POST':
        form = forms.TaskBasedContestForm(data=request.POST)
        if form.is_valid():
            with transaction.atomic():
                contest = get_object_or_404(models.TaskBasedContest, pk=contest_id)

                contest_data = copy.copy(form.cleaned_data)
                del contest_data['by_categories_tasks_opening_policy']
                del contest_data['manual_tasks_opening_policy']

                new_contest = models.TaskBasedContest(
                    **contest_data
                )
                new_contest.id = contest.id
                new_contest.save()

                if new_contest.tasks_grouping == models.TasksGroping.ByCategories:
                    if not hasattr(contest, 'categories_list'):
                        categories_models.ContestCategories(contest=new_contest).save()
                elif contest.tasks_grouping == models.TasksGroping.OneByOne:
                    if not hasattr(contest, 'tasks_list'):
                        tasks_models.ContestTasks(contest=new_contest).save()

                # Remove all old tasks opening policies
                # It's not just call contest.tasks_opening_policies.all().delete() because of
                # django + django-polymorphic + postgresql bug:
                # https://github.com/django-polymorphic/django-polymorphic/issues/34
                for policy in contest.tasks_opening_policies.all():
                    policy.delete()

                # And create new
                if form.cleaned_data['by_categories_tasks_opening_policy'] != '-':
                    tasks_models.ByCategoriesTasksOpeningPolicy(
                        contest=contest,
                        opens_for_all_participants=form.cleaned_data['by_categories_tasks_opening_policy'] == 'E'
                    ).save()

                if form.cleaned_data['manual_tasks_opening_policy']:
                    tasks_models.ManualTasksOpeningPolicy(
                        contest=contest
                    ).save()

            messages.success(request, 'Contest «%s» has been updated' % new_contest.name)
            return redirect(contest)
    else:
        contest_data = copy.copy(contest.__dict__)

        by_categories = contest.tasks_opening_policies.instance_of(tasks_models.ByCategoriesTasksOpeningPolicy).first()
        contest_data['by_categories_tasks_opening_policy'] = \
            '-' if by_categories is None else ['T', 'E'][by_categories.opens_for_all_participants]

        manual = contest.tasks_opening_policies.instance_of(tasks_models.ManualTasksOpeningPolicy).first()
        contest_data['manual_tasks_opening_policy'] = manual is not None

        form = forms.TaskBasedContestForm(initial=contest_data)

    participant = contest.get_participant_for_user(request.user)
    return render(request, 'contests/edit.html', {
        'current_contest': contest,
        'participant': participant,

        'form': form
    })