示例#1
0
def renumber_events(pctx):
    if pctx.role not in [
            participation_role.instructor,
            participation_role.teaching_assistant
    ]:
        raise PermissionDenied("only instructors and TAs may do that")

    request = pctx.request

    if request.method == "POST":
        form = RenumberEventsForm(request.POST, request.FILES)
        if form.is_valid():
            events = list(
                Event.objects.filter(
                    course=pctx.course,
                    kind=form.cleaned_data["kind"]).order_by('time'))

            if events:
                queryset = (Event.objects.filter(
                    course=pctx.course, kind=form.cleaned_data["kind"]))

                queryset.delete()

                ordinal = form.cleaned_data["starting_ordinal"]
                for event in events:
                    new_event = Event()
                    new_event.course = pctx.course
                    new_event.kind = form.cleaned_data["kind"]
                    new_event.ordinal = ordinal
                    new_event.time = event.time
                    new_event.end_time = event.end_time
                    new_event.all_day = event.all_day
                    new_event.shown_in_calendar = event.shown_in_calendar
                    new_event.save()

                    ordinal += 1

                messages.add_message(request, messages.SUCCESS,
                                     "Events renumbered.")
            else:
                messages.add_message(request, messages.ERROR,
                                     "No events found.")

    else:
        form = RenumberEventsForm()

    return render_course_page(pctx, "course/generic-course-form.html", {
        "form": form,
        "form_description": "Renumber events",
    })
示例#2
0
def renumber_events(pctx):
    if pctx.role not in [
            participation_role.instructor,
            participation_role.teaching_assistant]:
        raise PermissionDenied(_("only instructors and TAs may do that"))

    request = pctx.request

    if request.method == "POST":
        form = RenumberEventsForm(request.POST, request.FILES)
        if form.is_valid():
            events = list(Event.objects
                    .filter(course=pctx.course, kind=form.cleaned_data["kind"])
                    .order_by('time'))

            if events:
                queryset = (Event.objects
                    .filter(course=pctx.course, kind=form.cleaned_data["kind"]))

                queryset.delete()

                ordinal = form.cleaned_data["starting_ordinal"]
                for event in events:
                    new_event = Event()
                    new_event.course = pctx.course
                    new_event.kind = form.cleaned_data["kind"]
                    new_event.ordinal = ordinal
                    new_event.time = event.time
                    new_event.end_time = event.end_time
                    new_event.all_day = event.all_day
                    new_event.shown_in_calendar = event.shown_in_calendar
                    new_event.save()

                    ordinal += 1

                messages.add_message(request, messages.SUCCESS,
                        _("Events renumbered."))
            else:
                messages.add_message(request, messages.ERROR,
                        _("No events found."))

    else:
        form = RenumberEventsForm()

    return render_course_page(pctx, "course/generic-course-form.html", {
        "form": form,
        "form_description": _("Renumber events"),
    })
示例#3
0
def renumber_events(pctx):
    if not pctx.has_permission(pperm.edit_events):
        raise PermissionDenied(_("may not edit events"))

    request = pctx.request

    if request.method == "POST":
        form = RenumberEventsForm(request.POST, request.FILES)
        if form.is_valid():
            events = list(Event.objects
                    .filter(course=pctx.course, kind=form.cleaned_data["kind"])
                    .order_by('time'))

            if events:
                queryset = (Event.objects
                    .filter(course=pctx.course, kind=form.cleaned_data["kind"]))

                queryset.delete()

                ordinal = form.cleaned_data["starting_ordinal"]
                for event in events:
                    new_event = Event()
                    new_event.course = pctx.course
                    new_event.kind = form.cleaned_data["kind"]
                    new_event.ordinal = ordinal
                    new_event.time = event.time
                    new_event.end_time = event.end_time
                    new_event.all_day = event.all_day
                    new_event.shown_in_calendar = event.shown_in_calendar
                    new_event.save()

                    ordinal += 1

                messages.add_message(request, messages.SUCCESS,
                        _("Events renumbered."))
            else:
                messages.add_message(request, messages.ERROR,
                        _("No events found."))

    else:
        form = RenumberEventsForm()

    return render_course_page(pctx, "course/generic-course-form.html", {
        "form": form,
        "form_description": _("Renumber events"),
    })
示例#4
0
def _create_recurring_events_backend(course, time, kind, starting_ordinal, interval,
        count, duration_in_minutes, all_day, shown_in_calendar):
    ordinal = starting_ordinal

    import datetime

    for i in range(count):
        evt = Event()
        evt.course = course
        evt.kind = kind
        evt.ordinal = ordinal
        evt.time = time
        evt.all_day = all_day
        evt.shown_in_calendar = shown_in_calendar

        if duration_in_minutes:
            evt.end_time = evt.time + datetime.timedelta(
                    minutes=duration_in_minutes)
        try:
            evt.save()
        except IntegrityError:
            raise EventAlreadyExists(
                _("'%(event_kind)s %(event_ordinal)d' already exists") %
                {'event_kind': kind, 'event_ordinal': ordinal})

        date = time.date()
        if interval == "weekly":
            date += datetime.timedelta(weeks=1)
        elif interval == "biweekly":
            date += datetime.timedelta(weeks=2)
        else:
            raise ValueError(
                    string_concat(
                        pgettext_lazy(
                            "Unkown time interval",
                            "unknown interval"),
                        ": %s")
                    % interval)

        time = time.tzinfo.localize(
                datetime.datetime(date.year, date.month, date.day,
                    time.hour, time.minute, time.second))
        del date

        ordinal += 1
示例#5
0
def _create_recurring_events_backend(course, time, kind, starting_ordinal, interval,
        count, duration_in_minutes, all_day, shown_in_calendar):
    ordinal = starting_ordinal

    import datetime

    for i in range(count):
        evt = Event()
        evt.course = course
        evt.kind = kind
        evt.ordinal = ordinal
        evt.time = time
        evt.all_day = all_day
        evt.shown_in_calendar = shown_in_calendar

        if duration_in_minutes:
            evt.end_time = evt.time + datetime.timedelta(
                    minutes=duration_in_minutes)
        try:
            evt.save()
        except IntegrityError:
            raise EventAlreadyExists(
                _("'%(event_kind)s %(event_ordinal)d' already exists") %
                {'event_kind': kind, 'event_ordinal': ordinal})

        date = time.date()
        if interval == "weekly":
            date += datetime.timedelta(weeks=1)
        elif interval == "biweekly":
            date += datetime.timedelta(weeks=2)
        else:
            raise ValueError(
                    string_concat(
                        pgettext_lazy(
                            "Unkown time interval",
                            "unknown interval"),
                        ": %s")
                    % interval)

        time = time.tzinfo.localize(
                datetime.datetime(date.year, date.month, date.day,
                    time.hour, time.minute, time.second))
        del date

        ordinal += 1
示例#6
0
def _create_recurring_events_backend(course, time, kind, starting_ordinal,
                                     interval, count, duration_in_minutes,
                                     all_day, shown_in_calendar):
    ordinal = starting_ordinal
    assert ordinal is not None

    import datetime

    for i in range(count):
        evt = Event()
        evt.course = course
        evt.kind = kind
        evt.ordinal = ordinal
        evt.time = time
        evt.all_day = all_day
        evt.shown_in_calendar = shown_in_calendar

        if duration_in_minutes:
            evt.end_time = evt.time + datetime.timedelta(
                minutes=duration_in_minutes)

        if Event.objects.filter(course=course, kind=kind,
                                ordinal=ordinal).count():
            raise EventAlreadyExists(
                _("'%(exist_event)s' already exists") % {'exist_event': evt})

        evt.save()

        date = time.date()
        if interval == "weekly":
            date += datetime.timedelta(weeks=1)
        elif interval == "biweekly":
            date += datetime.timedelta(weeks=2)
        else:
            raise NotImplementedError()

        time = time.tzinfo.localize(
            datetime.datetime(date.year, date.month, date.day, time.hour,
                              time.minute, time.second))
        del date

        ordinal += 1
示例#7
0
def _create_recurring_events_backend(course, time, kind, starting_ordinal, interval,
        count, duration_in_minutes, all_day, shown_in_calendar):
    ordinal = starting_ordinal
    assert ordinal is not None

    import datetime

    for i in range(count):
        evt = Event()
        evt.course = course
        evt.kind = kind
        evt.ordinal = ordinal
        evt.time = time
        evt.all_day = all_day
        evt.shown_in_calendar = shown_in_calendar

        if duration_in_minutes:
            evt.end_time = evt.time + datetime.timedelta(
                    minutes=duration_in_minutes)

        if Event.objects.filter(course=course, kind=kind, ordinal=ordinal).count():
            raise EventAlreadyExists(
                _("'%(exist_event)s' already exists")
                % {'exist_event': evt})

        evt.save()

        date = time.date()
        if interval == "weekly":
            date += datetime.timedelta(weeks=1)
        elif interval == "biweekly":
            date += datetime.timedelta(weeks=2)
        else:
            raise NotImplementedError()

        time = time.tzinfo.localize(
                datetime.datetime(date.year, date.month, date.day,
                    time.hour, time.minute, time.second))
        del date

        ordinal += 1
示例#8
0
def renumber_events(pctx):
    if not pctx.has_permission(pperm.edit_events):
        raise PermissionDenied(_("may not edit events"))

    request = pctx.request

    if request.method == "POST":
        form = RenumberEventsForm(request.POST, request.FILES)
        if form.is_valid():
            events = list(Event.objects.filter(course=pctx.course, kind=form.cleaned_data["kind"]).order_by("time"))

            if events:
                queryset = Event.objects.filter(course=pctx.course, kind=form.cleaned_data["kind"])

                queryset.delete()

                ordinal = form.cleaned_data["starting_ordinal"]
                for event in events:
                    new_event = Event()
                    new_event.course = pctx.course
                    new_event.kind = form.cleaned_data["kind"]
                    new_event.ordinal = ordinal
                    new_event.time = event.time
                    new_event.end_time = event.end_time
                    new_event.all_day = event.all_day
                    new_event.shown_in_calendar = event.shown_in_calendar
                    new_event.save()

                    ordinal += 1

                messages.add_message(request, messages.SUCCESS, _("Events renumbered."))
            else:
                messages.add_message(request, messages.ERROR, _("No events found."))

    else:
        form = RenumberEventsForm()

    return render_course_page(
        pctx, "course/generic-course-form.html", {"form": form, "form_description": _("Renumber events")}
    )
示例#9
0
def renumber_events(pctx):
    if not pctx.has_permission(pperm.edit_events):
        raise PermissionDenied(_("may not edit events"))

    request = pctx.request

    message = None
    message_level = None

    if request.method == "POST":
        form = RenumberEventsForm(
            pctx.course.identifier, request.POST, request.FILES)
        if form.is_valid():
            kind = form.cleaned_data["kind"]
            order_field = "time"
            if form.cleaned_data["preserve_ordinal_order"]:
                order_field = "ordinal"
            events = list(
                Event.objects.filter(
                    course=pctx.course, kind=kind,

                    # there might be event with the same kind but no ordinal,
                    # we don't renumber that
                    ordinal__isnull=False)
                .order_by(order_field))

            assert events
            queryset = (Event.objects.filter(
                course=pctx.course, kind=kind,

                # there might be event with the same kind but no ordinal,
                # we don't renumber that
                ordinal__isnull=False))

            queryset.delete()

            ordinal = form.cleaned_data["starting_ordinal"]
            for event in events:
                new_event = Event()
                new_event.course = pctx.course
                new_event.kind = kind
                new_event.ordinal = ordinal
                new_event.time = event.time
                new_event.end_time = event.end_time
                new_event.all_day = event.all_day
                new_event.shown_in_calendar = event.shown_in_calendar
                new_event.save()

                ordinal += 1

            message = _("Events renumbered.")
            message_level = messages.SUCCESS

    else:
        form = RenumberEventsForm(pctx.course.identifier)

    if messages and message_level:
        messages.add_message(request, message_level, message)
    return render_course_page(pctx, "course/generic-course-form.html", {
        "form": form,
        "form_description": _("Renumber events"),
    })
示例#10
0
def renumber_events(pctx):
    if not pctx.has_permission(pperm.edit_events):
        raise PermissionDenied(_("may not edit events"))

    request = pctx.request

    message = None
    message_level = None

    if request.method == "POST":
        form = RenumberEventsForm(
            pctx.course.identifier, request.POST, request.FILES)
        if form.is_valid():
            kind = form.cleaned_data["kind"]
            order_field = "time"
            if form.cleaned_data["preserve_ordinal_order"]:
                order_field = "ordinal"
            events = list(
                Event.objects.filter(
                    course=pctx.course, kind=kind,

                    # there might be event with the same kind but no ordinal,
                    # we don't renumber that
                    ordinal__isnull=False)
                .order_by(order_field))

            assert events
            queryset = (Event.objects.filter(
                course=pctx.course, kind=kind,

                # there might be event with the same kind but no ordinal,
                # we don't renumber that
                ordinal__isnull=False))

            queryset.delete()

            ordinal = form.cleaned_data["starting_ordinal"]
            for event in events:
                new_event = Event()
                new_event.course = pctx.course
                new_event.kind = kind
                new_event.ordinal = ordinal
                new_event.time = event.time
                new_event.end_time = event.end_time
                new_event.all_day = event.all_day
                new_event.shown_in_calendar = event.shown_in_calendar
                new_event.save()

                ordinal += 1

            message = _("Events renumbered.")
            message_level = messages.SUCCESS

    else:
        form = RenumberEventsForm(pctx.course.identifier)

    if messages and message_level:
        messages.add_message(request, message_level, message)
    return render_course_page(pctx, "course/generic-course-form.html", {
        "form": form,
        "form_description": _("Renumber events"),
    })