def login_view(request): """ Returns login at '/login' and logs user in from POST. """ redirect_url = request.REQUEST.get('next') or request.META.get( 'HTTP_REFERER') or '/' if request.POST: try: username = request.POST['username'] password = request.POST['password'] except MultiValueDictKeyError: messages.error(request, 'Login failed.') user = authenticate(username=username, password=password) if user is not None and user.is_active: # merge LDAP user and scm user which with same email in database merge_users(user.email) login(request, user) log.info('login|%s|%s', user.username, user.email) return redirect(redirect_url) else: messages.error(request, 'Faulty login credentials.') return render(request, 'core/login.html', {'redirecto_to': redirect_url})
def login_view(request): """ Returns login at '/login' and logs user in from POST. """ redirect_url = request.REQUEST.get('next') or request.META.get('HTTP_REFERER') or '/' if request.POST: try: username = request.POST['username'] password = request.POST['password'] except MultiValueDictKeyError: messages.error(request, 'Login failed.') user = authenticate(username=username, password=password) if user is not None and user.is_active: # merge LDAP user and scm user which with same email in database merge_users(user.email) login(request, user) log.info('login|%s|%s', user.username, user.email) return redirect(redirect_url) else: messages.error(request, 'Faulty login credentials.') return render(request, 'core/login.html', { 'redirecto_to': redirect_url})
def test_merge_users(self): merge_users(self.mail) self.assertEqual( User.objects.filter(username=self.mail, email=self.mail).exists(), False) self.assertTrue(self.user_b in self.domainrole.user_set.all(), True) self.assertTrue(len(self.domainrole.user_set.all()) == 1, True)
def forwards(self, orm): "Write your forwards methods here." # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. # Django ORM does "group by" query by Aggregation API: # https://docs.djangoproject.com/en/dev/topics/db/aggregation/#order-of-annotate-and-values-clauses users = orm['auth.User'].objects.values('email').annotate(cnt=Count('email')).filter(cnt__gt=1) for us in users: merge_users(us['email'])
def forwards(self, orm): "Write your forwards methods here." # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. # Django ORM does "group by" query by Aggregation API: # https://docs.djangoproject.com/en/dev/topics/db/aggregation/#order-of-annotate-and-values-clauses users = orm['auth.User'].objects.values('email').annotate( cnt=Count('email')).filter(cnt__gt=1) for us in users: merge_users(us['email'])
def test_merge_users(self): merge_users(self.mail) self.assertEqual(User.objects.filter(username=self.mail, email=self.mail).exists(), False) self.assertTrue(self.user_b in self.domainrole.user_set.all(), True) self.assertTrue(len(self.domainrole.user_set.all()) == 1, True)