def create_link_student_file(student_data, subject, file_type, import_errors_link, user_create,
                             user_link):
    if file_type == "text/xml":
        data_form = StudentService.get_form_data_xml(student_data)
    else:
        data_form = StudentService.get_form_data_csv(student_data)

    form_student = UserEditForm(data=data_form)

    if form_student.is_valid():
        student = StudentService.create(form_student)
        StudentService.save(student)
        password = id_generator()
        UserService.save_password(student, password)
        user_create[student] = password
        student.asignaturas.add(subject)
    else:
        fullname = form_student.data['last_name'] + ", " + form_student.data['first_name']
        username = form_student.data['user']
        user = UserService.find_by_username(username)
        if user:
            student = Alumno.objects.get(id=user.id)
            if student:
                student.asignaturas.add(subject)
                user_link.append(student)
            else:
                import_errors_link.append(_("Student") + ": " + fullname + " - " + form_student.data['dni'])
        else:
            import_errors_link.append(_("Student") + ": " + fullname + " - " + form_student.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_user(request):
    if request.POST:
        form = UserEditForm(request.POST)
        if form.is_valid():

            if form.cleaned_data['type'] == 'st':
                # Is student
                user = StudentService.create(form)
                StudentService.save(user)
            elif form.cleaned_data['type'] == 'le':
                # Is lecturer
                user = LecturerService.create(form)
                LecturerService.save(user)
            password = id_generator()
            UserService.save_password(user, password)
            user_create = {user: password}
            EmailService.send_email_create_user(user_create, request)
            messages.success(request, _("Action completed successfully"))
            return HttpResponseRedirect("/admin/user/create")
    else:
        data = {}
        form = UserEditForm(initial=data)

    template_name = 'user/edit.html'
    template_data = {"form": form}
    return render_to_response(template_name,
                              template_data,
                              context_instance=RequestContext(request))
def create_link_student_file(student_data, subject, file_type,
                             import_errors_link, user_create, user_link):
    if file_type == "text/xml":
        data_form = StudentService.get_form_data_xml(student_data)
    else:
        data_form = StudentService.get_form_data_csv(student_data)

    form_student = UserEditForm(data=data_form)

    if form_student.is_valid():
        student = StudentService.create(form_student)
        StudentService.save(student)
        password = id_generator()
        UserService.save_password(student, password)
        user_create[student] = password
        student.asignaturas.add(subject)
    else:
        fullname = form_student.data['last_name'] + ", " + form_student.data[
            'first_name']
        username = form_student.data['user']
        user = UserService.find_by_username(username)
        if user:
            student = Alumno.objects.get(id=user.id)
            if student:
                student.asignaturas.add(subject)
                user_link.append(student)
            else:
                import_errors_link.append(
                    _("Student") + ": " + fullname + " - " +
                    form_student.data['dni'])
        else:
            import_errors_link.append(
                _("Student") + ": " + fullname + " - " +
                form_student.data['dni'])
def create_user(request):
    if request.POST:
        form = UserEditForm(request.POST)
        if form.is_valid():

            if form.cleaned_data["type"] == "st":
                # Is student
                user = StudentService.create(form)
                StudentService.save(user)
            elif form.cleaned_data["type"] == "le":
                # Is lecturer
                user = LecturerService.create(form)
                LecturerService.save(user)
            password = id_generator()
            UserService.save_password(user, password)
            user_create = {user: password}
            EmailService.send_email_create_user(user_create, request)
            messages.success(request, _("Action completed successfully"))
            return HttpResponseRedirect("/admin/user/create")
    else:
        data = {}
        form = UserEditForm(initial=data)

    template_name = "user/edit.html"
    template_data = {"form": form}
    return render_to_response(template_name, template_data, context_instance=RequestContext(request))
def create_student_file(data_form, import_errors_create, user_create):

    form_student = UserEditForm(data=data_form)
    if form_student.is_valid():
        student = StudentService.create(form_student)
        StudentService.save(student)
        password = id_generator()
        UserService.save_password(student, password)
        user_create[student] = password
    else:
        fullname = form_student.data["last_name"] + ", " + form_student.data["first_name"]
        import_errors_create.append(_("Student") + ": " + fullname + " - " + form_student.data["dni"])
def create_student_file(data_form, import_errors_create, user_create):

    form_student = UserEditForm(data=data_form)
    if form_student.is_valid():
        student = StudentService.create(form_student)
        StudentService.save(student)
        password = id_generator()
        UserService.save_password(student, password)
        user_create[student] = password
    else:
        fullname = form_student.data['last_name'] + ", " + form_student.data[
            'first_name']
        import_errors_create.append(
            _("Student") + ": " + fullname + " - " + form_student.data['dni'])
def details_user(request, user_id):
    template_data = {}
    try:
        user = UserService.find_one(user_id)
        assert not user.has_perm("principal.administrator")

        if user.has_perm("principal.alumno"):
            # Is student
            student = StudentService.find_one(user.id)
            subjects = student.asignaturas.all()
            template_data["subjects"] = subjects
            template_data["rol"] = "alumno"
            template_data["user_details"] = student
        else:
            # Is lecturer
            lecturer = LecturerService.find_one(user.id)
            impart_subject = lecturer.imparteasignatura_set.all()
            template_data["impart_subjects"] = impart_subject
            template_data["rol"] = "profesor"
            template_data["user_details"] = lecturer

        template_name = "user/details.html"
        return render_to_response(template_name, template_data, context_instance=RequestContext(request))

    except AssertionError:
        messages.error(request, _("Unable to display user details"))
        return HttpResponseRedirect("/admin/user/list")
Exemple #9
0
    def clean(self):

        username = self.cleaned_data.get('user')
        if username is not None:
            user = UserService.find_by_username(username)
            if user is not None:
                self.add_error(
                    'user',
                    _("There cannot be two users with the same username"))
            else:
                dni = self.cleaned_data.get('dni')
                user = StudentService.find_by_dni(dni)
                if user is not None:
                    self.add_error(
                        'dni',
                        _("There cannot be two users with the same ID number"))
                else:
                    user = LecturerService.find_by_dni(dni)
                    if user is not None:
                        self.add_error(
                            'dni',
                            _("There cannot be two users with the same ID number"
                              ))

        return self.cleaned_data
def details_user(request, user_id):
    template_data = {}
    try:
        user = UserService.find_one(user_id)
        assert not user.has_perm('principal.administrator')

        if user.has_perm('principal.alumno'):
            # Is student
            student = StudentService.find_one(user.id)
            subjects = student.asignaturas.all()
            template_data['subjects'] = subjects
            template_data['rol'] = 'alumno'
            template_data['user_details'] = student
        else:
            # Is lecturer
            lecturer = LecturerService.find_one(user.id)
            impart_subject = lecturer.imparteasignatura_set.all()
            template_data['impart_subjects'] = impart_subject
            template_data['rol'] = 'profesor'
            template_data['user_details'] = lecturer

        template_name = 'user/details.html'
        return render_to_response(template_name,
                                  template_data,
                                  context_instance=RequestContext(request))

    except AssertionError:
        messages.error(request, _('Unable to display user details'))
        return HttpResponseRedirect('/admin/user/list')
def list_users(request):
    students = StudentService.find_all()
    lecturers = LecturerService.fin_all()
    template_name = 'user/list.html'
    template_data = {'students': students, 'lecturers': lecturers}
    return render_to_response(template_name,
                              template_data,
                              context_instance=RequestContext(request))
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 unlink_user_subject(request, subject_id, user_id):
    subject = SubjectService.find_one(subject_id)
    user = UserService.find_one(user_id)
    assert not user.has_perm('principal.administrator')

    if user.has_perm('principal.alumno'):
        student = StudentService.find_one(user_id)
        student.asignaturas.remove(subject)
    else:
        lecturer = LecturerService.find_one(user_id)
        lecturer.imparteasignatura_set.all().get(profesor_id=user.id, asignatura_id=subject.id).delete()

    messages.success(request, _('Subject unlinked to the user'))
    return HttpResponseRedirect('/admin/user/details/' + str(user.id))
def unlink_user_subject(request, subject_id, user_id):
    subject = SubjectService.find_one(subject_id)
    user = UserService.find_one(user_id)
    assert not user.has_perm('principal.administrator')

    if user.has_perm('principal.alumno'):
        student = StudentService.find_one(user_id)
        student.asignaturas.remove(subject)
    else:
        lecturer = LecturerService.find_one(user_id)
        lecturer.imparteasignatura_set.all().get(
            profesor_id=user.id, asignatura_id=subject.id).delete()

    messages.success(request, _('Subject unlinked to the user'))
    return HttpResponseRedirect('/admin/user/details/' + str(user.id))
def search(request):
    template_data = {}
    if request.POST:
        search_text = request.POST["search_text"]
        if request.POST.get("subject_id"):
            subject_id = request.POST["subject_id"]
            template_data["subject_id"] = subject_id
    else:
        search_text = ""

    students = StudentService.search(search_text)
    lecturers = LecturerService.search(search_text)
    template_data["students"] = students
    template_data["lecturers"] = lecturers
    template_name = "user/ajax_search.html"
    return render_to_response(template_name, template_data, context_instance=RequestContext(request))
Exemple #16
0
    def clean(self):

        username = self.cleaned_data.get('user')
        if username is not None:
            user = UserService.find_by_username(username)
            if user is not None:
                self.add_error('user', _("There cannot be two users with the same username"))
            else:
                dni = self.cleaned_data.get('dni')
                user = StudentService.find_by_dni(dni)
                if user is not None:
                    self.add_error('dni', _("There cannot be two users with the same ID number"))
                else:
                    user = LecturerService.find_by_dni(dni)
                    if user is not None:
                        self.add_error('dni', _("There cannot be two users with the same ID number"))

        return self.cleaned_data
def search(request):
    template_data = {}
    if request.POST:
        search_text = request.POST['search_text']
        if request.POST.get('subject_id'):
            subject_id = request.POST['subject_id']
            template_data['subject_id'] = subject_id
    else:
        search_text = ''

    students = StudentService.search(search_text)
    lecturers = LecturerService.search(search_text)
    template_data['students'] = students
    template_data['lecturers'] = lecturers
    template_name = 'user/ajax_search.html'
    return render_to_response(template_name,
                              template_data,
                              context_instance=RequestContext(request))
def import_users(request):

    template_data = {}
    import_errors_create = []
    user_create = {}

    if request.POST:
        form = FileUploadForm(request.POST, request.FILES)

        if form.is_valid():
            file_name = form.cleaned_data['file_upload']
            data = file_name.read()
            file_name.close()

            try:
                if form.cleaned_data['file_upload'].content_type == "text/xml":
                    # File XML
                    data = xmltodict.parse(data)

                    # Iterate students
                    if data['usuarios']['alumnos']:
                        if not isinstance(
                                data['usuarios']['alumnos']['alumno'], list):
                            # 1 value
                            student_data = data['usuarios']['alumnos'][
                                'alumno']
                            data_form = StudentService.get_form_data_xml(
                                student_data)
                            create_student_file(data_form,
                                                import_errors_create,
                                                user_create)
                        else:
                            for student_data in data['usuarios']['alumnos'][
                                    'alumno']:
                                data_form = StudentService.get_form_data_xml(
                                    student_data)
                                create_student_file(data_form,
                                                    import_errors_create,
                                                    user_create)

                    # Iterate lecturers
                    if data['usuarios']['profesores']:
                        if not isinstance(
                                data['usuarios']['profesores']['profesor'],
                                list):
                            # 1 value
                            lecturer_data = data['usuarios']['profesores'][
                                'profesor']
                            data_form = LecturerService.get_form_data_xml(
                                lecturer_data)
                            create_lecturer_file(data_form,
                                                 import_errors_create,
                                                 user_create)
                        else:
                            for lecturer_data in data['usuarios'][
                                    'profesores']['profesor']:
                                data_form = LecturerService.get_form_data_xml(
                                    lecturer_data)
                                create_lecturer_file(data_form,
                                                     import_errors_create,
                                                     user_create)

                else:
                    # File CSV
                    for user in csv.reader(data.splitlines()):
                        type_user = user[0]
                        if type_user == 'Alumno':
                            data_form = StudentService.get_form_data_csv(user)
                            create_student_file(data_form,
                                                import_errors_create,
                                                user_create)
                        elif type_user == 'Profesor':
                            data_form = LecturerService.get_form_data_csv(user)
                            create_lecturer_file(data_form,
                                                 import_errors_create,
                                                 user_create)
                        else:
                            raise KeyError('Alumno o Profesor')

                message = _('Action completed successfully')
                EmailService.send_email_create_user(user_create, request)
                if import_errors_create:
                    messages.warning(request, message)
                    template_data[
                        'import_errors_create'] = import_errors_create
                else:
                    messages.success(request, message)

                form = FileUploadForm()

            except KeyError as e:
                message = _(
                    "The file structure is wrong. It needs a label called: "
                ) + e.message
                form.add_error('file_upload', message)
                UserService.rollback_users(user_create)
            except AttributeError:
                message = _("Please, check the attributes of the subjects")
                form.add_error('file_upload', message)
                UserService.rollback_users(user_create)
            except TypeError:
                message = _("Please, check the xml syntax and data values")
                form.add_error('file_upload', message)
                UserService.rollback_users(user_create)
            except Exception:
                message = _("Please, check the file")
                form.add_error('file_upload', message)
                UserService.rollback_users(user_create)

    else:
        form = FileUploadForm()

    template_data['form'] = form
    template_name = 'user/import_user.html'
    return render_to_response(template_name,
                              template_data,
                              context_instance=RequestContext(request))
def list_users(request):
    students = StudentService.find_all()
    lecturers = LecturerService.fin_all()
    template_name = "user/list.html"
    template_data = {"students": students, "lecturers": lecturers}
    return render_to_response(template_name, template_data, context_instance=RequestContext(request))
def import_users(request):

    template_data = {}
    import_errors_create = []
    user_create = {}

    if request.POST:
        form = FileUploadForm(request.POST, request.FILES)

        if form.is_valid():
            file_name = form.cleaned_data["file_upload"]
            data = file_name.read()
            file_name.close()

            try:
                if form.cleaned_data["file_upload"].content_type == "text/xml":
                    # File XML
                    data = xmltodict.parse(data)

                    # Iterate students
                    if data["usuarios"]["alumnos"]:
                        if not isinstance(data["usuarios"]["alumnos"]["alumno"], list):
                            # 1 value
                            student_data = data["usuarios"]["alumnos"]["alumno"]
                            data_form = StudentService.get_form_data_xml(student_data)
                            create_student_file(data_form, import_errors_create, user_create)
                        else:
                            for student_data in data["usuarios"]["alumnos"]["alumno"]:
                                data_form = StudentService.get_form_data_xml(student_data)
                                create_student_file(data_form, import_errors_create, user_create)

                    # Iterate lecturers
                    if data["usuarios"]["profesores"]:
                        if not isinstance(data["usuarios"]["profesores"]["profesor"], list):
                            # 1 value
                            lecturer_data = data["usuarios"]["profesores"]["profesor"]
                            data_form = LecturerService.get_form_data_xml(lecturer_data)
                            create_lecturer_file(data_form, import_errors_create, user_create)
                        else:
                            for lecturer_data in data["usuarios"]["profesores"]["profesor"]:
                                data_form = LecturerService.get_form_data_xml(lecturer_data)
                                create_lecturer_file(data_form, import_errors_create, user_create)

                else:
                    # File CSV
                    for user in csv.reader(data.splitlines()):
                        type_user = user[0]
                        if type_user == "Alumno":
                            data_form = StudentService.get_form_data_csv(user)
                            create_student_file(data_form, import_errors_create, user_create)
                        elif type_user == "Profesor":
                            data_form = LecturerService.get_form_data_csv(user)
                            create_lecturer_file(data_form, import_errors_create, user_create)
                        else:
                            raise KeyError("Alumno o Profesor")

                message = _("Action completed successfully")
                EmailService.send_email_create_user(user_create, request)
                if import_errors_create:
                    messages.warning(request, message)
                    template_data["import_errors_create"] = import_errors_create
                else:
                    messages.success(request, message)

                form = FileUploadForm()

            except KeyError as e:
                message = _("The file structure is wrong. It needs a label called: ") + e.message
                form.add_error("file_upload", message)
                UserService.rollback_users(user_create)
            except AttributeError:
                message = _("Please, check the attributes of the subjects")
                form.add_error("file_upload", message)
                UserService.rollback_users(user_create)
            except TypeError:
                message = _("Please, check the xml syntax and data values")
                form.add_error("file_upload", message)
                UserService.rollback_users(user_create)
            except Exception:
                message = _("Please, check the file")
                form.add_error("file_upload", message)
                UserService.rollback_users(user_create)

    else:
        form = FileUploadForm()

    template_data["form"] = form
    template_name = "user/import_user.html"
    return render_to_response(template_name, template_data, context_instance=RequestContext(request))