You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.5 KiB
93 lines
3.5 KiB
# -*- coding: utf-8 -*-
|
|
import logging
|
|
import traceback
|
|
|
|
from django.contrib import admin
|
|
from django.contrib import messages
|
|
from django.core.mail import mail_admins
|
|
from django.utils.text import Truncator
|
|
from django.conf import settings
|
|
|
|
from yandex_webmaster import YandexWebmaster
|
|
|
|
from .models import Category, Entry, Recipient
|
|
from .emails import mail_user_notification
|
|
from .forms import CategoryAdminForm
|
|
|
|
|
|
log = logging.getLogger('guestbook')
|
|
|
|
|
|
YANDEX_WEBMASTER_DOMAIN = getattr(settings, 'YANDEX_WEBMASTER_DOMAIN', None)
|
|
YANDEX_WEBMASTER_TOKEN = getattr(settings, 'YANDEX_WEBMASTER_TOKEN', None)
|
|
#YANDEX_WEBMASTER_TOKEN_EXPIRES = getattr(settings, 'YANDEX_WEBMASTER_TOKEN_EXPIRES', None)
|
|
|
|
YANDEX_WEBMASTER_ENABLED = YANDEX_WEBMASTER_DOMAIN and YANDEX_WEBMASTER_TOKEN
|
|
|
|
|
|
class CategoryAdmin(admin.ModelAdmin):
|
|
list_display = ['ordering', 'name', 'slug']
|
|
list_display_links = ['name']
|
|
list_editable = ['ordering']
|
|
search_fields = ['name']
|
|
form = CategoryAdminForm
|
|
|
|
|
|
class EntryAdmin(admin.ModelAdmin):
|
|
list_display = ['name', 'short_message', 'category', 'pub_date', 'created_at', 'published']
|
|
list_filter = ['published', 'category', 'created_at', 'pub_date']
|
|
list_per_page = 50
|
|
search_fields = ['name', 'email', 'message', 'reply']
|
|
readonly_fields = ['notified_user', 'added_origtext']
|
|
|
|
def short_message(self, obj):
|
|
return Truncator(obj.message).chars(500)
|
|
short_message.short_description = u'Вопрос'
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
obj.save()
|
|
|
|
# отправка уведомления
|
|
try:
|
|
fail_message = u'Не удалось отправить пользователю уведомление об ответе на его вопрос.'
|
|
fail_level = messages.WARNING
|
|
if obj.published and not obj.notified_user and obj.reply and obj.email:
|
|
result = mail_user_notification(obj.email, obj)
|
|
if result > 0:
|
|
obj.notified_user = True
|
|
obj.save()
|
|
self.message_user(request, u'Пользователю отправлено уведомление об ответе на его вопрос.')
|
|
else:
|
|
self.message_user(request, fail_message, level=fail_level)
|
|
except Exception as e:
|
|
log.info(u'Next exception for Entry with id=%s' % obj.pk)
|
|
log.exception(e)
|
|
self.message_user(request, fail_message, level=fail_level)
|
|
|
|
# отправка в оригинальные тексты
|
|
try:
|
|
if YANDEX_WEBMASTER_ENABLED and not obj.added_origtext and obj.reply:
|
|
text = (u'%s\n%s' % (obj.message, obj.reply)).strip()
|
|
text_len = len(text)
|
|
if text_len >= 500 and text_len <= 32000:
|
|
api = YandexWebmaster(YANDEX_WEBMASTER_TOKEN, YANDEX_WEBMASTER_DOMAIN)
|
|
api.add_original_text(text)
|
|
#
|
|
obj.added_origtext = True
|
|
obj.save()
|
|
except Exception as e:
|
|
log.info(u'Next exception for Entry with id=%s' % obj.pk)
|
|
log.exception(e)
|
|
mail_admins(subject=u'guestbook: original texts error',
|
|
message=u'Entry id=%s.\n\n%s' % (obj.pk, traceback.format_exc(e))
|
|
)
|
|
|
|
|
|
class RecipientAdmin(admin.ModelAdmin):
|
|
list_display = ['email']
|
|
search_fields = ['email']
|
|
|
|
|
|
admin.site.register(Category, CategoryAdmin)
|
|
admin.site.register(Entry, EntryAdmin)
|
|
admin.site.register(Recipient, RecipientAdmin)
|
|
|