# Path: stack_configs/influx_functions.py
# def getLastTimeInflux(index,device_id,channel_id):
#     logger.debug ('getting lasttime for device id: %s and channelid :%s',device_id,channel_id)
#     results=getLastInflux(index,device_id,channel_id)
#     datetime="error" 
#     for result in results:
#         logger.debug ('getLastInflux time result %s',result)
#         try:
#             datetime=parse_datetime(result['time'])  
#         except:
#             pass
#            
#     return datetime
# 
# Path: devices/models.py
# class Section(models.Model):
# class Device(models.Model):
# class Channel_tag(models.Model):
# class Channel(models.Model):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def last_published(self):


from django.contrib import admin
from stack_configs.influx_functions import getLastTimeInflux
from .models import Section,Device,Channel,User,Channel_tag
    
    
    list_display = ('device_id', 'device_desc','resetPasswordLink')
    search_fields = ['device_id','device_desc','model_name']
    inlines = [channelInline]
    
    fieldsets = (
        (None, {
            'fields': ('device_id','device_desc','group','section','subgroup' ,)
        }),
        ('Geo-position',{
            'description':'',
            'classes':('collapse',),
            'fields':('latitude','longitude',),
            }),
        
        ('Installation details',{
            'description':'',
            'classes':('collapse',),
            'fields':('model_name','install_date',),
        }),
        )
    
    
    
    
    
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        #this function restricts choice of foreign key to those belonging to user/account
        if db_field.name == "section":

# Path: stack_configs/influx_functions.py
# def getLastTimeInflux(index,device_id,channel_id):
#     logger.debug ('getting lasttime for device id: %s and channelid :%s',device_id,channel_id)
#     results=getLastInflux(index,device_id,channel_id)
#     datetime="error" 
#     for result in results:
#         logger.debug ('getLastInflux time result %s',result)
#         try:
#             datetime=parse_datetime(result['time'])  
#         except:
#             pass
#            
#     return datetime
# 
# Path: devices/models.py
# class Section(models.Model):
# class Device(models.Model):
# class Channel_tag(models.Model):
# class Channel(models.Model):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def last_published(self):


# Path: devices/admin.py
from django.contrib import admin
from stack_configs.influx_functions import getLastTimeInflux
from .models import Section,Device,Channel,User,Channel_tag

    
    
    list_display = ('device_id', 'device_desc','resetPasswordLink')
    search_fields = ['device_id','device_desc','model_name']
    inlines = [channelInline]
    
    fieldsets = (
        (None, {
            'fields': ('device_id','device_desc','group','section','subgroup' ,)
        }),
        ('Geo-position',{
            'description':'',
            'classes':('collapse',),
            'fields':('latitude','longitude',),
            }),
        
        ('Installation details',{
            'description':'',
            'classes':('collapse',),
            'fields':('model_name','install_date',),
        }),
        )
    
    
    
    
    
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        #this function restricts choice of foreign key to those belonging to user/account
        if db_field.name == "section":

            kwargs["queryset"] = Section.objects.filter(account=request.user)