Skip to content

Data manipulation ​

You can start a Django Shell to manipulate data on live systems:

shell
abra app run <domain> app -- python -m karrot.cli shell

Some example use cases ​

Group memberships ​

Reassign all members of group with ID 1 to group with ID 2.

python
# Verify first that the group with the ID 2 is the correct one.
Group.objects.get(id=2)

# Then, apply the membership update.
GroupMembership.objects.filter(group=1).update(group=2)

Add member with id 1 to group with id 3.

python
m = GroupMembership()
m.group_id = 3
m.user_id = 1
m.roles = ["membership_manager", "editor", "member", "approved"]
m.save()

Places ​

Remove prefix '[Pre] ' from all places in group with ID 1.

python
for p in Place.objects.filter(group=1):
     p.name = p.name.removeprefix("[Pre] ")
     p.save()

Set weeks open in advance to 2 for all places in the groups 1, 3 and 7.

python
for p in Place.objects.filter(group__in=[1,3,7]):
    p.weeks_in_advance = 2
    p.save()

Mark the place with the ID 8 as favorite for everyone in the group with the ID 2.

python
favorite=8
for m in GroupMembership.objects.filter(group=2):
    if not PlaceSubscription.objects.filter(user=m.user_id,place=favorite).exists():
        s = PlaceSubscription()
        s.place_id = favorite
        s.user_id = m.user_id
        s.save()

Delete the place with the ID 1.

python
# Verify the place match first.
Place.objects.get(id=1)

# Then, delete it.
Place.objects.get(id=1).delete()

Delete unneeded the place type with the name 'Accounting Firm' in the groups 1, 3 and 7.

python
PlaceType.objects.filter(group__in=[1,3,7],name="Accounting Firm").delete()

Moving places starting with the prefix '[Pre] ' from group with ID 1 to group with ID 2.

python
# 1. Assign places to the new group.
Place.objects.filter(group=1, name__istartswith='[C] ').update(group=2)

# 2. Copy place types to new group if they don't exist already.
for pt in PlaceType.objects.filter(group=1):
    pt.pk = None
    pt.group_id = 2
    pt.save()

# 3. Link the places to place types in the new group.
group_id = 2
for t in PlaceType.objects.filter(group=group_id):
    for p in Place.objects.filter(group=group_id):
        p.place_type_id = PlaceType.objects.get(group=group_id,name=t.name).id
        p.save()

# 4. Assign all places to the places statuses in the new group.
group_id = 2
for p in Place.objects.filter(group=group_id):
    ps = PlaceStatus.objects.get(group=group_id,name=p.status.name)
    p.status_id = ps.id
    p.save()

# If activities are present, the place activities and activity series need to be linked to the activity types of the new group too.

Activities ​

Delete activities created by mistake in the distant future.

python
Activity.objects.filter(date__startswith__gt='2030-01-01').delete()

Users ​

Delete the user with the ID 1 from the instance.

python
# Verify the user match first.
User.objects.get(id=1)

# Then, delete them.
User.objects.get(id=1).erase()

For removing one or more members from a group only, there is an administrative task available.

shell
abra app run <domain> app -- python manage.py remove_members --group <group-id> --message "<explanation>" <user1@example.net> [<user2@example.com>]...
shell
docker compose exec app python manage.py remove_members --group <group-id> --message "<explanation>" <user1@example.net> [<user2@example.com>]...
shell
python manage.py remove_members --group <group-id> --message "<explanation>" <user1@example.net> [<user2@example.com>]...