项目功能介绍

1.环境准备


前端:购买的INSPINIA完整版
django==2.2.3
python==3.6.3
pymysql==0.9.3
djangorestframework==3.10.0
1.把本地代码上传到linux上
https://blog.51cto.com/10983441/2380368
最后一条1.11配置,本地代码上传到linux上

2.linux系统
python安装
cd /opt
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
tar xf Python-3.6.3.tar.xz
cd /opt/Python-3.6.3/
./configure
yum install gcc-c++ gcc -y  #安装编译所需依赖
make  && make install

3.配置环境变量
cat /etc/profile
export PATH=$PATH:/usr/local/python3/bin/
生效
source /etc/profile

接下来就可以验证
[root@m01 opt]# python3
Python 3.6.3 (default, Jul 15 2019, 09:46:16) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[root@m01 opt]# pip3

Usage:   
  pip <command> [options]

4.连接mysql报错信息处理方法
 raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决方法
注释掉下面内容
#if version < (1, 3, 13):
#    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

File "/usr/local/python3/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query
    query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
解决方法:
把decode改为encode()
query = query.encode(errors='replace')

5.生成数据,在Linux上操作,windows上操作会报错
[root@m01 CMDB]# pwd
/project/CMDB
[root@m01 CMDB]# python3 manage.py makemigrations
[root@m01 CMDB]# python3 manage.py migrate

2.功能解析

2.1自定义用户认证

参考https://docs.djangoproject.com/zh-hans/2.1/topics/auth/customizing/
由于我后来为用户添加了头像字段,所以稍有更改,可以查看项目代码

models.py
from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)

class MyUserManager(BaseUserManager):
    def create_user(self, email, name, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
            name=name,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, name, password):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(
            email,
            password=password,
            name=name,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    name = models.CharField(max_length=32)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin
 admin.py
 from django.contrib import admin

# Register your models here.
from django import forms
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField

from web.models import MyUser

class UserCreationForm(forms.ModelForm):
    """A form for creating new users. Includes all the required
    fields, plus a repeated password."""
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = MyUser
        fields = ('email', 'name')

    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user

class UserChangeForm(forms.ModelForm):
    """A form for updating users. Includes all the fields on
    the user, but replaces the password field with admin's
    password hash display field.
    """
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = MyUser
        fields = ('email', 'password', 'name', 'is_active', 'is_admin')

    def clean_password(self):
        # Regardless of what the user provides, return the initial value.
        # This is done here, rather than on the field, because the
        # field does not have access to the initial value
        return self.initial["password"]

class UserAdmin(BaseUserAdmin):
    # The forms to add and change user instances
    form = UserChangeForm
    add_form = UserCreationForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
    list_display = ('email', 'name', 'is_admin')
    list_filter = ('is_admin',)
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ('name',)}),
        ('Permissions', {'fields': ('is_admin',)}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    # 创建用户时,列出的需要添加的字段信息
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'name', 'password1', 'password2')}
        ),
    )
    # 搜索字段
    search_fields = ('email',)
    # 排序字段
    ordering = ('email',)
    filter_horizontal = ()

# Now register the new UserAdmin...
admin.site.register(MyUser, UserAdmin)
# ... and, since we're not using Django's built-in permissions,
# unregister the Group model from admin.
admin.site.unregister(Group)
settings.py
# 由于自己重写了user表,所以需要在这里添加一条配置
AUTH_USER_MODEL = 'web.MyUser'
效果
[root@m01 CMDB]# python3 manage.py createsuperuser
(0.000) b'SELECT @@SQL_AUTO_IS_NULL'; args=None
(0.000) b'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED'; args=None
(0.001) b'SHOW FULL TABLES'; args=None
(0.001) b'SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`'; args=()
#输入邮箱
Email address: admin@qq.com

(0.001) b"SELECT `web_myuser`.`id`, `web_myuser`.`password`, `web_myuser`.`last_login`, `web_myuser`.`email`, `web_myuser`.`name`, `web_myuser`.`is_active`, `web_myuser`.`is_admin` FROM `web_myuser` WHERE `web_myuser`.`email` = 'admin@qq.com'"; args=('admin@qq.com',)
#输入用户名和密码
Name: admin
Password: 
Password (again): 

This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y

(0.006) b"INSERT INTO `web_myuser` (`password`, `last_login`, `email`, `name`, `is_active`, `is_admin`) VALUES ('pbkdf2_sha256$150000$UTuxK141CULH$MNQ4TR8iwU7pVl8pEX03eb9KmEASze+4CoNK8d1PaCc=', NULL, 'admin@qq.com', 'admin', 1, 0)"; args=['pbkdf2_sha256$150000$UTuxK141CULH$MNQ4TR8iwU7pVl8pEX03eb9KmEASze+4CoNK8d1PaCc=', None, 'admin@qq.com', 'admin', True, False]
(0.003) b"UPDATE `web_myuser` SET `password` = 'pbkdf2_sha256$150000$UTuxK141CULH$MNQ4TR8iwU7pVl8pEX03eb9KmEASze+4CoNK8d1PaCc=', `last_login` = NULL, `email` = 'admin@qq.com', `name` = 'admin', `is_active` = 1, `is_admin` = 1 WHERE `web_myuser`.`id` = 1"; args=('pbkdf2_sha256$150000$UTuxK141CULH$MNQ4TR8iwU7pVl8pEX03eb9KmEASze+4CoNK8d1PaCc=', 'admin@qq.com', 'admin', True, True, 1)
Superuser created successfully.

项目功能介绍
项目功能介绍

项目功能介绍

注意:
这里通过python manage.py createsuperuser 创建的是管理员账户,可以登录后台管理平台
其他新建的或自己注册的账户,默认是非管理员账户。

2.2restframwork

http://10.0.0.61:8000/api/
登录用户是上面注册的admin@qq.com/123

2.2.1查看数据

项目功能介绍

2.2.2post发送数据

项目功能介绍
项目功能介绍

项目功能介绍
项目功能介绍

项目功能介绍

2.2.3关联表插入数据

项目功能介绍

项目功能介绍

项目功能介绍

项目功能介绍

如果是
总结:
提交的数据形式与下面的形式相同

项目功能介绍

2.3角色权限

这里的角色权限是我自己设计的一种方式
对于左侧的菜单栏,通过给角色设置相应的parent_menu和child_munu和URL信息,
可以限制每个用户可以看到哪些菜单

至于页面中的按钮操作权限,是通过Privilege表控制的,角色与权限是多对多的关系
在页面中通过自定义过滤器操作的

项目功能介绍
项目功能介绍
项目功能介绍

2.4前后端传值

2.4.1前端cookie传入中文,后端解码

前端
jquery
{#    $.cookie需要该js#}
    <script src="/static/js/jquery.cookie.js"></script>

不编码
$.cookie('role_search', '测试', { path: '/' });

后端
from urllib.parse import unquote
print(search_val)
print(unquote(search_val, "utf-8"))# 测试
print(unquote(unquote(search_val,"utf-8")))# 测试

前端
编码
$.cookie('role_search', encodeURIComponent('测试'), { path: '/' });
解码
$("#search").val(decodeURIComponent(role_search));

后端
from urllib.parse import unquote
print(search_val)#'%25E6%25B5%258B%25E8%25AF%2595'
print(unquote(search_val, "utf-8"))# %E6%B5%8B%E8%AF%95
print(unquote(unquote(search_val,"utf-8")))# 测试

2.4.2前端传递多层字典(即object类型),后端解析

前端值类型
{"10.0.0.61":[1,2,3]}
前端要使用Json.Stringfy()进行序列化
后端从body中获取值

   $(function () {
        $(".start-server").click(function () {
             let host_ip_app_info = {};
            $("li.application[aria-selected='true']").each(function () {

                let app_id = $(this).attr("value");
                let ip = $(this).attr("host");
                if (host_ip_app_info[ip]){
                    host_ip_app_info[ip].push(app_id)
                }else{
                    host_ip_app_info[ip]=[app_id]

                }
            });
            console.log("----------------",host_ip_app_info);
            $.ajax({
                url:'{% url "task_manage:start_server" %}',
                type:'post',
                headers:{'X-CSRFtoken':$.cookie("csrftoken")},
                data:JSON.stringify({
                    host_ip_app_info:host_ip_app_info
                }),
                success:function (res) {

                }
            })
        })

    })

后端解析
def StartServer(request):
    print("------------------------",json.loads(request.body.decode()).get("host_ip_app_info"))
    # {'10.0.0.63': ['1', '2'], '10.0.0.61': ['1']}

    return JsonResponse({})

2.5celery定时任务

2.5.1celery软件架构

项目功能介绍

相关模块
# kombu版本
pip install kombu==4.2.0
# celery版本
pip install celery==4.1.1
版本不对,会报错
Django中解决redis-py versions 3.2.0 or later. You have 2.10.6版本问题
No module named 'kombu.matcher'

[root@m01 CMDB]# pip3 install celery
[root@m01 CMDB]# pip3 install django-celery-beat
[root@m01 CMDB]# pip3 install django-celery-results

注意:
celery命令,是pip install celery后生成的,默认放在了python安装路径下/usr/local/python3/bin/celery 
需要手动建立连接:ln -s /usr/local/python3/bin/celery  /usr/bin/celery 这样就可以直接使用了

项目功能介绍

2.5.2celery模块介绍

任务模块 Task.py
包含异步任务和定时任务
异步任务:如果不想让程序等着结果返回,而是返回一个任务ID,过一段时间根据task_id获取任务的结果。
周期任务:根据设置的执行周期,定时周期性执行任务

消息中间件模块 Broker
app = Celery('tasks',backend='redis://10.0.0.61:6379/1',broker='redis://localhost:6379/0')
Broker,即为任务调度队列,接收任务生产者发来的消息(任务),将任务存入队列。
Celery本身不提供队列服务,官方推荐使用RabbitMQ和Redis等。

任务执行模块 Worker
[root@m01 CMDB]# celery -A CMDB worker -l info
#CMDB是Django项目名,或py文件的文件名
Worker是执行任务的处理单元,它实时监控消息队列,获取队列中的调度任务,并执行它。

任务结果存储模块 Backend
app = Celery('tasks',backend='redis://10.0.0.61:6379/1',broker='redis://localhost:6379/0')
Backend用于存储任务的执行结果,以供查询。
同消息中间件一样,存储也可以使用RabbitMQ或Redis或MongoDB等数据库

周期任务模块 Beat
[root@m01 CMDB]# celery -A CMDB beat
用于监控周期任务,把任务放到消息中间件broker中
worker监控到了broker中的任务,就会执行任务

2.5.3celery异步任务1-没有设置backend

1.进入到py文件所在目录
[root@m01 project]# pwd
/project

2.查看写好的异步程序
[root@m01 project]# cat sync.py 
#/usr/bin/python3
from celery import Celery,shared_task

app = Celery('tasks', broker='redis://10.0.0.61:6379/1')

@shared_task(name="add")
def add(x, y):
    return x + y
[root@m01 project]# 

3.手动把异步任务放到消息队列中
[root@m01 project]# python3
Python 3.6.3 (default, Jul 15 2019, 09:46:16) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sync import add
>>> add.delay(2,3)   
<AsyncResult: 57ddfa56-8e80-448a-b912-243ce44fef70>

4.另打开一个xshell窗口,执行下面的命令,调用worker执行异步任务
[root@m01 project]# celery -A sync worker --loglevel=info
/usr/local/python3/lib/python3.6/site-packages/celery/platforms.py:801: RuntimeWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the --uid option.

User information: uid=0 euid=0 gid=0 egid=0

  uid=uid, euid=euid, gid=gid, egid=egid,

 -------------- celery@m01 v4.3.0 (rhubarb)
---- **** ----- 
--- * ***  * -- Linux-2.6.32-696.el6.x86_64-x86_64-with-centos-6.9-Final 2019-07-18 14:45:06
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         tasks:0x7f226e9250b8
- ** ---------- .> transport:   redis://10.0.0.61:6379/1
- ** ---------- .> results:     redis://10.0.0.61:6379/1
- *** --- * --- .> concurrency: 2 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

[tasks]
  . add

[2019-07-18 14:45:07,042: INFO/MainProcess] Connected to redis://10.0.0.61:6379/1
[2019-07-18 14:45:07,050: INFO/MainProcess] mingle: searching for neighbors
[2019-07-18 14:45:08,082: INFO/MainProcess] mingle: all alone
[2019-07-18 14:45:08,091: INFO/MainProcess] celery@m01 ready.
[2019-07-18 14:45:29,634: INFO/MainProcess] Received task: add[74d07c57-b74d-4ded-9831-73ec4f739940]  
[2019-07-18 14:45:29,649: INFO/ForkPoolWorker-1] Task add[74d07c57-b74d-4ded-9831-73ec4f739940] 
succeeded in 0.012957275001099333s: 5     #返回结果

2.5.4celery异步任务2-设置backend

1.进入到py文件所在目录
[root@m01 project]# pwd
/project

2.程序代码
[root@m01 project]# cat sync2.py 
#/usr/bin/python3
from celery import Celery,shared_task

app = Celery('tasks', backend='redis://10.0.0.61:6379/1',broker='redis://10.0.0.61:6379/1')

@shared_task(name="add")
def add(x, y):
    return x + y
[root@m01 project]# 

3.没有启动worker时,调用异步任务
[root@m01 project]# python3
Python 3.6.3 (default, Jul 15 2019, 09:46:16) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sync2 import add
>>> add.delay(2,3)   
<AsyncResult: 57ddfa56-8e80-448a-b912-243ce44fef70> # 任务ID
>>> re=add.delay(2,3)    
>>> re.ready() # 只有设置了backend才能调用ready()。orker没有启动,任务没有执行时,返回值为False
False
>>> re.get(timeout=1)  #worker没有启动,任务没有执行时,超时时间过了会报错
Traceback (most recent call last):
  File "/usr/local/python3/lib/python3.6/site-packages/celery/backends/asynchronous.py", line 255, in _wait_for_pending
    on_interval=on_interval):
  File "/usr/local/python3/lib/python3.6/site-packages/celery/backends/asynchronous.py", line 54, in drain_events_until
    raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3/lib/python3.6/site-packages/celery/result.py", line 226, in get
    on_message=on_message,
  File "/usr/local/python3/lib/python3.6/site-packages/celery/backends/asynchronous.py", line 188, in wait_for_pending
    for _ in self._wait_for_pending(result, **kwargs):
  File "/usr/local/python3/lib/python3.6/site-packages/celery/backends/asynchronous.py", line 259, in _wait_for_pending
    raise TimeoutError('The operation timed out.')
celery.exceptions.TimeoutError: The operation timed out.
>>> re.get() #没有启动worker,会阻塞在这里,等待结果

4.启动worker
[root@m01 project]# celery -A sync2 worker --loglevel=info
/usr/local/python3/lib/python3.6/site-packages/celery/platforms.py:801: RuntimeWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the --uid option.

User information: uid=0 euid=0 gid=0 egid=0

  uid=uid, euid=euid, gid=gid, egid=egid,

 -------------- celery@m01 v4.3.0 (rhubarb)
---- **** ----- 
--- * ***  * -- Linux-2.6.32-696.el6.x86_64-x86_64-with-centos-6.9-Final 2019-07-18 14:45:06
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         tasks:0x7f226e9250b8
- ** ---------- .> transport:   redis://10.0.0.61:6379/1
- ** ---------- .> results:     redis://10.0.0.61:6379/1
- *** --- * --- .> concurrency: 2 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

[tasks]
  . add

[2019-07-18 14:45:07,042: INFO/MainProcess] Connected to redis://10.0.0.61:6379/1
[2019-07-18 14:45:07,050: INFO/MainProcess] mingle: searching for neighbors
[2019-07-18 14:45:08,082: INFO/MainProcess] mingle: all alone
[2019-07-18 14:45:08,091: INFO/MainProcess] celery@m01 ready.
[2019-07-18 14:45:29,634: INFO/MainProcess] Received task: add[74d07c57-b74d-4ded-9831-73ec4f739940]  
[2019-07-18 14:45:29,649: INFO/ForkPoolWorker-1] Task add[74d07c57-b74d-4ded-9831-73ec4f739940] 
succeeded in 0.012957275001099333s: 5

5.调用处可以查看到结果
>>> re.get()
5
>>> re.get(propagate=False)
5

6.异步任务根据task_id获取任务结果,只有设置了backend才能调用
>>> add.delay(1,2)  
<AsyncResult: 57ddfa56-8e80-448a-b912-243ce44fef70>
>>> from celery.result import AsyncResult

>>> res=AsyncResult("57ddfa56-8e80-448a-b912-243ce44fef70")
>>> re.result
3

7.redis中查看结果

项目功能介绍

2.5.5celery周期任务

项目功能介绍
项目功能介绍
项目功能介绍
项目功能介绍
项目功能介绍
项目功能介绍

1.创建result表
[root@m01 project]# cd /tmp/untitled/
[root@m01 untitled]# ll
total 60
-rw-r--r-- 1 root root    89 Jul 18 13:13 celerybeat-schedule.bak
-rw-r--r-- 1 root root  8731 Jul 18 13:13 celerybeat-schedule.dat
-rw-r--r-- 1 root root    89 Jul 18 13:13 celerybeat-schedule.dir
-rw-r--r-- 1 root root 18432 Jul 16  2018 db.sqlite3
-rw-r--r-- 1 root root   540 Jul 16  2018 manage.py
-rw-r--r-- 1 root root  2862 Jul 18 15:54 readme.md
-rw-r--r-- 1 root root   188 Jul 18 14:46 te.py
drwxr-xr-x 4 root root  4096 Jul 18 13:05 testcelery
drwxr-xr-x 3 root root  4096 Jul 18 13:02 untitled
# 创建result表
[root@m01 untitled]# python3 manage.py migrate

2.启动worker,监听队列中的任务,并执行任务
[root@m01 untitled]# celery -A untitled worker -l info
/usr/local/python3/lib/python3.6/site-packages/celery/platforms.py:801: RuntimeWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the --uid option.

User information: uid=0 euid=0 gid=0 egid=0

  uid=uid, euid=euid, gid=gid, egid=egid,

 -------------- celery@m01 v4.3.0 (rhubarb)
---- **** ----- 
--- * ***  * -- Linux-2.6.32-696.el6.x86_64-x86_64-with-centos-6.9-Final 2019-07-18 13:13:52
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         untitled:0x7fa14a5312e8
- ** ---------- .> transport:   redis://10.0.0.61:6379/2
- ** ---------- .> results:     
- *** --- * --- .> concurrency: 2 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

[tasks]
  . printqw

[2019-07-18 13:13:52,288: INFO/MainProcess] Connected to redis://10.0.0.61:6379/2
[2019-07-18 13:13:52,297: INFO/MainProcess] mingle: searching for neighbors
[2019-07-18 13:13:53,319: INFO/MainProcess] mingle: all alone
[2019-07-18 13:13:53,330: WARNING/MainProcess] /usr/local/python3/lib/python3.6/site-packages/celery/fixups/django.py:202: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2019-07-18 13:13:53,331: INFO/MainProcess] celery@m01 ready.
[2019-07-18 13:13:53,448: INFO/MainProcess] Received task: printqw[e3d1792f-7628-4861-9fc7-05fd8e7ce537]  
[2019-07-18 13:13:53,469: INFO/ForkPoolWorker-1] Task printqw[e3d1792f-7628-4861-9fc7-05fd8e7ce537] succeeded in 0.01883281399932457s: 'hello celery and django...'
[2019-07-18 13:13:53,969: INFO/MainProcess] Received task: printqw[b874cb79-4aac-4b09-a9f8-5906bf09326b]  
[2019-07-18 13:13:53,979: INFO/ForkPoolWorker-1] Task printqw[b874cb79-4aac-4b09-a9f8-5906bf09326b] succeeded in 0.009641711996664526s: 'hello celery and django...'

3.启动beat,周期性把任务放到队列中
[root@m01 untitled]# celery -A untitled beat -l info
celery beat v4.3.0 (rhubarb) is starting.
__    -    ... __   -        _
LocalTime -> 2019-07-18 13:13:48
Configuration ->
    . broker -> redis://10.0.0.61:6379/2
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%INFO
    . maxinterval -> 5.00 minutes (300s)
[2019-07-18 13:13:48,957: INFO/MainProcess] beat: Starting...
[2019-07-18 13:13:48,978: INFO/MainProcess] Scheduler: Sending due task task-one (printqw)
[2019-07-18 13:13:53,967: INFO/MainProcess] Scheduler: Sending due task task-one (printqw)

4.任务结果

项目功能介绍

2.5.6celery周期任务之admin配置

项目功能介绍
项目功能介绍
项目功能介绍
项目功能介绍
项目功能介绍

task有修改,需要重新启动

[root@m01 CMDB]# celery -A CMDB worker --loglevel=info >/project/celery_work.log 2>&1 &
[1] 7071
[root@m01 CMDB]# celery -A CMDB beat --loglevel=info >/project/celery_beat.log 2>&1 &
[2] 7077

项目功能介绍
项目功能介绍
项目功能介绍
项目功能介绍
项目功能介绍


# 创建表结构
[root@m01 CMDB]# python3 manage.py migrate

[root@m01 CMDB]# pwd
/project/CMDB
# 启动worker,监听队列中的任务,执行任务
[root@m01 CMDB]# celery -A CMDB worker -l info
# 周期性的吧任务放到队列中
[root@m01 CMDB]# celery -A CMDB beat

2.6/login/访问频率限制

models.py

class AccessLog(models.Model):
    """
    用户访问的记录表
    """
    id = models.AutoField(primary_key=True)
    # user_email = models.CharField(verbose_name="访问者邮箱", max_length=32)
    remote_ip = models.CharField(verbose_name="访问者IP地址", max_length=32)
    request_path = models.CharField(verbose_name="访问的地址", max_length=255, default="/")
    access_time = models.DateTimeField(verbose_name="访问时间", auto_now_add=True)

    class Meta:
        verbose_name = '用户访问记录表'
        verbose_name_plural = "用户访问记录表"

    def __str__(self):
        return self.remote_ip

access_times_limit_middleware.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
import time
from CMDB import settings
from web.models import AccessLog

class AccessTimesLimitMiddleware(MiddlewareMixin):
    """
    定义访问频率限制的中间件
    """
    def process_request(self, request):
        # 这里只对登录做了限制,如果想对其他路径进行限制,可自行设置
        if request.path.__contains__("login") and request.method == "POST":
            # 设置时间间隔,默认设置60秒内的访问频率
            access_time = settings.ACCESS_TIME if hasattr(settings, 'ACCESS_TIME') else 60
            ip = request.META.get('REMOTE_ADDR')
            request_path = request.path
            # 查询该IP的访问记录
            ip_access_log = AccessLog.objects.filter(remote_ip=ip)
            now = time.time()
            if ip_access_log:

                # 按照ID升序排序,ID大的是最新插入进去的,所以第一条是最旧的记录
                last_access_log = ip_access_log.order_by('id').first()
                # print("=========================", last_access_log.access_time)

                first_access_time = last_access_log.access_time.timestamp()
            else:
                # 如果该访问IP不在访问记录中,就设置第一次访问时间为当前时间
                first_access_time = now
            # 时间间隔过了设置的access_time,用户就可以重新访问了,重新获得访问次数
            if float(now)-first_access_time > access_time:
                ip_access_log.delete()

            access_limit = settings.ACCESS_LIMIT if hasattr(settings, 'ACCESS_LIMIT') else 5
            if ip_access_log.count() >= access_limit:
                # 多少秒后访问,这个不懂,自己画图就懂了哦。哈哈
                wait_second = round(access_time-(float(now)-first_access_time), 2)
                # "%s 秒内只允许访问%s次" % (access_time, access_limit)
                return HttpResponse("%s 秒内只允许访问%s次,请%s 秒后尝试" % (access_time, access_limit, wait_second))

            AccessLog.objects.create(remote_ip=ip, request_path=request_path)

settings.py

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'web.access_times_limit_middleware.AccessTimesLimitMiddleware',
]
# 中间件中访问频率的限制
ACCESS_TIME = 60
ACCESS_LIMIT = 5

login.html

<!DOCTYPE html>
<html   lang="en">

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>DevOps</title>

    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/font-awesome/css/font-awesome.css" rel="stylesheet">

    <link href="/static/css/animate.css" rel="stylesheet">
    <link href="/static/css/style.css" rel="stylesheet">
<!--    设置标题旁边的图标-->
    <link rel="icon" href="/static/img/favicon.ico" >
</head>

<body class="gray-bg">

    <div class="middle-box text-center loginscreen animated fadeInDown">
        <div>
            <h3>Welcome to DevOps</h3>

            <form class="m-t" role="form">
                {% csrf_token %}
                <div class="form-group">
                    <input type="email" class="form-control" placeholder="Email" required="" id="login_user_email_id">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" placeholder="Password" required="" id="login_passwd_id">
                </div>
                <span class="error_info"></span>
                <input class="btn btn-primary block full-width m-b my_login_commit" value="Login">

                <p class="text-muted text-center"><small>Do not have an account?</small></p>
                <a class="btn btn-sm btn-white btn-block" href="{% url 'register' %}">Create an account</a>
            </form>
            <p class="m-t"> <small>author:vita © 2019</small> </p>
        </div>
    </div>

    <!-- Mainly scripts -->
    <script src="/static/js/jquery-3.1.1.min.js"></script>
    <script src="/static/js/popper.min.js"></script>
    <script src="/static/js/bootstrap.js"></script>
    <script src="/static/js/plugins/sweetalert/sweetalert.min.js"></script>
     <!-- Toastr script -->
    <script src="/static/js/plugins/toastr/toastr.min.js"></script>
    <script>
        $(function () {
            $('.my_login_commit').click(function () {
                $(".error_info").text("");
                $.ajax({
                    url:"{% url 'login' %}",
                    type:'post',
                    data:{
                        csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(),
                        email:$('#login_user_email_id').val(),
                        password:$('#login_passwd_id').val()
                    },
                    success:function (data) {
                        {#console.log("data",data)#}
                        if(data.user){
                            location.href="{% url 'index' %}"
                        }else if (data.info){

                            $(".error_info").text(data.info).css("color", "red")

                        }else{
                            {#访问频率限制处,HttpResponse会把数据返回给data#}
                            $(".error_info").text(data).css("color", "red")
                        }

                    },
                    exception: function (data) {
                        console.log("exceptdata",data)
                    }
                })
            })
        })
    </script>
</body>

</html>

views.py

def login(request):
    """
    注册功能
    :param request:
    :return:
    """
    if request.method == 'POST':
        res = {"user": None, "info": None}
        email = request.POST.get("email")
        password = request.POST.get("password")
        user = auth.authenticate(email=email, password=password)
        if user:
            auth.login(request, user)
            res["user"] = email
        else:
            res["info"] = "用户名或密码不正确"
        return JsonResponse(res)

    return render(request, "login.html")

项目功能介绍

2.7restAPI

restAPI的认证--》权限--》访问频率
message = "角色编码为API_user的用户才能请求"

项目功能介绍

2.8pyecharts资源引用

官网
https://pyecharts.org/#/zh-cn/assets_host
pyecharts 使用的所有静态资源文件存放于 pyecharts-assets 项目中,默认挂载在 https://assets.pyecharts.org/assets/

pyecharts 提供了更改全局 HOST 的快捷方式,下面以开发者启动本地 FILE SERVER 为例,操作如下。

1.获取 pyecharts-assets 项目

 $ git clone https://github.com/pyecharts/pyecharts-assets.git
2.启动 HTTP file server

 $ cd pyecharts-assets
 $ python -m http.server 8001
 # Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8001/) ...
 # 默认会在本地 8000 端口启动一个文件服务器
3.配置 pyecharts 全局 HOST

 # 只需要在顶部声明 CurrentConfig.ONLINE_HOST 即可
 from pyecharts.globals import CurrentConfig
 CurrentConfig.ONLINE_HOST = "http://10.0.0.61:8001/assets/"

 # 接下来所有图形的静态资源文件都会来自刚启动的服务器
 from pyecharts.charts import Bar
 bar = Bar()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值