import datetime
import os
from django.core.paginator import Paginator
from django.db.models import Count
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, JsonResponse
from django.shortcuts import render
from .models import User
from utils import common
from .models import *
workdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def login(request):
"""
跳转登录
:param request:
:return:
"""
return render(request, 'login.html')
def register(request):
"""
跳转注册
:param request:
:return:
"""
return render(request, 'register.html')
def logout(request):
return HttpResponseRedirect('/')
def index(request):
"""
跳转首页
:param request:
:return:
"""
username = request.session.get('username', '')
role = int(request.session.get('role', 3))
user_id = request.session.get('user_id', 1)
total_user = len(User.objects.all())
date = datetime.datetime.today()
month = date.month
year = date.year
day = date.day
current_day = "{}-{}-{}".format(year, month, day)
# 获取近七天的操作
seven_days = common.get_recent_seven_day()
seven_data_dict = dict.fromkeys(seven_days, 0)
for i in seven_days:
d = datetime.timedelta
r = Info.objects.filter(create_time__year=i.split('-')[0], create_time__month=i.split('-')[1],
create_time__day=i.split('-')[2]).all()
if r:
seven_data_dict[i] = r.count()
seven_count_list = [seven_data_dict[x] for x in seven_days]
return render(request, 'index.html', locals())
def welcome(request):
username = request.session['username']
role_id = request.session['role']
total_mass = Mass.objects.count()
total_activity = ActivityModel.objects.count()
user = User.objects.filter(name=username).first()
date = datetime.datetime.today()
month = date.month
year = date.year
day = date.day
current_day = "{}-{}-{}".format(year, month, day)
total_user = User.objects.filter(role_id=2).count()
total_admin = User.objects.filter(role_id=1).count()
notice = Notice.objects.all().order_by('-id')[0].text
return render(request, 'welcome.html', locals())
def club_list(request):
data_list = Mass.objects.all()
return render(request, 'clubs_list.html', locals())
def clubs_add(request):
if request.method == 'POST':
name = request.POST.get('name')
principal_name = request.POST.get('principal_name')
desc = request.POST.get('desc')
Mass.objects.create(
name=name, principal_name=principal_name, desc=desc
)
return JsonResponse({"status": 0})
title = '添加'
return render(request, 'clubs.html', locals())
def clubs_edit(request, uid):
if request.method == 'POST':
Mass.object.filter(id=uid).update(
name=request.POST.get('name'),
principal_name=request.POST.get('principal_name'),
desc=request.POST.get('desc')
)
return JsonResponse({"status": 0})
data = Mass.objects.filter(id=uid).first()
title = '更新'
return render(request, 'clubs.html', locals())
def activity_add(request):
if request.method == 'POST':
ActivityModel.objects.create(
mass_id=request.POST.get('clubs_name'),
title=request.POST.get('title'),
address=request.POST.get('address'),
principal=request.POST.get('principal'),
content=request.POST.get('content'),
activity_date=request.POST.get('activity_date')
)
return JsonResponse({"status": 0})
title = '添加'
return render(request, 'activity.html', locals())
# 社团活动列表
def activity_list(request):
if request.method == 'DELETE':
uid_s = request.POST.get('data_s')
if uid_s:
for i in uid_s:
ActivityModel.objects.filter(id=i).delete()
return JsonResponse({"status": 0})
uid = request.form.get('uid')
ActivityModel.objects.filter(id=uid).delete()
return JsonResponse({"status": 0})
data_list = ActivityModel.objects.all()
return render(request, 'activity_list.html', locals())
# 社团活动修改
def activity_edit(request, uid):
if request.method == 'POST':
ActivityModel.objects.filter(id=uid).update(
mass_id=request.POST.get('clubs_name'),
title=request.POST.get('title'),
address=request.POST.get('address'),
principal=request.POST.get('principal'),
content=request.POST.get('content'),
)
return JsonResponse({"status": 0})
title = '更新'
data = ActivityModel.objects.filter(id=uid).first()
return render(request, 'activity.html', locals())
# return render_template('activity.html', form=data, title='更新')
def login_out(request):
"""
注销登录
:param request:
:return:
"""
del request.session['username']
return HttpResponseRedirect('/')
def personal(request):
username = request.session['username']
role_id = request.session['role']
user = User.objects.filter(name=username).first()
return render(request, 'personal.html', locals())
def get_data(request):
"""
获取列表信息 | 模糊查询
:param request:
:return:
"""
keyword = request.GET.get('name')
page = request.GET.get("page", '')
limit = request.GET.get("limit", '')
response_data = {}
response_data['code'] = 0
response_data['msg'] = ''
data = []
if keyword is None:
results_obj = MessageBoard.objects.all()
else:
results_obj = MessageBoard.objects.filter(text__contains=keyword).all()
paginator = Paginator(results_obj, limit)
results = paginator.page(page)
if results:
for user in results:
record = {
"id": user.id,
"text": user.text,
"owner": user.owner,
"ipaddr": user.ipaddr,
'create_time': user.create_time.strftime('%Y-%m-%d %H:%m:%S'),
}
data.append(record)
response_data['count'] = len(results_obj)
response_data['data'] = data
return JsonResponse(response_data)
def get_gg(request):
"""
获取列表信息 | 模糊查询
:param request:
:return:
"""
keyword = request.GET.get('name')
page = request.GET.get("page", '')
limit = request.GET.get("limit", '')
response_data = {}
response_data['code'] = 0
response_data['msg'] = ''
data = []
if keyword is None:
results_obj = Notice.objects.all()
else:
results_obj = Notice.objects.filter(text__contains=keyword).all()
paginator = Paginator(results_obj, limit)
results = paginator.page(page)
if results:
for user in results:
record = {
"id": user.id,
"text": user.text,
"owner": user.owner,
"ipaddr": user.ipaddr,
'create_time': user.create_time.strftime('%Y-%m-%d %H:%m:%S'),
}
data.append(record)
response_data['count'] = len(results_obj)
response_data['data'] = data
return JsonResponse(response_data)
def get_bm(request):
"""
获取列表信息 | 模糊查询
:param request:
:return:
"""
keyword = request.GET.get('name')
page = request.GET.get("page", '')
limit = request.GET.get("limit", '')
response_data = {}
response_data['code'] = 0
response_data['msg'] = ''
data = []
if keyword is None:
results_