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_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): 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) 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): 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"})