def addMembers(request=None, group_id=None): uid = auth.current_user(request) if uid is not None: data = request.json id_role_member = db.session.query( Role.id).filter(Role.role_name == "member").first() for member in data['members']: if member.get('id', None) is None: new_user = createUser(member) db.session.add(new_user) db.session.flush() new_relation = GroupsUsers(user_id=new_user.id, group_id=data['group_id'], role_id=id_role_member) db.session.add(new_relation) db.session.commit() else: new_relation = GroupsUsers(user_id=member['id'], group_id=data['group_id'], role_id=id_role_member) db.session.add(new_relation) db.session.commit() return json({}, status=201) else: return json( { "error_code": "USER_NOT_FOUND", "error_message": "USER_NOT_FOUND" }, status=520)
def join_to_group(request=None): uid = auth.current_user(request) if uid is not None: data = request.json group_id = data.get('group_id') user_id = data.get('user_id') check_is_member = db.session.query(GroupsUsers.id).filter( GroupsUsers.group_id == group_id, GroupsUsers.user_id == user_id).first() if check_is_member is None: id_role_member = db.session.query( Role.id).filter(Role.role_name == 'member').first() new_relation = GroupsUsers(user_id=user_id, group_id=group_id, role_id=id_role_member) db.session.add(new_relation) db.session.commit() group_after_join = db.session.query(Group.id,Group.group_name,Group.description)\ .filter(Group.id == group_id, Group.deleted == False).first() group_after_join = format_list_group([group_after_join], uid) return json(group_after_join[0], status=201) else: return json({}, status=500) else: return json( { "error_code": "USER_NOT_FOUND", "error_message": "USER_NOT_FOUND" }, status=520)
async def addMembers(request=None, group_id=None): uid = auth.current_user(request) if uid is not None: data = request.json phone = data.get("phone") group_id = data.get("group_id") user_id = db.session.query(User.id).filter( User.phone == phone, User.is_active == True).first() if user_id is not None: id_role_member = db.session.query( Role.id).filter(Role.role_name == "member").first() check_is_member = db.session.query(GroupsUsers.id).filter( GroupsUsers.user_id == user_id, GroupsUsers.group_id == group_id).first() if check_is_member is None: new_relation = GroupsUsers(user_id=user_id, group_id=group_id, role_id=id_role_member) db.session.add(new_relation) db.session.commit() return json({}, status=201) else: return json({"error_message": "Không tìm được số điện thoại này"}, status=520) else: return json( { "error_code": "USER_NOT_FOUND", "error_message": "USER_NOT_FOUND" }, status=520)
def user_register(request): param = request.json phone = param['phone'] password = param['password'] display_name = param['display_name'] unsigned_display_name = no_accent_vietnamese(param['display_name']) # print(param) check_user_match = db.session.query(User).filter( User.phone == phone).first() letters = string.ascii_lowercase user_salt = ''.join(random.choice(letters) for i in range(64)) user_password = auth.encrypt_password(password, user_salt) new_user = None if check_user_match is None: new_user = User(phone=phone, unsigned_display_name=unsigned_display_name, password=user_password, display_name=display_name, salt=user_salt) else: new_user = check_user_match new_user.phone = phone new_user.unsigned_display_name = unsigned_display_name new_user.password = user_password new_user.display_name = display_name new_user.salt = user_salt new_user.is_active = True db.session.add(new_user) db.session.flush() new_group = Group( group_name="GROUP " + str(display_name), unsigned_name="GROUP " + str(unsigned_display_name), assignee_id=new_user.id, # members=[new_user] ) db.session.add(new_group) db.session.flush() new_relation = GroupsUsers(user_id=new_user.id, group_id=new_group.id, role_id=db.session.query(Role.id).filter( Role.role_name == "admin").scalar()) db.session.add(new_relation) new_user.group_last_access_id = new_group.id new_user.group_last_access = new_group db.session.add(new_user) db.session.commit() return json({ "id": str(new_user.id), "phone": phone, "display_name": display_name, "password": password })
def createGroup(request=None, data=None, Model=None): uid = auth.current_user(request) if uid is not None: group = request.json response = group group['unsigned_name'] = no_accent_vietnamese(group['group_name']) group['created_by'] = uid # members = group['members'] new_group = Group() for key in group.keys(): if hasattr(new_group, key) and not isinstance(group[key], (dict, list)): setattr(new_group, key, group[key]) db.session.add(new_group) db.session.flush() # #update group last access user to new group # user_update = db.session.query(User).filter(User.id == uid).first() # user_update.group_last_access_id = new_group.id # user_update.group = new_group # db.session.add(user_update) #set current user become admin default role_admin_id = db.session.query( Role.id).filter(Role.role_name == 'admin').first() new_group_user = GroupsUsers(user_id=uid, group_id=new_group.id, role_id=role_admin_id) db.session.add(new_group_user) # db.session.commit() all_roles = db.session.query(Role).all() db.session.commit() response['id'] = str(new_group.id) return json(response, status=201) else: return json( { "error_code": "USER_NOT_FOUND", "error_message": "USER_NOT_FOUND" }, status=520)
def create_admin(password='******'): """ Create default data. """ role_admin = Role.query.filter(Role.role_name == "admin").first() if (role_admin is None): role_admin = Role(role_name='admin', display_name="Admin") db.session.add(role_admin) db.session.flush() role_user = Role.query.filter(Role.role_name == "user").first() if (role_user is None): role_user = Role(role_name='user', display_name="User") db.session.add(role_user) db.session.flush() role_employee = Role.query.filter(Role.role_name == "employee").first() if (role_employee is None): role_employee = Role(role_name='employee', display_name="Employee") db.session.add(role_employee) db.session.flush() role_leader = Role.query.filter(Role.role_name == "leader").first() if (role_leader is None): role_leader = Role(role_name='leader', display_name="Leader") db.session.add(role_leader) db.session.flush() role_member = Role.query.filter(Role.role_name == "member").first() if (role_member is None): role_member = Role(role_name='member', display_name="member") db.session.add(role_member) db.session.flush() user = User.query.filter(User.email == "*****@*****.**").first() # employee = Employee(full_name="Admin User", email="*****@*****.**",phone_number="0968244158",\ # id_identifier=123456) # user.employee = employee # user.roles = [role_admin] # db.session.add(user) db.session.commit() if user is None: # create salt letters = string.ascii_lowercase user_salt = ''.join(random.choice(letters) for i in range(64)) print("user_salt", user_salt) # create user password user_password = auth.encrypt_password(password, user_salt) #create user # employee = Employee(full_name="Admin User", email="*****@*****.**",phone_number="0968244158",\ # id_identifier=123456) user = User(display_name="Admin User", email="*****@*****.**",\ password=user_password, salt=user_salt, phone="0333333333") db.session.add(user) db.session.flush() new_group = Group( group_name="group Admin User", unsigned_name="group Admin User", assignee_id=user.id, # members=[user] ) db.session.add(new_group) db.session.flush() new_relation = GroupsUsers(user_id=user.id, group_id=new_group.id, role_id=role_admin.id) db.session.add(new_relation) db.session.commit() return
def createTaskInfo(task_info,uid): # print(task_info['group']) task_info['unsigned_name'] = no_accent_vietnamese(task_info['task_info_name']) task_info['created_by'] = uid assignee = task_info.get('assignee',{}) group = task_info.get('group',{}) checklist = task_info.get('checklist',{}) new_task_info = TaskInfo() for key in task_info.keys(): if hasattr(new_task_info,key) and not isinstance(task_info[key], (dict, list )): setattr(new_task_info, key, task_info[key]) #set ssignee setattr(new_task_info, "assignee_id", assignee.get('id',None)) #set group # print('sdfddddddddd',isinstance(getattr(new_group,"check_lists"), (float, int, str, ))) if group.get("id",None) is not None: setattr(new_task_info, "group_id", group.get("id",None)) # CHECK EXISTS # print('CHECK EXISTS') check_member = db.session.query(GroupsUsers).filter(GroupsUsers.user_id == assignee.get('id'),\ GroupsUsers.group_id == group.get("id")).first() if check_member is None: #SET ASSIGNEE TO MEMBER OF GROUP new_relation_member = GroupsUsers( group_id = group.get('id'), user_id = assignee.get('id'), role_id = db.session.query(Role.id).filter(Role.role_name == "member").first() ) db.session.add(new_relation_member) elif group.get('id',None) is None and group.get('group_name') is not None and group.get('group_name') != '': new_group = Group() for key in group.keys(): if hasattr(new_group,key) and not isinstance(group[key], (dict, list )): setattr(new_group, key, group[key]) assignee = db.session.query(User).filter(User.id == assignee.get('id',None)).first() setattr(new_group, "assignee_id", assignee.id) # parent_id = db.session.query(Group.id).join(ChecklistGroup).filter( # ChecklistGroup.checklist_id == task_info.get('checklist_id',None), # Group.parent_id == None # ).first() # setattr(new_group, "parent_id", parent_id) db.session.add(new_group) db.session.flush() setattr(new_task_info, "group_id", new_group.id) #ASSIGNE BECOMT ADMIN OF NEW GROUP new_relation = GroupsUsers( group_id = new_group.id, user_id = assignee.id, role_id = db.session.query(Role.id).filter(Role.role_name == "admin").first() ) db.session.add(new_relation) db.session.flush() # db.session.add(new_task_info) # db.session.flush() else: pass db.session.add(new_task_info) db.session.flush() #set followers for follower in task_info.get('followers',[]): new_follower_task_info = FollowerTaskInfo() new_follower_task_info.user_id = follower.get('id',None) new_follower_task_info.task_info_id = new_task_info.id new_follower_task_info.note = task_info.get('Note',None) db.session.add(new_follower_task_info) db.session.commit() # print(new_task_info.__dict__) return new_task_info
def put_task_info(request,task_info_id): uid = auth.current_user(request) if uid is not None: task_info = request.json task_info['unsigned_name'] = no_accent_vietnamese(task_info['task_info_name']) task_info['created_by'] = uid assignee = task_info.get('assignee',{}) group = task_info.get('group',{}) checklist = task_info.get('checklist',{}) task_info_update = db.session.query(TaskInfo).filter(TaskInfo.id == task_info_id, TaskInfo.deleted == False).first() for key in task_info.keys(): if hasattr(task_info_update,key) and not isinstance(task_info[key], (dict, list )): setattr(task_info_update, key, task_info[key]) #set ssignee setattr(task_info_update, "assignee_id", assignee.get('id',None)) #set group if group.get("id",None) is not None: setattr(task_info_update, "group_id", group.get("id",None)) # CHECK EXISTS check_member = db.session.query(GroupsUsers).filter(GroupsUsers.user_id == assignee.get('id'),\ GroupsUsers.group_id == group.get("id")).first() if check_member is None: #SET ASSIGNEE BECOME MEMBER OF GROUP new_relation_member = GroupsUsers( group_id = group.get('id'), user_id = assignee.get('id'), role_id = db.session.query(Role.id).filter(Role.role_name == "member").first() ) db.session.add(new_relation_member) elif group.get('id',None) is None and group.get('group_name') is not None and group.get('group_name') != '': new_group = Group() for key in group.keys(): if hasattr(new_group,key) and not isinstance(group[key], (dict, list )): setattr(new_group, key, group[key]) assignee = db.session.query(User).filter(User.id == assignee.get('id',None)).first() setattr(new_group, "assignee_id", assignee.id) db.session.add(new_group) db.session.flush() setattr(task_info_update, "group_id", new_group.id) # setattr(task_info_update, "group", new_group) #set admin for new_group new_relation = GroupsUsers( group_id = new_group.id, user_id = assignee.id, role_id = db.session.query(Role.id).filter(Role.role_name == "admin").first() ) db.session.add(new_relation) db.session.flush() # db.session.add(task_info_update) # db.session.flush() else: pass db.session.add(task_info_update) db.session.flush() #set followers for follower in task_info.get('followers',[]): new_follower_task_info = FollowerTaskInfo() new_follower_task_info.user_id = follower.get('id',None) new_follower_task_info.task_info_id = task_info_update.id new_follower_task_info.note = task_info.get('Note',None) db.session.add(new_follower_task_info) db.session.commit() # print(task_info_update.__dict__) # return task_info_update return json(to_dict(task_info_update),status=201) else: return json({ "error_code": "USER_NOT_FOUND", "error_message":"USER_NOT_FOUND" }, status = 520)