欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

国庆续写商品管理系统(二)

程序员文章站 2023-11-06 23:28:28
`国庆有点懒散更新的内容不多,大家国庆快乐` 一.做的事情 "上次写到点我查看" 设置中国时区 修改表单存储位置 设计商品相关的表,主要是总库存,退货,进货,销售 优化登入验证码,去除 这些让人难以区分的内容 重新设计文件目录 二.配置相关 三.验证码相关 四.模型相关 ......

国庆有点懒散更新的内容不多,大家国庆快乐

一.做的事情

  • 设置中国时区
  • 修改表单存储位置
  • 设计商品相关的表,主要是总库存,退货,进货,销售
  • 优化登入验证码,去除1io0这些让人难以区分的内容
  • 重新设计文件目录

二.配置相关

setting.py

"""
django settings for drf_test project.

generated by 'django-admin startproject' using django 1.11.22.

for more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

for the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os
# build paths inside the project like this: os.path.join(base_dir, ...)
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# quick-start development settings - unsuitable for production
# see https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# security warning: keep the secret key used in production secret!
secret_key = 'ppa5l3jvxr4k^ow*4o+0_^7@&sa3x+!hb_$artwraa%60iq@g7'

# security warning: don't run with debug turned on in production!
debug = true

allowed_hosts = []


# application definition

installed_apps = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'drf_api',
]

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',
]

root_urlconf = 'drf_test.urls'

templates = [
    {
        'backend': 'django.template.backends.django.djangotemplates',
        'dirs': [os.path.join(base_dir, 'templates')]
        ,
        'app_dirs': true,
        'options': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

wsgi_application = 'drf_test.wsgi.application'


# database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

databases = {
    'default': {
        'engine': 'django.db.backends.mysql',   # 数据库引擎
        'name': 'mib',         # 你要存储数据的库名,事先要创建之
        'user': 'root',         # 数据库用户名
        'password': '16745',     # 密码
        'host': 'localhost',    # ip
        'port': '3306',         # 数据库使用的端口
    }
}


# password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

auth_password_validators = [
    {
        'name': 'django.contrib.auth.password_validation.userattributesimilarityvalidator',
    },
    {
        'name': 'django.contrib.auth.password_validation.minimumlengthvalidator',
    },
    {
        'name': 'django.contrib.auth.password_validation.commonpasswordvalidator',
    },
    {
        'name': 'django.contrib.auth.password_validation.numericpasswordvalidator',
    },
]


# internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

language_code = 'zh-hans'

time_zone = 'asia/shanghai'

use_i18n = true

use_l10n = true

use_tz = false


# static files (css, javascript, images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

static_url = '/static/'
staticfiles_dirs=(os.path.join(base_dir,'static'),)
auth_user_model = "drf_api.userinfo"
media_url  = "/img/"
media_root = os.path.join(base_dir, "img")

三.验证码相关

from django.shortcuts import render,httpresponse,redirect
from django.http import jsonresponse

from pil import image,imagedraw,imagefont
import random

from io import bytesio

from django.contrib import auth

from drf_api.userinfo_form import register
from drf_api import models
from rest_framework.throttling import simpleratethrottle


def test(request):
    return render(request, 'aa.html')



def register(request):
    if request.method=='get':
        form=register()
        return render(request, 'register.html', {'form':form})
    elif request.is_ajax():
        response={'code':100,'msg':none}
        form = register(request.post)
        if form.is_valid():
            #校验通过的数据
            clean_data=form.cleaned_data
            #把re_pwd剔除
            clean_data.pop('re_pwd')
            #取出头像
            avatar=request.files.get('avatar')
            if avatar:
                clean_data['avatar']=avatar
            user=models.userinfo.objects.create_user(**clean_data)
            if user:
                response['msg'] = '创建成功'
            else:
                response['code'] = 103
                response['msg'] = '创建失败'
        else:
            response['code']=101
            #把校验不通过的数据返回
            response['msg']=form.errors
            print(type(form.errors))
        return jsonresponse(response,safe=false)


def login(request):
    if request.method=='get':
        return render(request, 'login.html')
    else:
        print(request.post)
        user_name=request.post.get('name')
        pwd=request.post.get('pwd')
        code=request.post.get('code')

        user=auth.authenticate(username=user_name,password=pwd)
        print(user)
        if request.session.get('code').upper() !=code.upper(): #忽略大小写
            return httpresponse('验证码错误')
        elif not user:
            return httpresponse('账号密码错误')
        else:
            auth.login(request,user)
            return httpresponse('登入成功')



def get_code(request):
    if request.method == 'get':
        img = image.new('rgb', (350, 40), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        # 写文字
        # 生成一个字体对象
        font = imagefont.truetype('/static/gabriola.ttf', 34)
        # 调用方法,返回一个画板对象
        draw = imagedraw.draw(img)

        new_text =''
        x = 100
        # 生成随机8位数字
        text_chiose = 'zxcvbnmasdfghjklqwertyup23456789zxcvbnmasdfghjklqwertyup'
        text_list=random.sample(text_chiose,8)
        for text in text_list:
            x+=20
            y = random.randint(1,5)
            draw.text((x, y), text, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
                      font=font)
            new_text +=text



        # 加点线
        width = 320
        height = 35
        for i in range(2):
            x1 = random.randint(0, width)
            x2 = random.randint(0, width)
            y1 = random.randint(0, height)
            y2 = random.randint(0, height)
            # 在图片上画线
            draw.line((x1, y1, x2, y2), fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))

        for i in range(10):
            # 画点
            draw.point([random.randint(0, width), random.randint(0, height)], fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
            x = random.randint(0, width)
            y = random.randint(0, height)
            # 画弧形
            draw.arc((x, y, x + 4, y + 4), 0, 90, fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        print(new_text)
        #存在session中
        request.session['code']=new_text
        #存内存
        f = bytesio()
        img.save(f, 'png')
        return httpresponse(f.getvalue())

四.模型相关

from django.db import models
from django.contrib.auth.models import abstractuser

from rest_framework import mixins
# create your models here.
class userinfo(abstractuser):
    avatar = models.filefield(upload_to='avatar/', default='avatar/default.png')
    class meta:
        verbose_name='用户表'
        verbose_name_plural = verbose_name


class  commodity(models.model):
    id = models.smallintegerfield(primary_key=true,db_column='序号')
    name = models.charfield(max_length=60,db_column='商品名',null=true)
    inventory = models.integerfield(db_column='库存',null=true)
    cost = models.decimalfield (max_digits=60,decimal_places=2,db_column='单件成本',null=true)
    selling_price= models.decimalfield (max_digits=60,decimal_places=2,db_column='销售价格',null=true)
    quantity_in = models.integerfield(db_column='进货总数量',null=true)
    sales_volume = models.integerfield(db_column='销售量总数量',null=true)
    return_volume = models.integerfield(db_column='退货总数量',null=true)
    is_delete = models.booleanfield(default=false, db_column='是否删除')
    def __str__(self):
        return self.name

    class meta:
        db_table = "库存表"


class daysales(models.model):
    id = models.smallintegerfield(primary_key=true, db_column='日期序号')
    date = models.datetimefield(auto_now_add=true,db_column='日期')
    number = models.decimalfield(max_digits=60, decimal_places=2, db_column='营业总额', null=true)
    is_delete = models.booleanfield(default=false, db_column='是否删除')
    date_changed = models.datetimefield(auto_now=true,db_column='最后修改日期')
    def __str__(self):
        return self.date
    class meta:
        db_table = "日销售表"

class daystock(models.model):
    id = models.smallintegerfield(primary_key=true, db_column='日期序号')
    date = models.datetimefield(auto_now_add=true,db_column='日期')
    number  = models.decimalfield(max_digits=60, decimal_places=2, db_column='进货总额', null=true)
    is_delete = models.booleanfield(default=false, db_column='是否删除')
    date_changed = models.datetimefield(auto_now=true,db_column='最后修改日期')
    def __str__(self):
        return self.date
    class meta:
        db_table = "日进货表"

class dayreturn(models.model):
    id = models.smallintegerfield(primary_key=true, db_column='日期序号')
    date = models.datetimefield(auto_now_add=true,db_column='日期')
    number  = models.decimalfield(max_digits=60, decimal_places=2, db_column='退货总额', null=true)
    is_delete = models.booleanfield(default=false, db_column='是否删除')
    date_changed = models.datetimefield(auto_now=true,db_column='最后修改日期')
    def __str__(self):
        return self.date
    class meta:
        db_table = "日退货表"


class commoditysales(models.model):
    id = models.smallintegerfield(primary_key=true, db_column='日期序号')
    name = models.charfield(max_length=60,db_column='商品名',null=true)
    data = models.foreignkey('daysales','id',db_column='销售日期',null=true)
    selling_price= models.decimalfield (max_digits=60,decimal_places=2,db_column='销售价格',null=true)
    sales_volume = models.integerfield(db_column='销售量数量',null=true)
    batch = models.integerfield(db_column='批号',null=true)
    is_delete = models.booleanfield(default=false, db_column='是否删除')
    date_changed = models.datetimefield(auto_now=true,db_column='最后修改日期')
    def __str__(self):
        return self.name
    class meta:
        db_table = "商品销售表"


class commoditystock(models.model):
    id = models.smallintegerfield(primary_key=true, db_column='日期序号')
    name = models.charfield(max_length=60,db_column='商品名',null=true)
    data = models.foreignkey('daystock','id',db_column='进货日期',null=true)
    cost = models.decimalfield (max_digits=60,decimal_places=2,db_column='单件成本',null=true)
    stock_volume = models.integerfield( db_column='进货数量', null=true)
    batch = models.integerfield(db_column='批号',null=true)
    is_delete = models.booleanfield(default=false, db_column='是否删除')
    date_changed = models.datetimefield(auto_now=true,db_column='最后修改日期')
    def __str__(self):
        return self.name
    class meta:
        db_table = "商品进货表"

class commodityreturn(models.model):
    id = models.smallintegerfield(primary_key=true, db_column='日期序号')
    name = models.charfield(max_length=60,db_column='商品名',null=true)
    data = models.foreignkey('dayreturn','id',db_column='退货日期',null=true)
    cost = models.decimalfield (max_digits=60,decimal_places=2,db_column='退货价格',null=true)
    return_volume = models.integerfield( db_column='退货数量', null=true)
    batch = models.integerfield(db_column='批号',null=true)
    is_delete = models.booleanfield(default=false, db_column='是否删除')
    date_changed = models.datetimefield(auto_now=true,db_column='最后修改日期')
    def __str__(self):
        return self.name
    class meta:
        db_table = "商品退货表"