Exemplo n.º 1
0
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))
Exemplo n.º 2
0
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 import_link_subject(request, subject_id):
    template_data = {}
    initial = {}
    import_errors_link = []
    user_create = {}
    student_link = []
    lecturer_link = []

    subject = get_object_or_404(Asignatura, id=subject_id)
    initial['subject'] = subject.id
    initial['subject_name'] = subject.nombre

    if request.POST:

        form = FileUploadSubjectLinkForm(request.POST, request.FILES)

        if form.is_valid():

            file_name = form.cleaned_data['file_upload']
            subject = get_object_or_404(Asignatura, id=form.cleaned_data['subject'])
            data = file_name.read()
            file_name.close()
            file_type = form.cleaned_data['file_upload'].content_type

            try:
                if file_type == "text/xml":

                    # File XML
                    data = xmltodict.parse(data)

                    # Import students
                    if data['usuarios']['alumnos']:
                        if not isinstance(data['usuarios']['alumnos']['alumno'], list):
                            # 1 value
                            student_data = data['usuarios']['alumnos']['alumno']
                            create_link_student_file(student_data, subject, file_type,
                                                     import_errors_link, user_create, student_link)
                        else:
                            for student_data in data['usuarios']['alumnos']['alumno']:
                                create_link_student_file(student_data, subject, file_type,
                                                         import_errors_link, user_create, student_link)

                    # Import lecturers
                    if data['usuarios']['profesores']:
                        if not isinstance(data['usuarios']['profesores']['profesor'], list):
                            # 1 value
                            lecturer_data = data['usuarios']['profesores']['profesor']
                            create_link_lecturer_file(lecturer_data, subject, file_type,
                                                      import_errors_link, user_create, lecturer_link)
                        else:
                            for lecturer_data in data['usuarios']['profesores']['profesor']:
                                create_link_lecturer_file(lecturer_data, subject, file_type,
                                                          import_errors_link, user_create, lecturer_link)
                else:
                    # File CSV
                    for user in csv.reader(data.splitlines()):
                        type_user = user[0]
                        if type_user == 'Alumno':
                            create_link_student_file(user, subject, file_type, import_errors_link,
                                                     user_create, lecturer_link)
                        elif type_user == 'Profesor':
                            create_link_lecturer_file(user, subject, file_type,
                                                      import_errors_link, user_create, lecturer_link)
                        else:
                            raise KeyError('Alumno o Profesor')

                if import_errors_link:
                    message = _('Action completed successfully')
                    messages.warning(request, message)
                    template_data['import_errors_link'] = import_errors_link
                else:
                    EmailService.send_email_create_user(user_create, request)
                    messages.success(request, _('Action completed successfully'))
                    return HttpResponseRedirect("/subject/details/" + str(subject.id))

                form = FileUploadSubjectLinkForm(initial=initial)

            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(user_create, student_link, lecturer_link, subject_id)
            except AttributeError:
                message = _("Please, check the attributes of the users")
                form.add_error('file_upload', message)
                UserService.rollback(user_create, student_link, lecturer_link, subject_id)
            except TypeError:
                message = _("Please, check the xml syntax and data values")
                form.add_error('file_upload', message)
                UserService.rollback(user_create, student_link, lecturer_link, subject_id)
            except Exception:
                message = _("Please, check the file")
                form.add_error('file_upload', message)
                UserService.rollback(user_create, student_link, lecturer_link, subject_id)

    else:
        form = FileUploadSubjectLinkForm(initial=initial)

    template_data['subject'] = subject
    template_data['form'] = form
    template_name = 'subject_link/import_subject_link.html'
    return render_to_response(template_name, template_data, context_instance=RequestContext(request))
Exemplo n.º 4
0
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))
Exemplo n.º 5
0
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 import_link_subject(request, subject_id):
    template_data = {}
    initial = {}
    import_errors_link = []
    user_create = {}
    student_link = []
    lecturer_link = []

    subject = get_object_or_404(Asignatura, id=subject_id)
    initial['subject'] = subject.id
    initial['subject_name'] = subject.nombre

    if request.POST:

        form = FileUploadSubjectLinkForm(request.POST, request.FILES)

        if form.is_valid():

            file_name = form.cleaned_data['file_upload']
            subject = get_object_or_404(Asignatura,
                                        id=form.cleaned_data['subject'])
            data = file_name.read()
            file_name.close()
            file_type = form.cleaned_data['file_upload'].content_type

            try:
                if file_type == "text/xml":

                    # File XML
                    data = xmltodict.parse(data)

                    # Import students
                    if data['usuarios']['alumnos']:
                        if not isinstance(
                                data['usuarios']['alumnos']['alumno'], list):
                            # 1 value
                            student_data = data['usuarios']['alumnos'][
                                'alumno']
                            create_link_student_file(student_data, subject,
                                                     file_type,
                                                     import_errors_link,
                                                     user_create, student_link)
                        else:
                            for student_data in data['usuarios']['alumnos'][
                                    'alumno']:
                                create_link_student_file(
                                    student_data, subject, file_type,
                                    import_errors_link, user_create,
                                    student_link)

                    # Import lecturers
                    if data['usuarios']['profesores']:
                        if not isinstance(
                                data['usuarios']['profesores']['profesor'],
                                list):
                            # 1 value
                            lecturer_data = data['usuarios']['profesores'][
                                'profesor']
                            create_link_lecturer_file(lecturer_data, subject,
                                                      file_type,
                                                      import_errors_link,
                                                      user_create,
                                                      lecturer_link)
                        else:
                            for lecturer_data in data['usuarios'][
                                    'profesores']['profesor']:
                                create_link_lecturer_file(
                                    lecturer_data, subject, file_type,
                                    import_errors_link, user_create,
                                    lecturer_link)
                else:
                    # File CSV
                    for user in csv.reader(data.splitlines()):
                        type_user = user[0]
                        if type_user == 'Alumno':
                            create_link_student_file(user, subject, file_type,
                                                     import_errors_link,
                                                     user_create,
                                                     lecturer_link)
                        elif type_user == 'Profesor':
                            create_link_lecturer_file(user, subject, file_type,
                                                      import_errors_link,
                                                      user_create,
                                                      lecturer_link)
                        else:
                            raise KeyError('Alumno o Profesor')

                if import_errors_link:
                    message = _('Action completed successfully')
                    messages.warning(request, message)
                    template_data['import_errors_link'] = import_errors_link
                else:
                    EmailService.send_email_create_user(user_create, request)
                    messages.success(request,
                                     _('Action completed successfully'))
                    return HttpResponseRedirect("/subject/details/" +
                                                str(subject.id))

                form = FileUploadSubjectLinkForm(initial=initial)

            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(user_create, student_link, lecturer_link,
                                     subject_id)
            except AttributeError:
                message = _("Please, check the attributes of the users")
                form.add_error('file_upload', message)
                UserService.rollback(user_create, student_link, lecturer_link,
                                     subject_id)
            except TypeError:
                message = _("Please, check the xml syntax and data values")
                form.add_error('file_upload', message)
                UserService.rollback(user_create, student_link, lecturer_link,
                                     subject_id)
            except Exception:
                message = _("Please, check the file")
                form.add_error('file_upload', message)
                UserService.rollback(user_create, student_link, lecturer_link,
                                     subject_id)

    else:
        form = FileUploadSubjectLinkForm(initial=initial)

    template_data['subject'] = subject
    template_data['form'] = form
    template_name = 'subject_link/import_subject_link.html'
    return render_to_response(template_name,
                              template_data,
                              context_instance=RequestContext(request))