# Path: gogogo/models/cache.py
# def getCachedObjectOr404(model = None,key=None,key_name=None,id = None , id_or_name = None):
#     """
#     Get a object from cache. If it is not existed in the cache, 
#     it will query from database directly and save it in cache.
# 
#     If the object is not existed in the cache and database , it 
#     will raise Http404 exception
#     """
# 
#     if id_or_name:
#         try:
#             id = int(id_or_name)
#         except ValueError:
#             key_name = id_or_name
# 
#     cache_key = getCacheObjectKey(model = model , key=key , key_name = key_name,id = id)
#     
#     #print cache_key
#     object = memcache.get(cache_key)
# 
#     if object == None:
#         if key:
#             object = get_object_or_404(model,key)
#         else:
#             object = get_object_or_404(model,key_name=key_name, id = id)
#         if not memcache.add(cache_key, object, _default_cache_time):
#             logging.error("Memcache set %s failed." % cache_key)
# 
#     return object
# 
# Path: gogogo/models/utils.py
# def createEntity(object):
#     """  Create an entity from model instance object which is 
#     suitable for data import and export. 
# 
#     Operations:
#     - Convert all ReferenceProperty to the key_name/key
#     - set 'id' attribute (= id_or_name()
#     - set 'instance' attribute , the reference to the model instance
# 
#     @return A dict object contains the entity of the model instance. The fields are not translated , use trEntity to translate to user's locale
# 
#     """
#     entity = {}
# 
#     for prop in object.properties().values():
#         datastore_value = prop.get_value_for_datastore(object)
#         entity[prop.name] = datastore_value
#         
#         if datastore_value == None:
#             continue
#         if not datastore_value == []:
#             if isinstance(prop,db.ReferenceProperty):
#                 entity[prop.name] = datastore_value.id_or_name()
#             elif isinstance(prop,MLStringProperty):
#                 entity[prop.name] = u'|'.join(datastore_value)
#             elif isinstance(prop,KeyListProperty):
#                 entity[prop.name] = [ key.id_or_name() for key in datastore_value ]
#             
#     #entity['key_name'] = object.key().id_or_name()
# 
#     # The client side do not know the different between id and key_name, they just 
#     # "id" as the unique identifier of an entry
#     if object.is_saved():
#         entity['id'] = object.key().id_or_name()
#         
#     # Going to deprecate
#     entity['instance'] = object
#     return entity
# 
# Path: gogogo/views/widgets.py
# class ReferenceLinkField(forms.Field):	
# 	"""
# 	A read-only field with a link to the target of reporting object
# 	"""
# 
# 	def __init__(self,  *args, **kwargs):		
# 		if 'widget' not in kwargs:
# 			kwargs.update( { 'widget' : ReferenceLinkWidget() })
# 				
# 		super(ReferenceLinkField, self).__init__(*args, **kwargs)
# 
# 		
# 	def clean(self,value):
# 		
# 		value = super(ReferenceLinkField, self).clean(value)
# 		if not value:
# 			return None
# 		instance = db.get(value)
# 		if instance is None:
# 			raise db.BadValueError(self.error_messages['invalid_choice'])
# 		return instance


from django.http import HttpResponse
from django.http import Http404
from django.template import Context, loader , RequestContext
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.forms import ModelForm
from django.forms import Widget
from django.forms.widgets import Input
from django.http import HttpResponseRedirect
from ragendja.auth.decorators import staff_only , login_required , google_login_required
from ragendja.template import render_to_response
from gogogo.models import *
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from gogogo.models.cache import getCachedObjectOr404
from gogogo.models.utils import createEntity
from gogogo.views.widgets import ReferenceLinkField
from django import forms
"""
Invalid information report function
"""


#from gogogo.views.db import reverse as db_reverse


class ReportForm(ModelForm):
	subject = forms.CharField(required=True)
	detail = forms.CharField(required=True,widget=forms.Textarea)
	#reference = ReferenceLinkField(required=False,widget = ReferenceLinkWidget())

# Path: gogogo/models/cache.py
# def getCachedObjectOr404(model = None,key=None,key_name=None,id = None , id_or_name = None):
#     """
#     Get a object from cache. If it is not existed in the cache, 
#     it will query from database directly and save it in cache.
# 
#     If the object is not existed in the cache and database , it 
#     will raise Http404 exception
#     """
# 
#     if id_or_name:
#         try:
#             id = int(id_or_name)
#         except ValueError:
#             key_name = id_or_name
# 
#     cache_key = getCacheObjectKey(model = model , key=key , key_name = key_name,id = id)
#     
#     #print cache_key
#     object = memcache.get(cache_key)
# 
#     if object == None:
#         if key:
#             object = get_object_or_404(model,key)
#         else:
#             object = get_object_or_404(model,key_name=key_name, id = id)
#         if not memcache.add(cache_key, object, _default_cache_time):
#             logging.error("Memcache set %s failed." % cache_key)
# 
#     return object
# 
# Path: gogogo/models/utils.py
# def createEntity(object):
#     """  Create an entity from model instance object which is 
#     suitable for data import and export. 
# 
#     Operations:
#     - Convert all ReferenceProperty to the key_name/key
#     - set 'id' attribute (= id_or_name()
#     - set 'instance' attribute , the reference to the model instance
# 
#     @return A dict object contains the entity of the model instance. The fields are not translated , use trEntity to translate to user's locale
# 
#     """
#     entity = {}
# 
#     for prop in object.properties().values():
#         datastore_value = prop.get_value_for_datastore(object)
#         entity[prop.name] = datastore_value
#         
#         if datastore_value == None:
#             continue
#         if not datastore_value == []:
#             if isinstance(prop,db.ReferenceProperty):
#                 entity[prop.name] = datastore_value.id_or_name()
#             elif isinstance(prop,MLStringProperty):
#                 entity[prop.name] = u'|'.join(datastore_value)
#             elif isinstance(prop,KeyListProperty):
#                 entity[prop.name] = [ key.id_or_name() for key in datastore_value ]
#             
#     #entity['key_name'] = object.key().id_or_name()
# 
#     # The client side do not know the different between id and key_name, they just 
#     # "id" as the unique identifier of an entry
#     if object.is_saved():
#         entity['id'] = object.key().id_or_name()
#         
#     # Going to deprecate
#     entity['instance'] = object
#     return entity
# 
# Path: gogogo/views/widgets.py
# class ReferenceLinkField(forms.Field):	
# 	"""
# 	A read-only field with a link to the target of reporting object
# 	"""
# 
# 	def __init__(self,  *args, **kwargs):		
# 		if 'widget' not in kwargs:
# 			kwargs.update( { 'widget' : ReferenceLinkWidget() })
# 				
# 		super(ReferenceLinkField, self).__init__(*args, **kwargs)
# 
# 		
# 	def clean(self,value):
# 		
# 		value = super(ReferenceLinkField, self).clean(value)
# 		if not value:
# 			return None
# 		instance = db.get(value)
# 		if instance is None:
# 			raise db.BadValueError(self.error_messages['invalid_choice'])
# 		return instance


# Path: gogogo/views/db/report.py
from django.http import HttpResponse
from django.http import Http404
from django.template import Context, loader , RequestContext
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.forms import ModelForm
from django.forms import Widget
from django.forms.widgets import Input
from django.http import HttpResponseRedirect
from ragendja.auth.decorators import staff_only , login_required , google_login_required
from ragendja.template import render_to_response
from gogogo.models import *
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from gogogo.models.cache import getCachedObjectOr404
from gogogo.models.utils import createEntity
from gogogo.views.widgets import ReferenceLinkField
from django import forms

"""
Invalid information report function
"""


#from gogogo.views.db import reverse as db_reverse


class ReportForm(ModelForm):
	subject = forms.CharField(required=True)
	detail = forms.CharField(required=True,widget=forms.Textarea)
	#reference = ReferenceLinkField(required=False,widget = ReferenceLinkWidget())

	reference = ReferenceLinkField()