keep only import topmenu_items, the_user in myslice/viewutils
[myslice.git] / auth / backend.py
1 # import the User object
2 from django.contrib.auth.models import User
3
4 import time
5
6 # Name my backend 'MyCustomBackend'
7 class MyCustomBackend:
8
9     hard_wired_users = { 'jean': '1234',
10                          'root': '2345',
11                          'jacques': '3456',
12                          }
13
14
15     # Create an authentication method
16     # This is called by the standard Django login procedure
17     def authenticate(self, token=None):
18         username=token['username']
19         password=token['password']
20         users=MyCustomBackend.hard_wired_users
21         if username not in users: return None
22         if password != users[username]: return None
23         try:
24             # Check if the user exists in Django's local database
25             user = User.objects.get(email=username)
26         except User.DoesNotExist:
27             print 'creating django user',username
28             # Create a user in Django's local database
29             # warning: the trick here is pass current time as an id, and name as email
30             # create_user(username, email=None, password=None)
31             user = User.objects.create_user(time.time(), username, 'password-doesnt-matter')
32
33         return user
34
35     # Required for your backend to work properly - unchanged in most scenarios
36     def get_user(self, user_id):
37         try:
38             return User.objects.get(pk=user_id)
39         except User.DoesNotExist:
40             return None