Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
newpanjing committed Jun 13, 2019
1 parent ddceba9 commit aa93d0b
Show file tree
Hide file tree
Showing 16 changed files with 1,437 additions and 596 deletions.
21 changes: 0 additions & 21 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ python 博客系统,基于django

# 演示地址
https://www.88cto.com

使用豆瓣源安装依赖包

```shell
pip install -i https://pypi.douban.com/simple --trusted-host pypi.douban.com -r requirements.txt
```

随后运行,请在settings.py中切换数据

```shell
python manage.py runserver 8000
```
7 changes: 7 additions & 0 deletions article/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ class CommentAdmin(admin.ModelAdmin):
list_per_page = 10
list_filter = ('member', 'type')
search_fields = ('content',)


@admin.register(Cover)
class CoverAdmin(admin.ModelAdmin):
list_display = ('id', 'x', 'y', 'font_size', 'color_display', 'image_display')
list_per_page = 20
list_editable = ('x', 'y', 'font_size')
27 changes: 27 additions & 0 deletions article/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,30 @@ class Meta:

def __str__(self):
return self.content


class Cover(models.Model):
'''封面'''

x = models.IntegerField(verbose_name='X坐标')
y = models.IntegerField(verbose_name='Y坐标')
font_size = models.IntegerField(verbose_name='字体大小', default=24, null=True, blank=True)
color = models.CharField(max_length=12, verbose_name='颜色', default='#FFF', null=True, blank=True)

image = models.ImageField(verbose_name='图片')

class Meta:
verbose_name = "封面"
verbose_name_plural = "封面管理"

def image_display(self):
return format_html('<img src="{}!100" width=50 height=50>', self.image.url)

def color_display(self):
return format_html('<div style="border:#000 1px solid;height:30px;width:30px;background:{}"></div>', self.color)

image_display.short_description = '图片'
color_display.short_description = '颜色'

def __str__(self):
return self.image.url
23 changes: 23 additions & 0 deletions article/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
from django.test import TestCase
import django
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog.settings")

django.setup()

# Create your tests here.
from article.models import Cover
# Cover
#
# class TestCase(TestCase):
# # 测试函数执行前执行
# def setUp(self):
# print("======in setUp")
#
# # 需要测试的内容
# def test_add(self):
# pass
#
# # 需要测试的内容
#
# # 测试函数执行后执行
# def tearDown(self):
# print("======in tearDown")
59 changes: 59 additions & 0 deletions draw/draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
import requests
import os
from shortid import short_id
from oss2 import *
from models.models import Config


def __get_db_config(group):
dict = {}

datas = Config.objects.filter(group=group).values('key', 'value')

for i in datas:
dict[i.get('key')] = i.get('value')
return dict


# 初始化 oss
def get_oss_bucket():
config = __get_db_config('oss')

access_key_id = config.get('key')
access_key_secret = config.get('secret')
endpoint = config.get('endpoint')
bucket_name = config.get('bucket')
auth = Auth(access_key_id, access_key_secret)
cname = config.get('cname')
return {
"bucket": Bucket(auth, endpoint, bucket_name),
"cname": cname
}


def draw(text='', url=None, x=0, y=0, font_size=24, color='#FFF'):
font = ImageFont.truetype(os.path.abspath(os.path.dirname(__file__)) + "/pingfang.ttf", font_size)

r = requests.get(url, verify=False)
# r = requests.get('https://oss.88cto.com/4y6MS8Rs.png')
stream = BytesIO()
stream.write(r.content)

img1 = Image.open(stream)

d = ImageDraw.Draw(img1)
d.text((x, y), text, color, font=font)
io_obj = BytesIO()
# img1.save('/Users/panjing/Downloads/{}.png'.format(name))
img1.save(io_obj, 'png')
filename = short_id.get_short_id() + ".png"
result = get_oss_bucket().get('bucket').put_object(filename, io_obj.getvalue())
print(result)

return get_oss_bucket().get('cname') + "/" + filename


if __name__ == '__main__':
draw()
Binary file added draw/pingfang.ttf
Binary file not shown.
31 changes: 27 additions & 4 deletions myblog/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import os, sys, django

dicts={
'name':123
}
from django.test import TestCase

print(dicts.get('name1'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myblog.settings")
django.setup()

from draw import draw
from article.models import Cover, Article
import random
from django.db import models

all = Article.objects.all()

total = Cover.objects.count()

for item in all:
# if item.image != '':
# continue
c = Cover.objects.all()[random.randint(0, total - 1)]
url = draw.draw(text=item.title, url=c.image.url, font_size=c.font_size, color=c.color, x=c.x, y=c.y)
item.image.name = url
print(url)
item.save()

# all = Cover.objects.all()
# for index, item in enumerate(all):
# draw.draw(item, name=index, font_size=item.font_size, color=item.color, x=item.x, y=item.y)
# print(item, index)
14 changes: 14 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Django==2.2.1
django-simpleui
pytz==2019.1
shortid8==1.0
sqlparse==0.3.0
Pillow
markdown
django-haystack
django-compressor
mysqlclient
redis
oss2
jieba
Whoosh
Loading

0 comments on commit aa93d0b

Please sign in to comment.