admin.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from django.contrib import admin
  2. import datetime
  3. from .utilities import send_activation_notification
  4. from .models import AdvUser, SuperRubric, SubRubric, Ad, AdditionalImage, Comment
  5. from .forms import SubRubricForm
  6. def send_activation_notifications(modeladmin, request, queryset):
  7. for rec in queryset:
  8. if not rec.is_activated:
  9. send_activation_notification(rec)
  10. modeladmin.message_user(request, 'Письма с требованиями отправлены')
  11. send_activation_notifications.short_description = 'Отправка писем с требованиями активации'
  12. class NonactivatedFilter(admin.SimpleListFilter):
  13. title = 'Прошли активацию?'
  14. parameter_name = 'actstate'
  15. def lookups (self, request, model_admin):
  16. return(('activated', 'Прошли'),
  17. ('threedays', 'Не прошли более 3 дней'),
  18. ('week', 'Не прошли более недели'),
  19. )
  20. def queryset (self, request, queryset):
  21. val = self.value()
  22. if val == 'activated':
  23. return queryset.filter(is_active=True, is_activated=True)
  24. elif val == 'threedays':
  25. d = datetime.date.today() - datetime.timedelta(days=3)
  26. return queryset.filter(is_active=False, is_activated=False, date_joined__date__lt=d)
  27. elif val == 'week':
  28. d = datetime.date.today() - datetime.timedelta(weeks=1)
  29. return queryset.filter(is_active=False, is_activated=False, date_joined__date__lt=d)
  30. class AdvUserAdmin(admin.ModelAdmin):
  31. list_display = ('__str__', 'is_activated', 'date_joined')
  32. search_fields = ('username', 'email', 'first_name', 'last_name')
  33. list_filter = (NonactivatedFilter, )
  34. fields = (('username', 'email'),
  35. ('first_name', 'last_name'),
  36. ('send_messages', 'is_active', 'is_activated'),
  37. ('is_staff', 'is_superuser'),
  38. 'groups',
  39. 'user_permissions',
  40. ('last_login', 'date_joined'))
  41. readonly_fields = ('last_login', 'date_joined')
  42. actions = (send_activation_notifications, )
  43. admin.site.register(AdvUser, AdvUserAdmin)
  44. class SubRubricInline(admin.TabularInline):
  45. model = SubRubric
  46. class SuperRubricAdmin(admin.ModelAdmin):
  47. # При добавлении новой Главной рубрики можно сразу же заполнить ее Подрубрики с помощью втроенного редактора.
  48. exclude = ('super_rubric',) # Исключили необязательное поле для Главных рубрик
  49. inlines = (SubRubricInline,)
  50. admin.site.register(SuperRubric, SuperRubricAdmin)
  51. class SubRubricAdmin(admin.ModelAdmin):
  52. form = SubRubricForm
  53. admin.site.register(SubRubric, SubRubricAdmin)
  54. class AdditionalImageInline(admin.TabularInline):
  55. model = AdditionalImage
  56. class AdAdmin(admin.ModelAdmin):
  57. list_display = ('rubric', 'title', 'content', 'author', 'created_at')
  58. fields = ('rubric', 'author', 'title', 'content', 'price', 'contacts', 'image', 'is_active')
  59. inlines = (AdditionalImageInline, )
  60. admin.site.register(Ad, AdAdmin)
  61. class CommentAdmin(admin.ModelAdmin):
  62. list_display = ('author', 'content', 'created_at', 'is_active')
  63. list_display_links = ('author', 'content')
  64. list_filter = ('is_active',)
  65. search_fields = ('author', 'content', )
  66. date_hierarchy = 'created_at'
  67. fields = ('author', 'content', 'is_active', 'created_at')
  68. readonly_fields = ('created_at', )
  69. admin.site.register(Comment, CommentAdmin)