import shutil

from django.contrib.auth.models import User
from django.contrib.admin import site as default_admin_site
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied
from django.core.files import File
from django.conf import settings
from django.forms.formsets import DEFAULT_MAX_NUM

from django_webtest import WebTest
from webtest import Upload
import mock
import pytest

from posts.models import Post, Comment, Tag, Multimedia
from modelclone import ClonableModelAdmin

from .asserts import *


class ClonableModelAdminTests(WebTest):

    def setUp(self):
        rm_rf(settings.MEDIA_ROOT)
        User.objects.all().delete()
        User.objects.create_superuser(
            username='admin',
            password='admin',
            email='admin@sampleproject.com'
        )
        self.tag1 = Tag.objects.create(name='django')
        self.tag2 = Tag.objects.create(name='sports')

        self.post = Post.objects.create(
            title = 'How to learn windsurf',
            content = 'Practice a lot!',
        )

        self.post_with_comments = Post.objects.create(
            title = 'How to learn Django',
            content = 'Read https://docs.djangoproject.com/'
        )
        Comment.objects.create(
            author = 'Bob',
            content = 'Thanks! It really helped',
            post = self.post_with_comments
        )
        Comment.objects.create(
            author = 'Alice',
            content = 'Oh, really?!',
            post = self.post_with_comments
        )

        self.post_with_tags = Post.objects.create(
            title = 'Django resable apps',
        )
        self.post_with_tags.tags.add(self.tag1)

        self.post_url = '/admin/posts/post/{0}/clone/'.format(
            self.post.id)
        self.post_with_comments_url = '/admin/posts/post/{0}/clone/'.format(
            self.post_with_comments.id)
        self.post_with_tags_url = '/admin/posts/post/{0}/clone/'.format(
            self.post_with_tags.id)

        self.multimedia = Multimedia.objects.create(
            title = 'Jason Polakow',