Django 添加自定義命令

使用Django開(kāi)發(fā)偏塞,對(duì) python manage.py *** 命令模式肯定不會(huì)陌生施逾。比較常用的有 runservermigrate等呈野!

有時(shí)候會(huì)有這樣的需求低矮,為 Django 執(zhí)行一些定時(shí)任務(wù),比如通知搜索引擎被冒,例如百度军掂,提交網(wǎng)站的一些地址給他們,則可以通過(guò)為 Djangomanage.py 添加自定義命令可以很容易的解決這個(gè)問(wèn)題昨悼。

所以我們就來(lái)講講如何自定義擴(kuò)展manage命令蝗锥。

源碼分析

manage.py 文件是通過(guò) django-admin startproject project_name 生成的。

  1. manage.py的源碼

    • 首先設(shè)置了 settings 文件

    • 其次執(zhí)行了一個(gè)函數(shù)django.core.management.execute_from_command_line(sys.argv)率触,這個(gè)函數(shù)傳入了命令行參數(shù) sys.argv

      #!/usr/bin/env python
      import os
      import sys
          
      if __name__ == "__main__":
          os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CIServer.settings")
          try:
              from django.core.management import execute_from_command_line
          except ImportError:
              raise ImportError(
                  "Couldn't import Django. Are you sure it's installed and available "
                  "on your PATH environment variable? Did you forget to activate a "
                  "virtual environment?"
              )
          execute_from_command_line(sys.argv)
      
  2. execute_from_command_line

    里面調(diào)用了ManagementUtility類中的execute方法

    def execute_from_command_line(argv=None):
        """
        A simple method that runs a ManagementUtility.
        """
        utility = ManagementUtility(argv)
        utility.execute()
    

    execute 中主要是解析了傳入的參數(shù) sys.argv 终议,并且調(diào)用了get_command()

  3. get_command

    def get_commands():
        """
        Returns a dictionary mapping command names to their callback applications.
    
        This works by looking for a management.commands package in django.core, and
        in each installed application -- if a commands package exists, all commands
        in that package are registered.
    
        Core commands are always included. If a settings module has been
        specified, user-defined commands will also be included.
    
        The dictionary is in the format {command_name: app_name}. Key-value
        pairs from this dictionary can then be used in calls to
        load_command_class(app_name, command_name)
    
        If a specific version of a command must be loaded (e.g., with the
        startapp command), the instantiated module can be placed in the
        dictionary in place of the application name.
    
        The dictionary is cached on the first call and reused on subsequent
        calls.
        """
        commands = {name: 'django.core' for name in find_commands(upath(__path__[0]))}
    
        if not settings.configured:
            return commands
    
        for app_config in reversed(list(apps.get_app_configs())):
            path = os.path.join(app_config.path, 'management')
            commands.update({name: app_config.name for name in find_commands(path)})
    
        return commands
    

    get_command 里遍歷所有注冊(cè)的 INSTALLED_APPS 路徑下的management 尋找 (find_commands) 用戶自定義的命令。

    def find_commands(management_dir):
        """
        Given a path to a management directory, returns a list of all the command
        names that are available.
    
        Returns an empty list if no commands are defined.
        """
        command_dir = os.path.join(management_dir, 'commands')
        # Workaround for a Python 3.2 bug with pkgutil.iter_modules
        sys.path_importer_cache.pop(command_dir, None)
        return [name for _, name, is_pkg in pkgutil.iter_modules([npath(command_dir)])
                if not is_pkg and not name.startswith('_')]
    

    可以發(fā)現(xiàn)并注冊(cè)的命令是commands目錄下不以"_"開(kāi)頭的文件名。

  4. load_command_class

    將命令文件***.py中的Command類加載進(jìn)去穴张。

    def load_command_class(app_name, name):
        """
        Given a command name and an application name, returns the Command
        class instance. All errors raised by the import process
        (ImportError, AttributeError) are allowed to propagate.
        """
        module = import_module('%s.management.commands.%s' % (app_name, name))
        return module.Command()
    
  5. Command

    Command 類要繼承 BaseCommand 類细燎,其中很多方法,一定要實(shí)現(xiàn)的是 handle 方法皂甘,handle 方法是命令實(shí)際執(zhí)行的代碼玻驻。

具體實(shí)現(xiàn)

根據(jù)上面說(shuō)的原理,我們只需要在創(chuàng)建好的應(yīng)用的根目錄創(chuàng)建文件夾名為 management 的目錄偿枕,然后繼續(xù)在該目錄創(chuàng)建 commands 的目錄璧瞬,并在兩個(gè)目錄中都要?jiǎng)?chuàng)建__init__.py 的 python 文件。 目錄創(chuàng)建好之后繼續(xù)在commands 的目錄中添加 ping_baidu.py 文件益老,文件名將會(huì)是 manage.py 的命令名. 目錄結(jié)構(gòu)如下:

(python3) ?  blog tree   
.
├── __init__.py
└── management
    ├── __init__.py
    └── commands
        ├── __init__.py 
        └── ping_baidu.py

ping_baidu.py 中實(shí)現(xiàn)命令的具體內(nèi)容

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.py 也很簡(jiǎn)單:

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


class SpiderNotify():
    //提交百度統(tǒng)計(jì)
    @staticmethod
    def baidu_notify(urls):
        try:
            data = '\n'.join(urls)
            result = requests.post(settings.BAIDU_NOTIFY_URL, data=data)
            print(result.text)
        except Exception as e:
            print(e)
    //熊掌號(hào)接入
    @staticmethod
    def baidu_bear_notify(urls):
        try:
            data = '\n'.join(urls)
            result = requests.post(settings.BAIDU_BEAR_NOTIFY_URL, data=data)
            print(result.text)
        except Exception as e:
            print(e)
    //提交到谷歌
    @staticmethod
    def __google_notify():
        try:
            ping_google('/sitemap.xml')
        except Exception as e:
            print(e)

    @staticmethod
    def notify(url):

        SpiderNotify.baidu_notify(url)
        SpiderNotify.__google_notify()
        SpiderNotify.baidu_bear_notify(url)    

至此彪蓬,基本都完成了,可以終端執(zhí)行./manage.py查看輸出:

(python3) ?  DjangoBlog ./manage.py 

Type 'manage.py help <subcommand>' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[blog]
    ping_baidu

可以看到 ping_baidu 命令已經(jīng)出現(xiàn)了捺萌,./manage.py ping_baidu --help 可以查看幫助:

(python3) ?  DjangoBlog ./manage.py ping_baidu --help
usage: manage.py ping_baidu [-h] [--version] [-v {0,1,2,3}]
                            [--settings SETTINGS] [--pythonpath PYTHONPATH]
                            [--traceback] [--no-color]
                            {all,article,tag,category}

notify baidu url

positional arguments:
  {all,article,tag,category}
                        article : all article,tag : all tag,category: all
                        category,all: All of these

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.

最后在終端執(zhí)行: ./manage.py ping_baidu all 即可档冬。

此文章同時(shí)同步到我的個(gè)人博客緣來(lái)來(lái)來(lái) ? Django 添加自定義命令](https://www.fkomm.cn/article/2018/10/17/55.html)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市桃纯,隨后出現(xiàn)的幾起案子酷誓,更是在濱河造成了極大的恐慌,老刑警劉巖态坦,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盐数,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡伞梯,警方通過(guò)查閱死者的電腦和手機(jī)玫氢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)谜诫,“玉大人漾峡,你說(shuō)我怎么就攤上這事∮骺酰” “怎么了生逸?”我有些...
    開(kāi)封第一講書人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)且预。 經(jīng)常有香客問(wèn)我槽袄,道長(zhǎng),這世上最難降的妖魔是什么锋谐? 我笑而不...
    開(kāi)封第一講書人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任遍尺,我火速辦了婚禮,結(jié)果婚禮上怀估,老公的妹妹穿的比我還像新娘狮鸭。我一直安慰自己合搅,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布歧蕉。 她就那樣靜靜地躺著灾部,像睡著了一般。 火紅的嫁衣襯著肌膚如雪惯退。 梳的紋絲不亂的頭發(fā)上赌髓,一...
    開(kāi)封第一講書人閱讀 49,144評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音催跪,去河邊找鬼锁蠕。 笑死,一個(gè)胖子當(dāng)著我的面吹牛懊蒸,可吹牛的內(nèi)容都是我干的荣倾。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼骑丸,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼舌仍!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起通危,我...
    開(kāi)封第一講書人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤铸豁,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后菊碟,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體节芥,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年逆害,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了头镊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡魄幕,死狀恐怖拧晕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情梅垄,我是刑警寧澤,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布输玷,位于F島的核電站队丝,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏欲鹏。R本人自食惡果不足惜机久,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望赔嚎。 院中可真熱鬧膘盖,春花似錦胧弛、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至软棺,卻和暖如春红竭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背喘落。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工茵宪, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瘦棋。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓稀火,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親赌朋。 傳聞我的和親對(duì)象是個(gè)殘疾皇子凰狞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容