def wrap(request, *args, **kwargs): (uid, identity) = sessions.getUser(request) if (uid is None) or (identity is None): return HttpResponseRedirect('/login?from=' + request.get_full_path()) else: return function(request, *args, **kwargs)
def downloadActivity(request, aaid): uid = sessions.getUser(request)[0] username = getUsernameByUid(uid) if activityAuthorityCheck(uid, aaid) != 1: return JsonResponse({'status': 'error', 'msg': '无权下载此活动信息!'}) #excel part dirpath = r"dashboard/files/%s"%aaid print dirpath if not os.path.exists(dirpath): os.makedirs(dirpath) xlpath = dirpath+"/userinfo.xls" style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00') style1 = xlwt.easyxf(num_format_str='D-MMM-YY') wb = xlwt.Workbook() ws = wb.add_sheet('A Test Sheet') ws.write(0, 0, 1234.56, style0) ws.write(1, 0, datetime.datetime.now(), style1) ws.write(2, 0, 1) ws.write(2, 1, 1) ws.write(2, 2, xlwt.Formula("A3+B3")) wb.save(xlpath) #txt part #packagepart response = StreamingHttpResponse(file_iterator(xlpath)) response['Content-Disposition'] = 'attachment;filename="userinfo_%s.xls"'%aaid return response
def login(request): # 如果已经管理员session,转到activity # 如果已经普通用户session,转到404 # 否则,再渲染登录界面 if request.method == 'GET': user = sessions.getUser(request) print user print getIdentityByUid(user[0]) if user[0] == None or user[1] == None: return render(request, 'dashboard/login.html', {}) else: if user[1] == 1: raise Http404 else: return HttpResponseRedirect('/admin/activity') else: if not request.POST.has_key('username') or not request.POST.has_key('pwd'): return JsonResponse({'status': 'error', 'msg': '请输入用户名和密码。'}) username = request.POST['username'] pwd = request.POST['pwd'] uid = getUidByUsername(username) if uid == -1: # no such username return JsonResponse({'status': 'error', 'msg': '没有这个账号!'}) if verifyPassword(uid, pwd) == False: return JsonResponse({'status': 'error', 'msg': '用户名或密码错误!'}) identity = getIdentityByUid(uid) if sessions.login(request, uid, identity) == False: return JsonResponse({'status': 'error', 'msg': '登录失败!'}) return JsonResponse({'status': 'success', 'msg': '登陆成功!'})
def newBroadcast(request): dic = json.loads(request.body) dic['uid'] = sessions.getUser(request)[0] create_result = createBroadcast(dic) if create_result['status'] == "error": JsonResponse({'status': 'error', 'msg': create_result['msg']}) else: return JsonResponse({'status': 'success', 'msg': '消息发送成功!'})
def downloadUsers(request): uid = sessions.getUser(request)[0] username = getUsernameByUid(uid) file_name = "dashboard/files/1.txt" response = StreamingHttpResponse(file_iterator(file_name)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="userlist.txt"' return response
def signIn(request, aaid): uid, identity = sessions.getUser(request) if (uid == None): return JsonResponse({'status': 'error', 'msg': '身份验证出错,请重新登录!'}) result = userSignIn(uid, aaid) if result == 0: return JsonResponse({'status': 'error', 'msg': '报名失败!'}) elif result == 2: return JsonResponse({'status': 'error', 'msg': '您已报名。'}) else: return JsonResponse({'status': 'success', 'msg': '报名成功!'})
def deleteActivity(request, aaid): uid = sessions.getUser(request)[0] authority = activityAuthorityCheck(uid, aaid) if authority == -1: return JsonResponse({'status': 'error', 'msg': '无此活动!'}) elif authority == 0: return JsonResponse({'status': 'error', 'msg': '您无权删除此活动!'}) deleteresult = doDeleteActivity(uid, aaid) if deleteresult == 0: return JsonResponse({'status': 'error', 'msg': '删除活动失败!'}) else: return JsonResponse({'status': 'success', 'msg': '删除活动成功!'})
def login(request): if sessions.getUser(request)[0] != None: return HttpResponseRedirect("/") if request.method == 'GET': #GET METHOD print sessions.getUser(request) return render(request, 'participation/login.html', {}) else: #POST METHOD if not request.POST.has_key('username') or not request.POST.has_key( 'pwd'): return JsonResponse({'status': 'error', 'msg': '请输入用户名和密码。'}) username = request.POST['username'] pwd = request.POST['pwd'] uid = getUidByUsername(username) if uid == -1: # no such username return JsonResponse({'status': 'error', 'msg': '没有这个账号!'}) if verifyPassword(uid, pwd) == False: return JsonResponse({'status': 'error', 'msg': '用户名或密码错误!'}) identity = getIdentityByUid(uid) if sessions.login(request, uid, identity) == False: return JsonResponse({'status': 'error', 'msg': '登录失败!'}) updateUserLoginTime(uid) return JsonResponse({'status': 'success', 'msg': '登陆成功!'})
def newActivity(request): uid = sessions.getUser(request)[0] username = getUsernameByUid(uid) if request.method == 'GET': return render(request, 'dashboard/new_activity.html', {'tab': dashboard_tabs['activity'], 'type': 'new', 'username': username}) else: act_attributes = json.loads(request.POST['data']) imagefile = request.FILES['poster'] act_attributes['filename'] = imagefile.name act_attributes['image'] = ContentFile(imagefile.read()) create_result = createNewActivity(uid, act_attributes) if create_result['status'] == "error": JsonResponse({'status': 'error', 'msg': create_result['msg']}) else: return JsonResponse({'status': 'success', 'msg': '创建活动成功!', 'data': {'aaid': create_result['aaid']} })
def editActivity(request, aaid): uid = sessions.getUser(request)[0] username = getUsernameByUid(uid) activity = getActivityByAaid(aaid) if activity == -1: raise Http404 if request.method == 'GET': return render(request, 'dashboard/new_activity.html', {'tab': dashboard_tabs['activity'], 'activity': activity, 'username': username, 'type': 'edit'}) else: act_attributes = json.loads(request.POST['data']) act_attributes ['aaid'] = aaid authority = activityAuthorityCheck(uid, aaid) if authority == -1: return JsonResponse({'status': 'error', 'msg': '无此活动!'}) elif authority == 0: return JsonResponse({'status': 'error', 'msg': '您无权修改此活动!'}) imagefile = request.FILES['poster'] act_attributes['image'] = ContentFile(imagefile.read()) editresult = doEditActivity(uid, act_attributes) if editresult == 0: return JsonResponse({'status': 'error', 'msg': '修改活动失败!'}) else: return JsonResponse({'status': 'success', 'msg': '修改活动成功!'})
def wrap(request, *args, **kwargs): (uid, identity) = sessions.getUser(request) if (uid is not None) and (identity == identities['admin']): return function(request, *args, **kwargs) else: raise Http404
def activity(request): uid = sessions.getUser(request)[0] username = getUsernameByUid(uid) act_list = getActivitiesByUidSimple(uid) return render(request, 'dashboard/activity.html', {'tab': dashboard_tabs['activity'], 'username': username, 'activities': act_list})
def users(request): uid = sessions.getUser(request)[0] username = getUsernameByUid(uid) return render(request, 'dashboard/users.html', {'tab': dashboard_tabs['users'], 'username': username})