Some simple updates that allow the login page to render properly.
[philo.git] / philo / contrib / gilbert / plugins / auth.py
1 from django.conf import settings
2 from django.contrib.auth import logout
3 from django.contrib.auth.forms import PasswordChangeForm
4 from .base import Plugin
5 from ..extdirect import ext_action, ext_method
6 from ..models import UserPreferences
7
8
9 @ext_action(name='auth')
10 class Auth(Plugin):
11         @property
12         def index_js_urls(self):
13                 return super(Auth, self).index_js_urls + [
14                         settings.STATIC_URL + 'gilbert/plugins/auth.js',
15                 ]
16         
17         @property
18         def icon_names(self):
19                 return super(Auth, self).icon_names + [
20                         'user-silhouette',
21                         'mask',
22                         'key--pencil',
23                         'door-open-out',
24                 ]
25         
26         @ext_method
27         def whoami(self, request):
28                 user = request.user
29                 return user.get_full_name() or user.username
30         
31         @ext_method
32         def logout(self, request):
33                 logout(request)
34                 return True
35         
36         @ext_method
37         def get_passwd_form(self, request):
38                 return PasswordChangeForm(request.user).as_extdirect()
39         
40         @ext_method(form_handler=True)
41         def save_passwd_form(self, request):
42                 form = PasswordChangeForm(request.user, data=request.POST)
43                 if form.is_valid():
44                         form.save()
45                         return True, None
46                 else:
47                         return False, form.errors
48         
49         @ext_method
50         def get_preferences(self, request):
51                 user_preferences, created = UserPreferences.objects.get_or_create(user=request.user)
52                 
53                 return user_preferences.preferences
54         
55         @ext_method
56         def set_preferences(self, request, preferences):
57                 user_preferences, created = UserPreferences.objects.get_or_create(user=request.user)
58                 user_preferences.preferences = preferences
59                 
60                 user_preferences.save()
61                 return True
62         
63         @ext_method
64         def get_preference(self, request, key):
65                 preferences = self.get_preferences(request)
66                 return preferences.get(key, None)
67         
68         @ext_method
69         def set_preference(self, request, key, value):
70                 preferences = self.get_preferences(request)
71                 preferences[key] = value
72                 return self.set_preferences(request, preferences)