from django.contrib import admin
from .models import *
from django import forms
from django.contrib import admin
from mptt.admin import DraggableMPTTAdmin

#admin.site.register(Folder, DraggableMPTTAdmin)
@admin.register(Folder)
class FolderAdmin(admin.ModelAdmin):
	#change_list_template = "admin/folder_grid.html"
	list_display = ("name", "parent")
	def changelist_view(self, request, extra_context=None):
		parent_id = request.GET.get("parent")
		if parent_id:
		    parent_folder = Folder.objects.get(id=parent_id)
		    folders = Folder.objects.filter(parent=parent_folder)
		else:
		    parent_folder = None
		    folders = Folder.objects.filter(parent__isnull=True)

		extra_context = extra_context or {}
		extra_context["folders"] = folders
		extra_context["parent_folder"] = parent_folder
		return super().changelist_view(request, extra_context=extra_context)

#admin.site.register(Document)
#admin.site.register(Network)

class UploadFileWidget(forms.ClearableFileInput):
    template_name = 'widgets/upload_with_progress.html'  # On va le créer

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document
        fields = '__all__'
        widgets = {
            'file': UploadFileWidget,
        }

class DocumentAdmin(admin.ModelAdmin):
	form = DocumentForm
	search_fields = ('name','file')                  # champ de recherche
	ordering = ('name', )                      # ordre d’affichage
	def get_search_fields(self, request):
		search_fields = []
		for field in self.model._meta.fields:
			if isinstance(field, (models.CharField, models.TextField, models.IntegerField, models.DateField)):
				search_fields.append(field.name)
		return search_fields

admin.site.register(Document, DocumentAdmin)

