完善自定义命令

master
车亮亮 9 years ago
parent b70f811d13
commit 6093274d68

@ -23,7 +23,7 @@ SECRET_KEY = '&3g0bdza#c%dm1lf%5gi&0-*53p3t0m*hmcvo29cn^$ji7je(c'
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True # DEBUG = True
DEBUG = False DEBUG = True
# ALLOWED_HOSTS = [] # ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['www.lylinux.net', '127.0.0.1'] ALLOWED_HOSTS = ['www.lylinux.net', '127.0.0.1']
@ -213,5 +213,5 @@ EMAIL_HOST_USER = os.environ.get('DJANGO_EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('DJANGO_EMAIL_PASSWORD') EMAIL_HOST_PASSWORD = os.environ.get('DJANGO_EMAIL_PASSWORD')
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = os.environ.get('DJANGO_EMAIL_USER') SERVER_EMAIL = os.environ.get('DJANGO_EMAIL_USER')
#设置debug=false 未处理异常邮件通知 # 设置debug=false 未处理异常邮件通知
ADMINS = [('liangliang', 'liangliangyy@gmail.com')] ADMINS = [('liangliang', 'liangliangyy@gmail.com')]

@ -12,15 +12,17 @@
@file: spider_notify.py @file: spider_notify.py
@time: 2017/1/15 下午1:41 @time: 2017/1/15 下午1:41
""" """
from django.contrib.sitemaps import ping_google from django.contrib.sitemaps import ping_google
import requests import requests
from django.conf import settings from django.conf import settings
class sipder_notify(): class sipder_notify():
def baidu_notify(self, url): def baidu_notify(self, urls):
try: try:
result = requests.post(settings.BAIDU_NOTIFY_URL, data=url) data = '\n'.join(urls)
result = requests.post(settings.BAIDU_NOTIFY_URL, data=data)
print(result.text) print(result.text)
except Exception as e: except Exception as e:
print(e) print(e)

@ -14,20 +14,41 @@
""" """
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from blog.models import Article from blog.models import Article, Tag, Category
from DjangoBlog.spider_notify import sipder_notify from DjangoBlog.spider_notify import sipder_notify
from django.contrib.sites.models import Site
site = Site.objects.get_current().domain
class Command(BaseCommand): class Command(BaseCommand):
help = 'notify baidu url' help = 'notify baidu url'
def add_arguments(self, parser):
parser.add_argument('data_type', type=str, choices=['all', 'article', 'tag', 'category'],
help='article : all article,tag : all tag,category: all category,all: All of these')
def get_full_url(self, path):
url = "https://{site}{path}".format(site=site, path=path)
return url
def handle(self, *args, **options): def handle(self, *args, **options):
type = options['data_type']
self.stdout.write('start get %s' % type)
notify = sipder_notify() notify = sipder_notify()
for article in Article.objects.filter(status='p'): urls = []
try: if type == 'article' or type == 'all':
url = article.get_full_url() for article in Article.objects.filter(status='p'):
notify.baidu_notify(url=url) urls.append(article.get_full_url())
self.stdout.write(self.style.SUCCESS('Successfully notify article id : "%s"' % article.pk)) if type == 'tag' or type == 'all':
except Exception as e: for tag in Tag.objects.all():
self.stdout.write('error:' + str(e)) url = tag.get_absolute_url()
urls.append(self.get_full_url(url))
if type == 'category' or type == 'all':
for category in Category.objects.all():
url = category.get_absolute_url()
urls.append(self.get_full_url(url))
self.stdout.write(self.style.SUCCESS('start notify %d urls' % len(urls)))
notify.baidu_notify(urls)
self.stdout.write(self.style.SUCCESS('finish notify')) self.stdout.write(self.style.SUCCESS('finish notify'))

Loading…
Cancel
Save