def post(self, request): group_name = request.POST.get("group_name", None) group_description = request.POST.get("group_description", None) organization_id = request.POST.get("organization_id", None) message = request.POST.get("message", "") # validate if not group_name or not group_description or not organization_id: return JsonResponse({"status": "fail", "reason": u"错误:输入不正确"}) # check if the name is already being used if Group.objects.filter(organization_id=organization_id, name=group_name).count(): return JsonResponse({"status": "fail", "reason": u"错误:班级名称已经被使用"}) # find the organization try: target_users = find_organization_admin_users(int(organization_id)) for target in target_users: msg = UserMessage() msg.to_user = target.id msg.from_user = None # system message msg.title = u"用户{0}请求创建班级".format(request.user) msg.message_type = UserMessage.MSG_TYPE_CREATE_GROUP msg.message = json.dumps({ "organization_id": organization_id, "user_id": request.user.id, "group_name": group_name, "group_description": group_description, "extra_message": message }) msg.save() return JsonResponse({"status": "success"}) except: return JsonResponse({"status": "fail", "reason": "bad request"})
def post(self, request): user_id = request.POST.get("user_id", 0) group_id = request.POST.get("group_id", 0) role = request.POST.get("role", 1) student_id = request.POST.get("student_id", 0) try: user = UserProfile.objects.filter(id=user_id).get() group = Group.objects.filter(id=group_id).get() if not UserGroup.objects.filter(user=user, group=group).count(): ug = UserGroup() ug.user = user ug.group = group ug.role = role ug.student_id = student_id ug.save() m = UserMessage() m.from_user = request.user m.message_type = UserMessage.MSG_TYPE_JOIN_GROUP_OK m.title = u"你已经成功加入班级" + group.name m.message = json.dumps({ "group_name": group.name, "group_id": group.id }) m.to_user = user_id m.save() return JsonResponse({"status": "success"}) except Exception as e: return JsonResponse({"status": "fail"})
def post(self, request): group_id = request.POST.get("group_id", None) student_id = request.POST.get("student_id", 0) is_teacher = parse_bool(request.POST.get("is_teacher", False)) message = request.POST.get("message", "") if not group_id: return JsonResponse({"status": "fail", "reason": "bad input"}) target_users = [] # find group administrators admin_users = UserGroup.objects.filter(Q(role=2) | Q(role=3), group_id=group_id).all() if admin_users: for admin in admin_users: target_users.append(admin.user) else: # no admin user found. tell site admin target_users = UserProfile.objects.filter(is_staff=True) # put a message to all target users for target in target_users: msg = UserMessage() msg.to_user = target.id msg.from_user = None # system message msg.title = u"用户{0}请求加入班级".format(request.user) msg.message_type = UserMessage.MSG_TYPE_JOIN_GROUP msg.message = json.dumps({ "group_id": group_id, "user_id": request.user.id, "is_teacher": is_teacher, "student_id": student_id, "extra_message": message }) msg.save() return JsonResponse({"status": "success"})
def post(self, request, user_id): title = request.POST.get("title", "") text = request.POST.get("message", "") m = UserMessage() m.message_type = UserMessage.MSG_TYPE_USER m.title = title m.message = text m.from_user = request.user m.to_user = user_id m.save() return render(request, 'user_cowntact.html', {"message": u"消息发送成功"})
def post(self, request): user_id = request.POST.get("user_id", 0) message_type = request.POST.get("message_type", UserMessage.MSG_TYPE_USER) if user_id: # create a message to tell the user m = UserMessage() m.from_user = request.user m.to_user = user_id m.title = u"申请被拒绝" request_type = UserMessage( message_type=int(message_type)).get_message_type_display() m.message = u"你的{0}申请审核未通过。如有疑问,请和我私信联系。".format(request_type) m.save() return JsonResponse({"status": "success"})
def post(self, request): user_id = request.POST.get("user_id", 0) group_id = request.POST.get("group_id", 0) try: group = Group.objects.filter(id=group_id).get() deleted, _ = UserGroup.objects.filter(user_id=user_id, group_id=group_id).delete() if deleted: m = UserMessage() m.from_user = request.user m.message_type = UserMessage.MSG_TYPE_USER m.title = u"你已经成功退出班级'" + group.description + "'" m.message = u"" m.to_user = user_id m.save() return JsonResponse({"status": "success"}) except Exception as e: return JsonResponse({"status": "fail"})
def post(self, request): group_id = request.POST.get("group_id", None) message = request.POST.get("message", "") if not group_id: return JsonResponse({"status": "fail", "reason": "bad input"}) target_users = find_group_admin_users(int(group_id)) # put a message to all target users for target in target_users: msg = UserMessage() msg.to_user = target.id msg.from_user = None # system message msg.title = u"用户{0}请求退出班级".format(request.user) msg.message_type = UserMessage.MSG_TYPE_LEAVE_GROUP msg.message = json.dumps({ "group_id": group_id, "user_id": request.user.id, "extra_message": message }) msg.save() return JsonResponse({"status": "success"})
def post(self, request): register_mobile_form = RegisterMobileForm(request.POST) if register_mobile_form.is_valid(): mobile = request.POST.get("mobile") password = request.POST.get("password") nickname = request.POST.get("nickname") if UserProfile.objects.filter(mobile_phone=mobile): return render( request, "user_register.html", { "register_mobile_form": register_mobile_form, "msg": "用户{0}已经存在".format(mobile), "method": "mobile" }) user = UserProfile() user.nick_name = nickname user.mobile_phone = mobile user.username = mobile user.is_active = True user.email = "" user.password = make_password(password) user.save() # 写入欢迎消息 user_message = UserMessage() user_message.to_user = user.id user_message.from_user = user user_message.message = "欢迎注册" user_message.save() return render(request, "user_login.html", {"login_title": u"注册成功,请登录"}) else: return render(request, "user_register.html", { "register_mobile_form": register_mobile_form, "method": "mobile" })
def post(self, request): user_id = request.POST.get("user_id", 0) group_name = request.POST.get("group_name", None) group_description = request.POST.get("group_description", None) organization_id = request.POST.get("organization_id", None) # No validation. We assume they are all valid try: # check if the group alredy exists if Group.objects.filter(organization_id=organization_id, name=group_name).count() == 0: # create the group group = Group() group.organization_id = organization_id group.name = group_name group.description = group_description group.save() # add admin user user_group = UserGroup() user_group.user_id = user_id user_group.group = group user_group.role = 3 # 管理员 user_group.save() # send a message to user m = UserMessage() m.message_type = UserMessage.MSG_TYPE_USER m.title = u"你申请的班级已经创建: {0}".format(group.name) m.message = u"你申请的班级已经创建,可以在 “班级” “全部班级” 中找到。" m.to_user = user_id m.from_user = request.user m.save() return JsonResponse({"status": "success"}) except: return JsonResponse({"status": "fail", "reason": "bad request"})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get("email", "") if UserProfile.objects.filter(email=user_name): return render( request, "user_register.html", { "register_form": register_form, "msg": "用户{0}已经存在".format(user_name), "method": "email" }) pass_word = request.POST.get("password", "") user_profile = UserProfile() user_profile.nick_name = register_form.data["nickname"] user_profile.username = user_name user_profile.email = user_name user_profile.is_active = False user_profile.password = make_password(pass_word) user_profile.save() #写入欢迎注册消息 user_message = UserMessage() user_message.to_user = user_profile.id user_message.from_user = user_profile user_message.title = "欢迎注册" user_message.message = "欢迎来到Nova背单词。你可以先看看有没有感兴趣的单词书,或者去找到感兴趣的班级。" user_message.save() send_register_email_async.delay(user_name, "register", get_base_url(request)) return render(request, "user_login.html", {"login_title": u"注册成功,请检查你的邮箱中的确认邮件。账号激活之后就可以登录了。"}) else: return render(request, "user_register.html", { "register_form": register_form, "method": "email" })