Statistics
| Branch: | Revision:

root / crm / robocrm / admin.py @ 3e2fcd4e

History | View | Annotate | Download (2.14 KB)

1
from robocrm.models import Machine, Event, Project, RoboResource
2
from django.forms import ModelForm, ValidationError
3
from django.contrib import admin
4
from django.contrib.auth.admin import UserAdmin
5
from django.contrib.auth.models import User
6
from django.contrib.auth.forms import UserCreationForm
7
from robocrm.models import RoboUser
8

    
9
class UserProfileInline(admin.StackedInline):
10
  model = RoboUser
11
  can_delete = False
12
  verbose_name_plural = 'profile'
13
  filter_horizontal = ('machines',)
14

    
15
  def get_fieldsets(self, request, obj=None):
16
    if obj:
17
      # change user form
18
      return super(UserProfileInline, self).get_fieldsets(request, obj)
19
    else:
20
      # add user form
21
      return (
22
          (None, {'fields':
23
            ('cell', 'class_level', 'grad_year', 'major', 'sec_major_one',
24
              'sec_major_two', 'club_rank', 'dues_paid', 'tshirt_rec',
25
              'bench_status', 'shop_status')
26
          }),)
27

    
28
class RoboUserCreationForm(ModelForm):
29
  # This is modelled directly after django.contrib.auth.forms.UserCreationForm 
30

    
31
  error_messages = UserCreationForm.error_messages
32
  username = UserCreationForm.declared_fields['username']
33
  Meta = UserCreationForm.Meta
34

    
35
  def clean_username(self):
36
    username = self.cleaned_data['username']
37
    try:
38
      User.objects.get(username=username)
39
    except User.DoesNotExist:
40
      return username
41
    raise ValidationError(self.error_messages['duplicate_username'])
42

    
43
  def save(self, commit=True):
44
    user = super(RoboUserCreationForm, self).save(commit=False)
45
    user.set_password('geek6811')
46
    if commit:
47
      user.save()
48
    return user
49

    
50
class RoboUserAdmin(UserAdmin):
51
  inlines = (UserProfileInline, )
52
  add_fieldsets = (
53
      (None, {'fields': ('username',)}),
54
      ('Personal info', {
55
          'fields': ('first_name', 'last_name', 'email')}),
56
  )
57
  add_form = RoboUserCreationForm
58
  list_display = ('username', 'email', 'first_name', 'last_name')
59
  search_fields = ['username', 'email', 'first_name', 'last_name']
60

    
61

    
62
admin.site.unregister(User)
63
admin.site.register(User, RoboUserAdmin)
64
admin.site.register(Machine)
65
admin.site.register(Event)
66
admin.site.register(Project)
67
admin.site.register(RoboResource)