# Path: gogogo/models/utils.py
# def copyModel(object, **kwargs):
#     """
#     Copy a model instance
#     
#     @param kwargs Argument passed to the model constructor funtion for new copied model
#     """
#     model_class = db._kind_map[object.kind()]
#     
#     entity = {}
#     
#     for prop in object.properties().values():
#         datastore_value = prop.get_value_for_datastore(object)
#         entity[prop.name] = datastore_value
#         
#     entity.update(kwargs)
#         
#     return model_class(**entity)
# 
# 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/forms.py
# def next_key_name(model_class,key_name):
#     """
#     Get the next available key
#     """
#     if key_name == None:
#         return key_name # Use numeric key
#     
#     entity = model_class.get(db.Key.from_path(model_class.kind() , key_name))
# 
#     if not entity:
#         return key_name
#     else:
#         count = 0
#         while True:
#             count += 1
#             new_key_name = key_name + "-" + str(count)
#             entity = model_class.get(db.Key.from_path(model_class.kind() , new_key_name))
#             if not entity:
#                 return new_key_name 


from django.contrib import admin
from django import forms
from ragendja.forms import *
from gogogo.models import *
from gogogo.models.utils import copyModel,createEntity
from gogogo.models.forms import *
from gogogo.views.db.forms import next_key_name
from django.conf import settings
            new_obj = copyModel(obj,auto_set_key_name = True)
            #new_obj.save()
            return admin.ModelAdmin.save_model(self,request,new_obj,form,change)

admin.site.register(Stop, StopAdmin)

class RouteAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': (
                    'agency','short_name','long_name','desc','type','url','color','text_color'
                )
        }),
    )	

    form = RouteBasicForm
    list_filter = ('agency',)
    list_display = ('Route_ID','Route_Name')
    search_fields = ('long_name',)
    
    def Route_ID(self,obj):
        return obj.key().id_or_name()
        
    def Route_Name(self,obj):
        return "%s , %s" % (obj.short_name , u' | '.join(obj.long_name))

    def save_model(self,request,obj,form,change):
        if change:
            return admin.ModelAdmin.save_model(self,request,obj,form,change)
        else:            

# Path: gogogo/models/utils.py
# def copyModel(object, **kwargs):
#     """
#     Copy a model instance
#     
#     @param kwargs Argument passed to the model constructor funtion for new copied model
#     """
#     model_class = db._kind_map[object.kind()]
#     
#     entity = {}
#     
#     for prop in object.properties().values():
#         datastore_value = prop.get_value_for_datastore(object)
#         entity[prop.name] = datastore_value
#         
#     entity.update(kwargs)
#         
#     return model_class(**entity)
# 
# 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/forms.py
# def next_key_name(model_class,key_name):
#     """
#     Get the next available key
#     """
#     if key_name == None:
#         return key_name # Use numeric key
#     
#     entity = model_class.get(db.Key.from_path(model_class.kind() , key_name))
# 
#     if not entity:
#         return key_name
#     else:
#         count = 0
#         while True:
#             count += 1
#             new_key_name = key_name + "-" + str(count)
#             entity = model_class.get(db.Key.from_path(model_class.kind() , new_key_name))
#             if not entity:
#                 return new_key_name 


# Path: gogogo/admin.py
from django.contrib import admin
from django import forms
from ragendja.forms import *
from gogogo.models import *
from gogogo.models.utils import copyModel,createEntity
from gogogo.models.forms import *
from gogogo.views.db.forms import next_key_name
from django.conf import settings

            new_obj = copyModel(obj,auto_set_key_name = True)
            #new_obj.save()
            return admin.ModelAdmin.save_model(self,request,new_obj,form,change)

admin.site.register(Stop, StopAdmin)

class RouteAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': (
                    'agency','short_name','long_name','desc','type','url','color','text_color'
                )
        }),
    )	

    form = RouteBasicForm
    list_filter = ('agency',)
    list_display = ('Route_ID','Route_Name')
    search_fields = ('long_name',)
    
    def Route_ID(self,obj):
        return obj.key().id_or_name()
        
    def Route_Name(self,obj):
        return "%s , %s" % (obj.short_name , u' | '.join(obj.long_name))

    def save_model(self,request,obj,form,change):
        if change:
            return admin.ModelAdmin.save_model(self,request,obj,form,change)
        else:            

            new_obj = copyModel(obj,auto_set_key_name = True)