/jumodjango/users/admin.py

https://github.com/thatdatabaseguy/openjumo · Python · 167 lines · 128 code · 32 blank · 7 comment · 0 complexity · 7f9b571c2725ac11e3b8107fb20479fb MD5 · raw file

  1. from django import forms
  2. from django.contrib import admin
  3. from django.contrib.auth.models import Group, User as DjangoUser
  4. from django.contrib.sites.models import Site
  5. from djcelery.models import TaskState, WorkerState, IntervalSchedule, CrontabSchedule, PeriodicTask
  6. from etc import cache
  7. from issue.models import UserToIssueFollow
  8. from org.models import UserToOrgFollow
  9. from users.models import User, UserToUserFollow, Location
  10. from cust_admin.views.main import ExtChangeList
  11. ######## INLINES ########
  12. class UserFollowingInline(admin.StackedInline):
  13. model = UserToUserFollow
  14. fk_name = "follower"
  15. extra = 0
  16. raw_id_fields = ('followed',)
  17. related_field_lookups = {
  18. 'fk': ['followed']
  19. }
  20. verbose_name = "Followed Users"
  21. verbose_name_plural = "Followed Users"
  22. class OrgFollowingInline(admin.StackedInline):
  23. model = UserToOrgFollow
  24. fk_name = "user"
  25. extra = 0
  26. raw_id_fields = ('org',)
  27. related_field_lookups = {
  28. 'fk': ['org']
  29. }
  30. verbose_name = "Followed Org"
  31. verbose_name_plural = "Followed Orgs"
  32. class IssueFollowingInline(admin.StackedInline):
  33. model = UserToIssueFollow
  34. fk_name = "user"
  35. extra = 0
  36. raw_id_fields = ('issue',)
  37. related_field_lookups = {
  38. 'fk': ['issue']
  39. }
  40. verbose_name = "Followed Issue"
  41. verbose_name_plural = "Followed Issues"
  42. ######## MODEL FORM AND ADMIN ########
  43. class UserForm(forms.ModelForm):
  44. class Meta:
  45. model = User
  46. widgets = {
  47. 'facebook_id' : forms.TextInput(attrs={'class':'vTextField'}),
  48. }
  49. class UserAdmin(admin.ModelAdmin):
  50. form = UserForm
  51. #List Page Values
  52. search_fields = ['username','email', 'first_name', 'last_name']
  53. search_fields_verbose = ['Username', 'Email', 'First Name', 'Last Name']
  54. list_display = ('get_name', 'username', 'last_login','is_active', 'likely_org', 'org_probability', 'admin_classification')
  55. list_editable = ['admin_classification']
  56. list_filter = ('is_active', 'likely_org', 'admin_classification')
  57. ordering = ('username',)
  58. change_list_template = "cust_admin/change_list.html"
  59. raw_id_fields = ('location',)
  60. related_field_lookups = {
  61. 'fk': ['location']
  62. }
  63. change_list_template = "cust_admin/change_list.html"
  64. def get_changelist(self, request, **kwargs):
  65. return ExtChangeList
  66. #Change Page Values
  67. fieldsets = (
  68. ('User Profile', {
  69. 'fields': (
  70. ('username', 'is_active', 'is_superuser','is_staff'),
  71. ('first_name', 'last_name',),
  72. ('email',),
  73. ('date_joined', 'last_login',),
  74. 'bio',
  75. 'picture',
  76. ('url','blog_url',),
  77. 'gender',
  78. 'birth_year',
  79. 'location',
  80. )}),
  81. ('Settings', {
  82. 'fields':(('enable_jumo_updates',
  83. 'enable_followed_notification',
  84. 'email_stream_frequency',
  85. 'post_to_fb',),),
  86. }),
  87. ('Social Settings', {
  88. 'fields':(
  89. ('facebook_id','flickr_id','twitter_id',),
  90. ('vimeo_id','youtube_id',),
  91. )
  92. }),
  93. ("Extra Nonsense", {
  94. 'classes': ('collapse closed',),
  95. 'fields':('mongo_id','password','fb_access_token')
  96. }),
  97. )
  98. readonly_fields = ['mongo_id','fb_access_token','date_joined','last_login']
  99. inlines = [UserFollowingInline,OrgFollowingInline,IssueFollowingInline]
  100. def save_model(self, request, obj, form, change):
  101. cache.bust(obj, update=False)
  102. super(self.__class__, self).save_model(request, obj, form, change)
  103. class LocationForm(forms.ModelForm):
  104. class Meta:
  105. model = Location
  106. class LocationAdmin(admin.ModelAdmin):
  107. form = LocationForm
  108. #List Page Values
  109. search_fields = ['locality', 'region', 'country_name', 'postal_code', 'raw_geodata', 'classification']
  110. search_fields_verbose = ['City', 'State', 'Country', 'ZIP code', 'Geodata', 'Classification']
  111. list_display = ('__unicode__', 'locality', 'region', 'country_name','postal_code', 'classification')
  112. list_editable = ['classification']
  113. def get_changelist(self, request, **kwargs):
  114. return ExtChangeList
  115. change_list_template = "cust_admin/change_list.html"
  116. #Change Page Values
  117. fieldsets = (
  118. ('Geography', {
  119. 'fields': (
  120. ('__unicode__', 'locality', 'region', 'country_name','postal_code', 'classification'),
  121. )}
  122. ),
  123. )
  124. readonly_fields = ['__unicode__', 'locality', 'region', 'country_name','postal_code']
  125. admin.site.unregister(Group)
  126. admin.site.unregister(Site)
  127. admin.site.unregister(DjangoUser)
  128. admin.site.register(User, UserAdmin)
  129. admin.site.register(Location, LocationAdmin)
  130. # Unregister djcelery while we're at it
  131. admin.site.unregister(TaskState)
  132. admin.site.unregister(WorkerState)
  133. admin.site.unregister(IntervalSchedule)
  134. admin.site.unregister(CrontabSchedule)
  135. admin.site.unregister(PeriodicTask)