为django的manage.py添加自定义命令


有时候会有这样的需求,为django执行一些定时任务,比如通知搜索引擎,例如百度,提交网站的一些地址给他们,通过为djangomanage.py添加自定义命令可以很容易的解决这个问题。下面介绍下如何添加。

首先需要在创建好的应用的根目录创建文件夹名为management的目录,然后继续在该目录创建commands的目录,并在两个目录中都要创建__init__.pypython文件。 目录创建好之后继续在commands的目录中添加ping_baidu.py文件,文件名将会是manage.py的命令名. 目录结构如下: 文件目录结构
然后是代码:

from django.core.management.base import BaseCommand, CommandError
from blog.models import Article, Tag, Category
from DjangoBlog.spider_notify import sipder_notify
from django.contrib.sites.models import Site

site = Site.objects.get_current().domain


class Command(BaseCommand):
    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):
        type = options['data_type']
        self.stdout.write('start get %s' % type)
        notify = sipder_notify()
        urls = []
        if type == 'article' or type == 'all':
            for article in Article.objects.filter(status='p'):
                urls.append(article.get_full_url())
        if type == 'tag' or type == 'all':
            for tag in Tag.objects.all():
                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'))

sipder_notify也很简单:

from django.contrib.sitemaps import ping_google
import requests
from django.conf import settings


class sipder_notify():
    def baidu_notify(self, urls):
        try:
            data = '\n'.join(urls)
            result = requests.post(settings.BAIDU_NOTIFY_URL, data=data)
            print(result.text)
        except Exception as e:
            print(e)

    def __google_notify(self):
        try:
            ping_google('/sitemap.xml')
        except Exception as e:
            print(e)

    def notify(self, url):
        self.baidu_notify(url)
        self.__google_notify()

至此,基本都完成了,可以终端执行./manage.py查看输出: 输出
可以看到 ping_baidu命令已经出现了,./manage.py ping_baidu --help 可以查看帮助: 帮助 执行也很简单,终端执行: ./manage.py ping_baidu all