import boto3
from django.conf import settings
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    help = 'Set CORS rules on the Notice and Comment attachment bucket'

    def handle(self, *args, **options):
        session = boto3.Session(
            aws_access_key_id=settings.ATTACHMENT_ACCESS_KEY_ID,
            aws_secret_access_key=settings.ATTACHMENT_SECRET_ACCESS_KEY,
        )
        s3 = session.client('s3')
        s3.put_bucket_cors(
            Bucket=settings.ATTACHMENT_BUCKET,
            CORSConfiguration={
                'CORSRules': [
                    {
                        'AllowedMethods': ['PUT'],
                        'AllowedOrigins': ['*'],
                        'AllowedHeaders': ['*'],
                    },
                ],
            },
        )

Add command for S3 CORS configuration.
Add command for S3 CORS configuration.

To be run on app startup or manually.

[Resolves https://github.com/eregs/notice-and-comment/issues/57]
