text: # -*- coding: utf-8 -*-
  # Generated by Django 1.10.3 on 2016-11-07 11:38
  from __future__ import unicode_literals
  
  from datetime import timedelta
  
  from django.contrib.auth.hashers import is_password_usable, make_password
  from django.db import migrations
  from django.utils import timezone
  from oauthlib import common
  
  USER_USERNAME = 'nhsuk-frontend'
  APPLICATION_NAME = 'NHS.UK Frontend'
  
  
  def create_frontend_user_and_application(apps, schema_editor):
      # create user
      User = apps.get_model('auth', 'User')
      user = User.objects.create(
          username=USER_USERNAME,
          password=make_password(None)
      )
      assert not is_password_usable(user.password)  # double-check that the password is unusable
  
      # create application
      Application = apps.get_model("oauth2_provider", "Application")
      application = Application.objects.create(
          name=APPLICATION_NAME,
          user=user,
          client_type='confidential',
          authorization_grant_type='client-credentials'
      )
  
      # create access token
      AccessToken = apps.get_model("oauth2_provider", "AccessToken")
      AccessToken.objects.create(
          token=common.generate_token(),
          application=application,
          expires=timezone.now() + timedelta(days=(365 * 5)),
          scope='read'
      )
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('oauth2_provider', '0002_08_updates'),
      ]
  
      operations = [
          migrations.RunPython(create_frontend_user_and_application),
      ]
  