from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from datetime import datetime, timedelta from django.core import serializers from django.forms.models import model_to_dict import json from .data import admin_psw from .models import Volunteer, ServiceTimeSlot, ServiceBooking # Create your views here. def index(request): if request.method == 'GET': return render(request, 'volunteer_arrangement/index.html') if request.method == 'POST': volunteer = Volunteer.get_by_mobile(request.POST['mobile']) if volunteer: if volunteer.status == 'active': request.session['volunteer_id'] = volunteer.ID return HttpResponseRedirect('/cq/volunteer/booking_service/') else: return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '账户未激活', 'dirLink': '/cq/volunteer'}) else: return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '手机号码不存在', 'dirLink': '/cq/volunteer'}) def volunteer_reg(request): if request.method == 'GET': return render(request, 'volunteer_arrangement/volunteer_reg.html') if request.method == 'POST': if not Volunteer.get_by_mobile(request.POST['mobile']): volunteer = Volunteer() volunteer.status = 'pending' volunteer.name = request.POST['name'] volunteer.mobile = request.POST['mobile'] volunteer.gender = request.POST['gender'] volunteer.social_id = request.POST['social_id'] volunteer.political_outlook = request.POST['political_outlook'] volunteer.organization = request.POST['organization'] volunteer.district_street = request.POST['district_street'] volunteer.save() return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '提交成功,请等待审批', 'dirLink': '/cq/volunteer'}) else: return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '手机号码已经注册过了', 'dirLink': '/cq/volunteer'}) def admin(request): if request.method == 'GET': return render(request, 'volunteer_arrangement/admin_login.html') if request.method == 'POST': if request.POST['password'] == admin_psw: request.session['admin_login'] = True return HttpResponseRedirect('/cq/volunteer/admin_management/home/') else: return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '密码错误', 'dirLink': '/cq/volunteer/admin/'}) def admin_management(request, action): # 验证管理员登录状态 if not request.session.get('admin_login'): return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '请重新登录管理员', 'dirLink': '/cq/admin/'}) if action == 'home': return render(request, 'volunteer_arrangement/admin_home.html') if action == 'pending_list': pending_list = Volunteer.pending_list() return render(request, 'volunteer_arrangement/admin_pending_list.html', {'pending_list': pending_list}) if action == 'approve_volunteer': v_id = request.GET.get('v_id') Volunteer.approve_volunteer(v_id) return HttpResponseRedirect('/cq/volunteer/admin_management/home/') if action == 'reject_volunteer': v_id = request.GET.get('v_id') Volunteer.reject(v_id) return HttpResponseRedirect('/cq/volunteer/admin_management/home/') if action == 'delete_service': if request.method == 'GET': booking_list = None # booking_overall_status = [] # 如果有日期就获取当天的报名状况 if request.GET.get('date'): service_date = request.GET['date'] service_list = ServiceTimeSlot.search_by_date(service_date) # service_list_IDs = [] # for service in service_list: # service_list_IDs.append(service.ID) booking_list = ServiceBooking.search_by_service_timeslot_list(service_list) # booking_list_new = ServiceBooking.search_by_service_timeslot_list_with_volunteer_info(service_list) # service_list_json = serializers.serialize('json', service_list) # for service in service_list: # service_dict = model_to_dict(service) # service_exist_booking_count = len(booking_list.filter(timeslot_id=service)) # service_dict['current_booking'] = service_exist_booking_count # # service_dict['date'] = service_dict['date'].strftime("%Y-%m-%d") # service_dict['date'] = str(service_dict['date']) # service_dict['start_time'] = service_dict['start_time'].strftime("%H:%M:%S") # service_dict['end_time'] = service_dict['end_time'].strftime("%H:%M:%S") # booking_overall_status.append(service_dict) return render(request, 'volunteer_arrangement/admin_delete_service.html', {'booking_status': booking_list, 'date': str(request.GET.get('date'))}) if action == 'get_month_service': if request.method == 'GET': # 获取日期值 if request.GET.get('date'): service_date = request.GET.get('date') monthly_report = ServiceBooking.get_monthly_report(service_date) # 获取数据,生成response response = HttpResponse(content_type='text/csv;charset=ANSI') response['Content-Disposition'] = 'attachment; filename="MonthlyReport.csv"' import csv writer = csv.writer(response) # keys = monthly_report[0].keys() keys = ['日期', '开始时间', '结束时间', '姓名', '性别', '手机号码', '身份证号码', '政治面貌', '学校/单位', '居住区域(区或街道)'] writer.writerow(keys) for x in monthly_report: writer.writerow(x.values()) return response def logout(request): del request.session return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '已经退出', 'dirLink': '/cq/volunteer'}) def booking_service(request): # 志愿者注册服务时间页面 if request.method == 'GET': booking_overall_status = [] monthly_booking_status = [] count_month = [] current_year = datetime.now().year current_month = datetime.now().month # 验证登陆状态 if not request.session.get('volunteer_id'): return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '请登录', 'dirLink': '/cq/volunteer'}) volunteer = Volunteer.get_by_id(request.session.get('volunteer_id')) # 如果有日期就获取当天的报名状况 if request.GET.get('date'): service_date = request.GET['date'] if datetime.strptime(service_date, '%Y-%m-%d').date() < datetime.now().date(): return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '日期不能早于今天', 'dirLink': '/cq/volunteer/booking_service/?date='+str(datetime.now().date())}) else: service_date = str(datetime.now().date()) service_list = ServiceTimeSlot.search_by_date(service_date) # service_list_IDs = [] # for service in service_list: # service_list_IDs.append(service.ID) booking_list = ServiceBooking.search_by_service_timeslot_list(service_list) # service_list_json = serializers.serialize('json', service_list) for service in service_list: service_dict = model_to_dict(service) service_exist_booking_count = len(booking_list.filter(timeslot_id=service)) service_dict['current_booking'] = service_exist_booking_count # service_dict['date'] = service_dict['date'].strftime("%Y-%m-%d") service_dict['date'] = str(service_dict['date']) service_dict['start_time'] = service_dict['start_time'].strftime("%H:%M:%S") service_dict['end_time'] = service_dict['end_time'].strftime("%H:%M:%S") booking_overall_status.append(service_dict) selected_date = datetime.strptime(service_date, '%Y-%m-%d').date() current_year = selected_date.year current_month = selected_date.month monthly_service_list = ServiceTimeSlot.search_by_year_month(current_year, current_month) monthly_booking_list = ServiceBooking.search_by_service_timeslot_list(monthly_service_list) count_date = 0 count_people = 0 count_volunteer = 0 count_month = [] for service in monthly_service_list: service_dict = model_to_dict(service) service_exist_booking_count = len(monthly_booking_list.filter(timeslot_id=service)) service_dict['current_booking'] = service_exist_booking_count # service_dict['date'] = service_dict['date'].strftime("%Y-%m-%d") # service_dict['date'] = str(service_dict['date']) service_dict['start_time'] = service_dict['start_time'].strftime("%H:%M:%S") service_dict['end_time'] = service_dict['end_time'].strftime("%H:%M:%S") monthly_booking_status.append(service_dict) if count_date == 0 and service_dict['date'].day != count_date: count_date = service_dict['date'].day count_people += service_dict['people_count'] count_volunteer += service_dict['current_booking'] elif service_dict['date'].day == count_date: count_people += service_dict['people_count'] count_volunteer += service_dict['current_booking'] else: count_month.append({'day': count_date, 'total': count_people, 'current': count_volunteer}) count_date = service_dict['date'].day count_people = 0 count_volunteer = 0 count_people += service_dict['people_count'] count_volunteer += service_dict['current_booking'] if count_date != 0: count_month.append({'day': count_date, 'total': count_people, 'current': count_volunteer}) return render(request, 'volunteer_arrangement/booking_service.html', {'booking_status': booking_overall_status, 'monthly_status': count_month, 'date': str(request.GET.get('date')), 'volunteer': volunteer}) def my_service(request): # 验证登陆状态 if not request.session.get('volunteer_id'): return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '请登录', 'dirLink': '/cq/volunteer'}) my_booking_list = ServiceBooking.search_by_volunteer(request.session.get('volunteer_id')) return render(request, 'volunteer_arrangement/my_service.html', {'my_service': my_booking_list}) def booking_info_api(request): # 获取指定日期注册查询信息 service_date = request.GET['date'] service_list = ServiceTimeSlot.search_by_date(service_date) # service_list_IDs = [] # for service in service_list: # service_list_IDs.append(service.ID) booking_list = ServiceBooking.search_by_service_timeslot_list(service_list) booking_overall_status = [] service_list_json = serializers.serialize('json', service_list) for service in service_list: service_dict = model_to_dict(service) service_exist_booking_count = len(booking_list.filter(timeslot_id=service)) service_dict['current_booking'] = service_exist_booking_count # service_dict['date'] = service_dict['date'].strftime("%Y-%m-%d") service_dict['date'] = str(service_dict['date']) service_dict['start_time'] = service_dict['start_time'].strftime("%H:%M:%S") service_dict['end_time'] = service_dict['end_time'].strftime("%H:%M:%S") booking_overall_status.append(service_dict) booking_overall_status_json = json.dumps(booking_overall_status, ensure_ascii=False) return JsonResponse(json.loads(booking_overall_status_json), safe=False) # return HttpResponse(booking_overall_status_json) # return HttpResponse(booking_overall_status) def register(request, action): if action == 'add': # 验证登陆状态 if not request.session.get('volunteer_id'): return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '请登录', 'dirLink': '/cq/volunteer'}) # 验证是否传入timeslot_id if not request.GET.get('timeslot_id'): return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '参数错误', 'dirLink': '/cq/volunteer'}) volunteer = Volunteer.get_by_id(request.session.get('volunteer_id')) booking_timeslot = ServiceTimeSlot.get_by_id(request.GET.get('timeslot_id')) current_booking_count = len(ServiceBooking.search_by_service_timeslot(booking_timeslot)) if current_booking_count < booking_timeslot.people_count: service_booking = ServiceBooking() service_booking.volunteer_id = volunteer service_booking.timeslot_id = booking_timeslot if ServiceBooking.add(service_booking) == 0: return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '报名成功', 'dirLink': '/cq/volunteer/booking_service/?date=' + str(booking_timeslot.date)}) else: return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '你已经报名了', 'dirLink': '/cq/volunteer/booking_service/?date=' + str(booking_timeslot.date)}) if action == 'delete': # 验证管理员登录状态 if not request.session.get('admin_login'): return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '请重新登录管理员', 'dirLink': '/cq/admin/'}) # 验证是否传入timeslot_id and volunteer_id if not request.GET.get('timeslot_id') and request.GET.get('volunteer_id'): return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '参数错误', 'dirLink': '/cq/volunteer'}) volunteer_id = request.GET.get('volunteer_id') service_timeslot_id = request.GET.get('timeslot_id') service_timeslot = ServiceTimeSlot.get_by_id(service_timeslot_id) ServiceBooking.delete(service_timeslot_id, volunteer_id) return render(request, 'volunteer_arrangement/directPage.html', {'alertMsg': '删除成功', 'dirLink': '/cq/volunteer/admin_management/delete_service/?date=' + str(service_timeslot.date) })