def list(request, validated={}, *args, **kwargs): if request.method == 'GET': roles = Role.objects.filter(name__icontains=validated['match']).values( 'id', 'name', 'server') limit = validated['limit'] offset = validated['offset'] roles = roles[offset:] if limit < 0 else roles[offset:limit] return [g for g in roles] else: users = [] for user in validated['members']: for u in User.objects.filter(id=user): users.append(u) role = Role(name=validated['name']) if validated['flags'] is not None: flags = validated['flags'] permission = ServerPermission().convert(conv=flags) else: permission = ServerPermission() permission.save() role.flags = permission role.immunity = validated['immunity'] if validated['server'] is not None: role.server = Server.objects.get(id=validated['server']) if validated['usetime'] is not None: role.usetime = datetime.timedelta(seconds=validated['usetime']) role.save() for u in users: m = Membership(user=u, role=role) m.save() # role.user_set.set(users) # role.save() return 'passed'
def sourcemod(self): self.conn.query("""SELECT * FROM sm_groups""") r = self.conn.store_result() result = r.fetch_row(maxrows=0, how=1) roles = {} for raw in result: flags = ServerPermission().convert(raw['flags']) flags.save() role = Role.objects.get_or_create(name=raw['name'], default={ 'flags': flags, 'immunity': raw['immunity_level'] }) role.name = raw['name'] role.flags = flags role.immunity = raw['immunity_level'] role.save() roles[raw['id']] = role self.conn.query("""SELECT * FROM sm_admins""") r = self.conn.store_result() result = r.fetch_row(maxrows=0, how=1) generated = {} for raw in result: try: steamid = SteamID.from_text(raw['identity']).as_64() except: print("Could not add admin {}".format(raw['name'])) continue query = User.objects.filter(username=steamid) if not query: user = User.objects.create_user(username=steamid) user.is_active = False user.is_steam = True populate(user) else: user = query[0] user.namespace = raw['user'] user.save() self.conn.query("""SELECT * FROM sm_admins_groups WHERE admin_id = {}""".format(raw['id'])) r = self.conn.store_result() groups = r.fetch_row(maxrows=0, how=1) if not groups and raw['flags']: if raw['flags'] in generated: role = generated[raw['flags']] else: role = Role() role.name = raw['flags'] role.flags = ServerPermission().convert(raw['flags']) role.flags.save() role.immunity = 0 role.save() generated[raw['flags']] = role m = Membership() m.user = user m.role = role m.save() elif groups: for group in groups: role = roles[group["group_id"]] m = Membership() m.user = user m.role = role m.save() return True