from django.db import models
from django.core.exceptions import ValidationError
from kinetic_widget.forms import KineticFormField
# from kinetic_widget.forms import KineticWidget


class KineticField(models.TextField):
    description = "Kinetic field for graphic widget"

    def __init__(self, *args, **kwargs):
        defaults = {
            'help_text': 'Graphic widget',
            'default': '',
            # 'max_length': 1000,
        }
        defaults.update(kwargs)
        return super(KineticField, self).__init__(*args, **defaults)

    def formfield(self, **kwargs):
        defaults = {'form_class': KineticFormField}
        # defaults = {'widget': KineticWidget}
        defaults.update(kwargs)
        return super(KineticField, self).formfield(**defaults)



# TODO: delete these models (below). They are just for testing purposes.

def get_default_JSON():
	""" iba pre testovacie ucely """
	import urllib2
	response = urllib2.urlopen('http://team28-12.ucebne.fiit.stuba.sk/~kachman/value.json')
	json_data = response.read()
	# json_data = "<Test JSON>"
	# print(json_data)
	return str(json_data)

class TestWidgetSimple(models.Model):
	jeden = KineticField(default=get_default_JSON)  # TODO: bug v Djangu? (ked dam callable, tak to da ten input element dva krat za sebou!!!) # update: to nie je bug, ono dava vzdy druhe pole (hiddne) s defaultnou hodnotou - ktora bola na zaciatku - asi kvoli nejakym js-enabled widgetom, ktore umoznouju "undo" alebo nieco take

	class Meta:
	    verbose_name = 'Single Widget test'
	    verbose_name_plural = 'Single Widget tests'


class TestWidgetMulti(models.Model):
    jeden = KineticField(default=get_default_JSON())
    druhy = KineticField(default=get_default_JSON())
    treti = KineticField(default=get_default_JSON())
    
    class Meta:
        verbose_name = 'Multiple Widgets test'
        verbose_name_plural = 'Multiple Widgets tests'



@@ -874,17 +874,20 @@
 JSON():%0A
-%09
+    
 %22%22%22 iba 
@@ -911,17 +911,20 @@
 ely %22%22%22%0A
-%09
+    
 import u
@@ -930,17 +930,20 @@
 urllib2%0A
-%09
+    
 response
@@ -1022,17 +1022,20 @@
 .json')%0A
-%09
+    
 json_dat
@@ -1054,17 +1054,20 @@
 .read()%0A
-%09
+    
 # json_d
@@ -1086,17 +1086,20 @@
  JSON%3E%22%0A
-%09
+    
 # print(
@@ -1109,17 +1109,20 @@
 n_data)%0A
-%09
+    
 return s
@@ -1174,17 +1174,20 @@
 Model):%0A
-%09
+    
 jeden = 
@@ -1505,17 +1505,20 @@
 o take%0A%0A
-%09
+    
 class Me
@@ -1521,17 +1521,20 @@
 s Meta:%0A
-%09
+    
     verb
@@ -1569,9 +1569,12 @@
 st'%0A
-%09
+    
     
@@ -1618,16 +1618,95 @@
 tests'%0A%0A
+    def __unicode__(self):%0A        return 'Simple Widget #%7B%7D'.format(self.id)%0A%0A
 %0Aclass T
@@ -2018,9 +2018,87 @@
 tests'%0A%0A
+    def __unicode__(self):%0A        return 'Multi Widget #%7B%7D'.format(self.id)%0A%0A
 %0A

test widget: added __unicode__ method