Skip to content

Commit d940f9f

Browse files
Improved admin view and User form
1 parent 12c9db5 commit d940f9f

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

Diff for: account/admin.py

+39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from django.contrib import admin
2+
from django.contrib.auth.admin import UserAdmin
3+
from django.contrib.auth.models import User
4+
from django.utils.translation import ugettext_lazy as _
25
from simple_history.admin import SimpleHistoryAdmin
36

7+
from user_resource.models import ContactNumber, InstituteAddress, Program, SecondaryEmail
8+
49
from .models import UserProfile
510

611

@@ -10,4 +15,38 @@ class UserProfileAdmin(SimpleHistoryAdmin):
1015
search_fields = ['user__username', 'user__first_name', 'roll_number']
1116

1217

18+
class InstituteAddressInline(admin.TabularInline):
19+
model = InstituteAddress
20+
21+
22+
class UserProfileInline(admin.StackedInline):
23+
model = UserProfile
24+
25+
26+
class ProgramInline(admin.StackedInline):
27+
model = Program
28+
29+
30+
class ContactInline(admin.TabularInline):
31+
model = ContactNumber
32+
33+
34+
class SecondaryEmailInline(admin.TabularInline):
35+
model = SecondaryEmail
36+
37+
38+
class CustomUserAdmin(UserAdmin):
39+
fieldsets = (
40+
(None, {'fields': ('username', 'password')}),
41+
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
42+
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
43+
)
44+
45+
readonly_fields = ('username', 'first_name', 'last_name', 'email', 'last_login', 'date_joined')
46+
47+
inlines = [InstituteAddressInline, UserProfileInline, ProgramInline, ContactInline, SecondaryEmailInline]
48+
49+
50+
admin.site.unregister(User)
51+
admin.site.register(User, CustomUserAdmin)
1352
admin.site.register(UserProfile, UserProfileAdmin)

Diff for: application/forms.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from django import forms
22

3+
from core.utils import get_choices_with_blank_dash
4+
35
from .models import Application
46

57

68
class RegistrationForm(forms.ModelForm):
79
name = forms.CharField(max_length=255, widget=forms.TextInput(attrs={'class': 'form-control'}))
10+
authorization_grant_type = forms.ChoiceField(choices=get_choices_with_blank_dash(Application.GRANT_TYPES[:2]),
11+
widget=forms.Select(attrs={'class': 'form-control'}))
812

913
class Meta:
1014
model = Application
@@ -33,9 +37,6 @@ class Meta:
3337
'client_type': forms.Select(
3438
attrs={'class': 'form-control', }
3539
),
36-
'authorization_grant_type': forms.Select(
37-
attrs={'class': 'form-control', }
38-
),
3940
'redirect_uris': forms.Textarea(
4041
attrs={'class': 'form-control', 'rows': 4, }
4142
)

Diff for: core/utils.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import collections
22

3-
import six
3+
import django.utils.six as six
44
from django.conf import settings
55
from django.core.urlresolvers import reverse
66
from django.db.models import Model
@@ -34,14 +34,16 @@ def attr_to_dict(instance, key=None):
3434
return {key: instance}
3535

3636

37+
def get_choices_with_blank_dash(choices):
38+
return BLANK_CHOICE_DASH + list(choices)
39+
40+
3741
SEXES = [
3842
('male', 'Male'),
3943
('female', 'Female'),
4044
('other', 'Other'),
4145
]
4246

43-
BLANK_SEXES = BLANK_CHOICE_DASH + SEXES
44-
4547
DISCIPLINES = [
4648
# Departments
4749
['AE', 'Aerospace Engineering'],

Diff for: user_resource/forms.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.template.defaultfilters import filesizeformat
33
from django.utils.translation import ugettext_lazy as _
44

5-
from core.utils import BLANK_SEXES
5+
from core.utils import SEXES, get_choices_with_blank_dash
66

77
from .models import InstituteAddress, Program
88

@@ -23,7 +23,7 @@ def clean_profile_picture(self):
2323

2424

2525
class SexUpdateForm(forms.Form):
26-
sex = forms.ChoiceField(choices=BLANK_SEXES, required=False,
26+
sex = forms.ChoiceField(choices=get_choices_with_blank_dash(SEXES), required=False,
2727
widget=forms.Select(
2828
attrs={'class': 'form-control', },
2929
))
@@ -78,4 +78,5 @@ class Meta:
7878
}
7979
labels = {
8080
'department': 'Discipline',
81+
'graduation_year': '(Expected) Graduation Year',
8182
}

Diff for: user_resource/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Program(models.Model):
5151
_history_ = HistoricalRecords()
5252

5353
def __str__(self):
54-
return "%s, %s" % (self.degree, self.department)
54+
return "%s, %s" % (self.get_degree_display(), self.get_department_display())
5555

5656

5757
@python_2_unicode_compatible

0 commit comments

Comments
 (0)