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([
			{'fields': {'geoposition': '0,0', 'slug': 'event-1-approved', 'title': 'Event 1 - Approved'},
			 'model': 'api.event',
			 'pk': 2}
		])

		#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)

@@ -2397,37 +2397,65 @@
 %09%09%7B'
-fields': %7B'geoposition
+pk': 2, 'model': 'api.event', 'fields': %0A%09%09%09%7B'picture
 ': '
-0,0
 ', '
@@ -2514,48 +2514,91 @@
 ved'
-%7D
 ,
+ 
 %0A%09%09%09
- 'model': 'api.event',%0A%09%09%09 'pk': 2
+'description': 'Some description - Approved', 'geoposition': '0,0', %7D,%09 %0A%09%09%09 
 %7D%0A%09%09

update index with text with new fields