def setUp(self): self.admin = User(username='******') self.admin.save() self.anonym = User(username='******') self.anonym.save() self.app1 = Motion(submitter=self.admin) self.app1.save()
def setUp(self): self.user1 = User() self.user1.first_name = u'Max' self.user1.last_name = u'Mustermann' self.user1.username = gen_username( self.user1.first_name, self.user1.last_name) self.user1.default_password = gen_password() self.user1.save() self.django_user1 = self.user1.django_user
def create_or_reset_admin_user(): group_staff = Group.objects.get(pk=4) try: admin = User.objects.get(username="******") except User.DoesNotExist: admin = User() admin.username = '******' admin.last_name = 'Administrator' admin.default_password = '******' admin.set_password(admin.default_password) admin.save() admin.groups.add(group_staff)
def create_or_reset_admin_user(): # can't be imported in global scope as it already requires # the settings module during import from openslides.participant.models import User try: admin = User.objects.get(username="******") print("Password for user admin was reset to 'admin'") except User.DoesNotExist: admin = User() admin.username = '******' admin.last_name = 'Administrator' print("Created default admin user") admin.is_superuser = True admin.default_password = '******' admin.set_password(admin.default_password) admin.save()
class MotionTest(TestCase): def setUp(self): self.admin = User(username='******') self.admin.save() self.anonym = User(username='******') self.anonym.save() self.app1 = Motion(submitter=self.admin) self.app1.save() def refresh(self): self.app1 = Motion.objects.get(pk=self.app1.id) def testVersion(self): self.assertTrue(self.app1.versions.exists()) self.assertEqual(self.app1.last_version, self.app1.versions[0]) self.assertEqual(self.app1.creation_time, self.app1.last_version.time) self.app1.title = "app1" self.app1.save() self.refresh() self.assertEqual(self.app1.versions.count(), 2) self.assertEqual(self.app1.last_version, self.app1.versions[1])
class UserTest(TestCase): def setUp(self): self.user1 = User() self.user1.first_name = u'Max' self.user1.last_name = u'Mustermann' self.user1.username = gen_username(self.user1.first_name, self.user1.last_name) self.user1.default_password = gen_password() self.user1.save() self.django_user1 = self.user1.django_user def test_participant_user(self): self.assertEqual(self.django_user1.user, self.user1) self.assertEqual(self.django_user1, self.user1.django_user) def test_repr(self): self.assertEqual(unicode(self.user1), 'Max Mustermann') def test_name_suffix(self): self.user1.structure_level = u'München' self.user1.save() self.assertEqual(unicode(self.user1), u'Max Mustermann (München)') def test_reset_password(self): self.assertIsInstance(self.user1.default_password, basestring) self.assertEqual(len(self.user1.default_password), 8) self.user1.set_unusable_password() self.assertFalse(self.user1.check_password( self.user1.default_password)) self.user1.reset_password() self.assertTrue(self.user1.check_password(self.user1.default_password)) def test_person_api(self): self.assertTrue(hasattr(self.user1, 'person_id')) self.assertEqual(self.user1.person_id, 'user:2') self.assertEqual(get_person('user:2'), self.user1) self.assertEqual(len(Persons(person_prefix_filter='user')), 2)
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, }
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)
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)