mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-04 04:39:16 +02:00
See #152: added management command to execute one-time migration scripts
This commit is contained in:
parent
a57d975183
commit
ac7db73785
9 changed files with 202 additions and 4 deletions
2
api/funkwhale_api/common/scripts/__init__.py
Normal file
2
api/funkwhale_api/common/scripts/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
from . import django_permissions_to_user_permissions
|
||||
from . import test
|
|
@ -0,0 +1,29 @@
|
|||
"""
|
||||
Convert django permissions to user permissions in the database,
|
||||
following the work done in #152.
|
||||
"""
|
||||
from django.db.models import Q
|
||||
from funkwhale_api.users import models
|
||||
|
||||
from django.contrib.auth.models import Permission
|
||||
|
||||
mapping = {
|
||||
'dynamic_preferences.change_globalpreferencemodel': 'settings',
|
||||
'music.add_importbatch': 'library',
|
||||
'federation.change_library': 'federation',
|
||||
}
|
||||
|
||||
|
||||
def main(command, **kwargs):
|
||||
for codename, user_permission in sorted(mapping.items()):
|
||||
app_label, c = codename.split('.')
|
||||
p = Permission.objects.get(
|
||||
content_type__app_label=app_label, codename=c)
|
||||
users = models.User.objects.filter(
|
||||
Q(groups__permissions=p) | Q(user_permissions=p)).distinct()
|
||||
total = users.count()
|
||||
|
||||
command.stdout.write('Updating {} users with {} permission...'.format(
|
||||
total, user_permission
|
||||
))
|
||||
users.update(**{'permission_{}'.format(user_permission): True})
|
8
api/funkwhale_api/common/scripts/test.py
Normal file
8
api/funkwhale_api/common/scripts/test.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
"""
|
||||
This is a test script that does nothing.
|
||||
You can launch it just to check how it works.
|
||||
"""
|
||||
|
||||
|
||||
def main(command, **kwargs):
|
||||
command.stdout.write('Test script run successfully')
|
Loading…
Add table
Add a link
Reference in a new issue