from django.shortcuts import render,redirect,HttpResponse
from . import models
import json,datetime,hashlib,uuid
# Create your views here.
from utils import call
from utils import query
from utils import user_page
from django.db import connection
import os
from django.db.models import Q
def now_time():
n_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return n_time
def uid():
uid = str (uuid.uuid1())
suid = ''.join (uid.split ('-'))
return suid
def login(request):
'''动态窗口'''
if request.method == 'POST':
dlfs = request.POST.get('key',None)
if dlfs == 'login':
data = call.dlfs_login(request)
return HttpResponse(json.dumps(data))
else:
return redirect ('/login.html')
session_key=request.session.session_key
request.session.delete(session_key)
date = call.login_select()
return render(request,'system/login.html',date)
def index(request):
if call.is_login (request):
user_id = request.session['user_id']
data = query.index_data(user_id)
count = user_page.dic_msg_num(user_id)
data['count'] = count
return render (request, 'system/index.html',data)
else:
return redirect ('/login.html')
def home(request):
if call.is_login(request):
return render(request,'system/home.html')
else:
return redirect ('/login.html')
def design(request):
'''登陆窗口页面'''
if call.is_login(request):
re_url = request.path_info
user_id = request.session['user_id']
if call.is_see_grant(request, re_url):
date = call.design_login(re_url,user_id)
return render(request, 'system/design.html', date)
else:
return render(request, 'system/403.html')
else:
return redirect ('/login.html')
def design_submit(request):
'''登陆窗口设置'''
if call.is_login(request):
re_url = request.POST.get('type', None)
re_date = request.POST
if call.is_see_grant(request, re_url):
if request.method == 'POST':
result = call.login_setting(request,re_date)
return HttpResponse(json.dumps(result))
else:
return HttpResponse(json.dumps(None))
else:
return HttpResponse(json.dumps(False))
else:
return redirect ('/login.html')
def theme(request):
'''桌面壁纸'''
if call.is_login(request):
re_url = request.path_info
if call.is_grant(request, re_url):
str_data = query.creat_theme(request,re_url)
menu_str = query.menu_theme(request, re_url)
return render(request,'system/theme.html',{'url_data': re_url,'data':str_data,'menu_str':menu_str})
else:
return render(request,'system/403.html')
else:
return redirect ('/login.html')
def theme_use(request):
'''桌面壁纸生效'''
if call.is_login(request) and request.method == 'POST':
re_url = request.POST.get('type', None)
if call.is_edit_grant(request, re_url):
theme_id = request.POST
models.SysTheme.objects.filter(id=theme_id['base']).update(state=1)
models.SysTheme.objects.exclude(id=theme_id['base']).update(state=0)
call.create_log(request, 'update', '修改了', '桌面壁纸')
return HttpResponse(json.dumps(True))
else:
return HttpResponse(json.dumps(False))
else:
return redirect ('/login.html')
def theme_detail(request):
'''桌面背景预览'''
if call.is_login(request):
re_url = request.GET.get('type', None)
user_id = request.session['user_id']
if call.is_see_grant(request, re_url):
date = call.theme_setting(request,re_url, user_id)
return render(request, 'system/theme_detail.html',date)
else:
return render(request, 'system/403.html')
else:
return redirect ('/login.html')
def theme_upload(request):
'''桌面壁纸上传接口'''
if call.is_login(request):
re_url = request.POST.get('type', None)
if call.is_edit_grant(request, re_url):
if call.upload_theme(request):
return HttpResponse(json.dumps(True))
else:
return HttpResponse(json.dumps(False))
else:
return HttpResponse(json.dumps(False))
else:
return redirect ('/login.html')
def theme_delete(request):
'''删除背景图片'''
if call.is_login(request) and request.method == 'POST':
re_url = request.POST.get('type', None)
if call.is_delete_grant(request, re_url):
theme_id = request.POST
image_path = models.SysTheme.objects.filter(id=theme_id['base'])
for path in image_path.values('full_path'):
if os.path.exists(path['full_path']):
os.remove(path['full_path'])
models.SysTheme.objects.filter(id=theme_id['base']).delete()
call.create_log(request, 'delete', '删除了', '一张桌面壁纸')
return HttpResponse(json.dumps(True))
else:
return HttpResponse(json.dumps(False))
else:
return redirect ('/login.html')
def information(request):
'''个人资料页面'''
if call.is_login(request):
return render(request,'system/information.html')
else:
return redirect ('/login.html')
def information_data(request):
if call.is_login(request):
base = []
user_id = request.session['user_id']
info_data=user_page.dic_msg(user_id)
for info in info_data.values('id','content','user_id','state','type'):
if info['state'] == 1:
info['state'] = '已读'
else:
info['state'] = '未读'
base.append(info)
data = {'code': 0, 'page_num': 1, 'bg_hs': 10, 'count': 0, 'data':base}
return HttpResponse(json.dumps(data))
else:
return redirect ('/login.html')
def information_del(request):
if call.is_login(request):
data = request.POST
try:
models.SysInformation.objects.filter(id=data['ID']).delete()
return HttpResponse(json.dumps(True))
except:
return HttpResponse(json.dumps(False))
else:
return redirect ('/login.html')
def information_update(request):
if call.is_login(request):
up_id = request.POST
if up_id['state'] == '未读':
models.SysInformation.objects.filter(id=up_id['ID']).update(state=1)
return HttpResponse(json.dumps(True))
else:
return HttpResponse(json.dumps(False))
else:
return redirect ('/login.html')
def role(request):
if call.is_login (request):
re_url = request.path_info
if call.is_grant(request,re_url):
sfkbj = call.see_grant(request, re_url)
return render (request, 'system/role.html',{'url_data': re_url,'anzs':sfkbj})
else:
return render(request,'system/403.html')
else:
return redirect ('/login.html')
def role_insert(request):
if call.is_login (request):
if request.method == 'POST':
re_url = request.POST.get('type', None)
if call.is_edit_grant(request, re_url):
temp = request.POST
models.SysRole.objects.create (id=call.uid(),caption = temp['base[name]'],abbreviation = temp['base[level]'],remark = temp['base[remark]'],pxh = temp['base[pxh]'])
call.create_log(request,'create','创建角色',temp['base[name]'])
return HttpResponse(json.dumps (True))