Statistics
| Branch: | Revision:

root / crm / robocrm / admin.py @ 4d22fe8e

History | View | Annotate | Download (2.6 KB)

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

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

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

    
30
def subscribe_to_list(first_name, last_name, email, listname):
31
  if email == '':
32
    return
33

    
34
  name = string.strip(first_name + ' ' + last_name)
35
  if name == '':
36
    from_addr = email
37
  else:
38
    from_addr = '"' + name + '" <' + email + '>'
39

    
40
  to_addr = listname + '-subscribe@lists.andrew.cmu.edu'
41

    
42
  send_mail('', '', from_addr, [to_addr])
43

    
44
class RoboUserCreationForm(ModelForm):
45
  # This is modelled directly after django.contrib.auth.forms.UserCreationForm 
46

    
47
  error_messages = UserCreationForm.error_messages
48
  username = UserCreationForm.declared_fields['username']
49
  Meta = UserCreationForm.Meta
50

    
51
  def clean_username(self):
52
    username = self.cleaned_data['username']
53
    try:
54
      User.objects.get(username=username)
55
    except User.DoesNotExist:
56
      return username
57
    raise ValidationError(self.error_messages['duplicate_username'])
58

    
59
  def save(self, commit=True):
60
    user = super(RoboUserCreationForm, self).save(commit=False)
61
    user.set_password('geek6811')
62
    if commit:
63
      user.save()
64
    subscribe_to_list(user.first_name, user.last_name, user.email, 'roboclub-gb')
65
    return user
66

    
67
class RoboUserAdmin(UserAdmin):
68
  inlines = (UserProfileInline, )
69
  add_fieldsets = (
70
      (None, {'fields': ('username',)}),
71
      ('Personal info', {
72
          'fields': ('first_name', 'last_name', 'email')}),
73
  )
74
  add_form = RoboUserCreationForm
75
  list_display = ('username', 'email', 'first_name', 'last_name')
76
  search_fields = ['username', 'email', 'first_name', 'last_name']
77

    
78

    
79
admin.site.unregister(User)
80
admin.site.register(User, RoboUserAdmin)
81
admin.site.register(Machine)
82
admin.site.register(Event)
83
admin.site.register(Project)
84
admin.site.register(RoboResource)