# Path: gogogo/models/cache.py
# def getCachedEntityOr404(model = None,key=None,key_name=None , id = None , id_or_name = None):
# 	"""
# 	Get cached entity object generated by createEntity()
# 	"""
# 	
# 	if id_or_name:
# 		try:
# 			id = int(id_or_name)
# 		except ValueError:
# 			key_name = id_or_name	
# 	
# 	cache_key = getCacheEntityKey(model = model ,key=key , key_name = key_name, id = id)
# 	
# 	entity = memcache.get(cache_key)
# 
# 	if entity == None:
# 		object = getCachedObjectOr404(model, key=key,key_name=key_name, id = id)
# 		entity = createEntity(object)		
# 		
# 		if not memcache.add(cache_key, entity, _default_cache_time):
# 			logging.error("Memcache set %s failed." % cache_key)
# 	
# 	return entity
# 
# 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


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 import forms
from django.forms import ModelForm
from django.utils import simplejson
from ragendja.auth.decorators import staff_only
from ragendja.template import render_to_response
from django.core.urlresolvers import reverse
from ragendja.dbutils import get_object_or_404
from gogogo.models import *
from gogogo.models.cache import getCachedEntityOr404
from gogogo.models.utils import createEntity
	count = 0
	for row in query :
		count+=1
		entity = createEntity(row)
		#entity['id'] = row.key().id_or_name()
		entity['type'] = Changelog.get_type_name(entity['type'])
		entity['entry_name'] = unicode(row.reference)
		result.append(entity)
	
	prev_offset = offset - limit
	if prev_offset < 0 :
		prev_offset = 0
	
	return render_to_response( 
		request,
		'gogogo/db/changelog/list.html',
			{ "result" : result,
			  "offset" : offset + limit,
			  "prev_offset" : prev_offset,
			  "show_next" : count == limit,
			  "show_prev" : offset != 0,
			  "kind" : kind
		   })		
	

def browse(request,id):
    """
    Browse a changelog
    """	


# Path: gogogo/models/cache.py
# def getCachedEntityOr404(model = None,key=None,key_name=None , id = None , id_or_name = None):
# 	"""
# 	Get cached entity object generated by createEntity()
# 	"""
# 	
# 	if id_or_name:
# 		try:
# 			id = int(id_or_name)
# 		except ValueError:
# 			key_name = id_or_name	
# 	
# 	cache_key = getCacheEntityKey(model = model ,key=key , key_name = key_name, id = id)
# 	
# 	entity = memcache.get(cache_key)
# 
# 	if entity == None:
# 		object = getCachedObjectOr404(model, key=key,key_name=key_name, id = id)
# 		entity = createEntity(object)		
# 		
# 		if not memcache.add(cache_key, entity, _default_cache_time):
# 			logging.error("Memcache set %s failed." % cache_key)
# 	
# 	return entity
# 
# 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/db/changelog.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 import forms
from django.forms import ModelForm
from django.utils import simplejson
from ragendja.auth.decorators import staff_only
from ragendja.template import render_to_response
from django.core.urlresolvers import reverse
from ragendja.dbutils import get_object_or_404
from gogogo.models import *
from gogogo.models.cache import getCachedEntityOr404
from gogogo.models.utils import createEntity

	count = 0
	for row in query :
		count+=1
		entity = createEntity(row)
		#entity['id'] = row.key().id_or_name()
		entity['type'] = Changelog.get_type_name(entity['type'])
		entity['entry_name'] = unicode(row.reference)
		result.append(entity)
	
	prev_offset = offset - limit
	if prev_offset < 0 :
		prev_offset = 0
	
	return render_to_response( 
		request,
		'gogogo/db/changelog/list.html',
			{ "result" : result,
			  "offset" : offset + limit,
			  "prev_offset" : prev_offset,
			  "show_next" : count == limit,
			  "show_prev" : offset != 0,
			  "kind" : kind
		   })		
	

def browse(request,id):
    """
    Browse a changelog
    """	


    entity = getCachedEntityOr404(Changelog,id_or_name = id)