def link_user_subject(request, subject_id, user_id):
    subject = SubjectService.find_one(subject_id)
    assert subject is not None

    if user_id is None:
        students = StudentService.find_all()
        lecturers = LecturerService.fin_all()
        template_name = 'user/list.html'
        template_data = {
            'students': students,
            'lecturers': lecturers,
            'subject_id': subject.id
        }
        return render_to_response(template_name,
                                  template_data,
                                  context_instance=RequestContext(request))
    else:
        user = UserService.find_one(user_id)
        assert user.has_perm('principal.alumno') or user.has_perm(
            'principal.profesor')

        if user.has_perm('principal.alumno'):
            student = StudentService.find_one(user.id)
            student.asignaturas.add(subject)
        else:
            lecturer = LecturerService.find_one(user.id)

            if request.POST:
                form = UserLinkSubjectForm(request.POST)
                if form.is_valid():
                    ImpartSubjectService.reconstruct_and_save(form)
                    messages.success(request,
                                     _("Action completed successfully"))
                    return HttpResponseRedirect('/subject/details/' +
                                                str(subject_id))
            else:
                data = {
                    'subject_id': subject_id,
                    'lecturer_id': lecturer.id,
                    'lecturer': lecturer.first_name + " " + lecturer.last_name
                }
                form = UserLinkSubjectForm(initial=data)

            students = StudentService.find_all()
            lecturers = LecturerService.fin_all()

            template_name = 'user/list.html'
            template_data = {
                "form": form,
                'students': students,
                'lecturers': lecturers,
                'subject_id': subject_id,
                'lecturer_id': lecturer.id
            }
            return render_to_response(template_name,
                                      template_data,
                                      context_instance=RequestContext(request))

        messages.success(request, _('User linked to the subject'))
        return HttpResponseRedirect('/subject/details/' + str(subject_id))
def create_link_lecturer_file(lecturer_data, subject, file_type, import_errors_link, user_create,
                              user_link):
    if file_type == "text/xml":
        data_form = LecturerService.get_form_data_xml(lecturer_data)
    else:
        data_form = LecturerService.get_form_data_csv(lecturer_data)

    form_lecturer = UserEditForm(data=data_form)

    if form_lecturer.is_valid():
        lecturer = LecturerService.create(form_lecturer)
        LecturerService.save(lecturer)
        password = id_generator()
        UserService.save_password(lecturer, password)
        user_create[lecturer] = password

        if file_type == "text/xml":
            data_form_link = ImpartSubjectService.get_form_data_xml(lecturer_data, subject)
        else:
            data_form_link = ImpartSubjectService.get_form_data_csv(lecturer_data, subject)

        form_lecturer_link = UserLinkSubjectForm(data=data_form_link)

        if form_lecturer_link.is_valid():
            ImpartSubjectService.reconstruct_and_save(form_lecturer_link)
        else:
            fullname = form_lecturer.data['last_name'] + ", " + form_lecturer.data['first_name']
            import_errors_link.append(_("Lecturer") + ": " + fullname + " - " + form_lecturer.data['dni'])

    else:

        if file_type == "text/xml":
            data_form_link = ImpartSubjectService.get_form_data_xml(lecturer_data, subject)
        else:
            data_form_link = ImpartSubjectService.get_form_data_csv(lecturer_data, subject)

        form_lecturer_link = UserLinkSubjectForm(data=data_form_link)

        if form_lecturer_link.is_valid():
            lecturer_id = form_lecturer_link.data['lecturer_id']
            lecturer = Profesor.objects.get(id=lecturer_id)
            fullname = form_lecturer.data['last_name'] + ", " + form_lecturer.data['first_name']

            if lecturer:
                if form_lecturer_link.is_valid():
                    ImpartSubjectService.reconstruct_and_save(form_lecturer_link)
                    user_link.append(lecturer)
                else:
                    import_errors_link.append(_("Lecturer") + ": " + fullname + " - " + form_lecturer.data['dni'])
            else:
                import_errors_link.append(_("Lecturer") + ": " + fullname + " - " + form_lecturer.data['dni'])
        else:
            fullname = form_lecturer.data['last_name'] + ", " + form_lecturer.data['first_name']
            import_errors_link.append(_("Lecturer") + ": " + fullname + " - " + form_lecturer.data['dni'])
def link_user_subject(request, subject_id, user_id):
    subject = SubjectService.find_one(subject_id)
    assert subject is not None

    if user_id is None:
        students = StudentService.find_all()
        lecturers = LecturerService.fin_all()
        template_name = 'user/list.html'
        template_data = {'students': students, 'lecturers': lecturers, 'subject_id': subject.id}
        return render_to_response(template_name, template_data, context_instance=RequestContext(request))
    else:
        user = UserService.find_one(user_id)
        assert user.has_perm('principal.alumno') or user.has_perm('principal.profesor')

        if user.has_perm('principal.alumno'):
            student = StudentService.find_one(user.id)
            student.asignaturas.add(subject)
        else:
            lecturer = LecturerService.find_one(user.id)

            if request.POST:
                form = UserLinkSubjectForm(request.POST)
                if form.is_valid():
                    ImpartSubjectService.reconstruct_and_save(form)
                    messages.success(request, _("Action completed successfully"))
                    return HttpResponseRedirect('/subject/details/' + str(subject_id))
            else:
                data = {'subject_id': subject_id, 'lecturer_id': lecturer.id,
                        'lecturer': lecturer.first_name + " " + lecturer.last_name}
                form = UserLinkSubjectForm(initial=data)

            students = StudentService.find_all()
            lecturers = LecturerService.fin_all()

            template_name = 'user/list.html'
            template_data = {"form": form, 'students': students, 'lecturers': lecturers, 'subject_id': subject_id,
                             'lecturer_id': lecturer.id}
            return render_to_response(template_name, template_data, context_instance=RequestContext(request))

        messages.success(request, _('User linked to the subject'))
        return HttpResponseRedirect('/subject/details/' + str(subject_id))
def create_link_lecturer_file(lecturer_data, subject, file_type,
                              import_errors_link, user_create, user_link):
    if file_type == "text/xml":
        data_form = LecturerService.get_form_data_xml(lecturer_data)
    else:
        data_form = LecturerService.get_form_data_csv(lecturer_data)

    form_lecturer = UserEditForm(data=data_form)

    if form_lecturer.is_valid():
        lecturer = LecturerService.create(form_lecturer)
        LecturerService.save(lecturer)
        password = id_generator()
        UserService.save_password(lecturer, password)
        user_create[lecturer] = password

        if file_type == "text/xml":
            data_form_link = ImpartSubjectService.get_form_data_xml(
                lecturer_data, subject)
        else:
            data_form_link = ImpartSubjectService.get_form_data_csv(
                lecturer_data, subject)

        form_lecturer_link = UserLinkSubjectForm(data=data_form_link)

        if form_lecturer_link.is_valid():
            ImpartSubjectService.reconstruct_and_save(form_lecturer_link)
        else:
            fullname = form_lecturer.data[
                'last_name'] + ", " + form_lecturer.data['first_name']
            import_errors_link.append(
                _("Lecturer") + ": " + fullname + " - " +
                form_lecturer.data['dni'])

    else:

        if file_type == "text/xml":
            data_form_link = ImpartSubjectService.get_form_data_xml(
                lecturer_data, subject)
        else:
            data_form_link = ImpartSubjectService.get_form_data_csv(
                lecturer_data, subject)

        form_lecturer_link = UserLinkSubjectForm(data=data_form_link)

        if form_lecturer_link.is_valid():
            lecturer_id = form_lecturer_link.data['lecturer_id']
            lecturer = Profesor.objects.get(id=lecturer_id)
            fullname = form_lecturer.data[
                'last_name'] + ", " + form_lecturer.data['first_name']

            if lecturer:
                if form_lecturer_link.is_valid():
                    ImpartSubjectService.reconstruct_and_save(
                        form_lecturer_link)
                    user_link.append(lecturer)
                else:
                    import_errors_link.append(
                        _("Lecturer") + ": " + fullname + " - " +
                        form_lecturer.data['dni'])
            else:
                import_errors_link.append(
                    _("Lecturer") + ": " + fullname + " - " +
                    form_lecturer.data['dni'])
        else:
            fullname = form_lecturer.data[
                'last_name'] + ", " + form_lecturer.data['first_name']
            import_errors_link.append(
                _("Lecturer") + ": " + fullname + " - " +
                form_lecturer.data['dni'])