예제 #1
0
def import_users(csv_file):
    error_messages = []
    count_success = 0
    try:
        # check for valid encoding (will raise UnicodeDecodeError if not)
        csv_file.read().decode('utf-8')
        csv_file.seek(0)

        with transaction.commit_on_success():
            dialect = csv.Sniffer().sniff(csv_file.readline())
            dialect = csv_ext.patchup(dialect)
            csv_file.seek(0)

            for (line_no, line) in enumerate(csv.reader(csv_file,
                                                        dialect=dialect)):
                if line_no:
                    try:
                        (title, first_name, last_name, gender, email, groups,
                         structure_level, committee, about_me, comment, is_active) = line[:11]
                    except ValueError:
                        error_messages.append(_('Ignoring malformed line %d in import file.') % (line_no + 1))
                        continue
                    user = User()
                    user.title = title
                    user.last_name = last_name
                    user.first_name = first_name
                    user.username = gen_username(first_name, last_name)
                    user.gender = gender
                    user.email = email
                    user.structure_level = structure_level
                    user.committee = committee
                    user.about_me = about_me
                    user.comment = comment
                    if is_active == '1':
                        user.is_active = True
                    else:
                        user.is_active = False
                    user.default_password = gen_password()
                    user.save()
                    for groupid in groups:
                        try:
                            if groupid != ",":
                                Group.objects.get(pk=groupid).user_set.add(user)
                        except ValueError:
                            error_messages.append(_('Ignoring malformed group id in line %d.') % (line_no + 1))
                            continue
                        except Group.DoesNotExist:
                            error_messages.append(_('Group id %(id)s does not exists (line %(line)d).') % {'id': groupid, 'line': line_no + 1})
                            continue
                    user.reset_password()
                    count_success += 1
    except csv.Error:
        error_messages.append(_('Import aborted because of severe errors in the input file.'))
    except UnicodeDecodeError:
        error_messages.append(_('Import file has wrong character encoding, only UTF-8 is supported!'))
    return (count_success, error_messages)
예제 #2
0
def import_users(csv_file):
    error_messages = []
    count_success = 0
    try:
        # check for valid encoding (will raise UnicodeDecodeError if not)
        csv_file.read().decode('utf-8')
        csv_file.seek(0)

        with transaction.commit_on_success():
            dialect = csv.Sniffer().sniff(csv_file.readline())
            dialect = csv_ext.patchup(dialect)
            csv_file.seek(0)

            for (line_no,
                 line) in enumerate(csv.reader(csv_file, dialect=dialect)):
                if line_no:
                    try:
                        (first_name, last_name, gender, structure_level, type,
                         committee, comment) = line[:7]
                    except ValueError:
                        error_messages.append(
                            _('Ignoring malformed line %d in import file.') %
                            (line_no + 1))
                        continue
                    user = User()
                    user.last_name = last_name
                    user.first_name = first_name
                    user.username = gen_username(first_name, last_name)
                    user.gender = gender
                    user.structure_level = structure_level
                    user.type = type
                    user.committee = committee
                    user.comment = comment
                    user.default_password = gen_password()
                    user.save()
                    user.reset_password()
                    count_success += 1
    except csv.Error:
        error_messages.appen(
            _('Import aborted because of severe errors in the input file.'))
    except UnicodeDecodeError:
        error_messages.appen(
            _('Import file has wrong character encoding, only UTF-8 is supported!'
              ))
    return (count_success, error_messages)
예제 #3
0
파일: api.py 프로젝트: MechanisM/OpenSlides
def import_users(csv_file):
    error_messages = []
    count_success = 0
    try:
        # check for valid encoding (will raise UnicodeDecodeError if not)
        csv_file.read().decode('utf-8')
        csv_file.seek(0)

        with transaction.commit_on_success():
            dialect = csv.Sniffer().sniff(csv_file.readline())
            dialect = csv_ext.patchup(dialect)
            csv_file.seek(0)

            for (line_no, line) in enumerate(csv.reader(csv_file,
                                                        dialect=dialect)):
                if line_no:
                    try:
                        (first_name, last_name, gender, structure_level, type, committee, comment) = line[:7]
                    except ValueError:
                        error_messages.append(_('Ignoring malformed line %d in import file.') % (line_no + 1))
                        continue
                    user = User()
                    user.last_name = last_name
                    user.first_name = first_name
                    user.username = gen_username(first_name, last_name)
                    user.gender = gender
                    user.structure_level = structure_level
                    user.type = type
                    user.committee = committee
                    user.comment = comment
                    user.default_password = gen_password()
                    user.save()
                    user.reset_password()
                    count_success += 1
    except csv.Error:
        error_messages.appen(_('Import aborted because of severe errors in the input file.'))
    except UnicodeDecodeError:
        error_messages.appen(_('Import file has wrong character encoding, only UTF-8 is supported!'))
    return (count_success, error_messages)
예제 #4
0
def motion_import(request):
    if request.method == 'POST':
        form = MotionImportForm(request.POST, request.FILES)
        if form.is_valid():
            import_permitted = form.cleaned_data['import_permitted']
            try:
                # check for valid encoding (will raise UnicodeDecodeError if not)
                request.FILES['csvfile'].read().decode('utf-8')
                request.FILES['csvfile'].seek(0)

                users_generated = 0
                motions_generated = 0
                motions_modified = 0
                groups_assigned = 0
                groups_generated = 0
                with transaction.commit_on_success():
                    dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
                    dialect = csv_ext.patchup(dialect)
                    request.FILES['csvfile'].seek(0)
                    for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'], dialect=dialect)):
                        # basic input verification
                        if lno < 1:
                            continue
                        try:
                            (number, title, text, reason, first_name, last_name, is_group) = line[:7]
                            if is_group.strip().lower() in ['y', 'j', 't', 'yes', 'ja', 'true', '1', 1]:
                                is_group = True
                            else:
                                is_group = False
                        except ValueError:
                            messages.error(request, _('Ignoring malformed line %d in import file.') % (lno + 1))
                            continue
                        form = MotionForm({'title': title, 'text': text, 'reason': reason})
                        if not form.is_valid():
                            messages.error(request, _('Ignoring malformed line %d in import file.') % (lno + 1))
                            continue
                        if number:
                            try:
                                number = abs(long(number))
                                if number < 1:
                                    messages.error(request, _('Ignoring malformed line %d in import file.') % (lno + 1))
                                    continue
                            except ValueError:
                                messages.error(request, _('Ignoring malformed line %d in import file.') % (lno + 1))
                                continue

                        if is_group:
                            # fetch existing groups or issue an error message
                            try:
                                user = Group.objects.get(name=last_name)
                                if user.group_as_person == False:
                                    messages.error(request, _('Ignoring line %d because the assigned group may not act as a person.') % (lno + 1))
                                    continue
                                else:
                                    user = get_person(user.person_id)

                                groups_assigned += 1
                            except Group.DoesNotExist:
                                group = Group()
                                group.group_as_person = True
                                group.description = _('Created by motion import.')
                                group.name = last_name
                                group.save()
                                groups_generated += 1

                                user = get_person(group.person_id)
                        else:
                            # fetch existing users or create new users as needed
                            try:
                                user = User.objects.get(first_name=first_name, last_name=last_name)
                            except User.DoesNotExist:
                                user = None
                            if user is None:
                                if not first_name or not last_name:
                                    messages.error(request, _('Ignoring line %d because it contains an incomplete first / last name pair.') % (lno + 1))
                                    continue

                                user = User()
                                user.last_name = last_name
                                user.first_name = first_name
                                user.username = gen_username(first_name, last_name)
                                user.structure_level = ''
                                user.committee = ''
                                user.gender = ''
                                user.type = ''
                                user.default_password = gen_password()
                                user.save()
                                user.reset_password()
                                users_generated += 1
                        # create / modify the motion
                        motion = None
                        if number:
                            try:
                                motion = Motion.objects.get(number=number)
                                motions_modified += 1
                            except Motion.DoesNotExist:
                                motion = None
                        if motion is None:
                            motion = Motion(submitter=user)
                            if number:
                                motion.number = number
                            motions_generated += 1

                        motion.title = form.cleaned_data['title']
                        motion.text = form.cleaned_data['text']
                        motion.reason = form.cleaned_data['reason']
                        if import_permitted:
                            motion.status = 'per'

                        motion.save(user, trivial_change=True)

                if motions_generated:
                    messages.success(request, ungettext('%d motion was successfully imported.',
                                                '%d motions were successfully imported.', motions_generated) % motions_generated)
                if motions_modified:
                    messages.success(request, ungettext('%d motion was successfully modified.',
                                                '%d motions were successfully modified.', motions_modified) % motions_modified)
                if users_generated:
                    messages.success(request, ungettext('%d new user was added.', '%d new users were added.', users_generated) % users_generated)

                if groups_generated:
                    messages.success(request, ungettext('%d new group was added.', '%d new groups were added.', groups_generated) % groups_generated)

                if groups_assigned:
                    messages.success(request, ungettext('%d group assigned to motions.', '%d groups assigned to motions.', groups_assigned) % groups_assigned)
                return redirect(reverse('motion_overview'))

            except csv.Error:
                messages.error(request, _('Import aborted because of severe errors in the input file.'))
            except UnicodeDecodeError:
                messages.error(request, _('Import file has wrong character encoding, only UTF-8 is supported!'))
        else:
            messages.error(request, _('Please check the form for errors.'))
    else:
        messages.warning(request, _("Attention: Existing motions will be modified if you import new motions with the same number."))
        messages.warning(request, _("Attention: Importing an motions without a number multiple times will create duplicates."))
        form = MotionImportForm()
    return {
        'form': form,
    }
예제 #5
0
def motion_import(request):
    if request.method == 'POST':
        form = MotionImportForm(request.POST, request.FILES)
        if form.is_valid():
            import_permitted = form.cleaned_data['import_permitted']
            try:
                # check for valid encoding (will raise UnicodeDecodeError if not)
                request.FILES['csvfile'].read().decode('utf-8')
                request.FILES['csvfile'].seek(0)

                users_generated = 0
                motions_generated = 0
                motions_modified = 0
                groups_assigned = 0
                groups_generated = 0
                with transaction.commit_on_success():
                    dialect = csv.Sniffer().sniff(
                        request.FILES['csvfile'].readline())
                    dialect = csv_ext.patchup(dialect)
                    request.FILES['csvfile'].seek(0)
                    for (lno, line) in enumerate(
                            csv.reader(request.FILES['csvfile'],
                                       dialect=dialect)):
                        # basic input verification
                        if lno < 1:
                            continue
                        try:
                            (number, title, text, reason, first_name,
                             last_name, is_group) = line[:7]
                            if is_group.strip().lower() in [
                                    'y', 'j', 't', 'yes', 'ja', 'true', '1', 1
                            ]:
                                is_group = True
                            else:
                                is_group = False
                        except ValueError:
                            messages.error(
                                request,
                                _('Ignoring malformed line %d in import file.')
                                % (lno + 1))
                            continue
                        form = MotionForm({
                            'title': title,
                            'text': text,
                            'reason': reason
                        })
                        if not form.is_valid():
                            messages.error(
                                request,
                                _('Ignoring malformed line %d in import file.')
                                % (lno + 1))
                            continue
                        if number:
                            try:
                                number = abs(long(number))
                                if number < 1:
                                    messages.error(
                                        request,
                                        _('Ignoring malformed line %d in import file.'
                                          ) % (lno + 1))
                                    continue
                            except ValueError:
                                messages.error(
                                    request,
                                    _('Ignoring malformed line %d in import file.'
                                      ) % (lno + 1))
                                continue

                        if is_group:
                            # fetch existing groups or issue an error message
                            try:
                                user = Group.objects.get(name=last_name)
                                if user.group_as_person == False:
                                    messages.error(
                                        request,
                                        _('Ignoring line %d because the assigned group may not act as a person.'
                                          ) % (lno + 1))
                                    continue
                                else:
                                    user = get_person(user.person_id)

                                groups_assigned += 1
                            except Group.DoesNotExist:
                                group = Group()
                                group.group_as_person = True
                                group.description = _(
                                    'Created by motion import.')
                                group.name = last_name
                                group.save()
                                groups_generated += 1

                                user = get_person(group.person_id)
                        else:
                            # fetch existing users or create new users as needed
                            try:
                                user = User.objects.get(first_name=first_name,
                                                        last_name=last_name)
                            except User.DoesNotExist:
                                user = None
                            if user is None:
                                if not first_name or not last_name:
                                    messages.error(
                                        request,
                                        _('Ignoring line %d because it contains an incomplete first / last name pair.'
                                          ) % (lno + 1))
                                    continue

                                user = User()
                                user.last_name = last_name
                                user.first_name = first_name
                                user.username = gen_username(
                                    first_name, last_name)
                                user.structure_level = ''
                                user.committee = ''
                                user.gender = ''
                                user.type = ''
                                user.default_password = gen_password()
                                user.save()
                                user.reset_password()
                                users_generated += 1
                        # create / modify the motion
                        motion = None
                        if number:
                            try:
                                motion = Motion.objects.get(number=number)
                                motions_modified += 1
                            except Motion.DoesNotExist:
                                motion = None
                        if motion is None:
                            motion = Motion(submitter=user)
                            if number:
                                motion.number = number
                            motions_generated += 1

                        motion.title = form.cleaned_data['title']
                        motion.text = form.cleaned_data['text']
                        motion.reason = form.cleaned_data['reason']
                        if import_permitted:
                            motion.status = 'per'

                        motion.save(user, trivial_change=True)

                if motions_generated:
                    messages.success(
                        request,
                        ungettext('%d motion was successfully imported.',
                                  '%d motions were successfully imported.',
                                  motions_generated) % motions_generated)
                if motions_modified:
                    messages.success(
                        request,
                        ungettext('%d motion was successfully modified.',
                                  '%d motions were successfully modified.',
                                  motions_modified) % motions_modified)
                if users_generated:
                    messages.success(
                        request,
                        ungettext('%d new user was added.',
                                  '%d new users were added.', users_generated)
                        % users_generated)

                if groups_generated:
                    messages.success(
                        request,
                        ungettext('%d new group was added.',
                                  '%d new groups were added.',
                                  groups_generated) % groups_generated)

                if groups_assigned:
                    messages.success(
                        request,
                        ungettext('%d group assigned to motions.',
                                  '%d groups assigned to motions.',
                                  groups_assigned) % groups_assigned)
                return redirect(reverse('motion_overview'))

            except csv.Error:
                messages.error(
                    request,
                    _('Import aborted because of severe errors in the input file.'
                      ))
            except UnicodeDecodeError:
                messages.error(
                    request,
                    _('Import file has wrong character encoding, only UTF-8 is supported!'
                      ))
        else:
            messages.error(request, _('Please check the form for errors.'))
    else:
        messages.warning(
            request,
            _("Attention: Existing motions will be modified if you import new motions with the same number."
              ))
        messages.warning(
            request,
            _("Attention: Importing an motions without a number multiple times will create duplicates."
              ))
        form = MotionImportForm()
    return {
        'form': form,
    }
예제 #6
0
def import_users(csv_file):
    error_messages = []
    count_success = 0
    try:
        # check for valid encoding (will raise UnicodeDecodeError if not)
        csv_file.read().decode('utf-8')
        csv_file.seek(0)

        with transaction.commit_on_success():
            dialect = csv.Sniffer().sniff(csv_file.readline())
            dialect = csv_ext.patchup(dialect)
            csv_file.seek(0)

            for (line_no,
                 line) in enumerate(csv.reader(csv_file, dialect=dialect)):
                if line_no:
                    try:
                        (title, first_name, last_name, gender, email, groups,
                         structure_level, committee, about_me, comment,
                         is_active) = line[:11]
                    except ValueError:
                        error_messages.append(
                            _('Ignoring malformed line %d in import file.') %
                            (line_no + 1))
                        continue
                    user = User()
                    user.title = title
                    user.last_name = last_name
                    user.first_name = first_name
                    user.username = gen_username(first_name, last_name)
                    user.gender = gender
                    user.email = email
                    user.structure_level = structure_level
                    user.committee = committee
                    user.about_me = about_me
                    user.comment = comment
                    if is_active == '1':
                        user.is_active = True
                    else:
                        user.is_active = False
                    user.default_password = gen_password()
                    user.save()
                    for groupid in groups:
                        try:
                            if groupid != ",":
                                Group.objects.get(
                                    pk=groupid).user_set.add(user)
                        except ValueError:
                            error_messages.append(
                                _('Ignoring malformed group id in line %d.') %
                                (line_no + 1))
                            continue
                        except Group.DoesNotExist:
                            error_messages.append(
                                _('Group id %(id)s does not exists (line %(line)d).'
                                  ) % {
                                      'id': groupid,
                                      'line': line_no + 1
                                  })
                            continue
                    user.reset_password()
                    count_success += 1
    except csv.Error:
        error_messages.append(
            _('Import aborted because of severe errors in the input file.'))
    except UnicodeDecodeError:
        error_messages.append(
            _('Import file has wrong character encoding, only UTF-8 is supported!'
              ))
    return (count_success, error_messages)