import unittest

from django.test import TestCase
from django.contrib import admin, auth
from django.core.urlresolvers import reverse

from ..schema import get_schema_model
from .models import AwareModel, NaiveModel, User

Schema = get_schema_model()

class TestAdminAdditions(TestCase):
    def test_ensure_schema_schema_is_not_editable(self):
        Schema.objects.mass_create('a','b','c')
        
        user = User.objects.create_superuser(
            username="su",
            password="su",
            email="su@example.com"
        )
        
        self.client.login(username='su', password='su')
        response = self.client.get('/admin/boardinghouse/schema/a/')
        form = response.context['adminform'].form
        self.assertTrue('name' in form.fields)
        self.assertTrue('schema' not in form.fields, 'Schema.schema should be read-only on edit.')
        
        response = self.client.get('/admin/boardinghouse/schema/add/')
        form = response.context['adminform'].form
        self.assertTrue('name' in form.fields)
        self.assertTrue('schema' in form.fields, 'Schema.schema should be editable on create.')
    
    def test_schema_aware_models_when_no_schema_selected(self):
        Schema.objects.mass_create('a','b','c')
        
        user = User.objects.create_superuser(
            username="su",
            password="su",
            email="su@example.com"
        )
        
        self.client.login(username='su', password='su')
        
        response = self.client.get('/admin/boardinghouse/awaremodel/')
        # Should we handle this, and provide feedback?
        self.assertEquals(302, response.status_code)
    
    def test_schemata_list(self):
        from boardinghouse.admin import schemata
        
        user = User.objects.create_user(
            username='user', password='password', email='user@example.com'
        )
        Schema.objects.mass_create('a','b','c')
        self.assertEquals('', schemata(user))
        
        user.schemata.add(*Schema.objects.all())
        self.assertEquals('a<br>b<br>c', schemata(user))
    
    def test_admin_log_includes_schema(self):
        from django.contrib.admin.models import LogEntry, ADDITION
        from django.contrib.contenttypes.models import ContentType
        
        Schema.objects.mass_create('a')
        schema = Schema.objects.get(name='a')
        schema.activate()
        
        aware = AwareModel.objects.create(name='foo')
        user = User.objects.create_user(username='test', password='test')
        
        LogEntry.objects.log_action(
            user_id=user.pk,
            content_type_id=ContentType.objects.get_for_model(aware).pk,
            object_id=aware.pk,
            object_repr=unicode(aware),
            change_message='test',
            action_flag=ADDITION,
        )
        
        entry = LogEntry.objects.get()
        
        self.assertEquals('a', entry.object_schema.pk)
        self.assertEquals(2, len(entry.get_admin_url().split('?')))
        self.assertEquals('__schema=a', entry.get_admin_url().split('?')[1])
    
    def test_admin_log_naive_object_no_schema(self):
        from django.contrib.admin.models import LogEntry, ADDITION
        from django.contrib.contenttypes.models import ContentType
        
        Schema.objects.mass_create('a')
        schema = Schema.objects.get(name='a')
        schema.activate()
        
        naive = NaiveModel.objects.create(name='foo')
        user = User.objects.create_user(username='test', password='test')
        
        LogEntry.objects.log_action(
            user_id=user.pk,
            content_type_id=ContentType.objects.get_for_model(naive).pk,
            object_id=naive.pk,
            object_repr=unicode(naive),
            change_message='test',
            action_flag=ADDITION,
        )
        
        entry = LogEntry.objects.get()
        
        self.assertEquals(None, entry.object_schema_id)
        self.assertEquals(1, len(entry.get_admin_url().split('?')))
@@ -382,33 +382,25 @@
 a','b','c')%0A
-        
 %0A
+
         user
@@ -528,33 +528,25 @@
 %22%0A        )%0A
-        
 %0A
+
         self
@@ -854,25 +854,17 @@
 edit.')%0A
-        
 %0A
+
         
@@ -1119,21 +1119,17 @@
 eate.')%0A
-    
 %0A
+
     def 
@@ -1232,25 +1232,17 @@
 b','c')%0A
-        
 %0A
+
         
@@ -1374,33 +1374,25 @@
 %22%0A        )%0A
-        
 %0A
+
         self
@@ -1435,25 +1435,17 @@
 d='su')%0A
-        
 %0A
+
         
@@ -1615,21 +1615,17 @@
 s_code)%0A
-    
 %0A
+
     def 
@@ -1699,25 +1699,17 @@
 chemata%0A
-        
 %0A
+
         
@@ -1920,25 +1920,17 @@
 (user))%0A
-        
 %0A
+
         
@@ -2000,43 +2000,65 @@
 als(
-'a%3Cbr%3Eb%3Cbr%3Ec', schemata(user))%0A    
+set(%5B'a', 'b', 'c'%5D), set(schemata(user).split('%3Cbr%3E')))%0A
 %0A   
@@ -2230,33 +2230,25 @@
 ContentType%0A
-        
 %0A
+
         Sche
@@ -2343,33 +2343,25 @@
 .activate()%0A
-        
 %0A
+
         awar
@@ -2472,33 +2472,25 @@
 ord='test')%0A
-        
 %0A
+
         LogE
@@ -2763,33 +2763,25 @@
 ,%0A        )%0A
-        
 %0A
+
         entr
@@ -2803,33 +2803,25 @@
 jects.get()%0A
-        
 %0A
+
         self
@@ -3008,21 +3008,17 @@
 ?')%5B1%5D)%0A
-    
 %0A
+
     def 
@@ -3196,25 +3196,17 @@
 entType%0A
-        
 %0A
+
         
@@ -3309,25 +3309,17 @@
 ivate()%0A
-        
 %0A
+
         
@@ -3438,25 +3438,17 @@
 'test')%0A
-        
 %0A
+
         
@@ -3729,25 +3729,17 @@
       )%0A
-        
 %0A
+
         
@@ -3769,24 +3769,16 @@
 s.get()%0A
-        
 %0A       

Fix possible object ordering issue in test.