from django.contrib import admin import datetime from .utilities import send_activation_notification from .models import AdvUser, SuperRubric, SubRubric, Ad, AdditionalImage, Comment from .forms import SubRubricForm def send_activation_notifications(modeladmin, request, queryset): for rec in queryset: if not rec.is_activated: send_activation_notification(rec) modeladmin.message_user(request, 'Письма с требованиями отправлены') send_activation_notifications.short_description = 'Отправка писем с требованиями активации' class NonactivatedFilter(admin.SimpleListFilter): title = 'Прошли активацию?' parameter_name = 'actstate' def lookups (self, request, model_admin): return(('activated', 'Прошли'), ('threedays', 'Не прошли более 3 дней'), ('week', 'Не прошли более недели'), ) def queryset (self, request, queryset): val = self.value() if val == 'activated': return queryset.filter(is_active=True, is_activated=True) elif val == 'threedays': d = datetime.date.today() - datetime.timedelta(days=3) return queryset.filter(is_active=False, is_activated=False, date_joined__date__lt=d) elif val == 'week': d = datetime.date.today() - datetime.timedelta(weeks=1) return queryset.filter(is_active=False, is_activated=False, date_joined__date__lt=d) class AdvUserAdmin(admin.ModelAdmin): list_display = ('__str__', 'is_activated', 'date_joined') search_fields = ('username', 'email', 'first_name', 'last_name') list_filter = (NonactivatedFilter, ) fields = (('username', 'email'), ('first_name', 'last_name'), ('send_messages', 'is_active', 'is_activated'), ('is_staff', 'is_superuser'), 'groups', 'user_permissions', ('last_login', 'date_joined')) readonly_fields = ('last_login', 'date_joined') actions = (send_activation_notifications, ) admin.site.register(AdvUser, AdvUserAdmin) class SubRubricInline(admin.TabularInline): model = SubRubric class SuperRubricAdmin(admin.ModelAdmin): # При добавлении новой Главной рубрики можно сразу же заполнить ее Подрубрики с помощью втроенного редактора. exclude = ('super_rubric',) # Исключили необязательное поле для Главных рубрик inlines = (SubRubricInline,) admin.site.register(SuperRubric, SuperRubricAdmin) class SubRubricAdmin(admin.ModelAdmin): form = SubRubricForm admin.site.register(SubRubric, SubRubricAdmin) class AdditionalImageInline(admin.TabularInline): model = AdditionalImage class AdAdmin(admin.ModelAdmin): list_display = ('rubric', 'title', 'content', 'author', 'created_at') fields = ('rubric', 'author', 'title', 'content', 'price', 'contacts', 'image', 'is_active') inlines = (AdditionalImageInline, ) admin.site.register(Ad, AdAdmin) class CommentAdmin(admin.ModelAdmin): list_display = ('author', 'content', 'created_at', 'is_active') list_display_links = ('author', 'content') list_filter = ('is_active',) search_fields = ('author', 'content', ) date_hierarchy = 'created_at' fields = ('author', 'content', 'is_active', 'created_at') readonly_fields = ('created_at', ) admin.site.register(Comment, CommentAdmin)