Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
0030_update_spiir_acct_and_certs.py 1.51 KiB
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-03-15 20:38
from __future__ import unicode_literals

from django.db import migrations

ACCOUNT = {
    'old_username': 'gstlal-spiir',
    'new_username': 'spiir',
    'old_last_name': 'GSTLAL SPIIR Analysis',
    'new_last_name': 'SPIIR',
    'new_subject': '/DC=org/DC=cilogon/C=US/O=LIGO/OU=Robots/CN=ldas-pcdev11.ligo.caltech.edu/CN=spiir/CN=Qi Chu/CN=UID:qi.chu.robot',
}


def update_acct(apps, schema_editor):
    RobotUser = apps.get_model('ligoauth', 'RobotUser')

    # Get user
    user = RobotUser.objects.get(username=ACCOUNT['old_username'])

    # Create new certificate for user
    user.x509cert_set.create(subject=ACCOUNT['new_subject'])

    # Update user information
    user.username = ACCOUNT['new_username']
    user.last_name = ACCOUNT['new_last_name']
    user.save(update_fields=['username', 'last_name'])


def revert_acct(apps, schema_editor):
    RobotUser = apps.get_model('ligoauth', 'RobotUser')

    # Get user
    user = RobotUser.objects.get(username=ACCOUNT['new_username'])

    # Delete new certificate
    user.x509cert_set.get(subject=ACCOUNT['new_subject']).delete()

    # Revert user information
    user.username = ACCOUNT['old_username']
    user.last_name = ACCOUNT['old_last_name']
    user.save(update_fields=['username', 'last_name'])


class Migration(migrations.Migration):

    dependencies = [
        ('ligoauth', '0029_update_detchar_certs'),
    ]

    operations = [
        migrations.RunPython(update_acct, revert_acct),
    ]