w
clone your own copy | download snapshot

Snapshots | iceberg

No images in this repository’s iceberg at this time

Inside this repository

0024_auto_20190104_1426.py
text/x-python

Download raw (1.2 KB)

# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2019-01-04 14:26
from __future__ import unicode_literals

from django.db import migrations




def populate_permissions(apps, schema_editor):
    from guardian.shortcuts import get_users_with_perms
    from guardian.utils import get_anonymous_user

    # We can't import the models directly as it may be a newer
    # version than this migration expects. We use the historical version.
    Score = apps.get_model('playground', 'Score')
    User = apps.get_model('auth', 'User')

    creator = User.objects.get(username="jeanne")
    anonymous_user = get_anonymous_user()

    for score in Score.objects.all():
        score.created_by = creator

        users = get_users_with_perms(score)
        score.is_public = users.filter(id=anonymous_user.id).exists()

        users = users.exclude(id__in=[creator.id, anonymous_user.id])
        for user in users:
            score.shared_with.add(User.objects.get(id=user.id))

        score.save()


class Migration(migrations.Migration):

    dependencies = [
        ('playground', '0023_auto_20190104_1425'),
    ]

    operations = [
        migrations.RunPython(populate_permissions),
    ]