def remove_admin(request, username): result = Result() try: remove_user(username) result.code = Consts.SUCCESS_CODE except User.DoesNotExist: result.code = Consts.FAILED_CODE result.msg = Consts.NOT_FOUND_USER_MSG except: result.code = Consts.FAILED_CODE result.msg = Consts.UNKNOWN_ERROR return HttpResponse(json.dumps(result.to_dict()), content_type="application/json")
def upload_batch_admin(request): result = Result() try: csv_file = request.FILES.get("uploadInputFile") if not csv_file.name.endswith('.csv'): result.code = Consts.FAILED_CODE result.msg = Consts.INVALID_CSV_FILE_MSG return HttpResponse(json.dumps(result.to_dict()), content_type="application/json") if csv_file.multiple_chunks(): result.code = Consts.FAILED_CODE result.msg = Consts.TOO_LARGE_FILE_MSG return HttpResponse(json.dumps(result.to_dict()), content_type="application/json") reader = csv.DictReader(codecs.EncodedFile(csv_file, "utf-8")) success_count = 0 failed_count = 0 failed_idx = [] failed_reason = [] for i, row in enumerate(reader): username = row['username'] name = row['name'] phone = row['phone'] email = row['email'] if is_username_valid(username): failed_count += 1 failed_idx.append(i+1) failed_reason.append(Consts.USER_EXISTED_MSG) continue try: # todo: generate random password and send email to user password = "******" create_user(username, password, name, phone, email) success_count += 1 except: failed_count += 1 failed_idx.append(i+1) failed_reason.append(Consts.UNKNOWN_ERROR) result.code = Consts.SUCCESS_CODE if failed_count > 0: result.msg = u"成功导入%d条数据, 失败%d条, 原因: \n" % (success_count, failed_count) for i, r in zip(failed_idx, failed_reason): result.msg += "第%d行, %s \n" % (i, r) else: result.msg = u"成功导入%d条数据" % success_count except Exception as e: result.code = Consts.FAILED_CODE result.msg = e.message raise e return HttpResponse(json.dumps(result.to_dict()), content_type="application/json")
def update_admin_info(request): username = request.POST['username'] name = request.POST['name'] email = request.POST['email'] phone = request.POST['phone'] result = Result() try: update_user(username, name, phone, email) result.code = Consts.SUCCESS_CODE except User.DoesNotExist: result.code = Consts.FAILED_CODE result.msg = Consts.NOT_FOUND_USER_MSG except: result.code = Consts.FAILED_CODE result.msg = Consts.UNKNOWN_ERROR return HttpResponse(json.dumps(result.to_dict()), content_type="application/json")
def update_user_password(request): pw = request.POST['password'] confirm_pw = request.POST['confirm-password'] result = Result() if pw != confirm_pw: result.code = Consts.FAILED_CODE result.msg = Consts.UPDATE_PASSWORD_UNMATCH_MSG return HttpResponse(json.dumps(result.to_dict()), content_type="application/json") try: user = request.user user.set_password(pw) user.save() result.code = Consts.SUCCESS_CODE except: result.code = Consts.FAILED_CODE result.msg = Consts.UNKNOWN_ERROR return HttpResponse(json.dumps(result.to_dict()), content_type="application/json")
def create_admin(request): result = Result() username = request.POST['username'] name = request.POST['name'] # todo: generate random password and send email to user password = "******" email = request.POST['email'] phone = request.POST['phone'] if is_username_valid(username): result.code = Consts.FAILED_CODE result.msg = Consts.USER_EXISTED_MSG else: try: create_user(username, password, name, phone, email) result.code = Consts.SUCCESS_CODE except: result.code = Consts.FAILED_CODE result.msg = Consts.UNKNOWN_ERROR return HttpResponse(json.dumps(result.to_dict()), content_type="application/json")
def update_user_profile(request): name = request.POST['name'] email = request.POST['email'] phone = request.POST['phone'] result = Result() try: user = update_user(request.user.username, name, phone, email) return HttpResponse(json.dumps({"name": user.first_name, "phone": user.last_name, "email": user.email}), content_type="application/json") except: result.code = Consts.FAILED_CODE result.msg = Consts.UNKNOWN_ERROR return HttpResponse(json.dumps(result.to_dict()), content_type="application/json")
def auth(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) result = Result() if user: _login(request=request, user=user) result.code = Consts.SUCCESS_CODE else: result.code = Consts.FAILED_CODE result.msg = Consts.LOGIN_FAILED_MSG return HttpResponse(json.dumps(result.to_dict()), content_type="application/json")