import datetime
import json
from django.test import TestCase
from django.test import Client
from django.core.urlresolvers import reverse

from models.events import Event
from django.contrib.auth.models import User
from api.models import UserProfile

class EventViewsTestCase(TestCase):
	def setUp(self):
		self.u1 = User.objects.create(username='user1')
		self.up1 = UserProfile.objects.create(user=self.u1)

		pending = Event.objects.create(
			organizer="Organizer 1",
			creator=User.objects.filter(pk=1)[0],
			title="Event 1 - Pending",
			description="Some description - Pending",
			location="Near here",
			start_date=datetime.datetime.now() + datetime.timedelta(days=1, hours=3),
			end_date=datetime.datetime.now() + datetime.timedelta(days=3, hours=3),
			event_url="http://eee.com",
			contact_person="ss@ss.com",
			country="SI",
			pub_date=datetime.datetime.now(),
			tags=["tag1", "tag2"])

		client = Client()


	def test_index_view_without_approved_events(self):
		response = self.client.get(reverse('web.index'), {}, REMOTE_ADDR='93.103.53.11')

		self.assertEquals(200, response.status_code)
		self.assertJSONEqual('[]', json.loads(response.context['map_events']))
		self.assertEquals((46.0, 15.0), response.context['lan_lon'])
		self.assertQuerysetEqual([], response.context['latest_events'])
		self.assertTemplateUsed(response, 'pages/index.html')

	def test_index_view_changing_remote_in_request(self):
		#setup
		response = self.client.get(reverse('web.index'), {},
		                           HTTP_X_FORWARDED_FOR='93.103.53.11, 93.103.53.11')

		#assert
		self.assertEquals(200, response.status_code)
		self.assertEquals((46.0, 15.0), response.context['lan_lon'])

	def test_index_with_approved_events(self):
		#setup
		aproved = Event.objects.create(
			status="APPROVED",
			organizer="Organizer 1",
			creator=User.objects.filter(pk=1)[0],
			title="Event 1 - Approved",
			description="Some description - Approved",
			location="Near here",
			start_date=datetime.datetime.now() + datetime.timedelta(days=1, hours=3),
			end_date=datetime.datetime.now() + datetime.timedelta(days=3, hours=3),
			event_url="http://eee.com",
			contact_person="ss@ss.com",
			country="SI",
			pub_date=datetime.datetime.now(),
			tags=["tag1", "tag2"])

		response = self.client.get(reverse('web.index'), {}, REMOTE_ADDR='93.103.53.11')

		expected_map_events_result = json.dumps([
			{'pk': 2, 'model': 'api.event', 'fields': 
			{'picture': '', 'slug': 'event-1-approved', 'title': 'Event 1 - Approved', 
			'description': 'Some description - Approved', 'geoposition': '0,0', },	 
			 }
		])

		#assert
		self.assertJSONEqual(expected_map_events_result, json.loads(response.context['map_events']))
		self.assertEquals('SI', response.context['country']['country_code'])
		self.assertEquals(1, len(response.context['latest_events']))
		self.assertEquals(aproved.title, response.context['latest_events'][0].title)

@@ -2919,8 +2919,721 @@
 .title)%0A
+%0A%09def test_view_event_without_picture(self):%0A%09%09#setup%0A%09%09test_event = Event.objects.create(%0A%09%09%09organizer=%22Organizer 1%22,%0A%09%09%09creator=User.objects.filter(pk=1)%5B0%5D,%0A%09%09%09title=%22Test View Event Without Picture%22,%0A%09%09%09description=%22Some description%22,%0A%09%09%09location=%22Near here%22,%0A%09%09%09start_date=datetime.datetime.now() + datetime.timedelta(days=1, hours=3),%0A%09%09%09end_date=datetime.datetime.now() + datetime.timedelta(days=3, hours=3),%0A%09%09%09event_url=%22http://eee.com%22,%0A%09%09%09contact_person=%22ss@ss.com%22,%0A%09%09%09country=%22SI%22,%0A%09%09%09pub_date=datetime.datetime.now(),%0A%09%09%09tags=%5B%22tag1%22, %22tag2%22%5D)%0A%0A%09%09response = self.client.get(reverse('web.view_event', args=%5Btest_event.pk, test_event.slug%5D))%0A%0A%09%09#assert%0A%09%09self.assertEquals(200, response.status_code)%0A%0A

add view test for events without pictures