/
views.py
84 lines (70 loc) · 2.42 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# encoding: utf-8
import os
import random
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Template, Context
from django.shortcuts import render_to_response
from fontdict import fontdict
from draw import my_draw
from misc import getVersionInfor
def fontcss():
'''generate css for fonts; '''
format='''
/**** %s ****/
@font-face {
font-family: "%s";
src: url(/site_media/font/%s);
}'''
css=''
for font in fontdict.keys():
css += format % (fontdict[font], font, font)
return css
# response = HttpResponse(mimetype='text/plain; charset=utf-8')
# response.write(css)
# return response
def home(request):
'''index view'''
version,build = getVersionInfor()
t = get_template('index.html')
html = t.render(Context({'fontdict':fontdict,
# 'fontcss': fontcss(),
"version": version,
"build": build}))
return HttpResponse(html)
def generate(request):
bg = request.GET.get('bg', '') #can be changed to POST
if not bg or bg=='undefined':#undefined
bg='logo192.png'
text=request.GET.get('text', '')
font=request.GET.get('font', '')
textColor=request.GET.get('textColor', '')
shadowColor=request.GET.get('shadowColor', '')
#boolean args
border=eval(request.GET.get('border', ''))
shadow=eval(request.GET.get('shadow', ''))
highlight=eval(request.GET.get('highlight', ''))
return my_draw(request,
bg=bg,
text=text,
font=font,
textColor=textColor,
shadowColor=shadowColor,
border=border,
shadow=shadow,
highlight=highlight)
def generate_random(request):
bgPath = os.getcwd() + "/media/colors/"
fontPath = os.getcwd() + "/media/font"
def randomColorUnit():
return str(hex(random.randrange(0x00, 0xFF))[2:]).zfill(2)
bg = random.choice(os.listdir(bgPath))
text = request.GET.get('text', '')
font = random.choice(os.listdir(fontPath))
textColor = "#" + randomColorUnit() + randomColorUnit() + randomColorUnit()
shadowColor = "#" + randomColorUnit() + randomColorUnit() + randomColorUnit()
border = random.randint(0, 1)
shadow = random.randint(0, 2)
highlight = random.randint(0, 1)
return my_draw(request, bg, text, font, textColor, shadowColor, border, shadow, highlight)