Esempio n. 1
0
def branch_page(request, url, branch_slug=None):
    """
    this is copied from django.contrib.flatpages.views only in order to
    query the BranchPage table instead of FlatPage.
    """
    branch = get_object_or_404(Branch, slug=branch_slug)

    try:
        page = get_object_or_404(
            Page, url__exact=url, branch__pk=branch.pk, is_active=True)
    except Http404:
        if url.endswith('/'):
            url = url[:-1]
            page = get_object_or_404(
                Page,
                url__exact=url,
                branch__pk=branch.pk,
                is_active=True
            )
        else:
            raise

    # To avoid having to always use the "|safe" filter in flatpage templates,
    # mark the title and content as already safe (since they are raw HTML
    # content in the first place).
    page.title = mark_safe(page.title)
    page.content = mark_safe(page.content)

    view_templates = branch_templates(
        branch, 'page_detail.html', 'subpage.html')

    return render_to_response(view_templates.template.name, {
        'page': page,
        'templates': view_templates
    }, context_instance=RequestContext(request))
Esempio n. 2
0
def course_list(request, branch_slug=None):
    """display all upcoming courses for branch."""

    branch = get_object_or_404(Branch, slug=branch_slug)

    view_templates = branch_templates(
        branch, 'course_list.html', 'base.html')

    return render_to_response(view_templates.template.name, {
        'templates': view_templates,
    }, context_instance=RequestContext(request))
Esempio n. 3
0
def course_list_past(request, branch_slug=None):
    """display a list of past classes for the current branch."""

    branch = get_object_or_404(Branch, slug=branch_slug)

    view_templates = branch_templates(
        branch, 'course_list_past.html', 'subpage.html')

    return render_to_response(view_templates.template.name, {
        'branch': branch,
        'templates': view_templates
    }, context_instance=RequestContext(request))
Esempio n. 4
0
def course_view(request, branch_slug=None, course_slug=None):
    """
    """
    course = get_object_or_404(
        Course, slug=course_slug, branch__slug=branch_slug)

    view_templates = branch_templates(
        course.branch, 'course_view.html', 'base.html')

    return render_to_response(view_templates.template.name, {
        'course': course,
        'templates': view_templates,
    }, context_instance=RequestContext(request))
Esempio n. 5
0
def teacher_info(request, branch_slug=None):
    """
    display a content page with information for prospective teachers.
    This page leads to the class submission form page.
    """
    branch = get_object_or_404(Branch, slug=branch_slug)

    view_templates = branch_templates(
        branch, 'teacher-info.html', 'subpage.html')

    return render_to_response(view_templates.template.name, {
        'branch': branch,
        'templates': view_templates
    }, context_instance=RequestContext(request))
Esempio n. 6
0
def course_submitted(request, course_slug=None, branch_slug=None):
    """
    loaded after a successful submission of the course form.
    """

    course = get_object_or_404(Course, slug=course_slug)
    branch = get_object_or_404(Branch, slug=branch_slug)

    view_templates = branch_templates(
        branch, 'course_submitted.html', 'subpage.html')

    return render_to_response(view_templates.template.name, {
        'course': course,
        'templates': view_templates
    }, context_instance=RequestContext(request))
Esempio n. 7
0
def course_feedback(request, branch_slug=None, course_slug=None, feedback_type='student'):
    """
    """
    # don't display form unless course is approved
    course = get_object_or_404(
        Course, slug=course_slug, status='approved')
    branch = get_object_or_404(Branch, slug=branch_slug)

    # don't display form unless the scheduled class took place
    if course.is_past is False:
        raise Http404

    if request.method == 'POST' and course.is_past:
        form = FeedbackForm(data=request.POST)

        if form.is_valid():

            # save feedback
            feedback = form.save(commit=False)
            feedback.feedback_type = feedback_type
            feedback.course = course
            feedback.save()

            # redirect to thank you page
            return HttpResponseRedirect(reverse_lazy(
                course_list,
                kwargs={'branch_slug': branch_slug, })
            )

    else:
        form = FeedbackForm()

    view_templates = branch_templates(
        branch, 'course_feedback.html', 'subpage.html')

    return render_to_response(view_templates.template.name, {
        'form': form,
        'templates': view_templates
    }, context_instance=RequestContext(request))
Esempio n. 8
0
def course_unregister(request, branch_slug=None, course_slug=None, student_slug=None):
    """
    """
    registration = get_object_or_404(
        Registration, student__slug=student_slug, course__slug=course_slug)
    branch = get_object_or_404(Branch, slug=branch_slug)

    if request.method == 'POST':
        registration.registration_status = 'unregistered'
        registration.save()
        return HttpResponseRedirect(reverse_lazy(
            course_list,
            kwargs={'branch_slug': branch_slug, })
        )

    view_templates = branch_templates(
        branch, 'course_unregister.html', 'subpage.html')

    return render_to_response(view_templates.template.name, {
        'registration': registration,
        'templates': view_templates
    }, context_instance=RequestContext(request))
Esempio n. 9
0
def course_register(request, branch_slug=None, course_slug=None, data=None):
    """
    """
    branch = get_object_or_404(Branch, slug=branch_slug)
    course = get_object_or_404(Course, slug=course_slug)
    open_seat_percentage = round(
        (float(course.total_registered_students) /
            float(course.max_students)) * 100)
    seats_left = course.max_students - course.total_registered_students

    if request.method == 'POST' and not request.is_ajax():
        data = request.POST

    if data is not None or request.method == 'POST':
        student_form = StudentForm(data=data, prefix="student")
        registration_form = RegistrationForm(
            data=data, course=course, prefix="item")

        if registration_form.is_valid() and student_form.is_valid():
            # save student
            student = student_form.save(commit=False)
            student_data = student_form.cleaned_data
            student_data['slug'] = unique_slugify(Student, student.fullname)

            student = Person.objects.filter(email=student.email)

            if student.exists():
                student = student[0]

                # keep this object in case something goes wrong
                # when saving the registration
                original_student = student

                # update student data from form
                student.fullname = student_data['fullname']
                student.phone = student_data['phone']
            else:
                student = Person.objects.create_user(**student_data)

            student.is_student = True
            student.save()
            student.branches.add(branch)

            # save registration
            registration = registration_form.save(commit=False)
            registration.student = student
            registration.course = course

            # try saving the registration.
            # this may fail because of the unique_together db constraint
            # on student and course fields
            # if this Student is registered to this Course
            # an IntegrityError will occur
            try:
                registration.save()

                # save items in registration through RegisteredItem
                for barter_item in registration_form.cleaned_data['items']:
                    registration.items.add(barter_item)

                registration.save()

                # email confirmation to student
                course.email_student(
                    course.studentconfirmation, registration)

                # render thank you template
                view_templates = branch_templates(
                    branch, 'course_registered.html', 'base.html')
                return render_to_response(
                    view_templates.template.name, {
                        'registration': registration,
                        'templates': view_templates
                    },
                    context_instance=RequestContext(request),
                    mimetype="application/json"
                )

            # in case saving the registration failed
            # (see comment above the try block),
            # add an error to the registration form
            except IntegrityError:

                # restore data
                student.fullname = original_student.fullname
                student.phone = original_student.phone
                student.save()

                # display error
                registration_form._errors['items'] = \
                    registration_form.error_class(
                        [_('You are already registered to this class')])

    else:
        student_form = StudentForm(prefix="student")
        registration_form = RegistrationForm(course=course, prefix="item")

    # return content as either json or html depending on request type
    if request.is_ajax():
        view_templates = branch_templates(
            branch, 'course_register.html', 'base_ajax.html')
        popup_container_class = ''
        mimetype = "application/json"
    else:
        view_templates = branch_templates(
            branch, 'course_register.html', 'base.html')
        popup_container_class = 'visible'
        mimetype = "text/html"

    return render_to_response(view_templates.template.name, {
        'branch': branch,
        'course': course,
        'open_seat_percentage': open_seat_percentage,
        'seats_left': seats_left,
        'registration_form': registration_form,
        'student_form': student_form,
        'templates': view_templates,
        'popup_container_class': popup_container_class
    }, context_instance=RequestContext(request), mimetype=mimetype)
Esempio n. 10
0
def course_edit(request, course_slug=None, branch_slug=None):
    """
    """
    course = get_object_or_404(Course, slug=course_slug)
    branch = get_object_or_404(Branch, slug=branch_slug)

    if request.method == 'POST':
        BarterItemFormSet = formset_factory(
            BarterItemForm, extra=5, formset=BaseBarterItemFormSet)
        barter_item_formset = BarterItemFormSet(data=request.POST, prefix="item", branch=branch)
        course_form = CourseForm(
            request.POST, prefix="course", instance=course)
        teacher_form = TeacherForm(
            request.POST, prefix="teacher", instance=course.teacher)

        if barter_item_formset.is_valid() \
                and course_form.is_valid() \
                and teacher_form.is_valid():
            # save teacher
            teacher = teacher_form.save(commit=False)
            teacher.slug = unique_slugify(Teacher, teacher.fullname)
            teacher.save()

            # save course
            course = course_form.save(commit=False)
            course.slug = unique_slugify(Course, course.title)
            course.save()

            # remove all barter item relationships before saving them again
            for item in course.barteritem_set.all():
                item.delete()

            # save updated barter items
            for barter_item_form in barter_item_formset:
                barter_item_form_data = barter_item_form.cleaned_data
                barter_item, created = BarterItem.objects.get_or_create(
                    title=barter_item_form_data['title'],
                    course=course,
                    defaults=barter_item_form_data
                )
                barter_item.save()
            return HttpResponseRedirect(reverse_lazy(
                course_submitted,
                kwargs={
                    'branch_slug': branch.slug,
                    'course_slug': course.slug
                })
            )

    else:
        initial_item_data = []
        for item in course.barteritem_set.all():
            initial_item_data.append({'title': item.title, })

        BarterItemFormSet = formset_factory(
            BarterItemForm, extra=0, formset=BaseBarterItemFormSet,)
        barter_item_formset = BarterItemFormSet(
            prefix="item", initial=initial_item_data, branch=branch)
        course_form = CourseForm(
            prefix="course",
            instance=course
        )
        teacher_form = TeacherForm(
            prefix="teacher",
            instance=course.teacher
        )

    view_templates = branch_templates(
        branch, 'course_submit.html', 'subpage.html')

    return render_to_response(view_templates.template.name, {
        'barter_item_formset': barter_item_formset,
        'course_form': course_form,
        'teacher_form': teacher_form,
        'templates': view_templates
    }, context_instance=RequestContext(request))
Esempio n. 11
0
def course_add(request, branch_slug=None):
    """
    """
    branch = get_object_or_404(Branch, slug=branch_slug)

    if request.method == 'POST':
        BarterItemFormSet = formset_factory(
            BarterItemForm, extra=5, formset=BaseBarterItemFormSet)
        barter_item_formset = BarterItemFormSet(data=request.POST, prefix="item", branch=branch)
        course_form = CourseForm(request.POST, prefix="course")
        teacher_form = TeacherForm(request.POST, prefix="teacher")
        time_form = TimeSelectionForm(
            data=request.POST,
            prefix="time",
        )
        time_form.fields['time'].queryset = Time.objects.filter(branch=branch)

        if barter_item_formset.is_valid() \
                and course_form.is_valid() \
                and teacher_form.is_valid() \
                and time_form.is_valid():
            # process teacher
            teacher = teacher_form.save(commit=False)
            teacher_data = teacher_form.cleaned_data

            # create a slug for the teacher object
            teacher_data['slug'] = unique_slugify(Teacher, teacher.fullname)

            # check if the submitting teacher already exists in the system
            # we determine an existing teacher by their email
            teacher = Teacher.objects.filter(email=teacher.email)

            # if this is an existing teacher,
            # update the field with the data from the form
            if teacher.exists():
                teacher = teacher[0]
                teacher.fullname = teacher_form.cleaned_data['fullname']
                teacher.bio = teacher_form.cleaned_data['bio']
                teacher.website = teacher_form.cleaned_data['website']
                teacher.phone = teacher_form.cleaned_data['phone']
            else:
                teacher = Person.objects.create_user(**teacher_data)

            teacher.is_teacher = True

            # save teacher
            teacher.save()

            # add a teacher-branch relationship to the current branch
            teacher.branches.add(branch)

            # process course
            course = course_form.save(commit=False)
            course_data = course_form.cleaned_data

            # create a slug for the course object
            course_data['slug'] = unique_slugify(Course, course.title)

            # add the teacher as a foreign key
            course_data['teacher'] = teacher

            # course branch
            course_data['branch'] = branch

            # course status
            course_data['status'] = 'pending'

            # save time
            selected_time = time_form.cleaned_data['time']
            course_data['start_time'] = selected_time.start_time
            course_data['end_time'] = selected_time.end_time

            if selected_time.venue is not None:
                course_data['venue'] = selected_time.venue
            else:
                try:
                    course_data['venue'] = branch.venue_set.all()[0]
                except IndexError:
                    pass

            # save course
            course = Course(**course_data)
            course.save()

            # save barter items
            for barter_item_form in barter_item_formset:
                barter_item_form_data = barter_item_form.cleaned_data

                # check if the submitted barter item
                # already exists in the system.
                # we determine an existing barter item by its title
                barter_item, barter_item_created = BarterItem.objects.get_or_create(
                    title=barter_item_form_data['title'],
                    course=course,
                    defaults=barter_item_form_data
                )
                barter_item.save()

            # send confirmation email to teacher
            course.email_teacher(course.teacherconfirmation)

            # delete the selected time slot
            Time.objects.get(pk=selected_time.pk).delete()

            # redirect to thank you page
            return HttpResponseRedirect(reverse_lazy(
                course_submitted,
                kwargs={
                    'course_slug': course.slug,
                    'branch_slug': branch.slug
                })
            )

    else:
        BarterItemFormSet = formset_factory(
            BarterItemForm, extra=5, formset=BaseBarterItemFormSet)
        barter_item_formset = BarterItemFormSet(prefix="item", branch=branch)
        course_form = CourseForm(prefix="course")
        teacher_form = TeacherForm(prefix="teacher")
        time_form = TimeSelectionForm(prefix="time")
        time_form.fields['time'].queryset = Time.objects.filter(branch=branch)

    view_templates = branch_templates(
        branch, 'course_submit.html', 'subpage.html')

    return render_to_response(view_templates.template.name, {
        'branch': branch,
        'barter_item_formset': barter_item_formset,
        'course_form': course_form,
        'teacher_form': teacher_form,
        'time_form': time_form,
        'templates': view_templates
    }, context_instance=RequestContext(request))