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)
def start_a_tradeschool(request): branches = Branch.objects.public() pages = FlatPage.objects.all() if request.method == 'POST': branch_form = BranchForm(request.POST, prefix="branch") organizer_form = OrganizerForm(request.POST, prefix="organizer") if branch_form.is_valid() and organizer_form.is_valid(): try: # save branch branch = branch_form.save(commit=False) branch_data = branch_form.cleaned_data # create a title from the city field branch_data['title'] = branch_data['city'] # create a slug for the organizer object branch_data['slug'] = unique_slugify( Branch, branch_data['title']) branch_data['is_active'] = False branch_data['branch_status'] = 'pending' # save branch branch = Branch(**branch_data) branch.save() # save organizer organizer = organizer_form.save(commit=False) organizer_data = organizer_form.cleaned_data # create a slug for the organizer object organizer_data['slug'] = unique_slugify( Organizer, organizer.fullname) organizer_data['username'] = organizer.fullname organizer_data['is_active'] = False organizer_data['is_staff'] = True # save organizer organizer = Organizer(**organizer_data) organizer.default_branch = branch organizer.save() # add an organizer-branch relationship to the current branch organizer.branches.add(branch) organizer.groups.add(Group.objects.get(name='translators')) branch.organizers.add(organizer) return HttpResponseRedirect(reverse_lazy( branch_submitted, kwargs={ 'slug': branch.slug, }) ) # in case saving the branch failed # (see comment above the try block), # add an error to the branch form except IntegrityError: # display error branch_form._errors['city'] = \ branch_form.error_class( [_('This Trade School already exists')]) else: branch_form = BranchForm(prefix="branch") organizer_form = OrganizerForm(prefix="organizer") return render_to_response('hub/start_a_tradeschool.html', { 'branches': branches, 'pages': pages, 'branch_form': branch_form, 'organizer_form': organizer_form, }, context_instance=RequestContext(request))
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))
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))
def cluster_presave_callback(sender, instance, **kwargs): """ Check if there is slug and create one if there isn't. """ if instance.slug is None or instance.slug.__len__() == 0: instance.slug = unique_slugify(Cluster, instance.name)