123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 |
- from django.shortcuts import render
- from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
- from datetime import datetime, timedelta, date
- from django.forms.models import model_to_dict
- import json
- from .data import ADMIN_PSW
- from .setting import APPLICATION_NAME, get_media_url_prefix
- from .functions import is_volunteer_login, upload_file, get_ext, msg_and_direct, service_log
- from .models import Volunteer, ServiceTimeSlot, ServiceBooking, Bonus, BonusRecord, CreditRecord
- # from Niko.settings import DEBUG, ENVIRONMENT
- from django.db import transaction
- from backend.wechat import WeChatOA2, WeChatMsg
- from Niko.settings import DEBUG
- import csv
- # Create your views here.
- def index(request):
- if request.method == 'GET':
- # 尝试微信登录
- if 'micromessenger' in request.META['HTTP_USER_AGENT'].lower():
- wechat_id = request.session.get('wechat_openid')
- if DEBUG:
- service_log('打印session中的微信wechat_openid')
- service_log(wechat_id)
- if wechat_id is None:
- return HttpResponseRedirect('/cq/volunteer/wechat_entry/')
- else:
- wechat_id = None
- # wechat_id = 'o3MJZ02eAuLBNSMson2NeI19135Y'
- if wechat_id:
- volunteer = Volunteer.get_by_wechat_id(wechat_id)
- if volunteer:
- if volunteer.status == 'active':
- request.session['volunteer_id'] = volunteer.ID
- redirect_uri = request.session.get('redirect_uri') # 检索是否需要重定向
- if redirect_uri is None:
- redirect_uri = '/cq/volunteer/home/'
- else:
- request.session.pop('redirect_uri')
- return HttpResponseRedirect(redirect_uri)
- else:
- request.session.flush()
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '账户未激活', 'dirLink': '/cq/volunteer/'})
- return render(request, 'volunteer_arrangement/index.html', {'buttom_bg_color': 'rgb(88, 167, 255)'})
- if request.method == 'POST':
- volunteer = Volunteer.get_by_mobile_social_id(request.POST['mobile'], request.POST['social_id'].upper().strip())
- if volunteer:
- if volunteer.status == 'active':
- request.session['volunteer_id'] = volunteer.ID
- if volunteer.wechat_id is None: # 如果用户没有wechat_id,就写入wechat_id
- volunteer.wechat_id = request.session.get('wechat_openid')
- volunteer.save()
- redirect_uri = request.session.get('redirect_uri') # 检索是否需要重定向
- if redirect_uri is None:
- redirect_uri = '/cq/volunteer/home/'
- else:
- request.session.pop('redirect_uri')
- return HttpResponseRedirect(redirect_uri)
- 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 wechat_entry(request):
- # 微信登录
- wechat_oa2 = WeChatOA2()
- # 这里的Volunteer和Base是两个参数,表示volunteer应用调用微信登录,用户获取Base信息
- return HttpResponseRedirect(wechat_oa2.snsapi_base('VolunteerBase'))
- def volunteer_reg(request):
- if request.method == 'GET':
- return render(request, 'volunteer_arrangement/volunteer_reg.html', {'title': '志愿者注册'})
- if request.method == 'POST':
- if Volunteer.get_by_mobile(request.POST['mobile']) is None:
- volunteer = Volunteer()
- volunteer.status = 'pending'
- volunteer.name = request.POST['name'].strip()
- volunteer.mobile = request.POST['mobile'].strip()
- if len(volunteer.mobile) != 11:
- return msg_and_direct(request, 'http://127.0.0.1:8000/cq/volunteer/volunteer_reg/', '手机号码需要为11位')
- volunteer.gender = request.POST['gender']
- volunteer.social_id = request.POST['social_id'].upper().strip()
- if len(volunteer.social_id) != 18:
- return msg_and_direct(request, 'http://127.0.0.1:8000/cq/volunteer/volunteer_reg/', '身份证号码需要为18位')
- volunteer.political_outlook = request.POST['political_outlook']
- volunteer.organization = request.POST['organization'].strip()
- volunteer.district_street = request.POST['district_street'].strip()
- 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 volunteer_update_info(request):
- if request.method == 'GET':
- # 验证登陆状态
- 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'))
- return render(request, 'volunteer_arrangement/volunteer_update_info.html', {'volunteer': volunteer})
- if request.method == 'POST':
- volunteer = Volunteer.get_by_id(request.session.get('volunteer_id'))
- if volunteer.mobile != int(request.POST['mobile']): # POST上来一个新的手机号码
- if Volunteer.get_by_mobile(request.POST['mobile']) is not None: # 这个新的号码已经存在了
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '手机号码已经注册过了', 'dirLink': '/cq/volunteer/update_info/'})
- else:
- volunteer.status = 'pending'
- volunteer.name = request.POST['name'].strip()
- volunteer.mobile = request.POST['mobile']
- volunteer.gender = request.POST['gender']
- volunteer.social_id = request.POST['social_id'].upper()
- volunteer.political_outlook = request.POST['political_outlook']
- volunteer.organization = request.POST['organization'].strip()
- volunteer.district_street = request.POST['district_street'].strip()
- volunteer.save()
- request.session.flush()
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '更新成功,请等待审批', 'dirLink': '/cq/volunteer'})
- def volunteer_home(request):
- if request.method == 'GET':
- if not is_volunteer_login(request):
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '请登录', 'dirLink': '/cq/volunteer'})
- available_address = ServiceTimeSlot.available_address()
- volunteer = Volunteer.get_by_id(request.session.get('volunteer_id'))
- return render(request, 'volunteer_arrangement/volunteer_home.html',
- {'address': available_address, 'volunteer': 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/volunteer/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)
- # 微信推送批准消息
- volunteer = Volunteer.get_by_id(v_id)
- if volunteer.wechat_id != None:
- wechat_msg = WeChatMsg(volunteer.wechat_id, '-WK6XrgmvW7cR7pZFwpHQwLVABd1dmzc3MXkXsQ6dGc', APPLICATION_NAME)
- wechat_msg.set_first('你已通过了审核')
- wechat_msg.add_keyboard(volunteer.name)
- wechat_msg.add_keyboard(volunteer.mobile)
- wechat_msg.add_keyboard(str(datetime.today().date()))
- wechat_msg.add_keyboard('通过')
- wechat_msg.set_remark('现在可以报名志愿者服务了。')
- wechat_msg.submit()
- 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 == 'create_service':
- if request.method == 'GET':
- return render(request, 'volunteer_arrangement/admin_create_service.html')
- if request.method == 'POST':
- if request.POST.get('day') == 'full_month':
- new_service_list = []
- year = int(request.POST.get('year'))
- month = int(request.POST.get('month')) + 1
- if month > 12:
- month -= 12
- year += 1
- service_year_month = datetime(year=year, month=month, day=1)
- service_year_month = service_year_month - timedelta(days=1)
- for x in range(service_year_month.day):
- service_timeslot = ServiceTimeSlot()
- service_timeslot.date = date(year=service_year_month.year, month=service_year_month.month,
- day=x + 1)
- service_timeslot.start_time = request.POST.get('start_time')
- service_timeslot.end_time = request.POST.get('end_time')
- service_timeslot.address = request.POST.get('address')
- service_timeslot.service_address = request.POST.get('service_address')
- service_timeslot.service_content = request.POST.get('service_content')
- service_timeslot.people_count = request.POST.get('people_count')
- new_service_list.append(service_timeslot)
- ServiceTimeSlot.objects.bulk_create(new_service_list)
- return msg_and_direct(request, '/cq/volunteer/admin_management/home/',
- message='成功添加{}条记录'.format(service_year_month.day))
- elif request.POST.get('day') == 'not_full_month':
- service_timeslot = ServiceTimeSlot()
- service_timeslot.date = date(int(request.POST.get('year')), int(request.POST.get('month')),
- int(request.POST.get('date')))
- service_timeslot.start_time = request.POST.get('start_time')
- service_timeslot.end_time = request.POST.get('end_time')
- service_timeslot.address = request.POST.get('address')
- service_timeslot.service_address = request.POST.get('service_address')
- service_timeslot.service_content = request.POST.get('service_content')
- service_timeslot.people_count = request.POST.get('people_count')
- service_timeslot.save()
- return msg_and_direct(request, '/cq/volunteer/admin_management/home/', '成功添加1条记录')
- 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_address = request.GET.get('address')
- if request.GET.get('timeslot_id'): # 如果指定service timeslot id 就根据ID查找
- service_list = ServiceTimeSlot.get_by_id(request.GET.get('timeslot_id'))
- else:
- service_list = ServiceTimeSlot.search_by_date_and_address(service_date, service_address)
- # service_list = ServiceTimeSlot.search_by_date(service_date)
- booking_list = ServiceBooking.search_by_service_timeslot_list(service_list)
- 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)
- for item in monthly_report: # 添加一个tab,用来避免excel打开身份证号码最后几位显示为0的问题
- item['volunteer_id__social_id'] = '\t' + item['volunteer_id__social_id']
- # 获取数据,生成response
- response = HttpResponse(content_type='text/csv;charset=ANSI')
- response['Content-Disposition'] = 'attachment; filename="MonthlyReport.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
- # 列出所有未来的志愿者服务
- if action == 'future_timeslot':
- if request.method == 'GET':
- timeslots = ServiceTimeSlot.search_future_timeslots(datetime.today().date())
- return render(request, 'volunteer_arrangement/admin_future_timeslots.html', {'timeslots': timeslots})
- # 删除service time slot会cascade删除所有报名的记录
- if action == 'delete_timeslot':
- if request.method == 'POST':
- timeslot_id = request.POST.get('timeslot_id')
- timeslot = ServiceTimeSlot.get_by_id(timeslot_id)
- # 微信通知删除者
- notify_bookings = ServiceBooking.search_by_service_timeslot(timeslot)
- for booking in notify_bookings:
- if booking.volunteer_id.wechat_id is not None:
- wechat_msg = WeChatMsg(booking.volunteer_id.wechat_id,
- 'QKMJ1XFQoXWnxdc1ZDSXKraZpumcZbg_Ow8DdaP3Cuo', APPLICATION_NAME)
- wechat_msg.set_first('志愿者活动已取消')
- wechat_msg.add_keyboard('志愿者服务')
- wechat_msg.add_keyboard(str(booking.timeslot_id.date) + " " + str(booking.timeslot_id.start_time)
- + '-' + str(booking.timeslot_id.end_time))
- wechat_msg.add_keyboard(booking.timeslot_id.address)
- wechat_msg.set_remark('你的志愿者服务,已被管理员取消。')
- wechat_msg.submit()
- pass
- timeslot.delete()
- return msg_and_direct(request, '/cq/volunteer/admin_management/home/', '活动删除成功,并解除所有报名')
- # 修改服务人数
- if action == 'modify_people_count':
- if request.method == 'POST':
- timeslot_id = request.POST.get('timeslot_id')
- timeslot = ServiceTimeSlot.get_by_id(timeslot_id)
- new_people_count = int(request.POST.get('new_count'))
- if new_people_count < timeslot.booked_count: # 防止修改完成后的人数少于已经报名的人数
- return_value = {'result': 'fail', 'code': 10}
- return JsonResponse(return_value)
- # return msg_and_direct(request, '/cq/volunteer/admin_management/future_timeslot/', '总人数少于已报名人数')
- else:
- timeslot.people_count = new_people_count
- timeslot.save()
- return_value = {'result': 'success'}
- return JsonResponse(return_value)
- # return msg_and_direct(request, '/cq/volunteer/admin_management/future_timeslot/', '修改成功')
- # 调整积分
- if action == 'adjust_credit':
- if request.method == 'GET':
- return render(request, 'volunteer_arrangement/admin/adjust_credit.html')
- if request.method == 'POST':
- mobile = request.POST.get('mobile').strip()
- social_id = request.POST.get('social_id').strip()
- adjust_credit = request.POST.get('credit').strip()
- try:
- adjust_credit = int(adjust_credit)
- except Exception as e:
- print(e)
- return msg_and_direct(request, '/cq/volunteer/admin_management/adjust_credit/', '积分请输入数字')
- volunteer = Volunteer.get_by_mobile_social_id(mobile, social_id)
- if volunteer:
- volunteer.total_credit = volunteer.total_credit + adjust_credit
- volunteer.current_credit = volunteer.current_credit + adjust_credit
- volunteer.save()
- credit_record = CreditRecord()
- credit_record.volunteer = volunteer
- credit_record.reason = '管理员调整{}积分/{}'.format(volunteer.name, adjust_credit)
- credit_record.admin = None
- credit_record.change = adjust_credit
- credit_record.save()
- return msg_and_direct(request, '/cq/volunteer/admin_management/home/', '调整成功')
- else:
- return msg_and_direct(request, '/cq/volunteer/admin_management/adjust_credit/', '手机、身份证未匹配')
- # 导出现有的志愿者
- if action == 'export_volunteers':
- if request.method == 'GET':
- volunteer_list = Volunteer.volunteer_list()
- for v in volunteer_list: # 添加一个tab,用来避免excel打开身份证号码最后几位显示为0的问题
- v.social_id = '\t' + v.social_id
- # 获取数据,生成response
- response = HttpResponse(content_type='text/csv;charset=ANSI')
- response['Content-Disposition'] = 'attachment; filename="Volunteer_List.csv"'
- writer = csv.writer(response)
- # keys = monthly_report[0].keys()
- keys = ['ID', '姓名', '性别', '手机号码', '身份证号码', '政治面貌', '单位/学校', '居住区域(区或街道)', '是否激活', '微信ID', '目前积分', '总积分']
- writer.writerow(keys)
- for x in volunteer_list:
- x = model_to_dict(x)
- writer.writerow(x.values())
- return response
- def logout(request):
- volunteer = Volunteer.get_by_id(request.session.get('volunteer_id'))
- if volunteer:
- if volunteer.wechat_id:
- volunteer.wechat_id = None
- volunteer.save()
- request.session.flush()
- 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'):
- request.session['redirect_uri'] = request.get_full_path()
- 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('address'):
- address = request.GET.get('address')
- else:
- address = 'false'
- # 如果有日期就获取当天的报名状况
- 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={};address={}'.format
- (str(datetime.now().date()), address)})
- else:
- service_date = str(datetime.now().date())
- service_list = ServiceTimeSlot.search_by_date_and_address(service_date, address)
- # 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_address(current_year, current_month, address)
- 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:
- # 只显示今天以及以后的日期
- if service.date >= datetime.today().date():
- 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['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': service_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)
- booking_list = ServiceBooking.search_by_service_timeslot_list(service_list)
- booking_overall_status = []
- 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'] = 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)
- # 用户注册服务和管理员删除服务功能
- 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'})
- # 如果有指定地址
- if request.GET.get('address'):
- address = request.GET.get('address')
- else:
- address = 'false'
- volunteer = Volunteer.get_by_id(request.session.get('volunteer_id'))
- booking_timeslot = ServiceTimeSlot.get_by_id(request.GET.get('timeslot_id'))
- has_conflict = ServiceBooking.has_conflict_booking(volunteer, booking_timeslot)
- if has_conflict:
- # 一下N行是研发代码,测试完成后删除
- # from django.forms.models import model_to_dict
- # volunteer_json = volunteer.__dict__
- # volunteer_json = model_to_dict(volunteer)
- # return JsonResponse(volunteer_json, safe=False)
- # 研发代码结束
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '这个报名和你已经报名的服务时间上有冲突',
- 'dirLink': '/cq/volunteer/booking_service/?date={};address={}'.format(
- str(booking_timeslot.date), address)})
- else:
- 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) is True:
- booking_timeslot.booked_count += 1
- booking_timeslot.save()
- wechat_msg = WeChatMsg(volunteer.wechat_id,
- 'bsDiCVb3G2WERHvX4DJO83A-FonYYGRwTQD_Zp3fq7s', APPLICATION_NAME)
- wechat_msg.set_first('志愿者活动报名成功')
- wechat_msg.add_keyboard(volunteer.name)
- wechat_msg.add_keyboard('志愿者服务')
- wechat_msg.add_keyboard(str(booking_timeslot.date) + " " + str(booking_timeslot.start_time))
- wechat_msg.add_keyboard(str(booking_timeslot.date) + " " + str(booking_timeslot.end_time))
- wechat_msg.add_keyboard(booking_timeslot.address)
- wechat_msg.set_remark('志愿者服务报名成功,请准时参加。')
- wechat_msg.submit()
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '报名成功',
- 'dirLink': '/cq/volunteer/home/'})
- else:
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '你已经报名了',
- 'dirLink': '/cq/volunteer/home/'})
- return HttpResponse("test for conflict")
- if action == 'volunteer_self_delete':
- # 验证登陆状态
- if not request.session.get('volunteer_id'):
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '请登录', 'dirLink': '/cq/volunteer'})
- else:
- volunteer_id = request.session.get('volunteer_id')
- volunteer = Volunteer.get_by_id(volunteer_id)
- service_booking_id = request.GET.get('service_booking_id')
- service_booking = ServiceBooking.get_by_ID(service_booking_id)
- if volunteer.ID == service_booking.volunteer_id.ID: # 判断是否是本人删除
- if datetime.today().date() + timedelta(days=1) >= service_booking.timeslot_id.date:
- return msg_and_direct(request, '/cq/volunteer/my_service/', '距离活动开始时间过短,如果需要取消请联系管理员。')
- else:
- service_timeslot_id = service_booking.timeslot_id.ID
- service_timeslot = ServiceTimeSlot.get_by_id(service_timeslot_id)
- wechat_msg = WeChatMsg(volunteer.wechat_id,
- 'QKMJ1XFQoXWnxdc1ZDSXKraZpumcZbg_Ow8DdaP3Cuo', APPLICATION_NAME)
- wechat_msg.set_first('志愿者活动已取消')
- wechat_msg.add_keyboard('志愿者服务')
- wechat_msg.add_keyboard(str(service_timeslot.date) + " " + str(service_timeslot.start_time) + '-'
- + str(service_timeslot.end_time))
- wechat_msg.add_keyboard(service_timeslot.address)
- wechat_msg.set_remark('你的志愿者服务,已取消。')
- wechat_msg.submit()
- ServiceBooking.delete(service_timeslot_id, volunteer_id)
- service_timeslot.booked_count -= 1
- service_timeslot.save()
- return msg_and_direct(request, '/cq/volunteer/my_service/', '活动取消成功。')
- else:
- return HttpResponse('请使用本人账号删除')
- 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')
- volunteer = Volunteer.get_by_id(volunteer_id)
- service_timeslot_id = request.GET.get('timeslot_id')
- service_timeslot = ServiceTimeSlot.get_by_id(service_timeslot_id)
- wechat_msg = WeChatMsg(volunteer.wechat_id,
- 'QKMJ1XFQoXWnxdc1ZDSXKraZpumcZbg_Ow8DdaP3Cuo', APPLICATION_NAME)
- wechat_msg.set_first('志愿者活动已取消')
- # wechat_msg.add_keyboard(volunteer.name)
- wechat_msg.add_keyboard('志愿者服务')
- wechat_msg.add_keyboard(str(service_timeslot.date) + " " + str(service_timeslot.start_time) + '-'
- + str(service_timeslot.end_time))
- # wechat_msg.add_keyboard(str(service_timeslot.date) + " " + str(service_timeslot.end_time))
- wechat_msg.add_keyboard(service_timeslot.address)
- wechat_msg.set_remark('你的志愿者服务,已被管理员取消。')
- wechat_msg.submit()
- ServiceBooking.delete(service_timeslot_id, volunteer_id)
- service_timeslot.booked_count -= 1
- service_timeslot.save()
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '删除成功',
- 'dirLink': '/cq/volunteer/admin_management/delete_service/?date='
- + str(service_timeslot.date)})
- # 志愿者查看和兑换礼物
- @transaction.atomic
- def volunteer_bonus(request, action):
- # 验证登陆状态
- if not request.session.get('volunteer_id'):
- request.session['redirect_uri'] = '/cq/volunteer/bonus/list/'
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '请登录', 'dirLink': '/cq/volunteer'})
- volunteer = Volunteer.get_by_id(request.session.get('volunteer_id'))
- if action == 'list':
- bonus_list = Bonus.all_available_bonus()
- url_prefix = get_media_url_prefix()
- return render(request, 'volunteer_arrangement/volunteer_bonus_list.html',
- {'bonus_list': bonus_list, 'volunteer': volunteer, 'url_prefix': url_prefix})
- if action == 'enroll':
- bonus = request.GET.get('bonus_id')
- bonus = Bonus.get_by_id(bonus)
- if bonus is None:
- return msg_and_direct(request, '/cq/volunteer/home/', '参数错误')
- if BonusRecord.is_exist(volunteer, bonus): # 检查是否已经兑换过
- return msg_and_direct(request, '/cq/volunteer/bonus/list/', '你已经兑换过了')
- if volunteer.current_credit < bonus.cost:
- return msg_and_direct(request, '/cq/volunteer/bonus/list/', '积分不足,请继续努力')
- if bonus.used_qty < bonus.total_qty:
- bonus.used_qty += 1
- bonus.save()
- bonus_record = BonusRecord()
- bonus_record.volunteer = volunteer
- bonus_record.bonus = bonus
- bonus_record.save()
- volunteer.current_credit -= bonus.cost
- volunteer.save()
- credit_record = CreditRecord()
- credit_record.volunteer = volunteer
- credit_record.reason = '{}兑换{}/{}奖励'.format(volunteer.name, bonus.id, bonus.name)
- credit_record.admin = None
- credit_record.change = bonus.cost * -1
- credit_record.save()
- wechat_msg = WeChatMsg(volunteer.wechat_id,
- 'wkjPXVLn_OT57IKINx3uInDlA9S1-XndV7bO4d_sbcQ', APPLICATION_NAME)
- wechat_msg.set_first(bonus.name + '兑换成功')
- wechat_msg.add_keyboard(str(datetime.today().date()))
- wechat_msg.add_keyboard('通过')
- wechat_msg.add_keyboard('请保留这条消息。我们的工作人员会尽快和你联系。')
- wechat_msg.set_remark('消耗{}积分,还剩{}积分'.format(bonus.cost, volunteer.current_credit))
- wechat_msg.submit()
- return msg_and_direct(request, '/cq/volunteer/home/', '兑换成功')
- if action == 'my_enrollment':
- bonus_record_list = BonusRecord.search_by_volunteer(volunteer)
- url_prefix = get_media_url_prefix()
- return render(request, 'volunteer_arrangement/volunteer_bonus_my_enrollment.html',
- {'bonus_record_list': bonus_record_list, 'volunteer': volunteer, 'url_prefix': url_prefix})
- # 管理员后台管理礼物
- def admin_bonus(request, action):
- # 验证管理员登录状态
- if not request.session.get('admin_login'):
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '请重新登录管理员', 'dirLink': '/cq/volunteer/admin/'})
- if action == 'list':
- bonus_list = Bonus.get_bonus_list()
- url_prefix = get_media_url_prefix()
- return render(request, 'volunteer_arrangement/bonus/admin_list.html',
- {'bonus_list': bonus_list, 'url_prefix': url_prefix})
- if action == 'download_bonus_enrollment':
- bonus_id = request.GET.get('bonus_id')
- bonus = Bonus.get_by_id(bonus_id)
- bonus_list = BonusRecord.search_bonus_with_volunteer(bonus)
- for bonus in bonus_list: # 添加一个tab,用来避免excel打开身份证号码最后几位显示为0的问题
- bonus['volunteer__social_id'] = '\t' + bonus['volunteer__social_id']
- # 获取数据,生成response
- response = HttpResponse(content_type='text/csv;charset=ANSI')
- response['Content-Disposition'] = 'attachment; filename="Enrollment.csv"'
- writer = csv.writer(response)
- # keys = monthly_report[0].keys()
- keys = ['姓名', '性别', '手机号码', '身份证号码', '政治面貌', '学校/单位', '居住区域(区或街道)']
- writer.writerow(keys)
- for x in bonus_list:
- writer.writerow(x.values())
- return response
- if action == 'create':
- if request.method == 'GET':
- return render(request, 'volunteer_arrangement/bonus/create.html')
- if request.method == 'POST':
- bonus = Bonus()
- bonus.name = request.POST.get('name')
- bonus.description = request.POST.get('description')
- pic_filename = request.FILES.get('picture')
- if pic_filename is None:
- filename = None
- else:
- new_pic_name = hash(str(pic_filename) + str(datetime.now()))
- upload_file(request, filename='picture', prefix=new_pic_name)
- filename = str(new_pic_name) + '.' + get_ext(pic_filename)
- bonus.picture = filename
- bonus.type = request.POST.get('type')
- bonus.total_qty = request.POST.get('total_qty')
- bonus.cut_off_date = request.POST.get('cut_off_date')
- bonus.cost = request.POST.get('cost')
- bonus.save()
- return render(request, 'volunteer_arrangement/directPage.html',
- {'alertMsg': '添加成功', 'dirLink': '/cq/volunteer/admin_management/home/'})
- if action == 'delete':
- bonus_id = request.GET.get('bonus_id')
- if Bonus.del_bonus(bonus_id):
- return msg_and_direct(request, '/cq/volunteer/admin_management/home/', '删除成功')
- else:
- return msg_and_direct(request, '/cq/volunteer/admin_management/home/', '兑换已经有人报名了,不能删除')
|