from django.test import TestCase
from django.db import models
from .models import (Product, WarehouseEntry, ProductCategory, ExtremeWidget,
                        SaleInvoice, Employee, ProductRating, Property, PropertyOwner)
from .views import (index, rate_product, CategoryCreateView, ProductCreateView,
                    ProductDeleteView, ProductUpdateView, ExtremeWidgetCreateView,
                    PropertyOwnerCreateView, PropertyCreateView, PropertyUpdateView)
from django.test.client import Client

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^/$', index),
    url(r'^rate/(\d)/$', rate_product),
    url(r'^category/create/$', CategoryCreateView.as_view()),
    url(r'^product/create/$', ProductCreateView.as_view()),
    url(r'^product/update/(?P<pk>\d+)/$', ProductUpdateView.as_view()),
    url(r'^product/delete/(?P<pk>\d+)/$', ProductDeleteView.as_view()),
    url(r'^extremewidget/create/$', ExtremeWidgetCreateView.as_view()),
    url(r'^propertyowner/create/$', PropertyOwnerCreateView.as_view()),
    url(r'^property/create/$', PropertyCreateView.as_view()),
    url(r'^property/update/(?P<pk>\d+)/$', PropertyUpdateView.as_view()),

)



def __setup_admins():
    from django.contrib.auth.models import User
    User.objects.all().delete()
    admin = User(username = "admin@example.com", is_staff = True, is_superuser = True)
    admin.set_password("admin")
    admin.save()
    admin = User(username = "admin1@example.com", is_staff = True, is_superuser = True)
    admin.set_password("admin1")
    admin.save()

def __setup_employees():
    from .models import Employee
    Employee.objects.all().delete()
    admin = Employee(email = "admin@example.com",)
    admin.set_password("admin")
    admin.save()
    admin = Employee(email = "admin1@example.com",)
    admin.set_password("admin1")
    admin.save()

def _setup_admin():
    from django.conf import settings
    if settings.AUTH_USER_MODEL =="audit_log.Employee":
        __setup_employees()
    else:
        __setup_admins()


class LogEntryMetaOptionsTest(TestCase):


    def test_app_label(self):
        self.failUnlessEqual(Product.audit_log.model._meta.app_label, Product._meta.app_label)
        self.failUnlessEqual(WarehouseEntry.audit_log.model._meta.app_label, WarehouseEntry._meta.app_label)

    def test_table_name(self):
        self.failUnlessEqual(Product.audit_log.model._meta.db_table, "%sauditlogentry"%Product._meta.db_table)
        self.failUnlessEqual(WarehouseEntry.audit_log.model._meta.db_table, "%sauditlogentry"%WarehouseEntry._meta.db_table)


class TrackingAuthFieldsTest(TestCase):
    urls = __name__

    def setUp(self):
        category  = ProductCategory.objects.create(name = "gadgets", description = "gadgetry")
        category.product_set.create(name = "new gadget", description = "best gadget eva", price = 100)


    def test_logging_user(self):
        _setup_admin()
        product = Product.objects.get(pk = 1)
        self.assertEqual(product.productrating_set.all().count(), 0)
        c = Client()
        c.login(username = "admin@example.com", password = "admin")
        c.post('/rate/1/', {'rating': 4})
        self.assertEqual(product.productrating_set.all().count(), 1)
        self.assertEqual(product.productrating_set.all()[0].user.username, "admin@example.com")

    def test_logging_session(self):
        _setup_admin()
        product = Product.objects.get(pk = 1)