Skip to content

Data manipulation

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

bash
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.get(user=m.user_id,place=favorite):
        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()