Revision 3e2fcd4e crm/robocrm/admin.py

View differences:

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

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

  
16
class UserAdmin(UserAdmin):
17
  inlines = (UserProfileInLine, )
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
18 58
  list_display = ('username', 'email', 'first_name', 'last_name')
19 59
  search_fields = ['username', 'email', 'first_name', 'last_name']
20 60

  
21 61

  
22 62
admin.site.unregister(User)
23
admin.site.register(User, UserAdmin)
63
admin.site.register(User, RoboUserAdmin)
24 64
admin.site.register(Machine)
25 65
admin.site.register(Event)
26 66
admin.site.register(Project)
27 67
admin.site.register(RoboResource)
28

  

Also available in: Unified diff