summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorprathamesh2014-06-06 13:18:33 +0530
committerprathamesh2014-06-06 13:18:33 +0530
commit7d0753f2e0f590ed4376fdb5f8abaace7c4cfc19 (patch)
tree6cd9a54277bb7f968c1ab67cf6627802515ec972
parent8b5d13df2a8f4ac3acc075c522128722b987e57b (diff)
parentc5115d98867504076daacc093d09489033c6a584 (diff)
downloadonline_test-7d0753f2e0f590ed4376fdb5f8abaace7c4cfc19.tar.gz
online_test-7d0753f2e0f590ed4376fdb5f8abaace7c4cfc19.tar.bz2
online_test-7d0753f2e0f590ed4376fdb5f8abaace7c4cfc19.zip
Merge branch 'spoken_tutorial'
-rw-r--r--testapp/myauthentication/README12
-rw-r--r--testapp/myauthentication/__init__.py0
-rw-r--r--testapp/myauthentication/backend.py42
-rw-r--r--testapp/myauthentication/models_spoken.py26
-rw-r--r--testapp/myauthentication/router.py12
-rw-r--r--testapp/myauthentication/tests.py16
-rw-r--r--testapp/myauthentication/urls.py0
-rw-r--r--testapp/myauthentication/views.py1
-rw-r--r--testapp/settings.py15
9 files changed, 124 insertions, 0 deletions
diff --git a/testapp/myauthentication/README b/testapp/myauthentication/README
new file mode 100644
index 0000000..6d1a7b4
--- /dev/null
+++ b/testapp/myauthentication/README
@@ -0,0 +1,12 @@
+To use authentication from external source, follow the instructions given below:
+
+1. In settings.py,
+ Uncomment AUTHENTICATION_BACKENDS = ('myauthentication.backend.MyBackend',)
+ Uncomment AUTHENTICATION_BACKENDS = ('myauthentication.backend.MyBackend',)
+ Enter database name, username and password for the spoken database.
+
+2. From login.html template comment 'New User? Sign-Up' link.
+ This is to be done so that user will register only from external
+ application and their details will be stored in the external database.
+
+3. In urls.py comment 'register' url pattern.
diff --git a/testapp/myauthentication/__init__.py b/testapp/myauthentication/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testapp/myauthentication/__init__.py
diff --git a/testapp/myauthentication/backend.py b/testapp/myauthentication/backend.py
new file mode 100644
index 0000000..d82f11a
--- /dev/null
+++ b/testapp/myauthentication/backend.py
@@ -0,0 +1,42 @@
+import hashlib
+from django.contrib.auth.models import User, check_password
+from models_spoken import MdlUser
+
+class MyBackend:
+ supports_object_permissions = False
+ supports_anonymous_user = False
+ supports_inactive_user = False
+
+ def authenticate(self, username=None, password=None):
+ '''
+ Checks username and password with external User table.
+ If valid then adds the user details in django User table
+ and authenticates the user.
+ '''
+ try:
+ user = MdlUser.objects.get(username=username)
+ pwd = user.password
+ uid = user.id
+ firstname = user.firstname
+ lastname = user.lastname
+ email_id = user.email
+ p = hashlib.md5(password)
+ pwd_valid = (pwd == p.hexdigest())
+ if user and pwd_valid:
+ try:
+ user = User.objects.get(username=username)
+ return user
+ except Exception, e:
+ user=User(id=uid, username=username, password=pwd,\
+ first_name=firstname, last_name=lastname,\
+ email=email_id)
+ user.save()
+ return user
+ except Exception, e:
+ return None
+
+ def get_user(self, user_id):
+ try:
+ return User.objects.get(pk=user_id)
+ except Exception, e:
+ return None
diff --git a/testapp/myauthentication/models_spoken.py b/testapp/myauthentication/models_spoken.py
new file mode 100644
index 0000000..2526a0e
--- /dev/null
+++ b/testapp/myauthentication/models_spoken.py
@@ -0,0 +1,26 @@
+# This is an auto-generated Django model module.
+# You'll have to do the following manually to clean this up:
+# * Rearrange models' order
+# * Make sure each model has one field with primary_key=True
+# Feel free to rename the models, but don't rename db_table values or field names.
+#
+# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'
+# into your database.
+
+from django.db import models
+
+class MdlUser(models.Model):
+ id = models.BigIntegerField(primary_key=True)
+ username = models.CharField(unique=True, max_length=300)
+ password = models.CharField(max_length=96)
+ idnumber = models.CharField(max_length=765)
+ firstname = models.CharField(max_length=300)
+ lastname = models.CharField(max_length=300)
+ email = models.CharField(max_length=300)
+ institution = models.CharField(max_length=120)
+ department = models.CharField(max_length=90)
+ address = models.CharField(max_length=210)
+ city = models.CharField(max_length=360)
+ country = models.CharField(max_length=6)
+ class Meta:
+ db_table = u'mdl_user'
diff --git a/testapp/myauthentication/router.py b/testapp/myauthentication/router.py
new file mode 100644
index 0000000..eeb4cdb
--- /dev/null
+++ b/testapp/myauthentication/router.py
@@ -0,0 +1,12 @@
+class MyDatabaseRouter(object):
+ """
+ A router to manage database operations in the myauthentication app.
+ """
+ def db_for_read(self, model, **hints):
+ """
+ Point all read operations on myauthentication app to spoken
+ database.
+ """
+ if model._meta.app_label == 'myauthentication':
+ return 'spoken'
+ return None
diff --git a/testapp/myauthentication/tests.py b/testapp/myauthentication/tests.py
new file mode 100644
index 0000000..501deb7
--- /dev/null
+++ b/testapp/myauthentication/tests.py
@@ -0,0 +1,16 @@
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.assertEqual(1 + 1, 2)
diff --git a/testapp/myauthentication/urls.py b/testapp/myauthentication/urls.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testapp/myauthentication/urls.py
diff --git a/testapp/myauthentication/views.py b/testapp/myauthentication/views.py
new file mode 100644
index 0000000..60f00ef
--- /dev/null
+++ b/testapp/myauthentication/views.py
@@ -0,0 +1 @@
+# Create your views here.
diff --git a/testapp/settings.py b/testapp/settings.py
index 6015f0c..1d1b07d 100644
--- a/testapp/settings.py
+++ b/testapp/settings.py
@@ -23,6 +23,13 @@ SERVER_TIMEOUT = 2
# host.org/foo/exam set URL_ROOT='/foo'
URL_ROOT = ''
+# Authentication using other database table.
+# Comment the line below if you want the authentication to be done
+# using django user table.
+#AUTHENTICATION_BACKENDS = ('myauthentication.backend.MyBackend',)
+
+# Router for database
+#DATABASE_ROUTERS = ['myauthentication.router.MyDatabaseRouter',]
ADMINS = (
# ('Your Name', 'your_email@example.com'),
@@ -41,6 +48,14 @@ DATABASES = {
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
+ },
+ 'spoken' : {
+ 'ENGINE' : 'django.db.backends.mysql',
+ 'NAME' : 'YOUR DATABASE',
+ 'USER' : 'YOUR USERNAME',
+ 'PASSWORD': 'YOUR PASSWORD',
+ 'HOST' :'',
+ 'PORT' :'',
}
}